-
Notifications
You must be signed in to change notification settings - Fork 594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error Toggling GPIO pin #2384
Comments
Hi @rathbone01, sorry for the problem. Your exception shows The Raspberry boards continuously change revision and model numbers and this may affect the ability to recognize the board. This in turn affects the driver detection algorithm. Please provide the model number of your CM4 as described here: The detection happens with the following model numbers: https://github.com/dotnet/iot/blob/main/src/System.Device.Gpio/System/Device/Gpio/RaspberryBoardInfo.cs#L155 Please post the controller initialization code too. |
Alright, I will provide that when I get a chance. Here is where I init: public bool Initalized { get; private set; } = false;
private const int powerPanicPin = 14;
private bool powerPanic = false;
private int debounce = 10;
private const int stResetPin = 27;
private const int actLedPin = 10;
private const int usbPowerControl = 3;
private const int piPowerOverrride = 11;
private ILogger<GpioService>? _logger;
private GpioController? controller;
private GapMonitorService? GapMonitorService;
private List<int> relays = new() { 5, 6, 13, 19, 26, 7, 8, 9 }; // 1 is gpio 5 on rev D
private IServiceProvider serviceProvider;
public GpioService(ILogger<GpioService> logger, IServiceProvider serviceProvider)
{
_logger = logger;
_logger.LogInformation("GpioService created");
this.serviceProvider = serviceProvider;
}
public void Setup()
{
// if not on linux, don't setup the GPIO
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_logger?.LogInformation("Not on linux, skipping GPIO setup");
return;
}
GapMonitorService = serviceProvider?.GetService<GapMonitorService>();
if (GapMonitorService is null)
_logger?.LogError("GpioService: LevelMonitorService is null");
controller = new GpioController();
// Setup power signal pin
controller.OpenPin(usbPowerControl, PinMode.Output, PinValue.High);
// Setup power override pin
controller.OpenPin(piPowerOverrride, PinMode.Output, PinValue.Low);
// Setup power panic pin
controller.OpenPin(powerPanicPin, PinMode.InputPullUp);
controller.RegisterCallbackForPinValueChangedEvent(powerPanicPin, PinEventTypes.Falling, PowerPanicPinChanged);
foreach (int i in relays)
{
controller.OpenPin(i, PinMode.Output, PinValue.Low);
}
_logger?.LogInformation("GpioService setup");
Initalized = true;
// Start blinking light
BlinkLoop();
} |
The So the problem is that you probably does not initialize the pin by reading or writing an initial state. |
would this also work, setting it when it is opened? controller.OpenPin(actLedPin, PinMode.Output, PinValue.High); |
Yes:
public GpioPin OpenPin(int pinNumber, PinMode mode, PinValue initialValue)
{
var pin = OpenPin(pinNumber);
// Set the desired initial value
_openPins[pinNumber] = initialValue;
SetPinMode(pinNumber, mode);
return pin;
} |
Alright thanks! |
@rathbone01 We would appreciate if you could run the diagnostics on your Raspberry to verify the id number of your CM4 board. |
Probably more than you wanted, but here is the output of each of those commands: FlowTran@CBMD-9d56:/ $ cat /proc/cpuinfo processor : 1 processor : 2 processor : 3 Revision : b03141
|
Thank you. Apparently the "b03141" should match @pgrawehr I believe the "b" pre-pending the revision of this CM4 board is killing the detection. What do you think? |
Correct, the b will be included in the tryparse as a hex value |
int.TryParse("b03141", NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int firmware);
Console.WriteLine("0x" + firmware.ToString("X"));
string Connections = File.ReadAllText("connections.txt");
if (firmware == 0x3141)
{
Console.WriteLine("Firmware is 0x3141");
}
else if (firmware == 0xB03141)
{
Console.WriteLine("Firmware is 0xB03141");
}
else
{
Console.WriteLine("Firmware is unknown");
} gives the following output: |
@raffaeler we should fix issue detection or create a new issue detailing what needs to be done before we close this |
btw there is masking in that logic so in theory that should work |
Ok, I just did some quick checks to make sure this works as expected: bool success = int.TryParse("b03141", NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int firmware);
Console.WriteLine($"TryParse: {success}");
Console.WriteLine($"Firmware: {firmware:X2}");
var maskedFirmware = firmware & 0xFFFF;
Console.WriteLine($"Firmware (masked): {maskedFirmware:X2}"); and that worked as expected. I believe the problem will be in the settings loading: |
Totally agree, this is why I asked the OP to report the board result here. Given the growing revision numbers, I believe it would be better to entirely change the detection algorithm and search for the model string instead:
|
@rathbone01 could you please, cut-paste this class and run the detection code method on your CM4 to understand if this fails? Thank you |
Also if you find a simple way to fix please send it here (or send a PR) |
@rathbone01 Can you provide the output of Also, I think we can remove the |
Describe the bug
When trying to use a GpioController and opening pin 10 as an output on a RasPi CM4, then call .Toggle I am getting an exception.
Steps to reproduce
Expected behavior
I expect that Gpio to toggle on and off.
Actual behavior
Get the following exception:
2025-02-21 08:35:49.307 -06:00 [ERR] Error in BlinkLoop: The given key '10' was not present in the dictionary.: at System.Collections.Concurrent.ConcurrentDictionary
2.ThrowKeyNotFoundException(TKey key) at System.Collections.Concurrent.ConcurrentDictionary
2.get_Item(TKey key)at System.Device.Gpio.Drivers.Libgpiod.V1.LibGpiodV1Driver.Toggle(Int32 pinNumber)
at System.Device.Gpio.Drivers.RaspberryPi3LinuxDriver.Toggle(Int32 pinNumber)
at System.Device.Gpio.Drivers.RaspberryPi3Driver.Toggle(Int32 pinNumber)
at System.Device.Gpio.GpioController.Toggle(Int32 pinNumber)
at GapMonitor.Services.GpioService.BlinkLoop() in C:\Users\heatb\Desktop\GitHub_Repositories\DaTran-Fueling-Server\GapMonitor\Services\GpioService.cs:line 102
Versions used
8.0.8 -
dotnet --info
on the machine being used to build8.0.8 -
dotnet --info
on the machine where app is being run (not applicable for self-contained apps)3.2.0 - Version of
System.Device.Gpio
package3.2.0 - Version of
Iot.Device.Bindings
package (not needed if bug is inSystem.Device.Gpio
)The text was updated successfully, but these errors were encountered: