Pi IoT In Python Using Linux Drivers - GPIO Using Ioct |
Written by Mike James & Harry Fairhead | ||||||||
Monday, 29 July 2024 | ||||||||
Page 4 of 4
Putting all this together, including definitions for unused constants, gives: import fcntl import struct import io GPIO_GET_CHIPINFO_IOCTL = 0x8044B401 GPIO_GET_LINEHANDLE_IOCTL = 0xC16CB403 GPIOHANDLE_SET_LINE_VALUES_IOCTL = 0xC040B409 GPIOHANDLE_GET_LINE_VALUES_IOCTL =0xC040B408 GPIOHANDLE_REQUEST_OUTPUT = 0x02 GPIOHANDLE_REQUEST_INPUT = 0x01 GPIOHANDLE_REQUEST_ACTIVE_LOW = 0x04 GPIOHANDLE_REQUEST_OPEN_DRAIN = 0x08 GPIOHANDLE_REQUEST_OPEN_SOURCE = 0x10 gpiochip_info = struct.Struct("32s 32s L") gpiohandle_request = struct.Struct("64L L 64B 32s L L") gpiohandle_data = struct.Struct("64B") lines = [0]*64 lines[0] = 4 lines[1] = 17 values = [0]*64 buffer = gpiohandle_request.pack(*lines, Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5. 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 InputReading data from GPIO lines follow the same steps as writing, but with obvious changes: import fcntl import struct import io import os GPIO_GET_CHIPINFO_IOCTL = 0x8044B401 GPIO_GET_LINEHANDLE_IOCTL = 0xC16CB403 GPIOHANDLE_SET_LINE_VALUES_IOCTL = 0xC040B409 GPIOHANDLE_GET_LINE_VALUES_IOCTL =0xC040B408 GPIOHANDLE_REQUEST_OUTPUT = 0x02 GPIOHANDLE_REQUEST_INPUT = 0x01 GPIOHANDLE_REQUEST_ACTIVE_LOW = 0x04 GPIOHANDLE_REQUEST_OPEN_DRAIN = 0x08 GPIOHANDLE_REQUEST_OPEN_SOURCE = 0x10 gpiochip_info = struct.Struct("32s 32s L") gpiohandle_request = struct.Struct("64L L 64B 32s L L") gpiohandle_data = struct.Struct("64B") lines = [0]*64 lines[0] = 4 lines[1] = 17 values = [0]*64 buffer = gpiohandle_request.pack(*lines, 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
Summary
Raspberry Pi IoT In PythonUsing Linux Drivers
|
Postgres And Kubernetes Together In Harmony 17/10/2024 Yes, they can coexist thanks to CloudNativePG, the PostgreSQL Operator for Kubernetes. Furthermore, if you want to try before you buy you can, thanks to the new learning environment, CNPG Playground. [ ... ] |
52nd Mersenne Prime Found 27/10/2024 It has been nearly six years since the last Mersenne prime was discovered. Now, at last, we have Mersenne prime number 52 and it has 41,024,320 digits! |
More News
|
Comments
or email your comment to: comments@i-programmer.info