diff --git a/src/spi.rs b/src/spi.rs index 6239a91068d..559b444b8d5 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -623,7 +623,7 @@ pub struct SpiBusDriver<'d, T> where T: BorrowMut>, { - _lock: BusLock, + lock: Option, handle: spi_device_handle_t, driver: T, duplex: Duplex, @@ -646,7 +646,7 @@ where let lock = BusLock::new(handle)?; Ok(Self { - _lock: lock, + lock: Some(lock), handle, driver, duplex: config.duplex, @@ -825,6 +825,10 @@ where T: BorrowMut>, { fn drop(&mut self) { + // Need to drop the lock first, because it holds the device + // we are about to remove below + self.lock = None; + esp!(unsafe { spi_bus_remove_device(self.handle) }).unwrap(); } }