From 455374b7f9da0ebd55849e47d7800e313197cb33 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 6 Jul 2022 16:57:29 +0200 Subject: [PATCH] spi shared bus: assert/deassert CS inside the lock. --- .../src/shared_bus/blocking/spi.rs | 21 +++++++++---------- 1 file 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 cf9b55db..c08bcbf6 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 type Bus = BUS; fn transaction(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result) -> Result { - self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; - - let (f_res, flush_res) = self.bus.lock(|bus| { + self.bus.lock(|bus| { let mut bus = bus.borrow_mut(); + self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; + let f_res = f(&mut bus); + // On failure, it's important to still flush and deassert CS. 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)?; - flush_res.map_err(SpiBusDeviceError::Spi)?; - cs_res.map_err(SpiBusDeviceError::Cs)?; - - Ok(f_res) + Ok(f_res) + }) } }