Interrupt handling and user space notification

2020-02-26 12:49发布

I have several registered interrupts assigned to gpios, and application in user space. How to notify application about occurred interrupt and which interrupt there was?

Possibly fasync is applicable for this goal, but I can find examples how to send information from interrupt handler to user space application.

It will be good if you can present some useful examples.

Thanks in advance.

2条回答
Lonely孤独者°
2楼-- · 2020-02-26 13:08

You don't need fancy kernel to userspace communication. A userspace application has access to GPIOs using Sysfs. Read about it in Documentation/gpio.txt.

First, export a GPIO pin like this (the actual number depends on your setup):

# echo 23 > /sys/class/gpio/export

This will export GPIO pin #23, and thus create /sys/class/gpio/gpio23.

Set its direction:

# echo in > /sys/class/gpio/gpio23/direction

If the hardware GPIO controller supports interrupts generation, the driver should also support it and you will see /sys/class/gpio/gpio23/edge. Write either rising, falling or both to this file to indicate the signal edge(s) that will create a "userspace interrupt". Now, to get interrupted, use the poll(2) system call on /sys/class/gpio/gpio23/value. Then, when the poll call unblocks, read the new value (/sys/class/gpio/gpio23/value), which will be '0' or '1' (ASCII).

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-26 13:14

dinesh provided a C implementation of eepp's proposed solution, which requires that the application block in poll().

Here is a C++ implementation which abstracts this functionality, and provides callback/interrupt functionality instead. Note the GPIO constructor which takes a callback function as an argument. This provides the capability desired by the OP.

https://github.com/tweej/HighLatencyGPIO

查看更多
登录 后发表回答