Registered User (→Code) |
Registered User m (Implement Patrick Fulcheri's comment about gpioset -t0) Tag: 2017 source edit |
||
(17 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{{ApplicableFor | |||
{{ | |MPUs list=STM32MP13x, STM32MP15x, STM32MP25x | ||
|MPUs checklist=STM32MP13x, STM32MP15x, STM32MP25x | |||
}} | |||
</noinclude> | <noinclude></noinclude> | ||
==Purpose== | ==Purpose== | ||
This article shows two ways to control a GPIO in userspace: | This article shows two ways to control a GPIO in userspace: | ||
Line 11: | Line 10: | ||
==GPIO control through libgpiod == | ==GPIO control through libgpiod == | ||
libgpiod provides a C library and tools for interacting with the linux GPIO character device (gpiod stands for GPIO device). See the libgpiod repository<ref>[https:// | libgpiod provides a C library and tools for interacting with the linux GPIO character device (gpiod stands for GPIO device). See the libgpiod repository<ref>[https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git libgpiod repository]</ref> for further explanation. | ||
*'''gpiodetect''' | *'''gpiodetect''' | ||
Line 17: | Line 16: | ||
**Usage: | **Usage: | ||
{{Board$}} gpiodetect | {{Board$}} gpiodetect | ||
gpiochip0 [GPIOA] (16 lines) | |||
... | ... | ||
gpiochip9 [GPIOZ] (8 lines) | |||
*'''gpioinfo''' | *'''gpioinfo''' | ||
Line 26: | Line 24: | ||
**Usage: | **Usage: | ||
{{Board$}}gpioinfo | {{Board$}}gpioinfo | ||
gpiochip0 - 16 lines: | |||
line 0: | line 0: "PA0" unused input active-high | ||
line 1: | line 1: "PA1" unused input active-high | ||
... | ... | ||
or | or | ||
{{Board$}}gpioinfo -c gpiochip0 {{Highlight|--> to only print gpiochip0 lines}} | |||
{{Board$}}gpioinfo gpiochip0 {{Highlight|-->to only print gpiochip0 lines}} | |||
*'''gpioget''' | *'''gpioget''' | ||
**Read the values of the specified GPIO lines (not valid if the line is already requested) | **Read the values of the specified GPIO lines (not valid if the line is already requested). The line will be then configured as input. | ||
{{Board$}}gpioget -c gpiochip0 13 {{Highlight|--> to get value of GPIO PA5}} | |||
"13"=inactive {{Highlight|--> means the line is driven low}} | |||
{{Board$}}gpioget gpiochip0 | |||
*'''gpioset''' | *'''gpioset''' | ||
**Set the values of the specified GPIO lines, potentially keeping the lines exported, and wait until timeout, user input or signal. | **Set the values of the specified GPIO lines, potentially keeping the lines exported, and wait until timeout, user input or signal (not valid if the line is already requested). | ||
{{Board$}}gpioset | |||
{{ | {{Board$}}gpioset -c gpiochip0 14=0 {{Highlight|--> get GPIO PA14 and set it low (green led on)}} | ||
{{ | Ctrl + C | ||
{{ | {{Board$}}gpioset -c gpiochip0 14=1 {{Highlight|--> get GPIO PA14 and set it high (green led off)}} | ||
Ctrl + C | |||
{{Board$}}gpioset -t 500ms -c gpiochip0 14=active {{Highlight|--> get GPIO PA14 and toogle it every 500ms (green led blink)}} | |||
Ctrl + C | |||
{{InternalInfo | As STM32 GPIO driver does not restore initial GPIOs state we could make a non-blocking command, by specifying the parameters "-t0". | |||
But this is not recommended in a final software: | |||
{{Board$}}gpioset -t0 -c gpiochip0 14=...}} | |||
==GPIO control through your own application== | ==GPIO control through your own application== | ||
===Purpose=== | ===Purpose=== | ||
This application toggles | This application toggles GPIO PA14 (GPIO bank A, line 14). On [[:Category:STM32MP15 Evaluation boards | STM32MP15_Evaluation_boards]] or [[:Category:STM32MP15 Discovery kits | STM32MP15_Discovery_kits]] GPIO PA14 is connected to the green LED. | ||
This application must be cross compiled with same toolchain as the Kernel. | This application must be cross compiled with same toolchain as the Kernel. | ||
===Code=== | ===Code=== | ||
< | <syntaxhighlight lang="c"> | ||
#include <errno.h> | #include <errno.h> | ||
#include <fcntl.h> | #include <fcntl.h> | ||
Line 128: | Line 126: | ||
return ret; | return ret; | ||
} | } | ||
</ | </syntaxhighlight> | ||
===Build application=== | ===Build application=== | ||
See [[STM32MPU_Developer_Package#Adding_Linux_user_space_applications|Adding_Linux_user_space_applications]] to build this application. | |||
See [[ | |||
==References== | ==References== | ||
<references/> | |||
< | <noinclude> | ||
[[Category:IOs pin management|5]] | |||
{{PublicationRequestId | 8795 | 2018-09-18 | PhilipS}} | |||
</noinclude> |
Latest revision as of 15:47, 28 October 2024
1. Purpose
This article shows two ways to control a GPIO in userspace:
- using libgpiod
- by writing an application
2. GPIO control through libgpiod
libgpiod provides a C library and tools for interacting with the linux GPIO character device (gpiod stands for GPIO device). See the libgpiod repository[1] for further explanation.
- gpiodetect
- List all gpiochips present on the system
- Usage:
gpiodetect
gpiochip0 [GPIOA] (16 lines)
...
gpiochip9 [GPIOZ] (8 lines)
- gpioinfo
- list all lines of specified gpiochips, their names, consumers, and their settings
- Usage:
gpioinfo
gpiochip0 - 16 lines:
line 0: "PA0" unused input active-high
line 1: "PA1" unused input active-high
...
or
--> to only print gpiochip0 linesgpioinfo -c gpiochip0
- gpioget
- Read the values of the specified GPIO lines (not valid if the line is already requested). The line will be then configured as input.
--> to get value of GPIO PA5 "13"=inactive --> means the line is driven lowgpioget -c gpiochip0 13
- gpioset
- Set the values of the specified GPIO lines, potentially keeping the lines exported, and wait until timeout, user input or signal (not valid if the line is already requested).
--> get GPIO PA14 and set it low (green led on) Ctrl + C gpioset -c gpiochip0 14=1 --> get GPIO PA14 and set it high (green led off) Ctrl + C gpioset -t 500ms -c gpiochip0 14=active --> get GPIO PA14 and toogle it every 500ms (green led blink) Ctrl + Cgpioset -c gpiochip0 14=0
3. GPIO control through your own application
3.1. Purpose
This application toggles GPIO PA14 (GPIO bank A, line 14). On STM32MP15_Evaluation_boards or STM32MP15_Discovery_kits GPIO PA14 is connected to the green LED.
This application must be cross compiled with same toolchain as the Kernel.
3.2. Code
#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;
}
3.3. Build application
See Adding_Linux_user_space_applications to build this application.
4. References