Skip to content

Commit

Permalink
CriticalSection: Moved to Aerugo, removed from HAL
Browse files Browse the repository at this point in the history
  • Loading branch information
SteelPh0enix committed Sep 1, 2023
1 parent b4360ff commit 3eacbf7
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 95 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 0 additions & 1 deletion aerugo-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ license.workspace = true
description = "Hardware Abstraction Layer for Aerugo system"

[dependencies]
critical-section = "1.1.2"
fugit = "0.3.6"
3 changes: 0 additions & 3 deletions aerugo-hal/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
23 changes: 0 additions & 23 deletions aerugo-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, R>(f: F) -> R
where
F: FnOnce(CriticalSection) -> R;
}
1 change: 1 addition & 0 deletions arch/cortex-m/aerugo-samv71-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
33 changes: 5 additions & 28 deletions arch/cortex-m/aerugo-samv71-hal/src/hal.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SystemPeripherals> = None;

/// Global "restore state" that's used to manage

/// HAL implementation for Cortex-M based SAMV71 MCU.
pub struct Hal;

Expand All @@ -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<UserPeripherals> {
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() };
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -186,10 +187,6 @@ impl AerugoHal for Hal {
Ok(())
});

if config.disable_interrupts_during_setup {
Hal::enter_critical();
}

result
}

Expand Down Expand Up @@ -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
/// <div class="warning">This function should never be called from scope-bound critical sections (like the one created with <code>AerugoHal::execute_critical</code>)</div>
fn exit_critical() {
unsafe { cortex_m::interrupt::enable() };
}

fn execute_critical<F, R>(f: F) -> R
where
F: FnOnce(CriticalSection) -> R,
{
critical_section::with(f)
}
}

/// Type representing all TC0 channels in Waveform mode.
Expand Down
18 changes: 1 addition & 17 deletions arch/x86/aerugo-x86-hal/src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, R>(f: F) -> R
where
F: FnOnce(CriticalSection) -> R,
{
// There is no critical section implementation for x86 target.
f(unsafe { CriticalSection::new() })
}
}
14 changes: 3 additions & 11 deletions src/aerugo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -93,7 +94,6 @@ impl Aerugo {
unsafe {
self.time_source.mark_system_start();
}
Hal::exit_critical();
EXECUTOR.run_scheduler()
}
}
Expand Down Expand Up @@ -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, R>(f: F) -> R
where
F: FnOnce(CriticalSection) -> R,
{
Hal::execute_critical(f)
critical_section::with(f)
}
}

Expand Down
12 changes: 1 addition & 11 deletions src/api/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion testbins/test-hal-watchdog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ fn main() -> ! {

AERUGO.initialize(SystemHardwareConfig {
watchdog_timeout: Milliseconds::secs(5),
..Default::default()
});

initialize_tasks();
Expand Down

0 comments on commit 3eacbf7

Please sign in to comment.