waveform80 / rpi-lgpio

A compatibility shim for lgpio emulating the RPi.GPIO API
https://rpi-lgpio.readthedocs.io/
Other
9 stars 5 forks source link

Performance #2

Open phylax2020 opened 8 months ago

phylax2020 commented 8 months ago

So far I haven't found any performance data compared to RPi.GPIO. With RPi.GPIO I had no problems with a Python script for the HX711 load cell controller with a Pi Zero 2W under Bookworm OS. After I replaced RPi.GPIO with rpi-lgpio, the script for reading out the data from the HX711 no longer worked. The reason for this is that the execution of the following Python command sequence is approximately 10 times slower:

import time
import RPi.GPIO as GPIO

 timing_error = False
 start_counter = time.perf_counter()
 GPIO.output(self.PD_SCK, True)
 bitValue = GPIO.input(self.DOUT)
 GPIO.output(self.PD_SCK, False)
 end_counter = time.perf_counter()
 time_diff = end_counter - start_counter
 if (time_diff >= 0.00010):  # choose 100 us, check if the hx 711 did not turn off...
       timing_error = True
       break

RPi.GPIO requires an average of 15 microseconds. With rpi-lgpio, between 120 and 250 microseconds are required on average. This time period is too long, causing the HX711 controller to shut down during data transfer. This means that compatibility in terms of performance is not guaranteed.

waveform80 commented 4 months ago

I'm afraid this is one of those cases where there's very little I can do; rpi-lgpio is about as slim as I can make it in the form of pure Python.

I might try converting rpi-lgpio to a C module, but unfortunately it's unlikely I'm going to have time to tackle a major task like that for several months. Even then, it's never going to rival the speed of RPi.GPIO because that banged the hardware registers directly, and rpi-lgpio will always be talking to lgpio which will be talking to the kernel gpiochip device over ioctls, which will be banging hardware registers, but this time they're at the other end of a PCIe bus (on the Pi 5; on prior models they're still on the SoC).

I'll leave this open with a "help wanted" tag in case anyone else wants to give it a shot, but for now "this is as good as it's gonna get".