From 3eacbf733c044cdc9fb9a1e62e09d84e5b5fbbcc Mon Sep 17 00:00:00 2001 From: Wojciech Olech Date: Fri, 1 Sep 2023 16:24:55 +0200 Subject: [PATCH] CriticalSection: Moved to Aerugo, removed from HAL --- Cargo.toml | 1 + aerugo-hal/Cargo.toml | 1 - aerugo-hal/src/config.rs | 3 -- aerugo-hal/src/lib.rs | 23 --------------- arch/cortex-m/aerugo-samv71-hal/Cargo.toml | 1 + arch/cortex-m/aerugo-samv71-hal/src/hal.rs | 33 ++++------------------ arch/x86/aerugo-x86-hal/src/hal.rs | 18 +----------- src/aerugo.rs | 14 ++------- src/api/runtime_api.rs | 12 +------- testbins/test-hal-watchdog/src/main.rs | 1 - 10 files changed, 12 insertions(+), 95 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b7c8627b..9e9d66fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ aerugo-hal = { version = "0.1.0", path = "aerugo-hal" } aerugo-samv71-hal = { version = "0.1.0", path = "arch/cortex-m/aerugo-samv71-hal", optional = true } aerugo-x86 = { version = "0.1.0", path = "arch/x86/aerugo-x86", optional = true } aerugo-x86-hal = { version = "0.1.0", path = "arch/x86/aerugo-x86-hal", optional = true } +critical-section = "1.1.2" env-parser = { version = "1.0.0", path = "utils/env-parser" } heapless = "0.7" internal-cell = { version = "0.0.1", path = "utils/internal_cell" } diff --git a/aerugo-hal/Cargo.toml b/aerugo-hal/Cargo.toml index 3a0bfe8b..cab3e6da 100644 --- a/aerugo-hal/Cargo.toml +++ b/aerugo-hal/Cargo.toml @@ -10,5 +10,4 @@ license.workspace = true description = "Hardware Abstraction Layer for Aerugo system" [dependencies] -critical-section = "1.1.2" fugit = "0.3.6" diff --git a/aerugo-hal/src/config.rs b/aerugo-hal/src/config.rs index a5e276bd..e5f5ce2d 100644 --- a/aerugo-hal/src/config.rs +++ b/aerugo-hal/src/config.rs @@ -6,15 +6,12 @@ use crate::time; pub struct SystemHardwareConfig { /// Timeout for the watchdog. pub watchdog_timeout: time::MillisDurationU32, - /// If true, all interrupts will be disabled until `AERUGO.start()` is called. - pub disable_interrupts_during_setup: bool, } impl Default for SystemHardwareConfig { fn default() -> Self { SystemHardwareConfig { watchdog_timeout: time::MillisDurationU32::secs(3), - disable_interrupts_during_setup: true, } } } diff --git a/aerugo-hal/src/lib.rs b/aerugo-hal/src/lib.rs index 5e9ab702..600c1b79 100644 --- a/aerugo-hal/src/lib.rs +++ b/aerugo-hal/src/lib.rs @@ -11,8 +11,6 @@ HAL (Hardware Abstract Layer) for Aerugo system. mod config; pub use config::SystemHardwareConfig; -pub use critical_section; -pub use critical_section::CriticalSection; pub use fugit as time; /// Constant representing system timer frequency. @@ -42,25 +40,4 @@ pub trait AerugoHal { /// Feeds the system watchdog. fn feed_watchdog(); - - /// Enters critical section - fn enter_critical(); - - /// Exits critical section - fn exit_critical(); - - /// Executes closure `f` in an interrupt-free context. - /// - /// # Generic Parameters - /// * `F` - Closure type. - /// * `R` - Closure return type. - /// - /// # Parameters - /// * `f` - Closure to execute. - /// - /// # Return - /// Closure result. - fn execute_critical(f: F) -> R - where - F: FnOnce(CriticalSection) -> R; } diff --git a/arch/cortex-m/aerugo-samv71-hal/Cargo.toml b/arch/cortex-m/aerugo-samv71-hal/Cargo.toml index 11a8c0de..0a571abb 100644 --- a/arch/cortex-m/aerugo-samv71-hal/Cargo.toml +++ b/arch/cortex-m/aerugo-samv71-hal/Cargo.toml @@ -11,6 +11,7 @@ description = "Crate with AerugoHal implementation for SAMV71" [dependencies] aerugo-hal = { version = "0.1.0", path = "../../../aerugo-hal" } +critical-section = "1.1.2" samv71-hal = { version = "0.1.0", path = "../samv71-hal" } [features] diff --git a/arch/cortex-m/aerugo-samv71-hal/src/hal.rs b/arch/cortex-m/aerugo-samv71-hal/src/hal.rs index 13518a06..c96400ea 100644 --- a/arch/cortex-m/aerugo-samv71-hal/src/hal.rs +++ b/arch/cortex-m/aerugo-samv71-hal/src/hal.rs @@ -1,7 +1,6 @@ //! System HAL implementation for Cortex-M SAMV71 target. -use aerugo_hal::critical_section; -use aerugo_hal::{AerugoHal, CriticalSection, Instant, SystemHardwareConfig}; +use aerugo_hal::{AerugoHal, Instant, SystemHardwareConfig}; use crate::cortex_m; use crate::error::HalError; @@ -23,6 +22,8 @@ use samv71_hal::watchdog::{Watchdog, WatchdogConfig}; /// Safety of this cell is managed by HAL instead, guaranteeing that undefined behavior will not occur. static mut HAL_SYSTEM_PERIPHERALS: Option = None; +/// Global "restore state" that's used to manage + /// HAL implementation for Cortex-M based SAMV71 MCU. pub struct Hal; @@ -47,7 +48,7 @@ impl Hal { /// [`Some(UserPeripherals)`] if called for the first time after HAL initialization, /// [`None`] otherwise. pub fn create_user_peripherals() -> Option { - Hal::execute_critical(|_| { + critical_section::with(|_| { if let Some(system_peripherals) = unsafe { &mut HAL_SYSTEM_PERIPHERALS } { let mcu_peripherals = unsafe { pac::Peripherals::steal() }; let core_peripherals = unsafe { pac::CorePeripherals::steal() }; @@ -134,7 +135,7 @@ impl AerugoHal for Hal { /// # Return /// `()` on success, [`HalError`] if HAL was already initialized. fn configure_hardware(config: SystemHardwareConfig) -> Result<(), HalError> { - let result = Hal::execute_critical(|_| { + let result = critical_section::with(|_| { Hal::initialize()?; // SAFETY: Immutable access to system peripherals is safe, as we're in critical section @@ -186,10 +187,6 @@ impl AerugoHal for Hal { Ok(()) }); - if config.disable_interrupts_during_setup { - Hal::enter_critical(); - } - result } @@ -234,26 +231,6 @@ impl AerugoHal for Hal { peripherals.watchdog.feed(); } - - /// Enters critical section by disabling global interrupts. - fn enter_critical() { - cortex_m::interrupt::disable(); - } - - /// Exits critical section by enabling global interrupts. - /// - /// # Safety - ///
This function should never be called from scope-bound critical sections (like the one created with AerugoHal::execute_critical)
- fn exit_critical() { - unsafe { cortex_m::interrupt::enable() }; - } - - fn execute_critical(f: F) -> R - where - F: FnOnce(CriticalSection) -> R, - { - critical_section::with(f) - } } /// Type representing all TC0 channels in Waveform mode. diff --git a/arch/x86/aerugo-x86-hal/src/hal.rs b/arch/x86/aerugo-x86-hal/src/hal.rs index c6a50d8e..3577142b 100644 --- a/arch/x86/aerugo-x86-hal/src/hal.rs +++ b/arch/x86/aerugo-x86-hal/src/hal.rs @@ -4,7 +4,7 @@ use std::convert::TryInto; use std::time::SystemTime; use aerugo_hal::Instant; -use aerugo_hal::{AerugoHal, CriticalSection, SystemHardwareConfig}; +use aerugo_hal::{AerugoHal, SystemHardwareConfig}; use once_cell::sync::Lazy; use crate::error::HalError; @@ -45,20 +45,4 @@ impl AerugoHal for Hal { fn feed_watchdog() { // There is no watchdog for x86 target. } - - fn enter_critical() { - // There is no critical section implementation for x86 target. - } - - fn exit_critical() { - // There is no critical section implementation for x86 target. - } - - fn execute_critical(f: F) -> R - where - F: FnOnce(CriticalSection) -> R, - { - // There is no critical section implementation for x86 target. - f(unsafe { CriticalSection::new() }) - } } diff --git a/src/aerugo.rs b/src/aerugo.rs index 14c4a993..af6c91cd 100644 --- a/src/aerugo.rs +++ b/src/aerugo.rs @@ -5,7 +5,8 @@ //! //! This module also contains singleton instances of all system parts. -use aerugo_hal::{AerugoHal, CriticalSection, SystemHardwareConfig}; +use aerugo_hal::{AerugoHal, SystemHardwareConfig}; +use critical_section::CriticalSection; use env_parser::read_env; use crate::api::{InitApi, InitError, RuntimeApi, RuntimeError, SystemApi}; @@ -93,7 +94,6 @@ impl Aerugo { unsafe { self.time_source.mark_system_start(); } - Hal::exit_critical(); EXECUTOR.run_scheduler() } } @@ -745,19 +745,11 @@ impl RuntimeApi for Aerugo { todo!() } - fn enter_critical() { - Hal::enter_critical(); - } - - fn exit_critical() { - Hal::exit_critical(); - } - fn execute_critical(f: F) -> R where F: FnOnce(CriticalSection) -> R, { - Hal::execute_critical(f) + critical_section::with(f) } } diff --git a/src/api/runtime_api.rs b/src/api/runtime_api.rs index 4e1bd9fb..9c23bc86 100644 --- a/src/api/runtime_api.rs +++ b/src/api/runtime_api.rs @@ -2,7 +2,7 @@ //! //! This API can be used by the user in tasklet functions to interact with the system. -use aerugo_hal::CriticalSection; +use critical_section::CriticalSection; use crate::api::RuntimeError; use crate::event::EventId; @@ -62,16 +62,6 @@ pub trait RuntimeApi { /// Execution statistics for this tasklet. fn get_execution_statistics(&'static self, task_id: TaskletId) -> ExecutionStats; - /// Enters critical section - fn enter_critical() - where - Self: Sized; - - /// Exits critical section - fn exit_critical() - where - Self: Sized; - /// Executes closure `f` in an interrupt-free context. /// /// # Generic Parameters diff --git a/testbins/test-hal-watchdog/src/main.rs b/testbins/test-hal-watchdog/src/main.rs index df132c1c..f6b641ae 100644 --- a/testbins/test-hal-watchdog/src/main.rs +++ b/testbins/test-hal-watchdog/src/main.rs @@ -103,7 +103,6 @@ fn main() -> ! { AERUGO.initialize(SystemHardwareConfig { watchdog_timeout: Milliseconds::secs(5), - ..Default::default() }); initialize_tasks();