Skip to content

Commit

Permalink
Merge pull request #629 from wu-yue-yu/main
Browse files Browse the repository at this point in the history
update lpi3h docs
  • Loading branch information
Zepan authored Feb 28, 2024
2 parents 1a37f67 + 3e25629 commit 0576e00
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 1 deletion.
82 changes: 82 additions & 0 deletions docs/hardware/en/longan/h618/lpi3h/6_peripheral.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

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
Expand Down
89 changes: 88 additions & 1 deletion docs/hardware/zh/longan/h618/lpi3h/6_peripheral.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

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

### 系统串口
Expand Down

0 comments on commit 0576e00

Please sign in to comment.