Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5. If you try this out on a Pi Zero you will find that the pulses are faster than you might expect – a pulse width of 40µs giving a frequency of 12kHz. Notice that the switching of the two lines is not in sync. There is a 150ns lag between switching each line. On a Pi Zero 2W the pulse is 10.8µs and 45kHz with a lag of 96ns.
On a Pi 4, the pulse is 4.5µs and 115kHz with a lag of 50ns. On a Pi 5 the pulse is 2.3µs and 218kHz with a lag of 10ns.
These figures should be compared to the performance of the obsolete sysfs approach, which produced 130kHz and a pulse width of 3.6µs on a Pi Zero and 450kHz and 1.1µs on a Pi 4, i.e. the new system is roughly twice as fast as sysfs. However, compared to using the hardware directly from C, when the Pi Zero pulses at about 70ns and the Pi 4 at around 75ns, it is more than ten times slower.
GPIO Input
Reading data from GPIO lines follow the same steps as writing, but with obvious changes:
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
You can see that now the request is to set an input line and the reading part of the program uses the GET and then displays the values in the data structure.
In chapter but not in book
Measuring R And C Using I/O Control
Summary
The raw GPIO character driver is easy to use once you know the ioctl operations supported and their structs.
The big problem with using ioctl is finding out the request numbers to use for any particular driver. Despite attempts to organize the allocation of values, this hasn’t really worked.
The C structs have to be converted into Python Struct objects and set to appropriate values using pack. Results are similarly unpacked into standard variables.
The GPIO lines have to remain open while you use them and you need to remember to close them when you have finished using them.
You can set and read multiple GPIO lines in a single function call.
The GPIO character driver is about twice as fast as the deprecated sysfs-based interface, but still ten times slower than direct access.
Raspberry Pi IoT In PythonUsing Linux Drivers Second Edition
TestSprite has announced an early access beta program for its end-to-end QA tool, along with $1.5 million pre-seed funding aimed at accelerating product development, expanding the team, and scaling op [ ... ]