In the previous article, we operate the LED file location by manipulating the brightness file, control the LED light and out, and on the Dragonboard 410c to achieve the flashing function of the LED. At the same time, I believe you also have a deep understanding of hardware development on the Dragonboard 410c. Next we will delve into other hardware and operate and interpret one by one. Background Knowledge
Whether it is the output square wave signal or the processing complex communication protocol, we must use directly or indirectly the GPIO to carry on the output and the input. Gpio is one of the most commonly used resources for communication between chips and the outside world, and its importance is self-evident. It is also one of the simplest drivers in Linux. Most programs can use the provided interface to control Gpio.
The Dragonboard 410c is filled with various forms of GPIO, such as high-speed or low-speed ports, as well as gpio that can be reused as communication ports. Thus, the integer identity between 0~max_int is used in Linux to explicitly specify the gpio that needs to be invoked, and the identity must be a positive integer. Linux provides a unified interface to operate GPIO, and then the actual control Gpio before you introduce the Gpio method of use. functions that need to be used
First we can detect if the Gpio port is available (legal):
int gpio_is_valid (int number);
Secondly, we need to export the Gpio port to user space so that we can have direct control in the shell:
Int gpio_export (unsigned gpio, bool direction_may_change);
The parameter direction_may_change indicates whether the user program is allowed to modify the direction of the Gpio, and if so, the parameter direction_may_change is true and Gpio_export's inverse function is Gpio_unexport (). This function can remove a GPIO node from the user space. files that need to be invoked
How user space accesses Gpio: The general approach is to access Gpio through the Sysfs interface, which is by manipulating the files in the file system. Below are the three files in the/sys/class/gpio directory, and the files in these three directories contain all the interfaces that can operate Gpio: Export/unexport file gpion: Refers to the specific GPIO pin GPIO_CHIPN: Referring to the GPIO controller
Prior to the operation, the developer must anticipate that the above interfaces do not have device files connected to them, and then explain each file: Export/unexport file interface: The interface can only write unreadable
The user program writes the GPIO number to the kernel to request control of a gpio and export it to user space. Of course, the premise is that there is no kernel code to request this Gpio port, such as
The above operation creates a node gpio19 for number 19th Gpio, at which point a gpio19 directory is generated below the/sys/class/gpio directory. /sys/class/gpio/unexport and exporting have the opposite effect, such as:
echo > Unexport
The above operation will remove the GPIO19 node. /sys/class/gpio/gpion: Refers to a specific GPIO port
The inside is like subordinate file:
The direction represents the direction of the Gpio port, and the read result is in or out. The file can also be written and written out when the Gpio is set to output and the level defaults to low. Writing low or high can not only be set to output but also to set the output level. Of course, if the kernel does not support or the kernel code is unwilling, there will be no such attribute, such as the kernel call Gpio_export (n,0), which means that the kernel is unwilling to modify the Gpio Port directional properties
value represents the level of the Gpio pin, 0 (Low level) 1 (High level), if the GPIO is configured for output, this value is writable, remembering that any Non-zero value will output a high level, and if a pin can and has been configured to interrupt, you can invoke the poll (2) function to listen for the interrupt. The poll (2) function returns when the interrupt is triggered.
edge means the trigger of the interrupt, the edge file has the following four values: "None", "Rising", "Falling", "both".
None represents pins for input, not interrupt pins rising for interrupt input, rising along triggering falling indicating pins for interrupt input, descent along triggering both indicating pins for interrupt input, edge triggering
This file node only exists when the pins are configured to enter pins. When the value is None, you can change to a break pin by using the following method:
echo "both" > edge;
For both,falling or rising the trigger mode of the interrupt that relies on specific hardware Dragonboard 410c LED control program to write header file
/*
============================================================================
name:togglygpio.c
Author:zhoujunyu
version:0.0.1
copyright:your Copyright notice
description:basic hardware access, Ansi -style
============================================================================
* *
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
# Include <string.h>
defines identifiers for Max_buf and Gpio
#define MAX_BUF
#define GPIO_A
#define GPIO_B 12
define write functions and read functions for GPIO and import/export functions
int Export_gpio (int GPIO);
int Unexport_gpio (int GPIO);
int Write_gpio (int GPIO, int value);
int Read_gpio (int GPIO, int *value);
describe the function of the main function
This section first export the specified GPIO, thereby controlling the gpio in the user space. The Gpio is then manipulated by writing functions and reading functions.
int main (void) {char c= ';
int ret;
int out_value = 1;
int in_value = 0;
will be developed GPIO Kou Export//ret = Export_gpio (gpio_a);
if (ret!= 0) printf ("Error exporting gpio_%d", gpio_a);
ret = Export_gpio (Gpio_b);
if (ret!= 0) printf ("Error exporting gpio_%d", gpio_b);
Writes a value to the specified GPIO/printf ("Press any key to toggle gpio_a or Q ' to quit:\n");
while (c!= ' Q ') {printf ("Writing gpio_%d:value=%d \ n", Gpio_a, Out_value);
ret = Write_gpio (gpio_a, Out_value);
if (ret!= 0) printf ("Error writing gpio_%d", gpio_a);
ret = Read_gpio (Gpio_b, (int*) &in_value);
if (ret!= 0) printf ("Error reading gpio_%d", Gpio_b);
printf ("Reading gpio_%d:value=%d \ n", Gpio_b, In_value);
Out_value =!out_value;
C= GetChar ();
Unexport//ret = Unexport_gpio (gpio_a) for the specified GPIO port; if (ret!= 0) PrintF ("Error unexporting gpio_%d", gpio_a);
ret = Unexport_gpio (Gpio_b);
if (ret!= 0) printf ("Error unexporting gpio_%d", Gpio_b);
return 0; }
Export Function
This function registers the Gpio so that the Gpio can operate in user space
int Export_gpio (int GPIO) {
int fd;
Char Buf[max_buf];
Export
////sys/class/gpio the GPIO at the specified location will generate an available Gpio file
sprintf (buf, "%d", gpio);
Open and operate the Export folder
FD = open ("/sys/class/gpio/export", o_wronly);
if (FD < 0)
return-1;
Write (FD, buf, strlen (BUF));
Close (FD);
return 0;
}
unexport function
This function removes Gpio from the user space so that the program cannot invoke Gpio
int Unexport_gpio (int GPIO) {
int fd;
Char Buf[max_buf];
sprintf (buf, "%d", gpio);
FD = open ("/sys/class/gpio/unexport", o_wronly);
if (FD < 0)
return-1;
Write (FD, buf, strlen (BUF));
Close (FD);
return 0;
}
Write Gpio value function
This function modifies the level of the GPIO output according to the input Gpio identifier and value
int Write_gpio (int GPIO, int value) {
int fd;
Char Buf[max_buf];
The Gpio direction is set to the output
sprintf (buf, "/sys/class/gpio/gpio%d/direction", Gpio);
FD = open (buf, o_wronly);
if (fd<0)
return-1;
Write (FD, "Out", 3);//Set out direction close
(FD);
sprintf (buf, "/sys/class/gpio/gpio%d/value", Gpio);
FD = open (buf, o_wronly);
if (fd<0)
return-1;
Write value to Gpio port
sprintf (buf, "%d", value);
Write (FD, buf, strlen (BUF));
Close (FD);
return 0;
}
read the Gpio value function
That means it's self-evident.
int Read_gpio (int GPIO, int *value) {
int fd;
Char Val;
Char Buf[max_buf];
sprintf (buf, "/sys/class/gpio/gpio%d/value", Gpio);
FD = open (buf, o_rdonly);
if (fd<0)
return-1;
Read the value of the Gpio read
(FD, &val, 1);
*value = Atoi (&val);
Close (FD);
return 0;
}
Summary
After running, the user can use the keyboard to control the high and low level of GPIO, press Q key can exit the program.
The effect is shown in the following illustration:
Gpio control may be a bit more complicated than LEDs, but with this tutorial, I want you to have a basic understanding of how to operate under Dragonboard 410c embedded Linux. Some people may find it very complex and difficult to remember, but because of the huge Linux system, the need to deal with countless hardware and software features, naturally there is no simple STM32 and other single-chip so easy. But if you learn these skills, then in the future development, will be a duck in the way.