spi shared bus: assert/deassert CS inside the lock.

This commit is contained in:
Dario Nieuwenhuis 2022-07-06 16:57:29 +02:00
parent ba2412ff7e
commit 455374b7f9

View File

@ -37,22 +37,21 @@ where
type Bus = BUS; type Bus = BUS;
fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> { fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> {
self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; self.bus.lock(|bus| {
let (f_res, flush_res) = self.bus.lock(|bus| {
let mut bus = bus.borrow_mut(); let mut bus = bus.borrow_mut();
self.cs.set_low().map_err(SpiBusDeviceError::Cs)?;
let f_res = f(&mut bus); let f_res = f(&mut bus);
// On failure, it's important to still flush and deassert CS. // On failure, it's important to still flush and deassert CS.
let flush_res = bus.flush(); let flush_res = bus.flush();
(f_res, flush_res) let cs_res = self.cs.set_high();
});
let cs_res = self.cs.set_high(); let f_res = f_res.map_err(SpiBusDeviceError::Spi)?;
flush_res.map_err(SpiBusDeviceError::Spi)?;
cs_res.map_err(SpiBusDeviceError::Cs)?;
let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; Ok(f_res)
flush_res.map_err(SpiBusDeviceError::Spi)?; })
cs_res.map_err(SpiBusDeviceError::Cs)?;
Ok(f_res)
} }
} }