Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 9165

C/C++ • Re: Example of GPIO use with Linux only (no third party libraries)

$
0
0
I advise to run it as root, because it tries to change Linux scheduling (more important) and elevate its priority (less important) for its two processes - main thread and reader thread.
Alternatively, be aware that the kernel buffers events, so you can also increase the size of the event buffer in the kernel. By default it is 16 times the number of lines requested. And you can check the event sequence numbers to see if the buffer is overflowing and you are losing events.
int poll_result = poll(&poll_file_descriptor, 1, 1); // time out after 1 milliseconds
And don't wake the poll() after a millisecond, that just wakes for no reason and wastes CPU. Block indefinitely.
I look similarly on the gpiod and other "new" root-only GPIO interfaces.
The gpiochip interface does not require root access, it requires that the user has been granted rights to access the relevant /dev/gpiochip,
which is typically granted to members of the gpio group on Raspberry Pi OS:

Code:

$ ls -alrt /dev/gpiochip*crw-rw---- 1 root gpio 254, 0 Apr 24 07:17 /dev/gpiochip0crw-rw---- 1 root gpio 254, 1 Apr 24 07:17 /dev/gpiochip1
May as well just use the bcm2835 library at that point - it's faster and lighter.
That can be dangerous as it may conflict with kernel drivers that may also be writing to those hardware registers. And it wont port to a Pi5. But if you feel the need, the need the speed, and don't have a problem with living dangerously then nothing beats directly writing to the hardware registers.

Statistics: Posted by warthog618 — Mon May 06, 2024 10:07 am



Viewing all articles
Browse latest Browse all 9165

Latest Images

Trending Articles



Latest Images