From 91ebdc3e3d9476696257c93b9a73bf97751b24e2 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 1 Aug 2022 10:03:39 +0200 Subject: [PATCH 1/2] Leveraging shared_bus over custom mutex --- src/hardware/mod.rs | 5 ++--- src/hardware/mutex.rs | 52 ------------------------------------------- src/hardware/setup.rs | 4 ++-- 3 files changed, 4 insertions(+), 57 deletions(-) delete mode 100644 src/hardware/mutex.rs diff --git a/src/hardware/mod.rs b/src/hardware/mod.rs index 1ccafb72..17f0c292 100644 --- a/src/hardware/mod.rs +++ b/src/hardware/mod.rs @@ -15,7 +15,6 @@ pub mod chassis_fans; pub mod delay; pub mod external_mac; pub mod metadata; -mod mutex; pub mod net_interface; pub mod platform; pub mod rf_channel; @@ -69,8 +68,8 @@ pub type NetworkManager = external_mac::Manager<'static, ExternalMac>; pub type NetworkStack = smoltcp_nal::NetworkStack<'static, external_mac::SmoltcpDevice<'static>, SystemTimer>; -pub type I2cBusManager = mutex::AtomicCheckManager; -pub type I2cProxy = shared_bus::I2cProxy<'static, mutex::AtomicCheckMutex>; +pub type I2cBusManager = shared_bus::BusManagerAtomicCheck; +pub type I2cProxy = shared_bus::I2cProxy<'static, shared_bus::AtomicCheckMutex>; pub type I2cError = hal::i2c::Error; pub type UsbBus = hal::otg_fs::UsbBus; diff --git a/src/hardware/mutex.rs b/src/hardware/mutex.rs deleted file mode 100644 index 7b643b87..00000000 --- a/src/hardware/mutex.rs +++ /dev/null @@ -1,52 +0,0 @@ -/// A "mutex" that guarantees only a single user of a bus at a time through panicking. -pub struct AtomicCheckMutex { - bus: core::cell::UnsafeCell, - busy: core::sync::atomic::AtomicBool, -} - -impl shared_bus::BusMutex for AtomicCheckMutex { - type Bus = BUS; - - fn create(v: BUS) -> Self { - Self { - bus: core::cell::UnsafeCell::new(v), - busy: core::sync::atomic::AtomicBool::from(false), - } - } - - fn lock R>(&self, f: F) -> R { - self.busy - .compare_exchange( - false, - true, - core::sync::atomic::Ordering::SeqCst, - core::sync::atomic::Ordering::SeqCst, - ) - .expect("Bus conflict"); - let result = f(unsafe { &mut *self.bus.get() }); - - self.busy.store(false, core::sync::atomic::Ordering::SeqCst); - - result - } -} - -// It is explicitly safe to share this across threads because there is a coherency check using an -// atomic bool comparison. -unsafe impl Sync for AtomicCheckMutex {} - -/// A convenience type for declaring use with shared-bus. -pub type AtomicCheckManager = shared_bus::BusManager>; - -/// Construct a statically allocated bus manager. -#[macro_export] -macro_rules! new_atomic_check_manager { - ($bus_type:ty = $bus:expr) => {{ - let m: Option<&'static mut _> = cortex_m::singleton!( - : $crate::hardware::mutex::AtomicCheckManager<$bus_type> = - $crate::hardware::mutex::AtomicCheckManager::new($bus) - ); - - m - }}; -} diff --git a/src/hardware/setup.rs b/src/hardware/setup.rs index e8acafe6..dcfbd07b 100644 --- a/src/hardware/setup.rs +++ b/src/hardware/setup.rs @@ -17,7 +17,7 @@ use super::{ HardwareVersion, NetworkManager, NetworkStack, SystemTimer, Systick, UsbBus, CPU_FREQ, I2C, }; -use crate::{new_atomic_check_manager, settings::BoosterSettings}; +use crate::settings::BoosterSettings; use stm32f4xx_hal as hal; @@ -189,7 +189,7 @@ pub fn setup( hal::i2c::I2c::i2c1(i2c_peripheral, (scl, sda), 100.khz(), clocks) }; - new_atomic_check_manager!(I2C = i2c).unwrap() + shared_bus::new_atomic_check!(I2C = i2c).unwrap() }; // Instantiate the I2C interface to the I2C mux. Use a shared-bus so we can share the I2C From 82ca34ca84a27b33c13d01d1d93378f156a805c0 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 1 Aug 2022 12:22:13 +0200 Subject: [PATCH 2/2] Adding CHANGELOG entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94db779c..5164e46f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Settings are backwards compatible with previous Booster releases. * Fan speed is now stored in EEPROM and configurable via the serial interface. +### Changed +* Removed custom `mutex` implementation in favor of leveraging `shared-bus` + ### Fixed * A few dependencies were deprecated because changes landed upstream. These dependencies were corrected to point upstream.