diff --git a/src/System.Device.Gpio/System/Device/Gpio/GpioController.cs b/src/System.Device.Gpio/System/Device/Gpio/GpioController.cs
index 965c71eadb..d90d819421 100644
--- a/src/System.Device.Gpio/System/Device/Gpio/GpioController.cs
+++ b/src/System.Device.Gpio/System/Device/Gpio/GpioController.cs
@@ -463,18 +463,20 @@ private static GpioDriver GetBestDriverForBoardOnLinux()
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)
- var chips = LibGpiodDriver.GetAvailableChips();
- // The RP1 chip reports 54 lines
- GpioChipInfo? selectedChip = chips.FirstOrDefault(x => x.NumLines == 54);
- if (selectedChip is null)
+ // Raspberry Pi 5 uses an entirely different GPIO controller (RP1) which reports 54 lines.
+ // Try V1 (libgpiod.so.2) first, then V2 (libgpiod.so.3) for compatibility
+ // with different Raspberry Pi OS versions.
+ if (GpioDriver.TryCreate(() => CreatePi5Driver(LibGpiodDriver.GetAvailableChips(), id => new LibGpiodDriver(id)), out GpioDriver? pi5Driver))
{
- throw new NotSupportedException("Couldn't find the default GPIO chip. You might need to create the LibGpiodDriver explicitly");
+ return pi5Driver;
}
- return new LibGpiodDriver(selectedChip.Id);
+ if (GpioDriver.TryCreate(() => CreatePi5Driver(LibGpiodV2Driver.GetAvailableChips(), id => new LibGpiodV2Driver(id)), out pi5Driver))
+ {
+ return pi5Driver;
+ }
+
+ throw new NotSupportedException("Couldn't find the default GPIO chip. You might need to create the LibGpiodDriver explicitly");
default:
@@ -482,6 +484,24 @@ private static GpioDriver GetBestDriverForBoardOnLinux()
}
}
+ ///
+ /// Creates a libgpiod-based driver for the Raspberry Pi 5 RP1 chip (54 lines).
+ ///
+ /// Available GPIO chips from a specific libgpiod version.
+ /// Factory to create the driver for the selected chip.
+ /// A GPIO driver for the RP1 chip.
+ /// The RP1 chip was not found.
+ private static GpioDriver CreatePi5Driver(IList chips, Func driverFactory)
+ {
+ var selectedChip = chips.FirstOrDefault(x => x.NumLines == 54);
+ if (selectedChip is null)
+ {
+ throw new PlatformNotSupportedException("Couldn't find the RP1 GPIO chip");
+ }
+
+ return driverFactory(selectedChip.Id);
+ }
+
///
/// Attempt to get the best applicable driver for the board the program is executing on.
///
diff --git a/src/System.Device.Gpio/System/Device/Gpio/RaspberryBoardInfo.cs b/src/System.Device.Gpio/System/Device/Gpio/RaspberryBoardInfo.cs
index 03d04e9cff..198b5139c0 100644
--- a/src/System.Device.Gpio/System/Device/Gpio/RaspberryBoardInfo.cs
+++ b/src/System.Device.Gpio/System/Device/Gpio/RaspberryBoardInfo.cs
@@ -172,7 +172,7 @@ public Model BoardModel
0x3111 or 0x3112 or 0x3114 or 0x3115 => Model.RaspberryPi4,
0x3140 or 0x3141 => Model.RaspberryPiComputeModule4,
0x3130 or 0x3131 => Model.RaspberryPi400,
- 0x4170 => Model.RaspberryPi5,
+ 0x4170 or 0x4171 => Model.RaspberryPi5,
_ => Model.Unknown,
};