Skip to content

Commit

Permalink
Merge pull request #580 from wu-yue-yu/main
Browse files Browse the repository at this point in the history
Fix some typo and update gpio examples
  • Loading branch information
Zepan authored Nov 27, 2023
2 parents f2b01c4 + 6478dbd commit fc2c265
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 11 deletions.
88 changes: 79 additions & 9 deletions docs/hardware/en/lichee/th1520/lpi4a/6_peripheral.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ If it boots successfully, then you can check the following values to confirm tha

Take PWM1 which is connected to the cooling fan as an example, you can enable the fan with the following code:

``bash
```bash
echo 1 > /sys/class/pwm/pwmchip0/export
echo 1000000 > /sys/class/pwm/pwmchip0/pwm1/period
echo 1000000 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable
```

## GPIOs
## GPIO

The LicheePi 4A has 2x10pin pins on board with 16 native IOs, including 6 normal IOs, 3 pairs of serial ports, and one SPI.
> Note: All native IOs of the SOC are at 1.8V level, please pay attention to the level shift.
Expand All @@ -96,7 +96,7 @@ Where the 4Byte (32bit) at offset 0x0 is the GPIO data register and the 4Byte (3



The GPIO correspondences of the pins on the LicheePi 4A are:
The GPIO correspondences of the pins on the LicheePi 4A are(from the perspective of overlooking the front of the BOTTOM plate, TOP is the left side, BOTTOM is the right side):
![io_map](./../../../../zh/lichee/th1520/lpi4a/assets/peripheral/io_map.png)

> Subject to the labeling of the document, the silkscreen labeling of the internal test version may be incorrect
Expand All @@ -114,7 +114,7 @@ echo 0 > /sys/class/gpio/gpio${num}/value

The mapping of GPIO is shown in the following figure:

[gpio_num](./../../../../zh/lichee/th1520/lpi4a/assets/peripheral/gpio_num.png)
![gpio_num](./../../../../zh/lichee/th1520/lpi4a/assets/peripheral/gpio_num.png)

For example, if you want to operate the 4 GPIOs on the pin, the correspondence is as follows, change the num in the above code to the number corresponding to the GPIO pin you want to operate.

Expand Down Expand Up @@ -143,6 +143,75 @@ Here are the sample results:

![peripheral_gpio_information](./../../../../zh/lichee/th1520/lpi4a/assets/peripheral/peripheral_gpio_information.png)

Next, taking GPIO1_3 on the baseplate pin as an example, we use libgpiod to operate gpio in user space. First install libgpiod:
```shell
sudo apt update
sudo apt install libgpiod-dev
```

Then run the `vi gpio.c` command, and write the following code to the file:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gpiod.h>

int main()
{
int i;
int ret;

struct gpiod_chip * chip;
struct gpiod_line * line;

chip = gpiod_chip_open("/dev/gpiochip1");
if(chip == NULL)
{
printf("gpiod_chip_open error\n");
return -1;
}

line = gpiod_chip_get_line(chip, 3);
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;
}
```

Compile with the following command:
```shell
gcc gpio.c -I /usr/include/ -L /usr/lib/riscv64-linux-gnu/ -lgpiod -o gpio
```

Then execute with root privileges:
```shell
sudo ./gpio
```

At this time, use a multimeter to measure the IO1_3 pin on the baseplate. You can find that the voltage of the pin changes every second.

<!--
```bash
sipeed@lpi4a:~$ sudo cat /sys/kernel/debug/gpio
Expand Down Expand Up @@ -547,7 +616,12 @@ make -j4
#授予设备权限,每次开机执行一次即可
. exec.sh
./tft_demo
``` -->
```
#### Renderings
![效果图1](./../../../../zh/lichee/th1520/lpi4a/assets/peripheral/tft_demo.png)
-->

Common ioctl commands for SPI:

Expand Down Expand Up @@ -644,10 +718,6 @@ int main(int argc, char *argv[]) {
}
```
#### Renderings
![效果图1](./../../../../zh/lichee/th1520/lpi4a/assets/peripheral/tft_demo.png)
## USB
Connect the USB SSD:
Expand Down
73 changes: 71 additions & 2 deletions docs/hardware/zh/lichee/th1520/lpi4a/6_peripheral.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ TH1520 SOC 具有4个GPIO bank,每个bank最大有32个IO:

其中 0x0 偏移处的 4Byte(32bit)是 GPIO 数据寄存器,0x4 偏移处的 4Byte(32bit)是 GPIO 方向寄存器

LicheePi 4A上的插针的 GPIO 对应关系为:
LicheePi 4A上的插针的 GPIO 对应关系为(以俯视底板正面为视角,TOP为左侧,BOTTOM为右侧)
![io_map](./assets/peripheral/io_map.png)

> 以文档的标注为准,内测版的丝印标注可能有误
Expand All @@ -112,7 +112,7 @@ echo 0 > /sys/class/gpio/gpio${num}/value

GPIO 号的对应关系如下图所示:

[gpio_num](./assets/peripheral/gpio_num.png)
![gpio_num](./assets/peripheral/gpio_num.png)

比如要操作插针上的4个 GPIO,对应关系如下,将上述代码的num改为想要操作的 GPIO 脚对应的数字即可:

Expand Down Expand Up @@ -141,6 +141,75 @@ sipeed@lpi4a:~$ sudo cat /sys/kernel/debug/gpio

![peripheral_gpio_information](./assets/peripheral/peripheral_gpio_information.png)

接下来,以底板插针上的 GPIO1_3 为例,我们通过 libgpiod 在用户空间操作 gpio。首先安装 libgpiod:
```shell
sudo apt update
sudo apt install libgpiod-dev
```

使用 `vi gpio.c` 命令,将下面的代码写入文件:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gpiod.h>

int main()
{
int i;
int ret;

struct gpiod_chip * chip;
struct gpiod_line * line;

chip = gpiod_chip_open("/dev/gpiochip1");
if(chip == NULL)
{
printf("gpiod_chip_open error\n");
return -1;
}

line = gpiod_chip_get_line(chip, 3);
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
gcc gpio.c -I /usr/include/ -L /usr/lib/riscv64-linux-gnu/ -lgpiod -o gpio
```

然后以 root 权限执行:
```shell
sudo ./gpio
```

此时用万用表测量底板上的 IO1_3 引脚,可以发现每隔一秒该引脚电压会发生变化。

<!--
```bash
sipeed@lpi4a:~$ sudo cat /sys/kernel/debug/gpio
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fc2c265

Please sign in to comment.