golemparts / rppal

A Rust library that provides access to the Raspberry Pi's GPIO, I2C, PWM, SPI and UART peripherals.
MIT License
1.25k stars 98 forks source link

Input pin mode not set correctly on Raspberry Pi 5 #142

Closed lukeburong closed 7 months ago

lukeburong commented 7 months ago

Hello,

In some circumstances, the GPIO pin mode is not set correctly when creating an InputPin on the Raspberry Pi 5.

For example:

use std::error::Error;

use rppal::gpio::Gpio;

fn main() -> Result<(), Box<dyn Error>> {
    let mut pin = Gpio::new()?.get(25)?.into_input();
    pin.set_reset_on_drop(false);

    Ok(())
}

pinctrl 25 output before:

25: no    pd | -- // GPIO25 = none

pinctrl 25 output after:

25: no    pn | -- // GPIO25 = none

As you can see, the pull-down resistor on GPIO25 has been disabled, but the pin is not set as an input (and does not function as one).

I believe this may occur due to rppal not handling FUNCSEL in the relevant GPIO CTRL register being 31 (i.e. NULL, see https://datasheets.raspberrypi.com/rp1/rp1-peripherals.pdf page 22)

This causes the following match expression to hit the wildcard pattern and return Mode::Input:

https://github.com/golemparts/rppal/blob/77ec1098f0df1d295de9db0e481abe5e27d9ed2e/src/gpio/gpiomem/rp1.rs#L226

Meaning InputPin::new does not set the pin mode as it believes that it is already set to input:

https://github.com/golemparts/rppal/blob/77ec1098f0df1d295de9db0e481abe5e27d9ed2e/src/gpio/pin.rs#L431-L435

golemparts commented 7 months ago

Thank you for reporting this issue. I've seen a few Pi 5 specific problems reported lately. Unfortunately I haven't had a Pi 5 available for testing to check these out myself, but I should have one coming in this or next week.

It does sound like your assessment of the cause of the problem is likely correct, but I'm hesitant to implement a fix I can't verify. You're welcome to submit a PR that addresses FUNCSEL being set to NULL (31) on the Pi 5, which I'm happy to merge so you can continue working on your project, and then release the update as soon as I'm able to check things on a Pi 5 myself.