5

Assume that the gpio X can be exported in sysfs as an input pin, after doing that a directory called gpioX will be created into /sys/class/gpio/. gpioX/ contains few file such as "value" which represents the current state of the gpio X (high or low).

What happens (in kernel space) when the signal applied to the pin X changes its state (for example from low to high)?

I mean, before the transition gpioX/value contains "low", but after that it will contain "high" value. How is this file updated by the OS?

I think that an interrupt mechanism is required.Does it use an interrupt mechanism to update sysfs?

AAI
  • 290
  • 1
  • 3
  • 20
b0b0b
  • 134
  • 1
  • 8
  • I put together some notes about sysfs and the pseudo files in an answer to this question, [what is the /sys/class/gpio/export and `/sys/class/gpio/unexport mechanism and what is the underlying sysfs functionality?](https://stackoverflow.com/questions/63769403/what-is-the-sys-class-gpio-export-and-sys-class-gpio-unexport-mechanism-and-w). It doesn't exactly answer your question however there are some thoughts on the underlying sysfs functionality that someone may find helpful. – Richard Chambers Sep 25 '20 at 19:58

1 Answers1

7

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).

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • I'm trying to do something similar except that I am using Kotlin under Android on a DragonBoard 410C. The documentation talks about using the `poll(2)` system service of Linux to monitor the `/sys/class/gpio/gpionn/value` pseudo file for an event after setting the `/sys/class/gpio/gpionn/edge` pseudo file. [how to use poll(2) or select(2) service call to watch a pseudo file for changes with Kotlin](https://stackoverflow.com/questions/64028130/how-to-use-poll2-with-kotlin-files) – Richard Chambers Sep 23 '20 at 18:24
  • Do you have the `edge` file? If not, then `poll()` is not supported. The hardware has to be able to generate an interrupt on a gpio change. All hardware will allow read of the current gpio value, but only some hardware will support an interrupt (and `edge` and `poll()`). If `edge` does not exist, then running `poll()` on `value` will do nothing. – artless noise Sep 23 '20 at 19:39
  • Yes, the `edge` pseudo file does exist for the GPIO pins. I'm stuck trying to figure out how to use the `poll(2)` service call from Kotlin source code. I've found something to try later this evening and if it works then I'll create an answer to my posted question. I just thought you might be able to point me in the right direction. Take care and enjoy your day. – Richard Chambers Sep 23 '20 at 20:28