Linux system controls gpio based on syfs

1 Introduction

The way sysfs controls gpio is mainly based on the gpio control interface file provided by the kernel. That is, the corresponding gpio interface is controlled by reading and writing files in the /sys/class/gpio directory.

2. sysfs controls gpio

  • GPIO pin calculation formula: pin = bank * 32 + number
    GPIO group number calculation formula: number = group * 8 + X
    For example: gpio3 RK_PA0 : 32 * 3 + 0 = 96

**注意**:使用命令:dmesg | grep cannot检查是否有gpio冲突,有冲突的话驱动代码会中断,后面的dts就不会生效。

  • Use the command in the terminal /sys/class/gpio path to turn on the LED light:
1. echo 96 > /sys/class/gpio/export                  //gpio_request          申请导出相应的gpio
2. echo out > /sys/class/gpio/gpio96/direction       //gpio_direction_output 设置相应gpio为输出方向
3. echo 1 > /sys/class/gpio/gpio96/value             // gpio_set_value       设置输出高电平
4. cat /sys/class/gpio/gpio96/value                  // gpio_get_value       获取gpio当前状态值
5. echo 96 > /sys/class/gpio/unexport                // gpio_free            释放申请的gpio

**注意**:如果驱动程序已经使用了该引脚,那么将会export失败,会提示下面的错误:
Device or resource busy

3. gpio debugging skills

3.1 View gpio occupancy status:

cat /sys/kernel/debug/gpio

insert image description here

3.2 View pinmux-pins: gpio full information:

cat /sys/kernel/debug/pinctrl/pinctrl-rockchip-pinctrl/pinmux-pins
insert image description here

Guess you like

Origin blog.csdn.net/nb124667390/article/details/131104841