3956 views|7 replies

2865

Posts

4

Resources
The OP
 

STM32MP157A-DK1 Evaluation + HelloWorld and GPIO (2) [Copy link]


What impressed me about the environment of HelloWorld
OpenSTlinux is that the board directly displays the graphical interface after startup, and does not output console information like the screen! This is very "cool!". The console information gives me a headache. It is also annoying to output these useless information to the user. Refer to the official website of ST "Create a simple hello-world application" to create a program.
1. First confirm whether the development environment is correct
$CC --version
If it is not correct, use the environment script file to configure it correctly!
source /opt/st/stm32mp1/2.6-openstlinux-20-02-19/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi

2. Start programming!
Use your favorite text editor. I use
vi gtk_hello_world.c
and enter the following code

#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *button_box;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_container_add (GTK_CONTAINER (window), button_box);

  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  gtk_container_add (GTK_CONTAINER (button_box), button);

  gtk_widget_show_all (window);
}

int
main (int    argc, char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Save the file for later use

3. Compile the program

Edit the Makefile file
vi Makefile.
Note that you cannot directly test the script! You must add a tab in front of the command!!!
This is for new Linux developers. If you don't understand, then have fun learning.

PROG = gtk_hello_world
SRCS = gtk_hello_world.c

CLEANFILES = $(PROG)

# Add / change option in CFLAGS and LDFLAGS
CFLAGS += -Wall $(shell pkg-config --cflags gtk+-3.0)
LDFLAGS += $(shell pkg-config --libs gtk+-3.0)

all: $(PROG)

$(PROG): $(SRCS)
	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

clean:
	rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS))

If there is no error in compiling the file
make
, you can see the generated gtk_hello_world file
4. Upload the file,
scp gtk_hello_world root@192.168.0.103:/usr/local
Replace your IP address. <board ip address>
Go to the console to execute the program.
./gtk_hello_world
You can see a graphical window on the monitor. Click the button, and the program will output "Hello, World!" in the console.

This example uses the gtk library. gtk development is very troublesome and does not have many functions. It is said that qt5 can be used. I will study it when I have time.
GPIO operation lights up the LED

1. GPIO Introduction

For embedded developers, it is a bit unreasonable not to be able to operate hardware. So being able to light up the LED is the authentic "Hello World!" For the unix system, "everything is a file!", the device files under the linux system are in the /dev directory. By checking, I found a total of 10 device files from gpiochip0 to gpiochip9. Each device file corresponds to a GPIOs port. That is, gpiochip0 corresponds to the GPIOA port. Detailed information can be viewed using
cat /sys/kernel/debug/gpio
. According to the information, LED5 is connected to the 14th pin of GPIOA. Using the gpioset tool to operate the 14th pin of gpiochip0 should be able to operate the disconnection of led5.
gpioset gpiochip0 14=0
can see that the led5 next to the rest button is glowing. The information says that Green is turned into Yellow. Yellow is also good! As long as it is bright.

2. Use files to operate GPIO

I wrote a LED5 program using C code. At first I wanted to use the libgpiod library to operate gpio. I debugged it for a long time but it didn't work. I couldn't find a code to directly operate the device file.

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include <linux/gpio.h>

int main(int argc, char **argv)
{
	struct gpiohandle_request req;
	struct gpiohandle_data data;
	char chrdev_name[20];
	int fd, ret;

	strcpy(chrdev_name, "/dev/gpiochip0");

	/*  Open device: gpiochip0 for GPIO bank A */
	fd = open(chrdev_name, 0);
	if (fd == -1) {
		ret = -errno;
		fprintf(stderr, "Failed to open %s\n", chrdev_name);

		return ret;
	}

	/* request GPIO line: GPIO_A_14 */
	req.lineoffsets[0] = 14;
	req.flags = GPIOHANDLE_REQUEST_OUTPUT;
	memcpy(req.default_values, &data, sizeof(req.default_values));
	strcpy(req.consumer_label, "led_gpio_a_14");
	req.lines  = 1;

	ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
	if (ret == -1) {
		ret = -errno;
		fprintf(stderr, "Failed to issue GET LINEHANDLE IOCTL (%d)\n",
			ret);
	}
	if (close(fd) == -1)
		perror("Failed to close GPIO character device file");

	/*  Start led blinking */
	while(1) {

		data.values[0] = !data.values[0];
		ret = ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
		if (ret == -1) {
			ret = -errno;
			fprintf(stderr, "Failed to issue %s (%d)\n",
					"GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret);
		}
		sleep(1);
	}

	/*  release line */
	ret = close(req.fd);
	if (ret == -1) {
		perror("Failed to close GPIO LINEHANDLE device file");
		ret = -errno;
	}
	return ret;
}

After compilation, LED5 can be successfully controlled

Now Hello world is complete!

For beginners, please refer to

How to cross-compile with the Developer Package

This content is originally created by bigbat , a user of EEWORLD forum. If you want to reprint or use it for commercial purposes, you need to obtain the author's consent and indicate the source

This post is from stm32/stm8

Latest reply

Got it, it turned out that the compiler made a mistake   Details Published on 2020-5-12 22:38
 

661

Posts

18

Resources
2
 

The review by the host is really good!!

This post is from stm32/stm8
 
 

9702

Posts

24

Resources
3
 

Not bad, fast enough.

This post is from stm32/stm8
 
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 

5979

Posts

8

Resources
4
 

Very good, looking forward to your masterpiece

This post is from stm32/stm8
 
Personal signature生活就是油盐酱醋再加一点糖,快活就是一天到晚乐呵呵的忙
===================================
做一个简单的人,踏实而务实,不沉溺幻想,不庸人自扰
 
 

336

Posts

2

Resources
5
 

Learned, learned

This post is from stm32/stm8
 
 
 

3

Posts

0

Resources
6
 

The compilation fails

This post is from stm32/stm8

Comments

This program can definitely be compiled. You need to pay attention to the fact that the compilation environment of your machine must be correct! ! ! source /opt/st/stm32mp1/2.6-openstlinux-20-02-19/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi and follow the instructions after execution  Details Published on 2020-4-10 08:43
 
 
 

2865

Posts

4

Resources
7
 
xinshuwei posted on 2020-4-9 20:53 The compilation failed

This program can definitely be compiled, but you need to pay attention that the compilation environment of your machine must be correct!!!

source /opt/st/stm32mp1/2.6-openstlinux-20-02-19/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi

After execution, check the environment according to the operation steps. Also, if you don't know how to write a makefile, please learn it.

If the above still doesn't work, then it's a character issue...

This post is from stm32/stm8
 
 
 

3

Posts

0

Resources
8
 
bigbat posted on 2020-4-10 08:43 This program can definitely be compiled, but you need to pay attention to the correct compilation environment of your machine! ! ! source /opt/st/stm32mp1/2. ...

Got it, it turned out that the compiler made a mistake

This post is from stm32/stm8
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list