問題描述
GPIO 更改狀態時如何更新 sysfs? (How is sysfs updated when a GPIO changes state?)
假設 gpio X 可以作為輸入引腳在 sysfs 中導出,之後將在 /sys/class/gpio/ 中創建一個名為 gpioX 的目錄。gpioX/ 包含一些文件,例如表示 gpio X 的當前狀態(高或低)的“值”。
當施加到引腳 X 的信號改變其狀態時(在內核空間中)會發生什麼(例如從低到高)?
我的意思是,在轉換之前 gpioX/value 包含“低”,但之後它將包含“高”值。操作系統如何更新此文件?
我認為需要中斷機制。是否使用中斷機制來更新sysfs?
參考解法
方法 1:
How is this file updated by the OS? I think that an interrupt mechanism is required.
It does not require an interrupt mechanism unless it supports polling (man poll) or alternate asynchronous notifications. At least with most version, the /sys/class/gpio/ only does a read of the GPIO level when someone reads the file.
sysfs, debugfs, configfs, procfs, etc are virtual file systems. When you access the file, code within the Linux kernel runs to provide the value. sysfs only provides a file like interface; that doesn't mean it is backed with actual state. The state is the GPIO level which can be read at any time.
gpio_value_show() appears to be the current implementation. What you describe with interrupts is possible. It can be done through the sysfs_set_active_low()
function or the sysfs file /sys/class/gpio/gpioN/edge. Writing to the file may return an error if the GPIO doesn't support interrupts. See gpio.txt for more (especially for your particular version of Linux).
(by b0b0b、artless noise)