Fix a crash when dropping SpiDriver

This commit is contained in:
ivmarkov 2024-10-29 16:04:37 +00:00
parent 7d5e233ac9
commit 30d4874def

View File

@ -623,7 +623,7 @@ pub struct SpiBusDriver<'d, T>
where where
T: BorrowMut<SpiDriver<'d>>, T: BorrowMut<SpiDriver<'d>>,
{ {
_lock: BusLock, lock: Option<BusLock>,
handle: spi_device_handle_t, handle: spi_device_handle_t,
driver: T, driver: T,
duplex: Duplex, duplex: Duplex,
@ -646,7 +646,7 @@ where
let lock = BusLock::new(handle)?; let lock = BusLock::new(handle)?;
Ok(Self { Ok(Self {
_lock: lock, lock: Some(lock),
handle, handle,
driver, driver,
duplex: config.duplex, duplex: config.duplex,
@ -825,6 +825,10 @@ where
T: BorrowMut<SpiDriver<'d>>, T: BorrowMut<SpiDriver<'d>>,
{ {
fn drop(&mut self) { 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(); esp!(unsafe { spi_bus_remove_device(self.handle) }).unwrap();
} }
} }