树莓派 - 通过sysfs操控GPIO
点亮或熄灭LED
硬件上,一个LED灯接在pi的Pin-25. 该引脚为BCM的GPIO26
$ gpio readall
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT5 | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | ALT5 | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 1 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | OUT | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+├
按照下面的命令点亮及熄灭LED
pi@raspberrypi:/sys/class/gpio $echo 26 > export
pi@raspberrypi:/sys/class/gpio $ cd gpio26
pi@raspberrypi:/sys/class/gpio/gpio26 $ ls
active_low device direction edge power subsystem uevent value
pi@raspberrypi:/sys/class/gpio/gpio26 $ cat direction
out
pi@raspberrypi:/sys/class/gpio/gpio26 $ cat value
1
pi@raspberrypi:/sys/class/gpio/gpio26 $ echo 0 >value
pi@raspberrypi:/sys/class/gpio/gpio26 $ cat value
0
sysfs
Linux 2.6以后的内核引入了sysfs文件系统, sysfs被看作是与proc, devfs和devpty同类别的文件系统。该文件系统是一个虚拟的文件系统,它可以产生一个包括所有系统硬件的层级视图。
sysfs把连接在系统上的设备和总线组织成一个分级的文件,它可以由用户空间存取, 向用户空间导出内核数据结构以及他们的属性。
其顶层目录包括, block, bus, dev, devices, class, fs, kernel, power 和firmware
从下面的bus, devices, class中对应的gpiochip1, 可以看到Linux设备模型与总线,设备,驱动,类的状态是对应的。
在linux内核中分别使用bus_type, device_driver, device来描述总线,驱动和设备。
device_driver和device分别表示驱动和设备,这两者都必须依附于一种总线,因此都包含struct bus_type指针。
/sys/bus
pi@raspberrypi:/sys/bus $ tree
├── gpio
│ ├── devices
│ │ ├── gpiochip0 -> ../../../devices/platform/soc/3f200000.gpio/gpiochip0
│ │ ├── gpiochip1 -> ../../../devices/gpiochip1
│ │ └── gpiochip2 -> ../../../devices/gpiochip2
│ ├── drivers
│ ├── drivers_autoprobe
│ ├── drivers_probe
│ └── uevent
/sys/devices
pi@raspberrypi:/sys/devices $ tree | more
├── gpiochip1
│ ├── dev
│ ├── gpio
│ │ └── gpiochip128
│ │ ├── base
│ │ ├── device -> ../../../gpiochip1
│ │ ├── label
│ │ ├── ngpio
│ │ ├── power
│ │ │ ├── autosuspend_delay_ms
│ │ │ ├── control
│ │ │ ├── runtime_active_time
│ │ │ ├── runtime_status
│ │ │ └── runtime_suspended_time
│ │ ├── subsystem -> ../../../../class/gpio
│ │ └── uevent
│ ├── of_node -> ../../firmware/devicetree/base/soc/expgpio
│ ├── power
│ │ ├── autosuspend_delay_ms
│ │ ├── control
│ │ ├── runtime_active_time
│ │ ├── runtime_status
│ │ └── runtime_suspended_time
│ ├── subsystem -> ../../bus/gpio
│ └── uevent
/sys/class
pi@raspberrypi:/sys/class $ tree
├── gpio
│ ├── export
│ ├── gpio26 -> ../../devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio26
│ ├── gpiochip0 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0
│ ├── gpiochip100 -> ../../devices/gpiochip2/gpio/gpiochip100
│ ├── gpiochip128 -> ../../devices/gpiochip1/gpio/gpiochip128
│ └── unexport