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,16 +37,14 @@ 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.bus.lock(|bus| {
let mut bus = bus.borrow_mut();
self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; self.cs.set_low().map_err(SpiBusDeviceError::Cs)?;
let (f_res, flush_res) = self.bus.lock(|bus| {
let mut bus = bus.borrow_mut();
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)?; let f_res = f_res.map_err(SpiBusDeviceError::Spi)?;
@ -54,5 +52,6 @@ where
cs_res.map_err(SpiBusDeviceError::Cs)?; cs_res.map_err(SpiBusDeviceError::Cs)?;
Ok(f_res) Ok(f_res)
})
} }
} }