From 3aecfb2efb8e94e145e35f06e438730a21557b59 Mon Sep 17 00:00:00 2001 From: Tiandi Zhou <59756623+wu-yue-yu@users.noreply.github.com> Date: Wed, 28 Feb 2024 15:44:28 +0800 Subject: [PATCH] lpi3h: add instructions for libgpiod usage --- .../en/longan/h618/lpi3h/6_peripheral.md | 82 +++++++++++++++++ .../zh/longan/h618/lpi3h/6_peripheral.md | 89 ++++++++++++++++++- 2 files changed, 170 insertions(+), 1 deletion(-) diff --git a/docs/hardware/en/longan/h618/lpi3h/6_peripheral.md b/docs/hardware/en/longan/h618/lpi3h/6_peripheral.md index 49e1d818f9..cf1c8168de 100644 --- a/docs/hardware/en/longan/h618/lpi3h/6_peripheral.md +++ b/docs/hardware/en/longan/h618/lpi3h/6_peripheral.md @@ -43,6 +43,88 @@ echo out > /sys/class/gpio/gpio${num}/direction echo 0 > /sys/class/gpio/gpio${num}/value ``` +In addition to the above methods, GPIO can also be controlled using the libgpiod library in the C language. The following example still uses the LED lights on the board: + +```c +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int i; + int ret; + + struct gpiod_chip * chip; + struct gpiod_line * line; + + chip = gpiod_chip_open("/dev/gpiochip0"); + if(chip == NULL) + { + printf("gpiod_chip_open error\n"); + return -1; + } + + line = gpiod_chip_get_line(chip, 194); + if(line == NULL) + { + printf("gpiod_chip_get_line error\n"); + gpiod_line_release(line); + } + + ret = gpiod_line_request_output(line,"gpio",0); + if(ret < 0) + { + printf("gpiod_line_request_output error\n"); + gpiod_chip_close(chip); + } + + for(i = 0; i < 10; i++) + { + gpiod_line_set_value(line,1); + sleep(1); + gpiod_line_set_value(line,0); + sleep(1); + } + + gpiod_line_release(line); + gpiod_chip_close(chip); + + return 0; +} +``` + +To install the required dependencies: +```shell +sudo apt update +sudo apt install build-essential libgpiod-dev gpiod +``` + +After compiling, you need to execute the program with root privileges: +```shell +gcc gpio.c -I /usr/include/ -L /usr/lib/aarch64-linux-gnu/ -lgpiod -o gpio +sudo ./gpio +``` + +The libgpiod library also provides some commands to manipulate GPIO. Here are some commonly used commands: +gpiodetect: List all GPIO controllers +```shell +sudo gpiodetect +``` +gpioinfo: Display the pin information of a GPIO controller, showing which pins are already in use. +```shell +sudo gpioinfo gpiochip0 +``` +gpioset: Set the state of a GPIO pin. +```shell +sudo gpioset gpiochip0 196=0 +``` +gpioget: Get the state of a GPIO pin. +```shell +sudo gpioget gpiochip0 196 +``` + ## UART ### System serial port diff --git a/docs/hardware/zh/longan/h618/lpi3h/6_peripheral.md b/docs/hardware/zh/longan/h618/lpi3h/6_peripheral.md index 3b43fa5014..ca715b2199 100644 --- a/docs/hardware/zh/longan/h618/lpi3h/6_peripheral.md +++ b/docs/hardware/zh/longan/h618/lpi3h/6_peripheral.md @@ -30,7 +30,12 @@ TODO ![pin_num](./assets/peripheral/pin_num.png) -参考上面的两个表格,可以找到要使用的 GPIO 对应的位置和序号,以点亮底板上的两个 LED 灯为例,可以使用命令在用户空间操作对应的 GPIO : +参考上面的两个表格,可以找到要使用的 GPIO 对应的位置和序号,以点亮底板上的两个 LED 灯为例,可以使用命令在用户空间操作对应的 GPIO: + +使用前先检查 GPIO 是否被占用 +```shell +sudo cat /sys/kernel/debug/gpio +``` ```shell num=194 @@ -43,6 +48,88 @@ echo out > /sys/class/gpio/gpio${num}/direction echo 0 > /sys/class/gpio/gpio${num}/value ``` +除了上述方法外,还可以通过 C 语言的 libgpiod 库来操作 GPIO,下面仍然以底板上的 LED 灯为例 + +```c +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int i; + int ret; + + struct gpiod_chip * chip; + struct gpiod_line * line; + + chip = gpiod_chip_open("/dev/gpiochip0"); + if(chip == NULL) + { + printf("gpiod_chip_open error\n"); + return -1; + } + + line = gpiod_chip_get_line(chip, 194); + if(line == NULL) + { + printf("gpiod_chip_get_line error\n"); + gpiod_line_release(line); + } + + ret = gpiod_line_request_output(line,"gpio",0); + if(ret < 0) + { + printf("gpiod_line_request_output error\n"); + gpiod_chip_close(chip); + } + + for(i = 0; i < 10; i++) + { + gpiod_line_set_value(line,1); + sleep(1); + gpiod_line_set_value(line,0); + sleep(1); + } + + gpiod_line_release(line); + gpiod_chip_close(chip); + + return 0; +} +``` + +首先安装需要的依赖: +```shell +sudo apt update +sudo apt install build-essential libgpiod-dev gpiod +``` + +编译后,需要使用 root 权限来执行程序: +```shell +gcc gpio.c -I /usr/include/ -L /usr/lib/aarch64-linux-gnu/ -lgpiod -o gpio +sudo ./gpio +``` + +libgpiod 也提供了一些命令来操作 gpio,常用的命令如下: +gpiodetect:列出所有的 GPIO 控制器 +```shell +sudo gpiodetect +``` +gpioinfo:列出 GPIO 控制器的引脚情况,可以查看哪些引脚已经被使用 +```shell +sudo gpioinfo gpiochip0 +``` +gpioset:设置 GPIO 引脚的状态 +```shell +sudo gpioset gpiochip0 196=0 +``` +gpioget:获取 GPIO 引脚状态 +```shell +sudo gpioget gpiochip0 196 +``` + ## UART ### 系统串口