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)
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");
fd = open(chrdev_name, 0);
if (fd == -1) {
ret = -errno;
fprintf(stderr, "Failed to open %s\n", chrdev_name);
return ret;
}
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");
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);
}
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