antecedent
Based on linux gpio, files will be generated in the /sys/class/gpio
directory export, unexport
, and of course there are gpiochipx
files , which gpiochipx
are the management of pins. For example, a certain chip may control a certain number of pins, and the number of pins ngpio
is .
When executing echo 4 > /sys/class/gpio/export
, a directory will /sys/class/gpio
be generated under the gpio4
directory , in which there will be value, edge, direction
related files such as , value is the current value, edge is the pin trigger mode, and direction is the pin input and output mode. But how did this come about echo
after that.
Process scan
- In the initial initialization process there
gpiolib_dev_init
are exported ascore_initcall
(gpio/gpiolib.c) - This function calls
bus_register
register gpio , corresponding to the generated/sys/bus/gpio
directory - call again
alloc_chrdev_region
- Then set the gpiochip
gpiochip_setup_devs
- This processes the total number of gpiochips, and for each call
gpiochip_sysfs_register
, the file goesgpiolib-sysfs.c
to - call
device_create_wtih_groups
to creategpiochip%d
- The parameter corresponding to this function
gpio_class
is aclass
class - The name of this
class
definition is, and attrgpio
is also defined - attr includes
export - export _store
,unexport - unexport_store
- The former is the property name, the latter is the function
- That is, the
echo
action is a triggerexport_store
function - This function call
gpio_request
prepares pin - call again
gpiod_export
in this functiondevice_create_with_groups
to creategpio%d
, among other things
Remark
- Of course, there are still many details that are still unclear, just an overview, which will be added later