nrf-rs / nrf-hal

A Rust HAL for the nRF family of devices
Apache License 2.0
499 stars 139 forks source link

SPI master (spim) stops working after `.free()` #392

Open eflukx opened 2 years ago

eflukx commented 2 years ago

Hi I'm building a battery-powered sensing device with Rust, based on the nRF52833.

For conserving power, I need to disable the SPI peripheral when not in use. Currently a nice solution for enabling/disabling peripherals is not provided in the nrf-hal (tracking #279), so for the moment I will be juggling the SPI in a wrapping enum encapsulating the enabled and disabled types/modes.

I ran into an unexpected problem though:

It seems something is not working well when creating the new spim instance from the parts returned by spidev.free()

Freeing and re-creating the SPI instance:

    let (spim, pins) = spidev.free();
    let mut spidev = spim::Spim::new(spim, pins, spim::Frequency::M16, spim::MODE_0, 0);

for completeness, initial initialization is done like this:

    let epd_mosi = port1.p1_06.into_push_pull_output(Level::Low).degrade();
    let epd_sck = port1.p1_07.into_push_pull_output(Level::Low).degrade();

    let mut epd_spi = spim::Spim::new(
        periphs.SPIM2,
        spim::Pins {
            sck: epd_sck,
            miso: None,
            mosi: Some(epd_mosi),
        },
        spim::Frequency::M16,
        spim::MODE_0,
        0,
    );
jonas-schievink commented 2 years ago

Looks like we never reset the enable register in free, that might cause this.