iot: Access to GPIO fails with all versions of RaspiOS starting from 2024-10-28 on Raspberry Pi 5
Describe the bug
It was ok with previous versions of RaspiOS until 2024-07-04. The code runs in a container under IotHub module.
Steps to reproduce First of all, I’ve solved with a workaround using this code that isn’t production ready
#pragma warning disable SDGPIO0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
gpioController = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver(0, LibGpiodDriverVersion.V1));
#pragma warning restore SDGPIO0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}
and with the following Dockerfile (I’m sure I can remove some packages)
RUN apt-get update \
&& apt-get install -y sudo libgpiod2 gpiod libgpiod-dev libpigpiod-if-dev libpigpiod-if1 libpigpiod-if2-1
Full very simple code at my repo gpio-tester Previous attempts as suggested in #2361
- 1 Create a simple Console App and run outside the container -> OK
- 2 Containerized the app, but using the old approach that has worked fine in the last year -> KO
gpioController = new GpioController();
and with the following Dockerfile
RUN apt-get update \
&& apt install -y sudo libgpiod-dev
Made a lot of attempt trying different package without success until I’ve found the working solution described above
Expected behavior
All should work with
gpioController = new GpioController();
or at least this code inside your library should be modified from this
case RaspberryBoardInfo.Model.RaspberryPi5:
// For now, for Raspberry Pi 5, we'll use the LibGpiodDriver.
// We need to create a new driver for the Raspberry Pi 5,
// because the Raspberry Pi 5 uses an entirely different GPIO controller (RP1)
return new LibGpiodDriver(4);
to this
case RaspberryBoardInfo.Model.RaspberryPi5:
// For now, for Raspberry Pi 5, we'll use the LibGpiodDriver.
// We need to create a new driver for the Raspberry Pi 5,
// because the Raspberry Pi 5 uses an entirely different GPIO controller (RP1)
return new LibGpiodDriver(0, LibGpiodDriverVersion.V1);
I can make a PR, but I think this part is so crucial and you’re already working on the implementation for RP1
Actual behavior This exception is thown
11/11/2024 14:22:45 +01:00 System.IO.IOException: Unable to find a chip, error code: 2
at System.Device.Gpio.Drivers.Libgpiod.V1.LibGpiodV1Driver..ctor(Int32 gpioChip)
at System.Device.Gpio.Drivers.LibGpiodDriverFactory.CreateInternal(LibGpiodDriverVersion version, Int32 chipNumber)
at System.Device.Gpio.Drivers.LibGpiodDriverFactory.CreateAutomaticallyChosenDriver(Int32 chipNumber)
at System.Device.Gpio.Drivers.LibGpiodDriverFactory.Create(Int32 chipNumber)
at System.Device.Gpio.Drivers.LibGpiodDriver..ctor(Int32 gpioChip)
at System.Device.Gpio.GpioController.GetBestDriverForBoardOnLinux()
at System.Device.Gpio.GpioController.GetBestDriverForBoard()
at System.Device.Gpio.GpioController..ctor(PinNumberingScheme numberingScheme)
at System.Device.Gpio.GpioController..ctor()
Versions used
Latest version 3.2.0 of System.Device.Gpio and net9
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 27 (14 by maintainers)
@ciacco85 Apparently this is “just” a kernel update unrelated to the hardware revision board. We have to investigate whether there is a reliable way to detect the chip number without making any assumptions.
With regards to the OS kernel issues, I think we can’t do much about it from our library. On your side there are a couple of things that may be useful:
HTH
@ciacco85 It is definitely hardware-related, however, I’m not qualified enough to say how different RaspiOS versions handle this situation. Since plenty of people complain that programs stopped working after a certain RaspiOS update in 2024, it may be that RaspiOS tried to rectify the bug in software, and with newer RPI1 editions, it may also be fixed in hardware.
So maybe HW 1.0 and RaspiOS pre 2024-10 was the problematic combo.
Apparently the issue is also hardware-related.
I was having trouble running my containerized program on a new batch of RPI5’s, running into this exact issue. What I noticed is that PCBs were noticeably different. I also found in the forums (cannot find that thread now unfortunately), that initially RPI moved gpio chip id to 4, then moved back to 0 and also created an alias at 4 pointing to 0. This probably depends on the RaspiOS version, too.
Currently, I have too boards, let’s call them A and B. I have updated all the bootloaders and EEPROMs to the newest version (2025-02-17). The same SD Card is used for testing, with everything updated, too (kernel version: “6.6.77-v8-16k+ #1851 SMP PREEMPT Thu Feb 13 13:02:57 GMT 2025 aarch64”).
Board A
RP1 markings: “23/51 P3PB71.00”
_gpioController = new GpioController(); // Works_gpioController = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver(0, LibGpiodDriverVersion.V1)); // WorksBoard B
RP1 markings: “23/29 P14T33.00”
_gpioController = new GpioController(); // Doesn't work_gpioController = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver(0, LibGpiodDriverVersion.V1)); // Works@raffaeler I agree to that. I was thinking of a static method in
GpioDriveror similar. We should however not forget that this is still linux-specific, so not completely hardware-independent.@ciacco85 I’ve just had a look: The SysFs driver also includes logic to read out the chip name and identification. It seems that we just need to make this logic public in some fashion.
BTW I just see a lot of posts saying that the recent kernel update changed the mappings of the chip id. This article best describes what happened, but there are others specific to the Raspbian distribution. http://git.munts.com/muntsos/doc/AppNote11-link-gpiochip.pdf
I just had access to a RPi5 and the result is very different:
@ciacco85 I invite you to do the same and compare the results. Thanks
[Triage] Can you confirm we understand this correctly? V1 works but with
gpioChip = 0and V2 does not work at all?