diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-07-06 16:57:29 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-07-06 16:57:29 +0200 |
| commit | 455374b7f9da0ebd55849e47d7800e313197cb33 (patch) | |
| tree | 24ab8956f759f1eb148c7d88673a93a6f342ad62 | |
| parent | ba2412ff7ea1eeb5eb1a09dad4399214f0e22d1e (diff) | |
spi shared bus: assert/deassert CS inside the lock.
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index cf9b55db9..c08bcbf62 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |||
| @@ -37,22 +37,21 @@ where | |||
| 37 | type Bus = BUS; | 37 | type Bus = BUS; |
| 38 | 38 | ||
| 39 | fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> { | 39 | fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> { |
| 40 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | 40 | self.bus.lock(|bus| { |
| 41 | |||
| 42 | let (f_res, flush_res) = self.bus.lock(|bus| { | ||
| 43 | let mut bus = bus.borrow_mut(); | 41 | let mut bus = bus.borrow_mut(); |
| 42 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 43 | |||
| 44 | let f_res = f(&mut bus); | 44 | let f_res = f(&mut bus); |
| 45 | |||
| 45 | // On failure, it's important to still flush and deassert CS. | 46 | // On failure, it's important to still flush and deassert CS. |
| 46 | let flush_res = bus.flush(); | 47 | let flush_res = bus.flush(); |
| 47 | (f_res, flush_res) | 48 | let cs_res = self.cs.set_high(); |
| 48 | }); | ||
| 49 | |||
| 50 | let cs_res = self.cs.set_high(); | ||
| 51 | 49 | ||
| 52 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | 50 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; |
| 53 | flush_res.map_err(SpiBusDeviceError::Spi)?; | 51 | flush_res.map_err(SpiBusDeviceError::Spi)?; |
| 54 | cs_res.map_err(SpiBusDeviceError::Cs)?; | 52 | cs_res.map_err(SpiBusDeviceError::Cs)?; |
| 55 | 53 | ||
| 56 | Ok(f_res) | 54 | Ok(f_res) |
| 55 | }) | ||
| 57 | } | 56 | } |
| 58 | } | 57 | } |
