Skip to content
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

SpiProxy is still unsound, even in a single thread #23

Open
GrantM11235 opened this issue Aug 1, 2021 · 1 comment
Open

SpiProxy is still unsound, even in a single thread #23

GrantM11235 opened this issue Aug 1, 2021 · 1 comment

Comments

@GrantM11235
Copy link

The "fix" for #8 assumes that drivers will only activate their CS right before communicating and deactivate it right afterwards and that there is no way for any other code in the same thread to do anything else in that time. However, this is not part of any API contract and drivers don't always work this way in practice.

The following hypothetical driver is perfectly valid, but using it with SpiProxy would lead to some unfortunate bugs.

pub struct DoomsdayDevice<SPI, CS> {
    spi: SPI,
    cs: CS,
}

impl<SPI, CS> DoomsdayDevice<SPI, CS>
where
    SPI: embedded_hal::blocking::spi::Write<u8>,
    CS: embedded_hal::digital::v2::OutputPin,
    CS::Error: core::fmt::Debug,
{
    pub fn new(spi: SPI, mut cs: CS) -> Self {
        // We own the SPI struct, so it is fine to just keep the CS pin active (low)
        cs.set_low().unwrap();
        Self { spi, cs }
    }

    pub fn detonate(&mut self) -> Result<(), SPI::Error> {
        // Send the super-secure password to the device, ending all life on earth
        self.spi.write(&[0x00])
    }

    pub fn split(self) -> (SPI, CS) {
        let Self { spi, mut cs } = self;
        // Set the CS pin to idle (high)
        cs.set_high().unwrap();
        (spi, cs)
    }
}

For my suggestion about how to fix this problem, see rust-embedded/embedded-hal#299

@Rahix
Copy link
Owner

Rahix commented Aug 3, 2021

Ack, I am aware of this. I'm in the process of writing up a response to the issue you linked, but didn't get to finishing it yet. Sorry for the radio silence in the last week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants