Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/System.Device.Gpio/System/Device/Gpio/GpioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,25 +463,45 @@ 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:

return UnixDriver.Create();
}
}

/// <summary>
/// Creates a libgpiod-based driver for the Raspberry Pi 5 RP1 chip (54 lines).
/// </summary>
/// <param name="chips">Available GPIO chips from a specific libgpiod version.</param>
/// <param name="driverFactory">Factory to create the driver for the selected chip.</param>
/// <returns>A GPIO driver for the RP1 chip.</returns>
/// <exception cref="PlatformNotSupportedException">The RP1 chip was not found.</exception>
private static GpioDriver CreatePi5Driver(IList<GpioChipInfo> chips, Func<int, GpioDriver> 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);
}

/// <summary>
/// Attempt to get the best applicable driver for the board the program is executing on.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
Loading