From 88e1802c166c123ea3f8c26e626d882976f8af31 Mon Sep 17 00:00:00 2001 From: Wojciech Olech Date: Wed, 30 Aug 2023 18:44:54 +0200 Subject: [PATCH 1/6] AerugoHal: Renamed `SystemHal` to `AerugoHal` and simplifed it's crate --- aerugo-hal/src/{system_hal => }/config.rs | 6 ++- aerugo-hal/src/lib.rs | 46 +++++++++++++++++- aerugo-hal/src/system_hal.rs | 48 ------------------- arch/cortex-m/samv71-hal/src/hal.rs | 12 ++--- .../samv71-hal/src/system_peripherals.rs | 2 +- arch/x86/x86-hal/src/hal.rs | 4 +- src/aerugo.rs | 2 +- src/lib.rs | 4 +- src/time_source.rs | 3 +- 9 files changed, 61 insertions(+), 66 deletions(-) rename aerugo-hal/src/{system_hal => }/config.rs (80%) delete mode 100644 aerugo-hal/src/system_hal.rs diff --git a/aerugo-hal/src/system_hal/config.rs b/aerugo-hal/src/config.rs similarity index 80% rename from aerugo-hal/src/system_hal/config.rs rename to aerugo-hal/src/config.rs index 83319473..2d370ca3 100644 --- a/aerugo-hal/src/system_hal/config.rs +++ b/aerugo-hal/src/config.rs @@ -1,9 +1,11 @@ //! System HAL configuration structures. +use crate::Duration; + /// System hardware configuration. pub struct SystemHardwareConfig { /// Timeout for the watchdog. - pub watchdog_timeout: crate::Duration, + pub watchdog_timeout: Duration, /// If true, all interrupts will be disabled until `AERUGO.start()` is called. pub disable_interrupts_during_setup: bool, } @@ -11,7 +13,7 @@ pub struct SystemHardwareConfig { impl Default for SystemHardwareConfig { fn default() -> Self { SystemHardwareConfig { - watchdog_timeout: crate::Duration::secs(1), + watchdog_timeout: Duration::secs(1), disable_interrupts_during_setup: true, } } diff --git a/aerugo-hal/src/lib.rs b/aerugo-hal/src/lib.rs index fc0a3c6b..eaf5070d 100644 --- a/aerugo-hal/src/lib.rs +++ b/aerugo-hal/src/lib.rs @@ -8,8 +8,11 @@ HAL (Hardware Abstract Layer) for Aerugo system. #![warn(clippy::missing_docs_in_private_items)] #![warn(rustdoc::missing_crate_level_docs)] -pub mod system_hal; +mod config; +use bare_metal::CriticalSection; + +pub use config::SystemHardwareConfig; pub use fugit as time; /// Constant representing system timer frequency. @@ -20,3 +23,44 @@ pub const SYSTEM_TIMER_FREQUENCY: u32 = 1_000_000; pub type Instant = time::TimerInstantU64; /// Type representing Aerugo duration. pub type Duration = time::TimerDurationU64; + +/// System HAL trait. +pub trait AerugoHal { + /// Type for system HAL error. + type Error; + + /// Configure system hardware. + /// + /// Implementation should initialize and configure all core system peripherals. + /// + /// # Parameters + /// * `config` - System hardware configuration. + fn configure_hardware(config: SystemHardwareConfig) -> Result<(), Self::Error>; + + /// Gets current system time timestamp. + fn get_system_time() -> Instant; + + /// 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/aerugo-hal/src/system_hal.rs b/aerugo-hal/src/system_hal.rs deleted file mode 100644 index a8bbc12a..00000000 --- a/aerugo-hal/src/system_hal.rs +++ /dev/null @@ -1,48 +0,0 @@ -//! System HAL. - -mod config; - -pub use self::config::SystemHardwareConfig; - -use bare_metal::CriticalSection; - -/// System HAL trait. -pub trait SystemHal { - /// Type for system HAL error. - type Error; - - /// Configure system hardware. - /// - /// Implementation should initialize and configure all core system peripherals. - /// - /// # Parameters - /// * `config` - System hardware configuration. - fn configure_hardware(config: SystemHardwareConfig) -> Result<(), Self::Error>; - - /// Gets current system time timestamp. - fn get_system_time() -> crate::Instant; - - /// 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/samv71-hal/src/hal.rs b/arch/cortex-m/samv71-hal/src/hal.rs index 9ec05909..d6f297b1 100644 --- a/arch/cortex-m/samv71-hal/src/hal.rs +++ b/arch/cortex-m/samv71-hal/src/hal.rs @@ -1,7 +1,7 @@ //! System HAL implementation for Cortex-M SAMV71 target. -use aerugo_hal::system_hal::{SystemHal, SystemHardwareConfig}; use aerugo_hal::Instant; +use aerugo_hal::{AerugoHal, SystemHardwareConfig}; use bare_metal::CriticalSection; use cortex_m::asm; @@ -38,7 +38,7 @@ impl Hal { /// /// Some of these peripherals are taken from SystemPeripherals structure, hence /// this function should not be called before finishing HAL initialization (via - /// [`SystemHal::configure_hardware] function). + /// [`AerugoHal::configure_hardware] function). /// /// This function executes in critical section, as it modifies HAL_SYSTEM_PERIPHERALS. /// @@ -76,7 +76,7 @@ impl Hal { /// Initializes global HAL instance using PAC peripherals. /// /// Calling this function begins HAL initialization process. This process must be finished - /// by calling [`SystemHal::configure_hardware`]. Until then, no other HAL functions should + /// by calling [`AerugoHal::configure_hardware`]. Until then, no other HAL functions should /// be called, as they will most likely fail. /// /// # Safety @@ -107,7 +107,7 @@ impl Hal { /// Creates system peripherals of HAL. /// /// This function steals PAC peripherals and returns a [`SystemPeripherals`] structure - /// containing peripherals used by [`SystemHal`] API implementation. + /// containing peripherals used by [`AerugoHal`] API implementation. /// /// # Safety /// This function should be only called once inside [`Hal::initialize`]. @@ -127,7 +127,7 @@ impl Hal { } } -impl SystemHal for Hal { +impl AerugoHal for Hal { type Error = HalError; /// This function performs SAMV71 hardware configuration required for the HAL to work correctly. @@ -254,7 +254,7 @@ impl SystemHal for Hal { /// Exits critical section by enabling global interrupts. /// /// # Safety - ///
This function should never be called from scope-bound critical sections (like the one created with SystemHal::execute_critical)
+ ///
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() }; } diff --git a/arch/cortex-m/samv71-hal/src/system_peripherals.rs b/arch/cortex-m/samv71-hal/src/system_peripherals.rs index 462e5dab..8030a88c 100644 --- a/arch/cortex-m/samv71-hal/src/system_peripherals.rs +++ b/arch/cortex-m/samv71-hal/src/system_peripherals.rs @@ -9,7 +9,7 @@ use crate::drivers::{ /// System peripherals structure. These peripherals are represented as HAL drivers. /// Some of these peripherals are available only during HAL initialization -/// (between `SystemHal::initialize` and `SystemHal::configure_hardware` calls). +/// (between `AerugoHal::initialize` and `AerugoHal::configure_hardware` calls). pub struct SystemPeripherals { /// Watchdog instance. pub watchdog: Watchdog, diff --git a/arch/x86/x86-hal/src/hal.rs b/arch/x86/x86-hal/src/hal.rs index c385f216..0527cde9 100644 --- a/arch/x86/x86-hal/src/hal.rs +++ b/arch/x86/x86-hal/src/hal.rs @@ -3,8 +3,8 @@ use std::convert::TryInto; use std::time::SystemTime; -use aerugo_hal::system_hal::{SystemHal, SystemHardwareConfig}; use aerugo_hal::Instant; +use aerugo_hal::{AerugoHal, SystemHardwareConfig}; use bare_metal::CriticalSection; use once_cell::sync::Lazy; @@ -24,7 +24,7 @@ impl Hal { } } -impl SystemHal for Hal { +impl AerugoHal for Hal { type Error = HalError; fn configure_hardware(_config: SystemHardwareConfig) -> Result<(), HalError> { diff --git a/src/aerugo.rs b/src/aerugo.rs index c6bcd2ea..cc81438e 100644 --- a/src/aerugo.rs +++ b/src/aerugo.rs @@ -5,7 +5,7 @@ //! //! This module also contains singleton instances of all system parts. -use aerugo_hal::system_hal::{SystemHal, SystemHardwareConfig}; +use aerugo_hal::{AerugoHal, SystemHardwareConfig}; use bare_metal::CriticalSection; use env_parser::read_env; diff --git a/src/lib.rs b/src/lib.rs index d7919de6..25f20558 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,9 +33,7 @@ pub use self::boolean_condition::{ pub use self::event::{EventEnabler, EventId}; pub use self::message_queue::{MessageQueueHandle, MessageQueueStorage}; pub use self::tasklet::{TaskletConfig, TaskletStorage}; -pub use aerugo_hal::system_hal::SystemHardwareConfig; - -pub use aerugo_hal::{time, Duration, Instant}; +pub use aerugo_hal::{time, Duration, Instant, SystemHardwareConfig}; #[cfg(feature = "use-aerugo-cortex-m")] pub(crate) use aerugo_cortex_m as arch; diff --git a/src/time_source.rs b/src/time_source.rs index 8ccc8d4d..974a3b6d 100644 --- a/src/time_source.rs +++ b/src/time_source.rs @@ -1,11 +1,10 @@ //! Module containing Aerugo's time source module, providing configurable timestamps for the system //! Should be used internally by the system. -use aerugo_hal::system_hal::SystemHal; - use crate::hal::Hal; use crate::internal_cell::InternalCell; use crate::{Duration, Instant}; +use aerugo_hal::AerugoHal; /// Time source, responsible for creating timestamps. /// From 0b5d60a6d691dd0a5bc08fe6f542cb4fc9f09171 Mon Sep 17 00:00:00 2001 From: Wojciech Olech Date: Thu, 31 Aug 2023 14:50:28 +0200 Subject: [PATCH 2/6] HAL: Separated AerugoHal implementation from SAMV71 HAL into separate crate --- Cargo.toml | 20 +++++----- aerugo-hal/src/config.rs | 6 +-- aerugo-hal/src/lib.rs | 6 +-- arch/cortex-m/aerugo-samv71-hal/Cargo.toml | 19 +++++++++ .../src/error.rs | 0 .../src/hal.rs | 39 +++++++------------ arch/cortex-m/aerugo-samv71-hal/src/lib.rs | 17 ++++++++ .../src/system_peripherals.rs | 4 +- .../src/user_peripherals.rs | 2 +- arch/cortex-m/samv71-hal/Cargo.toml | 10 ++--- arch/cortex-m/samv71-hal/README.md | 21 ++++++++++ arch/cortex-m/samv71-hal/src/drivers/mod.rs | 4 -- arch/cortex-m/samv71-hal/src/lib.rs | 24 +++++------- .../samv71-hal/src/{drivers => }/timer.rs | 0 .../src/{drivers => }/timer/channel.rs | 11 ++++-- .../src/{drivers => }/timer/channel_config.rs | 2 +- .../{drivers => }/timer/channel_waveform.rs | 2 +- .../src/{drivers => }/timer/tc_metadata.rs | 6 +-- .../src/{drivers => }/timer/timer_config.rs | 2 +- .../src/{drivers => }/timer/timer_error.rs | 0 .../{drivers => }/timer/waveform_config.rs | 2 +- .../samv71-hal/src/{drivers => }/watchdog.rs | 13 ++----- .../{drivers => }/watchdog/watchdog_config.rs | 6 +-- .../{drivers => }/watchdog/watchdog_error.rs | 0 arch/cortex-m/samv71q21-pac/README.md | 7 ++++ .../{x86-hal => aerugo-x86-hal}/Cargo.toml | 2 +- .../{x86-hal => aerugo-x86-hal}/src/error.rs | 0 .../{x86-hal => aerugo-x86-hal}/src/hal.rs | 6 +-- .../{x86-hal => aerugo-x86-hal}/src/lib.rs | 1 + .../src/system_peripherals.rs | 0 .../src/user_peripherals.rs | 0 examples/samv71-basic-execution/src/main.rs | 8 +--- examples/samv71-fizz-buzz/src/main.rs | 9 ++--- examples/samv71-hal-timer/src/main.rs | 11 ++---- examples/samv71-system-time/src/main.rs | 8 +--- src/aerugo.rs | 10 ++--- src/api/runtime_api.rs | 10 ++--- src/lib.rs | 6 +-- src/tasklet.rs | 10 ++--- src/tasklet/tasklet_ptr.rs | 6 +-- src/tasklet/tasklet_vtable.rs | 10 ++--- src/time_source.rs | 8 ++-- testbins/test-hal-timer/src/main.rs | 12 +++--- testbins/test-hal-watchdog/src/main.rs | 5 ++- tests/requirements/test/test_hal_watchdog.py | 2 +- tests/requirements/test/test_hal_watchdog.rs | 6 +-- 46 files changed, 193 insertions(+), 160 deletions(-) create mode 100644 arch/cortex-m/aerugo-samv71-hal/Cargo.toml rename arch/cortex-m/{samv71-hal => aerugo-samv71-hal}/src/error.rs (100%) rename arch/cortex-m/{samv71-hal => aerugo-samv71-hal}/src/hal.rs (92%) create mode 100644 arch/cortex-m/aerugo-samv71-hal/src/lib.rs rename arch/cortex-m/{samv71-hal => aerugo-samv71-hal}/src/system_peripherals.rs (94%) rename arch/cortex-m/{samv71-hal => aerugo-samv71-hal}/src/user_peripherals.rs (96%) create mode 100644 arch/cortex-m/samv71-hal/README.md delete mode 100644 arch/cortex-m/samv71-hal/src/drivers/mod.rs rename arch/cortex-m/samv71-hal/src/{drivers => }/timer.rs (100%) rename arch/cortex-m/samv71-hal/src/{drivers => }/timer/channel.rs (96%) rename arch/cortex-m/samv71-hal/src/{drivers => }/timer/channel_config.rs (98%) rename arch/cortex-m/samv71-hal/src/{drivers => }/timer/channel_waveform.rs (99%) rename arch/cortex-m/samv71-hal/src/{drivers => }/timer/tc_metadata.rs (92%) rename arch/cortex-m/samv71-hal/src/{drivers => }/timer/timer_config.rs (97%) rename arch/cortex-m/samv71-hal/src/{drivers => }/timer/timer_error.rs (100%) rename arch/cortex-m/samv71-hal/src/{drivers => }/timer/waveform_config.rs (99%) rename arch/cortex-m/samv71-hal/src/{drivers => }/watchdog.rs (91%) rename arch/cortex-m/samv71-hal/src/{drivers => }/watchdog/watchdog_config.rs (89%) rename arch/cortex-m/samv71-hal/src/{drivers => }/watchdog/watchdog_error.rs (100%) create mode 100644 arch/cortex-m/samv71q21-pac/README.md rename arch/x86/{x86-hal => aerugo-x86-hal}/Cargo.toml (93%) rename arch/x86/{x86-hal => aerugo-x86-hal}/src/error.rs (100%) rename arch/x86/{x86-hal => aerugo-x86-hal}/src/hal.rs (93%) rename arch/x86/{x86-hal => aerugo-x86-hal}/src/lib.rs (85%) rename arch/x86/{x86-hal => aerugo-x86-hal}/src/system_peripherals.rs (100%) rename arch/x86/{x86-hal => aerugo-x86-hal}/src/user_peripherals.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index c618af0c..a72d68e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,10 +2,11 @@ members = [ "aerugo-hal", "arch/cortex-m/aerugo-cortex-m", + "arch/cortex-m/aerugo-samv71-hal", "arch/cortex-m/samv71-hal", "arch/cortex-m/samv71q21-pac", "arch/x86/aerugo-x86", - "arch/x86/x86-hal", + "arch/x86/aerugo-x86-hal", "utils/env-parser", "utils/env-parser-tests", "utils/internal_cell", @@ -44,15 +45,16 @@ keywords = ["rtos", "space"] categories = ["aerospace", "embedded", "hardware-support", "no-std"] [dependencies] -heapless = "0.7" -bare-metal = "0.2.4" -aerugo-hal = { version = "0.1.0", path = "aerugo-hal" } aerugo-cortex-m = { version = "0.1.0", path = "arch/cortex-m/aerugo-cortex-m", optional = true } -samv71-hal = { version = "0.1.0", path = "arch/cortex-m/samv71-hal", optional = true } +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 } -x86-hal = { version = "0.1.0", path = "arch/x86/x86-hal", optional = true } +aerugo-x86-hal = { version = "0.1.0", path = "arch/x86/aerugo-x86-hal", optional = true } +bare-metal = "0.2.4" env-parser = { version = "1.0.0", path = "utils/env-parser" } +heapless = "0.7" internal-cell = { version = "0.0.1", path = "utils/internal_cell" } +samv71-hal = { version = "0.1.0", path = "arch/cortex-m/samv71-hal", optional = true } [dev-dependencies] test-binary = "3.0" @@ -60,10 +62,10 @@ assert_cmd = "2.0" [features] default = ["log"] -use-aerugo-cortex-m = ["aerugo-cortex-m", "samv71-hal"] -use-aerugo-x86 = ["aerugo-x86", "x86-hal"] +use-aerugo-cortex-m = ["aerugo-cortex-m", "aerugo-samv71-hal"] +use-aerugo-x86 = ["aerugo-x86", "aerugo-x86-hal"] test-aerugo-cortex-m = ["use-aerugo-x86"] -rt = ["samv71-hal?/rt"] +rt = ["aerugo-samv71-hal?/rt"] log = ["aerugo-cortex-m?/log", "aerugo-x86?/log"] [profile.release] diff --git a/aerugo-hal/src/config.rs b/aerugo-hal/src/config.rs index 2d370ca3..a5e276bd 100644 --- a/aerugo-hal/src/config.rs +++ b/aerugo-hal/src/config.rs @@ -1,11 +1,11 @@ //! System HAL configuration structures. -use crate::Duration; +use crate::time; /// System hardware configuration. pub struct SystemHardwareConfig { /// Timeout for the watchdog. - pub watchdog_timeout: Duration, + pub watchdog_timeout: time::MillisDurationU32, /// If true, all interrupts will be disabled until `AERUGO.start()` is called. pub disable_interrupts_during_setup: bool, } @@ -13,7 +13,7 @@ pub struct SystemHardwareConfig { impl Default for SystemHardwareConfig { fn default() -> Self { SystemHardwareConfig { - watchdog_timeout: Duration::secs(1), + 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 eaf5070d..a4b6d23d 100644 --- a/aerugo-hal/src/lib.rs +++ b/aerugo-hal/src/lib.rs @@ -20,9 +20,9 @@ pub use fugit as time; /// Aerugo requires a timer with frequency of 1MHz to measure time with microsecond precision. pub const SYSTEM_TIMER_FREQUENCY: u32 = 1_000_000; /// Type representing Aerugo timestamp. -pub type Instant = time::TimerInstantU64; +pub type SystemInstant = time::TimerInstantU64; /// Type representing Aerugo duration. -pub type Duration = time::TimerDurationU64; +pub type SystemDuration = time::TimerDurationU64; /// System HAL trait. pub trait AerugoHal { @@ -38,7 +38,7 @@ pub trait AerugoHal { fn configure_hardware(config: SystemHardwareConfig) -> Result<(), Self::Error>; /// Gets current system time timestamp. - fn get_system_time() -> Instant; + fn get_system_time() -> SystemInstant; /// Feeds the system watchdog. fn feed_watchdog(); diff --git a/arch/cortex-m/aerugo-samv71-hal/Cargo.toml b/arch/cortex-m/aerugo-samv71-hal/Cargo.toml new file mode 100644 index 00000000..e7714d87 --- /dev/null +++ b/arch/cortex-m/aerugo-samv71-hal/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "aerugo-samv71-hal" +version.workspace = true +authors.workspace = true +edition.workspace = true +rust-version.workspace = true +repository.workspace = true +# homepage.workspace = true +license.workspace = true +description = "Crate with AerugoHal implementation for SAMV71" + +[dependencies] +aerugo-hal = { version = "0.1.0", path = "../../../aerugo-hal" } +bare-metal = "0.2.5" +cortex-m = "0.7.7" +samv71-hal = { version = "0.1.0", path = "../samv71-hal" } + +[features] +rt = ["samv71-hal/rt"] diff --git a/arch/cortex-m/samv71-hal/src/error.rs b/arch/cortex-m/aerugo-samv71-hal/src/error.rs similarity index 100% rename from arch/cortex-m/samv71-hal/src/error.rs rename to arch/cortex-m/aerugo-samv71-hal/src/error.rs diff --git a/arch/cortex-m/samv71-hal/src/hal.rs b/arch/cortex-m/aerugo-samv71-hal/src/hal.rs similarity index 92% rename from arch/cortex-m/samv71-hal/src/hal.rs rename to arch/cortex-m/aerugo-samv71-hal/src/hal.rs index d6f297b1..63de732b 100644 --- a/arch/cortex-m/samv71-hal/src/hal.rs +++ b/arch/cortex-m/aerugo-samv71-hal/src/hal.rs @@ -1,31 +1,29 @@ //! System HAL implementation for Cortex-M SAMV71 target. -use aerugo_hal::Instant; +use aerugo_hal::SystemInstant; use aerugo_hal::{AerugoHal, SystemHardwareConfig}; use bare_metal::CriticalSection; use cortex_m::asm; -use crate::drivers::timer::channel_config::ChannelClock; -use crate::drivers::timer::timer_config::{ExternalClock, ExternalClockSource}; -use crate::drivers::timer::waveform_config::{ - ComparisonEffect, OutputSignalEffects, WaveformModeConfig, -}; -use crate::drivers::timer::{Ch0, Ch1, Ch2, Channel, Timer, Waveform}; -use crate::drivers::watchdog::watchdog_config::WatchdogConfig; -use crate::drivers::watchdog::Watchdog; use crate::error::HalError; use crate::system_peripherals::SystemPeripherals; use crate::user_peripherals::UserPeripherals; -use internal_cell::InternalCell; -use pac::{self, PMC, TC0}; +use samv71_hal::pac::{self, PMC, TC0}; +use samv71_hal::timer::channel_config::ChannelClock; +use samv71_hal::timer::timer_config::{ExternalClock, ExternalClockSource}; +use samv71_hal::timer::waveform_config::{ + ComparisonEffect, OutputSignalEffects, WaveformModeConfig, +}; +use samv71_hal::timer::{Ch0, Ch1, Ch2, Channel, Timer, Waveform}; +use samv71_hal::watchdog::{Watchdog, WatchdogConfig}; /// Global system peripherals instance, used internally by HAL. /// /// # Safety /// Mutex is not used here, because it would imply a critical section at every access to HAL. /// Safety of this cell is managed by HAL instead, guaranteeing that undefined behavior will not occur. -static HAL_SYSTEM_PERIPHERALS: InternalCell> = InternalCell::new(None); +static mut HAL_SYSTEM_PERIPHERALS: Option = None; /// HAL implementation for Cortex-M based SAMV71 MCU. pub struct Hal; @@ -52,7 +50,7 @@ impl Hal { /// [`None`] otherwise. pub fn create_user_peripherals() -> Option { Hal::execute_critical(|_| { - if let Some(system_peripherals) = unsafe { HAL_SYSTEM_PERIPHERALS.as_mut_ref() } { + if let Some(system_peripherals) = unsafe { &mut HAL_SYSTEM_PERIPHERALS } { let mcu_peripherals = unsafe { pac::Peripherals::steal() }; let core_peripherals = unsafe { pac::CorePeripherals::steal() }; @@ -95,11 +93,7 @@ impl Hal { return Err(HalError::HalAlreadyInitialized); } - unsafe { - HAL_SYSTEM_PERIPHERALS - .as_mut_ref() - .replace(Hal::create_system_peripherals()) - }; + unsafe { HAL_SYSTEM_PERIPHERALS.replace(Hal::create_system_peripherals()) }; Ok(()) } @@ -147,7 +141,7 @@ impl AerugoHal for Hal { // SAFETY: Immutable access to system peripherals is safe, as we're in critical section // of single-core MCU and no other references to peripherals should exist at this time. - let is_hal_created = unsafe { HAL_SYSTEM_PERIPHERALS.as_ref().is_some() }; + let is_hal_created = unsafe { HAL_SYSTEM_PERIPHERALS.is_some() }; if !is_hal_created { return Err(HalError::HalNotInitialized); } @@ -157,7 +151,6 @@ impl AerugoHal for Hal { // We also checked that peripherals exist, so it should realistically never panic. let peripherals = unsafe { HAL_SYSTEM_PERIPHERALS - .as_mut_ref() .as_mut() .expect("HAL is not initialized") }; @@ -202,12 +195,11 @@ impl AerugoHal for Hal { result } - fn get_system_time() -> Instant { + fn get_system_time() -> SystemInstant { // SAFETY: This is safe, because this is a single-core system, and no other references to // system peripherals should exist during this call. let peripherals = unsafe { HAL_SYSTEM_PERIPHERALS - .as_ref() .as_ref() .expect("HAL cannot be accessed before initialization") }; @@ -230,7 +222,7 @@ impl AerugoHal for Hal { let time_ch0 = ch0.counter_value(); // Timer's clock is 1MHz, so returned value is in microseconds. - Instant::from_ticks(as_48bit_unsigned(time_ch0, time_ch1, time_ch2)) + SystemInstant::from_ticks(as_48bit_unsigned(time_ch0, time_ch1, time_ch2)) } fn feed_watchdog() { @@ -238,7 +230,6 @@ impl AerugoHal for Hal { // system peripherals should exist during this call. let peripherals = unsafe { HAL_SYSTEM_PERIPHERALS - .as_mut_ref() .as_mut() .expect("HAL cannot be accessed before initialization") }; diff --git a/arch/cortex-m/aerugo-samv71-hal/src/lib.rs b/arch/cortex-m/aerugo-samv71-hal/src/lib.rs new file mode 100644 index 00000000..01927766 --- /dev/null +++ b/arch/cortex-m/aerugo-samv71-hal/src/lib.rs @@ -0,0 +1,17 @@ +/*! +SAMV71 implementation of aerugo HAL. +*/ +#![no_std] +#![warn(missing_docs)] +#![warn(clippy::missing_docs_in_private_items)] +#![warn(rustdoc::missing_crate_level_docs)] + +mod system_peripherals; + +pub mod error; +pub mod hal; +pub mod user_peripherals; + +pub use hal::Hal; +pub use samv71_hal as drivers; +pub use user_peripherals::UserPeripherals; diff --git a/arch/cortex-m/samv71-hal/src/system_peripherals.rs b/arch/cortex-m/aerugo-samv71-hal/src/system_peripherals.rs similarity index 94% rename from arch/cortex-m/samv71-hal/src/system_peripherals.rs rename to arch/cortex-m/aerugo-samv71-hal/src/system_peripherals.rs index 8030a88c..966c4c46 100644 --- a/arch/cortex-m/samv71-hal/src/system_peripherals.rs +++ b/arch/cortex-m/aerugo-samv71-hal/src/system_peripherals.rs @@ -1,8 +1,8 @@ //! Module representing peripherals internally used by Aerugo. -use pac::{PMC, TC0}; +use samv71_hal::pac::{PMC, TC0}; -use crate::drivers::{ +use samv71_hal::{ timer::{Ch0, Ch1, Ch2, Channel, Timer, Waveform}, watchdog::Watchdog, }; diff --git a/arch/cortex-m/samv71-hal/src/user_peripherals.rs b/arch/cortex-m/aerugo-samv71-hal/src/user_peripherals.rs similarity index 96% rename from arch/cortex-m/samv71-hal/src/user_peripherals.rs rename to arch/cortex-m/aerugo-samv71-hal/src/user_peripherals.rs index 5b2e1039..9d163a6f 100644 --- a/arch/cortex-m/samv71-hal/src/user_peripherals.rs +++ b/arch/cortex-m/aerugo-samv71-hal/src/user_peripherals.rs @@ -1,6 +1,6 @@ //! Module representing user-accessible peripherals. -use pac; +use samv71_hal::pac; /// Peripherals structure. /// These peripherals can be used to create HAL drivers in user code. diff --git a/arch/cortex-m/samv71-hal/Cargo.toml b/arch/cortex-m/samv71-hal/Cargo.toml index ed9d0d74..ecf80bdb 100644 --- a/arch/cortex-m/samv71-hal/Cargo.toml +++ b/arch/cortex-m/samv71-hal/Cargo.toml @@ -7,16 +7,12 @@ rust-version.workspace = true repository.workspace = true # homepage.workspace = true license.workspace = true -description = "SAMV71 implementation of aerugo HAL" +description = "Crate containing drivers for SAMV71 peripherals" [dependencies] -aerugo-hal = { version = "0.1.0", path = "../../../aerugo-hal" } -cortex-m = "0.7.7" -bare-metal = "0.2.4" -samv71q21-pac = { version = "0.1.0", path = "../samv71q21-pac" } embedded-hal = "0.2.7" -aerugo-cortex-m = { version = "0.1.0", path = "../aerugo-cortex-m" } -internal-cell = { version = "0.0.1", path = "../../../utils/internal_cell" } +fugit = "0.3.7" +samv71q21-pac = { version = "0.1.0", path = "../samv71q21-pac" } [features] rt = ["samv71q21-pac/rt"] diff --git a/arch/cortex-m/samv71-hal/README.md b/arch/cortex-m/samv71-hal/README.md new file mode 100644 index 00000000..0a9a72d1 --- /dev/null +++ b/arch/cortex-m/samv71-hal/README.md @@ -0,0 +1,21 @@ +# SAMV71 HAL + +This crate contains HAL modules for SAMV71 MCU peripherals. + +Currently, it supports only SAMV71Q21 MCU, but it's possible to add support for other MCUs from SAMV71 series, if needed. + +HAL modules provide relatively safe and easy to use abstraction over microcontroller's peripherals. This should be preferred method of interfacing with MCU hardware. + +For time types, `fugit` crate is used. You can find aliases for time types in `lib.rs`. + +For more information about hardware abstraction architecture used in Rust, [consult this page](https://docs.rust-embedded.org/book/portability/index.html) + +## Philosophy + +This crate uses SAMV71Q21 PAC as register interface. It makes the development much quicker and provides abstraction over registers, which makes adding support for similar part numbers easy. + +In typical scenario, the user should use HAL to fetch a structure containing all the available (and supported) peripherals of the MCU. This structure should be fetched only once during program's lifetime, as having multiple instances of them would break safety rules. + +These instances are marked as `Send` automatically, due to the fact that PAC peripherals are also marked as `Send` explicitly. `Send` is enforced correctly as long as a type is safe to send to another thread - or, in context of microcontrollers, [an interrupt](https://docs.rust-embedded.org/book/concurrency/#concurrency). Since there can be only one instance of each peripheral, as long as we're using safe method of getting them, `Send` is enforced because there's at most one pointer to each peripheral's registers at once, therefore no memory is ever shared. + +However, HAL peripherals do not implement `Sync`, as sharing references between threads/interrupts is not safe for the same reason `Send` stops being enforced when more than one instance of a peripheral exists. User of this crate must enforce the interrupt safety manually, either by design, or by wrapping the peripherals in safe `Sync` wrapper. [Consult Rust Embedded book for an example](https://docs.rust-embedded.org/book/concurrency/#sharing-peripherals) diff --git a/arch/cortex-m/samv71-hal/src/drivers/mod.rs b/arch/cortex-m/samv71-hal/src/drivers/mod.rs deleted file mode 100644 index 8e6c54b2..00000000 --- a/arch/cortex-m/samv71-hal/src/drivers/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! This module contains all the drivers for supported peripherals. - -pub mod timer; -pub mod watchdog; diff --git a/arch/cortex-m/samv71-hal/src/lib.rs b/arch/cortex-m/samv71-hal/src/lib.rs index 68e4d18b..08ce60dd 100644 --- a/arch/cortex-m/samv71-hal/src/lib.rs +++ b/arch/cortex-m/samv71-hal/src/lib.rs @@ -1,23 +1,19 @@ -/*! -SAMV71 implementation of aerugo HAL. -*/ +//! This module contains all the drivers for supported peripherals. + #![no_std] #![warn(missing_docs)] #![warn(clippy::missing_docs_in_private_items)] #![warn(rustdoc::missing_crate_level_docs)] -extern crate internal_cell; -extern crate samv71q21_pac as pac; - -pub mod drivers; -pub mod error; -pub mod hal; -mod system_peripherals; -pub mod user_peripherals; - -pub use self::hal::Hal; pub use embedded_hal; -pub use pac::{NVIC, PMC}; +pub use fugit as time; +pub use samv71q21_pac as pac; + +/// Type representing millisecond duration used by this crate. +pub type Milliseconds = time::MillisDurationU32; #[cfg(feature = "rt")] pub use pac::interrupt; + +pub mod timer; +pub mod watchdog; diff --git a/arch/cortex-m/samv71-hal/src/drivers/timer.rs b/arch/cortex-m/samv71-hal/src/timer.rs similarity index 100% rename from arch/cortex-m/samv71-hal/src/drivers/timer.rs rename to arch/cortex-m/samv71-hal/src/timer.rs diff --git a/arch/cortex-m/samv71-hal/src/drivers/timer/channel.rs b/arch/cortex-m/samv71-hal/src/timer/channel.rs similarity index 96% rename from arch/cortex-m/samv71-hal/src/drivers/timer/channel.rs rename to arch/cortex-m/samv71-hal/src/timer/channel.rs index 0acab4d7..1ebefc54 100644 --- a/arch/cortex-m/samv71-hal/src/drivers/timer/channel.rs +++ b/arch/cortex-m/samv71-hal/src/timer/channel.rs @@ -1,7 +1,7 @@ //! Module representing timer counter's channel +use crate::pac::tc0::tc_channel::TC_CHANNEL; use core::marker::PhantomData; -use pac::tc0::tc_channel::TC_CHANNEL; use super::channel_config::*; use super::waveform_config::WaveformModeConfig; @@ -20,8 +20,13 @@ pub struct Channel { } /// Assuming that the user does not create an instance of channel by himself, and instead relies on -/// instances provided by HAL, it's safe to share channel instances as there's only a single instance that can -/// access hardware channel's registers at once, and it cannot be copied. +/// instances provided by HAL, it's safe to send channels to other threads, as there's only a single +/// instance that can access hardware channel's registers at once, and it cannot be copied. +/// +/// Sharing references (`Sync`) to a channel between threads is not safe, and should be managed by the user. +/// +/// If that invariant is broken by the user, any usage of cloned Channels from other thread's context (including +/// interrupt context) can be considered unsafe. unsafe impl Send for Channel {} /// Enumeration listing available channels. diff --git a/arch/cortex-m/samv71-hal/src/drivers/timer/channel_config.rs b/arch/cortex-m/samv71-hal/src/timer/channel_config.rs similarity index 98% rename from arch/cortex-m/samv71-hal/src/drivers/timer/channel_config.rs rename to arch/cortex-m/samv71-hal/src/timer/channel_config.rs index 743b85c9..29f9c880 100644 --- a/arch/cortex-m/samv71-hal/src/drivers/timer/channel_config.rs +++ b/arch/cortex-m/samv71-hal/src/timer/channel_config.rs @@ -1,6 +1,6 @@ //! Module containing channel configuration and status structures. -use pac::tc0::tc_channel::cmr_waveform_mode::TCCLKSSELECT_A as PacClockId; +use crate::pac::tc0::tc_channel::cmr_waveform_mode::TCCLKSSELECT_A as PacClockId; /// Structure representing available channel interrupts. #[derive(Debug, Eq, PartialEq)] diff --git a/arch/cortex-m/samv71-hal/src/drivers/timer/channel_waveform.rs b/arch/cortex-m/samv71-hal/src/timer/channel_waveform.rs similarity index 99% rename from arch/cortex-m/samv71-hal/src/drivers/timer/channel_waveform.rs rename to arch/cortex-m/samv71-hal/src/timer/channel_waveform.rs index f2e1a2bb..eb2b7671 100644 --- a/arch/cortex-m/samv71-hal/src/drivers/timer/channel_waveform.rs +++ b/arch/cortex-m/samv71-hal/src/timer/channel_waveform.rs @@ -1,6 +1,6 @@ //! Module with functionalities of timer's channel in waveform mode. -use pac::tc0::tc_channel::CMR_WAVEFORM_MODE; +use crate::pac::tc0::tc_channel::CMR_WAVEFORM_MODE; use super::{ waveform_config::{ diff --git a/arch/cortex-m/samv71-hal/src/drivers/timer/tc_metadata.rs b/arch/cortex-m/samv71-hal/src/timer/tc_metadata.rs similarity index 92% rename from arch/cortex-m/samv71-hal/src/drivers/timer/tc_metadata.rs rename to arch/cortex-m/samv71-hal/src/timer/tc_metadata.rs index 7550835b..d409a0f2 100644 --- a/arch/cortex-m/samv71-hal/src/drivers/timer/tc_metadata.rs +++ b/arch/cortex-m/samv71-hal/src/timer/tc_metadata.rs @@ -1,7 +1,7 @@ //! Module with PAC TC metadata implementation. -pub(super) use pac::tc0::RegisterBlock; -use pac::Interrupt; -pub use pac::{TC0, TC1, TC2, TC3}; +pub(super) use crate::pac::tc0::RegisterBlock; +use crate::pac::Interrupt; +pub use crate::pac::{TC0, TC1, TC2, TC3}; /// Amount of channels per timer instance. const CHANNELS_COUNT_PER_TIMER: usize = 3; diff --git a/arch/cortex-m/samv71-hal/src/drivers/timer/timer_config.rs b/arch/cortex-m/samv71-hal/src/timer/timer_config.rs similarity index 97% rename from arch/cortex-m/samv71-hal/src/drivers/timer/timer_config.rs rename to arch/cortex-m/samv71-hal/src/timer/timer_config.rs index 841cb94f..6706e231 100644 --- a/arch/cortex-m/samv71-hal/src/drivers/timer/timer_config.rs +++ b/arch/cortex-m/samv71-hal/src/timer/timer_config.rs @@ -1,6 +1,6 @@ //! Module containing configuration structures for Timer -use pac::tc0::bmr::{TC0XC0SSELECT_A, TC1XC1SSELECT_A, TC2XC2SSELECT_A}; +use crate::pac::tc0::bmr::{TC0XC0SSELECT_A, TC1XC1SSELECT_A, TC2XC2SSELECT_A}; /// External clock signal source. /// diff --git a/arch/cortex-m/samv71-hal/src/drivers/timer/timer_error.rs b/arch/cortex-m/samv71-hal/src/timer/timer_error.rs similarity index 100% rename from arch/cortex-m/samv71-hal/src/drivers/timer/timer_error.rs rename to arch/cortex-m/samv71-hal/src/timer/timer_error.rs diff --git a/arch/cortex-m/samv71-hal/src/drivers/timer/waveform_config.rs b/arch/cortex-m/samv71-hal/src/timer/waveform_config.rs similarity index 99% rename from arch/cortex-m/samv71-hal/src/drivers/timer/waveform_config.rs rename to arch/cortex-m/samv71-hal/src/timer/waveform_config.rs index 6c0dc5af..8bf11f24 100644 --- a/arch/cortex-m/samv71-hal/src/drivers/timer/waveform_config.rs +++ b/arch/cortex-m/samv71-hal/src/timer/waveform_config.rs @@ -1,6 +1,6 @@ //! Waveform-mode related configuration structures. -use pac::tc0::tc_channel::cmr_waveform_mode::{ +use crate::pac::tc0::tc_channel::cmr_waveform_mode::{ ACPASELECT_A, EEVTEDGSELECT_A, EEVTSELECT_A, WAVSELSELECT_A, }; diff --git a/arch/cortex-m/samv71-hal/src/drivers/watchdog.rs b/arch/cortex-m/samv71-hal/src/watchdog.rs similarity index 91% rename from arch/cortex-m/samv71-hal/src/drivers/watchdog.rs rename to arch/cortex-m/samv71-hal/src/watchdog.rs index 450476df..34032fd6 100644 --- a/arch/cortex-m/samv71-hal/src/drivers/watchdog.rs +++ b/arch/cortex-m/samv71-hal/src/watchdog.rs @@ -14,7 +14,7 @@ pub mod watchdog_config; pub mod watchdog_error; use crate::pac::WDT; -use aerugo_hal::Duration; +use crate::Milliseconds; pub use watchdog_config::WatchdogConfig; pub use watchdog_error::WatchdogError; @@ -28,11 +28,6 @@ pub struct Watchdog { configured: bool, } -/// # Safety -/// Watchdog does not auto-implement Sync due to WDT structure containing a pointer. -/// Since it owns WDT, and it's running in single-core environment, it's safe to share. -unsafe impl Sync for Watchdog {} - impl Watchdog { /// Create a watchdog instance from PAC peripheral. /// @@ -131,7 +126,7 @@ impl Watchdog { /// `duration` must be in inclusive range [0, [`MAXIMUM_WATCHDOG_DURATION`]]. /// Since it's internal, private function, it does not perform any checks. /// To safely convert any duration into watchdog counter value, use [`clamp_and_convert_duration`](Watchdog::clamp_and_convert_duration). - fn convert_duration_to_counter_value(duration: Duration) -> u16 { + fn convert_duration_to_counter_value(duration: Milliseconds) -> u16 { let duration_ratio: f32 = (duration.to_secs() as f32) / (MAXIMUM_WATCHDOG_DURATION.to_secs() as f32); @@ -146,8 +141,8 @@ impl Watchdog { /// /// # Returns /// Watchdog counter value representing passed duration. - fn clamp_and_convert_duration(duration: Duration) -> u16 { - let clamped_duration = duration.clamp(Duration::secs(0), Duration::secs(16)); + fn clamp_and_convert_duration(duration: Milliseconds) -> u16 { + let clamped_duration = duration.clamp(Milliseconds::secs(0), Milliseconds::secs(16)); Watchdog::convert_duration_to_counter_value(clamped_duration) } diff --git a/arch/cortex-m/samv71-hal/src/drivers/watchdog/watchdog_config.rs b/arch/cortex-m/samv71-hal/src/watchdog/watchdog_config.rs similarity index 89% rename from arch/cortex-m/samv71-hal/src/drivers/watchdog/watchdog_config.rs rename to arch/cortex-m/samv71-hal/src/watchdog/watchdog_config.rs index d51d4648..58c33cb7 100644 --- a/arch/cortex-m/samv71-hal/src/drivers/watchdog/watchdog_config.rs +++ b/arch/cortex-m/samv71-hal/src/watchdog/watchdog_config.rs @@ -1,9 +1,9 @@ //! Module containing watchdog configuration types. -use aerugo_hal::Duration; +use crate::Milliseconds; /// Maximum duration that watchdog can wait before triggering an event. -pub const MAXIMUM_WATCHDOG_DURATION: Duration = Duration::secs(16); +pub const MAXIMUM_WATCHDOG_DURATION: Milliseconds = Milliseconds::secs(16); /// Structure representing Watchdog configuration. /// @@ -15,7 +15,7 @@ pub struct WatchdogConfig { /// If true, watchdog will reset the MCU on timeout. pub reset_enabled: bool, /// Defines the reset value for watchdog's counter in watchdog clock cycles. - pub duration: Duration, + pub duration: Milliseconds, /// If true, watchdog will run in idle state. pub run_in_idle: bool, /// If true, watchdog will run in debug state. diff --git a/arch/cortex-m/samv71-hal/src/drivers/watchdog/watchdog_error.rs b/arch/cortex-m/samv71-hal/src/watchdog/watchdog_error.rs similarity index 100% rename from arch/cortex-m/samv71-hal/src/drivers/watchdog/watchdog_error.rs rename to arch/cortex-m/samv71-hal/src/watchdog/watchdog_error.rs diff --git a/arch/cortex-m/samv71q21-pac/README.md b/arch/cortex-m/samv71q21-pac/README.md new file mode 100644 index 00000000..74dfac19 --- /dev/null +++ b/arch/cortex-m/samv71q21-pac/README.md @@ -0,0 +1,7 @@ +# SAMV71Q21 Peripheral Access Crate + +This crate contains code generated with [`svd2rust`](https://github.com/rust-embedded/svd2rust) using SVD file downloaded from [Microchip repository](https://packs.download.microchip.com/). + +Peripheral Access Crate (PAC) contains definitions, structures and traits for MCU peripherals registers, and provides a relatively safe abstraction over typical register operations. Content of this library is supposed to be used as a building block of HAL, using it directly is not recommended (with rare exceptions, where PAC driver has safe to use and readable interface for some peripherals that does not require further abstraction). + +For more information about hardware abstraction architecture used in Rust, [consult this page](https://docs.rust-embedded.org/book/portability/index.html) diff --git a/arch/x86/x86-hal/Cargo.toml b/arch/x86/aerugo-x86-hal/Cargo.toml similarity index 93% rename from arch/x86/x86-hal/Cargo.toml rename to arch/x86/aerugo-x86-hal/Cargo.toml index dc7bfdd4..688ff0a2 100644 --- a/arch/x86/x86-hal/Cargo.toml +++ b/arch/x86/aerugo-x86-hal/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "x86-hal" +name = "aerugo-x86-hal" version.workspace = true authors.workspace = true edition.workspace = true diff --git a/arch/x86/x86-hal/src/error.rs b/arch/x86/aerugo-x86-hal/src/error.rs similarity index 100% rename from arch/x86/x86-hal/src/error.rs rename to arch/x86/aerugo-x86-hal/src/error.rs diff --git a/arch/x86/x86-hal/src/hal.rs b/arch/x86/aerugo-x86-hal/src/hal.rs similarity index 93% rename from arch/x86/x86-hal/src/hal.rs rename to arch/x86/aerugo-x86-hal/src/hal.rs index 0527cde9..f8eb020d 100644 --- a/arch/x86/x86-hal/src/hal.rs +++ b/arch/x86/aerugo-x86-hal/src/hal.rs @@ -3,7 +3,7 @@ use std::convert::TryInto; use std::time::SystemTime; -use aerugo_hal::Instant; +use aerugo_hal::SystemInstant; use aerugo_hal::{AerugoHal, SystemHardwareConfig}; use bare_metal::CriticalSection; use once_cell::sync::Lazy; @@ -32,8 +32,8 @@ impl AerugoHal for Hal { Ok(()) } - fn get_system_time() -> Instant { - Instant::from_ticks( + fn get_system_time() -> SystemInstant { + SystemInstant::from_ticks( TIME_START .elapsed() .expect("{}") diff --git a/arch/x86/x86-hal/src/lib.rs b/arch/x86/aerugo-x86-hal/src/lib.rs similarity index 85% rename from arch/x86/x86-hal/src/lib.rs rename to arch/x86/aerugo-x86-hal/src/lib.rs index 3f74ed6c..9ee96a45 100644 --- a/arch/x86/x86-hal/src/lib.rs +++ b/arch/x86/aerugo-x86-hal/src/lib.rs @@ -11,3 +11,4 @@ mod system_peripherals; pub mod user_peripherals; pub use self::hal::Hal; +pub use user_peripherals::UserPeripherals; diff --git a/arch/x86/x86-hal/src/system_peripherals.rs b/arch/x86/aerugo-x86-hal/src/system_peripherals.rs similarity index 100% rename from arch/x86/x86-hal/src/system_peripherals.rs rename to arch/x86/aerugo-x86-hal/src/system_peripherals.rs diff --git a/arch/x86/x86-hal/src/user_peripherals.rs b/arch/x86/aerugo-x86-hal/src/user_peripherals.rs similarity index 100% rename from arch/x86/x86-hal/src/user_peripherals.rs rename to arch/x86/aerugo-x86-hal/src/user_peripherals.rs diff --git a/examples/samv71-basic-execution/src/main.rs b/examples/samv71-basic-execution/src/main.rs index 4a12df6c..479ee936 100644 --- a/examples/samv71-basic-execution/src/main.rs +++ b/examples/samv71-basic-execution/src/main.rs @@ -6,8 +6,7 @@ extern crate cortex_m_rt as rt; extern crate panic_rtt_target; use aerugo::{ - logln, Duration, InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, TaskletStorage, - AERUGO, + logln, InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, TaskletStorage, AERUGO, }; use rt::entry; @@ -27,10 +26,7 @@ static DUMMY_TASK_STORAGE: TaskletStorage<(), DummyTaskContext, 0> = TaskletStor #[entry] fn main() -> ! { - AERUGO.initialize(SystemHardwareConfig { - watchdog_timeout: Duration::secs(5), - ..Default::default() - }); + AERUGO.initialize(SystemHardwareConfig::default()); logln!("Hello, world! Aerugo initialized!"); diff --git a/examples/samv71-fizz-buzz/src/main.rs b/examples/samv71-fizz-buzz/src/main.rs index dc20d740..f8bc681c 100644 --- a/examples/samv71-fizz-buzz/src/main.rs +++ b/examples/samv71-fizz-buzz/src/main.rs @@ -6,8 +6,8 @@ extern crate cortex_m_rt as rt; extern crate panic_rtt_target; use aerugo::{ - log, logln, BooleanConditionHandle, BooleanConditionSet, BooleanConditionStorage, Duration, - EventId, InitApi, MessageQueueHandle, MessageQueueStorage, RuntimeApi, SystemHardwareConfig, + log, logln, BooleanConditionHandle, BooleanConditionSet, BooleanConditionStorage, EventId, + InitApi, MessageQueueHandle, MessageQueueStorage, RuntimeApi, SystemHardwareConfig, TaskletConfig, TaskletStorage, AERUGO, }; @@ -114,10 +114,7 @@ impl From for FizzBuzzEvents { #[entry] fn main() -> ! { - AERUGO.initialize(SystemHardwareConfig { - watchdog_timeout: Duration::secs(5), - ..Default::default() - }); + AERUGO.initialize(SystemHardwareConfig::default()); logln!("Hello, world! Aerugo initialized!"); diff --git a/examples/samv71-hal-timer/src/main.rs b/examples/samv71-hal-timer/src/main.rs index 37cbed44..104329a1 100644 --- a/examples/samv71-hal-timer/src/main.rs +++ b/examples/samv71-hal-timer/src/main.rs @@ -7,16 +7,16 @@ extern crate panic_rtt_target; use core::cell::RefCell; +use aerugo::hal::drivers::pac::PMC; use aerugo::hal::drivers::timer::{ channel_config::ChannelClock, waveform_config::WaveformModeConfig, Ch0, Channel, Waveform, TC1, }; -use aerugo::hal::PMC; use cortex_m::interrupt::free as irq_free; use cortex_m::interrupt::Mutex; use aerugo::{ - hal::drivers::timer::Timer, logln, Duration, InitApi, RuntimeApi, SystemHardwareConfig, - TaskletConfig, TaskletStorage, AERUGO, + hal::drivers::timer::Timer, logln, InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, + TaskletStorage, AERUGO, }; use rt::entry; @@ -99,10 +99,7 @@ fn init_tasks() { #[entry] fn main() -> ! { - let peripherals = AERUGO.initialize(SystemHardwareConfig { - watchdog_timeout: Duration::secs(5), - ..Default::default() - }); + let peripherals = AERUGO.initialize(SystemHardwareConfig::default()); logln!("Hello, world! Aerugo initialized!"); diff --git a/examples/samv71-system-time/src/main.rs b/examples/samv71-system-time/src/main.rs index bd689b2d..4f11d1ca 100644 --- a/examples/samv71-system-time/src/main.rs +++ b/examples/samv71-system-time/src/main.rs @@ -6,8 +6,7 @@ extern crate cortex_m_rt as rt; extern crate panic_rtt_target; use aerugo::{ - logln, Duration, InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, TaskletStorage, - AERUGO, + logln, InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, TaskletStorage, AERUGO, }; use rt::entry; @@ -44,10 +43,7 @@ static DUMMY_TASK_STORAGE: TaskletStorage<(), DummyTaskContext, 0> = TaskletStor #[entry] fn main() -> ! { - AERUGO.initialize(SystemHardwareConfig { - watchdog_timeout: Duration::secs(5), - ..Default::default() - }); + AERUGO.initialize(SystemHardwareConfig::default()); logln!("Hello, world! Aerugo initialized!"); diff --git a/src/aerugo.rs b/src/aerugo.rs index cc81438e..265c49f6 100644 --- a/src/aerugo.rs +++ b/src/aerugo.rs @@ -19,12 +19,12 @@ use crate::event::{Event, EventEnabler, EventId}; use crate::event_manager::EventManager; use crate::execution_monitoring::ExecutionStats; use crate::executor::Executor; -use crate::hal::{user_peripherals::UserPeripherals, Hal}; +use crate::hal::{Hal, UserPeripherals}; use crate::message_queue::{MessageQueueHandle, MessageQueueStorage}; use crate::tasklet::{StepFn, TaskletConfig, TaskletHandle, TaskletId, TaskletPtr, TaskletStorage}; use crate::time_manager::TimeManager; use crate::time_source::TimeSource; -use crate::{Duration, Instant}; +use crate::{SystemDuration, SystemInstant}; /// Core system. /// @@ -718,7 +718,7 @@ impl RuntimeApi for Aerugo { } } - fn set_system_time_offset(&'static self, offset: Duration) { + fn set_system_time_offset(&'static self, offset: SystemDuration) { // SAFETY: This is safe, because it's called from non-IRQ context, and // system time cannot be accessed from IRQ context unsafe { @@ -728,13 +728,13 @@ impl RuntimeApi for Aerugo { /// Returns time elapsed between system initialization and start of the scheduler. /// If called before [`Aerugo::start`](crate::Aerugo::start), returns `None`. - fn get_startup_time(&'static self) -> Option { + fn get_startup_time(&'static self) -> Option { self.time_source.startup_duration() } /// Returns time elapsed since scheduler's start. /// If called before [`Aerugo::start`](crate::Aerugo::start), returns `None`. - fn get_time_since_startup(&'static self) -> Option { + fn get_time_since_startup(&'static self) -> Option { self.time_source.time_since_start() } diff --git a/src/api/runtime_api.rs b/src/api/runtime_api.rs index dc135d66..454e4e11 100644 --- a/src/api/runtime_api.rs +++ b/src/api/runtime_api.rs @@ -8,7 +8,7 @@ use crate::api::RuntimeError; use crate::event::EventId; use crate::execution_monitoring::ExecutionStats; use crate::tasklet::TaskletId; -use crate::{Duration, Instant}; +use crate::{SystemDuration, SystemInstant}; /// System runtime API. pub trait RuntimeApi { @@ -34,21 +34,21 @@ pub trait RuntimeApi { fn clear_event_queue(&'static self); /// Gets current system time timestamp. - fn get_system_time(&'static self) -> Instant; + fn get_system_time(&'static self) -> SystemInstant; /// Sets system time offset. /// /// # Parameters /// * `offset` - Time offset. - fn set_system_time_offset(&'static self, offset: Duration); + fn set_system_time_offset(&'static self, offset: SystemDuration); /// Returns time elapsed between system initialization and start of the scheduler. /// If called before scheduler's start, should return `None`. - fn get_startup_time(&'static self) -> Option; + fn get_startup_time(&'static self) -> Option; /// Returns time elapsed since scheduler's start. /// If called before scheduler's start, should return `None`. - fn get_time_since_startup(&'static self) -> Option; + fn get_time_since_startup(&'static self) -> Option; /// Returns an iterator to the list with IDs of registered tasklets. fn query_tasks(&'static self) -> core::slice::Iter; diff --git a/src/lib.rs b/src/lib.rs index 25f20558..44ff46b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,17 +33,17 @@ pub use self::boolean_condition::{ pub use self::event::{EventEnabler, EventId}; pub use self::message_queue::{MessageQueueHandle, MessageQueueStorage}; pub use self::tasklet::{TaskletConfig, TaskletStorage}; -pub use aerugo_hal::{time, Duration, Instant, SystemHardwareConfig}; +pub use aerugo_hal::{time, SystemDuration, SystemHardwareConfig, SystemInstant}; #[cfg(feature = "use-aerugo-cortex-m")] pub(crate) use aerugo_cortex_m as arch; #[cfg(feature = "use-aerugo-cortex-m")] -pub use samv71_hal as hal; +pub use aerugo_samv71_hal as hal; #[cfg(feature = "use-aerugo-x86")] pub(crate) use aerugo_x86 as arch; #[cfg(feature = "use-aerugo-x86")] -pub use x86_hal as hal; +pub use aerugo_x86_hal as hal; #[cfg(feature = "log")] pub use arch::{log, logln}; diff --git a/src/tasklet.rs b/src/tasklet.rs index badceb5a..8336b4e0 100644 --- a/src/tasklet.rs +++ b/src/tasklet.rs @@ -32,7 +32,7 @@ use crate::arch::Mutex; use crate::boolean_condition::BooleanConditionSet; use crate::data_provider::DataProvider; use crate::internal_cell::InternalCell; -use crate::Instant; +use crate::SystemInstant; /// Type of function that is executed by the tasklet in its step. pub(crate) type StepFn = fn(T, &mut C, &'static dyn RuntimeApi); @@ -55,7 +55,7 @@ pub(crate) struct Tasklet { /// Tasklet status. status: Mutex, /// Last execution time. - last_execution_time: Mutex, + last_execution_time: Mutex, /// Step function. step_fn: StepFn, /// Context data. @@ -78,7 +78,7 @@ impl Tasklet { name: config.name, priority: config.priority, status: Mutex::new(TaskletStatus::Sleeping), - last_execution_time: Mutex::new(Instant::from_ticks(0)), + last_execution_time: Mutex::new(SystemInstant::from_ticks(0)), step_fn, context: InternalCell::new(context), condition_set, @@ -110,7 +110,7 @@ impl Tasklet { } /// Returns last execution time. - pub(crate) fn get_last_execution_time(&self) -> Instant { + pub(crate) fn get_last_execution_time(&self) -> SystemInstant { self.last_execution_time.lock(|t| *t) } @@ -118,7 +118,7 @@ impl Tasklet { /// /// # Parameters /// * `time` - Last execution time. - pub(crate) fn set_last_execution_time(&self, time: Instant) { + pub(crate) fn set_last_execution_time(&self, time: SystemInstant) { self.last_execution_time.lock(|t| *t = time) } diff --git a/src/tasklet/tasklet_ptr.rs b/src/tasklet/tasklet_ptr.rs index ecb7c417..71a6722d 100644 --- a/src/tasklet/tasklet_ptr.rs +++ b/src/tasklet/tasklet_ptr.rs @@ -14,7 +14,7 @@ use core::cmp::Ordering; use crate::tasklet::{tasklet_vtable, Tasklet, TaskletStatus, TaskletVTable}; -use crate::Instant; +use crate::SystemInstant; /// Raw tasklet pointer. #[derive(Clone)] @@ -70,13 +70,13 @@ impl TaskletPtr { /// See: [get_last_execution_time](crate::tasklet::Tasklet::get_last_execution_time()) #[inline(always)] - pub(crate) fn get_last_execution_time(&self) -> Instant { + pub(crate) fn get_last_execution_time(&self) -> SystemInstant { (self.vtable.get_last_execution_time)(self.ptr) } /// See: [set_last_execution_time](crate::tasklet::Tasklet::set_last_execution_time()) #[inline(always)] - pub(crate) fn set_last_execution_time(&self, time: Instant) { + pub(crate) fn set_last_execution_time(&self, time: SystemInstant) { (self.vtable.set_last_execution_time)(self.ptr, time) } diff --git a/src/tasklet/tasklet_vtable.rs b/src/tasklet/tasklet_vtable.rs index c68c9c56..6fb0e6aa 100644 --- a/src/tasklet/tasklet_vtable.rs +++ b/src/tasklet/tasklet_vtable.rs @@ -7,7 +7,7 @@ //! For more information look at `TaskletPtr` structure. use crate::tasklet::{Tasklet, TaskletStatus}; -use crate::Instant; +use crate::SystemInstant; /// Hand-made tasklet virtual table. pub(crate) struct TaskletVTable { @@ -20,9 +20,9 @@ pub(crate) struct TaskletVTable { /// Pointer to [set_status](set_status()) function. pub(crate) set_status: fn(*const (), TaskletStatus), /// Pointer to [get_last_execution_time](get_last_execution_time()) function. - pub(crate) get_last_execution_time: fn(*const ()) -> Instant, + pub(crate) get_last_execution_time: fn(*const ()) -> SystemInstant, /// Pointer to [set_last_execution_time](set_last_execution_time()) function. - pub(crate) set_last_execution_time: fn(*const (), Instant), + pub(crate) set_last_execution_time: fn(*const (), SystemInstant), /// Pointer to [has_work](has_work()) function. pub(crate) has_work: fn(*const ()) -> bool, /// Pointer to [is_active](is_active()) function. @@ -104,7 +104,7 @@ fn set_status( #[inline(always)] fn get_last_execution_time( ptr: *const (), -) -> Instant { +) -> SystemInstant { // SAFETY: This is safe, because `Tasklet` is the only structure that implements `Task` trait, // and so is the only type that we store in the `*const ()`. let tasklet = unsafe { &*(ptr as *const Tasklet) }; @@ -117,7 +117,7 @@ fn get_last_execution_time( #[inline(always)] fn set_last_execution_time( ptr: *const (), - time: Instant, + time: SystemInstant, ) { // SAFETY: This is safe, because `Tasklet` is the only structure that implements `Task` trait, // and so is the only type that we store in the `*const ()`. diff --git a/src/time_source.rs b/src/time_source.rs index 974a3b6d..dd462d46 100644 --- a/src/time_source.rs +++ b/src/time_source.rs @@ -3,7 +3,7 @@ use crate::hal::Hal; use crate::internal_cell::InternalCell; -use crate::{Duration, Instant}; +use crate::{SystemDuration, SystemInstant}; use aerugo_hal::AerugoHal; /// Time source, responsible for creating timestamps. @@ -18,9 +18,9 @@ use aerugo_hal::AerugoHal; /// unless it's explicitly guaranteed by design that mutations will not occur during interrupt's execution. pub struct TimeSource { /// Time since system's scheduler start. - system_start_offset: InternalCell>, + system_start_offset: InternalCell>, /// User-defined offset. - user_offset: InternalCell>, + user_offset: InternalCell>, } impl TimeSource { @@ -78,7 +78,7 @@ impl TimeSource { /// /// # Parameters /// * `duration` - Duration to offset the time source with. - pub(crate) unsafe fn set_user_offset(&self, duration: Duration) { + pub(crate) unsafe fn set_user_offset(&self, duration: SystemDuration) { let offset_ref = unsafe { self.user_offset.as_mut_ref() }; offset_ref.replace(duration); } diff --git a/testbins/test-hal-timer/src/main.rs b/testbins/test-hal-timer/src/main.rs index 1a51a73c..a635a593 100644 --- a/testbins/test-hal-timer/src/main.rs +++ b/testbins/test-hal-timer/src/main.rs @@ -11,8 +11,11 @@ use aerugo::{ channel_config::ChannelClock, waveform_config::WaveformModeConfig, Ch0, Channel, Timer, Waveform, TC1, }, - hal::{drivers::timer::channel_config::ChannelInterrupts, interrupt, NVIC, PMC}, - Duration, InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, TaskletStorage, AERUGO, + hal::{ + drivers::interrupt, drivers::pac::NVIC, drivers::pac::PMC, + drivers::timer::channel_config::ChannelInterrupts, + }, + InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, TaskletStorage, AERUGO, }; use calldwell::with_rtt_out; use core::{cell::RefCell, fmt::Write, ops::AddAssign}; @@ -150,10 +153,7 @@ fn get_irq_count() -> u32 { fn main() -> ! { calldwell::start_session(); - let peripherals = AERUGO.initialize(SystemHardwareConfig { - watchdog_timeout: Duration::secs(3), - ..Default::default() - }); + let peripherals = AERUGO.initialize(SystemHardwareConfig::default()); let timer = Timer::new(peripherals.timer_counter1.expect("TC1 already taken!")); diff --git a/testbins/test-hal-watchdog/src/main.rs b/testbins/test-hal-watchdog/src/main.rs index 2de9c077..df132c1c 100644 --- a/testbins/test-hal-watchdog/src/main.rs +++ b/testbins/test-hal-watchdog/src/main.rs @@ -7,7 +7,8 @@ extern crate cortex_m; extern crate cortex_m_rt as rt; use aerugo::{ - Duration, InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, TaskletStorage, AERUGO, + hal::drivers::Milliseconds, InitApi, RuntimeApi, SystemHardwareConfig, TaskletConfig, + TaskletStorage, AERUGO, }; use calldwell::with_rtt_out; use cortex_m::asm; @@ -101,7 +102,7 @@ fn main() -> ! { calldwell::start_session(); AERUGO.initialize(SystemHardwareConfig { - watchdog_timeout: Duration::secs(3), + watchdog_timeout: Milliseconds::secs(5), ..Default::default() }); diff --git a/tests/requirements/test/test_hal_watchdog.py b/tests/requirements/test/test_hal_watchdog.py index 19677787..2f339414 100644 --- a/tests/requirements/test/test_hal_watchdog.py +++ b/tests/requirements/test/test_hal_watchdog.py @@ -28,7 +28,7 @@ def main(): exit(2) logging.info("Expecting a watchdog-induced MCU reset now...") - # Default watchdog timeout is 16s. Watchdog in this test is set to 3s, but timeout must be + # Default watchdog timeout is 16s. Watchdog in this test is set to 5s, but timeout must be # few seconds higher to compensate for communication delays and MCU clock inaccuracies. gdb.wait_for_reset(timeout=10) logging.info("Watchdog-induced reset detected!") diff --git a/tests/requirements/test/test_hal_watchdog.rs b/tests/requirements/test/test_hal_watchdog.rs index a16d67ef..d1d70ac2 100644 --- a/tests/requirements/test/test_hal_watchdog.rs +++ b/tests/requirements/test/test_hal_watchdog.rs @@ -1,7 +1,7 @@ // Test scenario: -// - Configure Aerugo with watchdog that will reset the MCU after 3 seconds -// - Execute a task that will run shorter than 3 seconds and send a message to host -// - Execute a task that will run longer than 3 seconds +// - Configure Aerugo with watchdog that will reset the MCU after 5 seconds +// - Execute a task that will run shorter than 5 seconds and send a message to host +// - Execute a task that will run longer than 5 seconds // - Validate that MCU has rebooted /// @SRS{ROS-FUN-BSP-WDT-020} From fb6c9c815c0272c5c2e69bce227d0b16e9998e26 Mon Sep 17 00:00:00 2001 From: Wojciech Olech Date: Thu, 31 Aug 2023 14:55:35 +0200 Subject: [PATCH 3/6] TimeSource: Fixed duration type names after rebase --- src/aerugo.rs | 2 +- src/time_source.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/aerugo.rs b/src/aerugo.rs index 265c49f6..cac461a9 100644 --- a/src/aerugo.rs +++ b/src/aerugo.rs @@ -711,7 +711,7 @@ impl RuntimeApi for Aerugo { EVENT_MANAGER.clear() } - fn get_system_time(&'static self) -> Instant { + fn get_system_time(&'static self) -> SystemInstant { match self.time_source.time_since_user_offset() { Some(system_time) => system_time, None => TimeSource::time_since_init(), diff --git a/src/time_source.rs b/src/time_source.rs index dd462d46..8164a3af 100644 --- a/src/time_source.rs +++ b/src/time_source.rs @@ -34,7 +34,7 @@ impl TimeSource { /// Returns time since system initialization (call to [`Aerugo::initialize`](crate::Aerugo::initialize), start of the hardware timer) #[inline(always)] - pub fn time_since_init() -> Instant { + pub fn time_since_init() -> SystemInstant { Hal::get_system_time() } @@ -44,7 +44,7 @@ impl TimeSource { /// This is safe as long as it's used in single-core context, and `TimeSource` does not pass interrupt boundary. /// Calling [`TimeSource::mark_system_start`] in parallel with this function (interrupt is treated as different thread) /// is an undefined behavior. - pub fn time_since_start(&self) -> Option { + pub fn time_since_start(&self) -> Option { match unsafe { *self.system_start_offset.as_ref() } { Some(start_offset) => TimeSource::time_since_init().checked_sub_duration(start_offset), None => None, @@ -57,7 +57,7 @@ impl TimeSource { /// This is safe as long as it's used in single-core context, and `TimeSource` does not pass interrupt boundary. /// Calling [`TimeSource::set_user_offset`] in parallel with this function (interrupt is treated as different thread) /// is an undefined behavior. - pub fn time_since_user_offset(&self) -> Option { + pub fn time_since_user_offset(&self) -> Option { match unsafe { *self.user_offset.as_ref() } { Some(user_offset) => TimeSource::time_since_init().checked_add_duration(user_offset), None => None, @@ -89,7 +89,7 @@ impl TimeSource { /// This is safe as long as it's used in single-core context, and `TimeSource` does not pass interrupt boundary. /// Calling [`TimeSource::mark_system_start`] in parallel with this function (interrupt is treated as different /// thread) is an undefined behavior. - pub fn startup_duration(&self) -> Option { + pub fn startup_duration(&self) -> Option { unsafe { *self.system_start_offset.as_ref() } } From d0cfdb81697b2d8e60ad0c01d4c9574397dcc131 Mon Sep 17 00:00:00 2001 From: Wojciech Olech Date: Thu, 31 Aug 2023 15:02:33 +0200 Subject: [PATCH 4/6] HAL: Moved cortex-m dependency to samv71-hal --- arch/cortex-m/aerugo-samv71-hal/Cargo.toml | 1 - arch/cortex-m/aerugo-samv71-hal/src/hal.rs | 5 ++--- arch/cortex-m/aerugo-samv71-hal/src/lib.rs | 1 + arch/cortex-m/samv71-hal/Cargo.toml | 1 + arch/cortex-m/samv71-hal/src/lib.rs | 1 + 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/cortex-m/aerugo-samv71-hal/Cargo.toml b/arch/cortex-m/aerugo-samv71-hal/Cargo.toml index e7714d87..ded8f818 100644 --- a/arch/cortex-m/aerugo-samv71-hal/Cargo.toml +++ b/arch/cortex-m/aerugo-samv71-hal/Cargo.toml @@ -12,7 +12,6 @@ description = "Crate with AerugoHal implementation for SAMV71" [dependencies] aerugo-hal = { version = "0.1.0", path = "../../../aerugo-hal" } bare-metal = "0.2.5" -cortex-m = "0.7.7" 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 63de732b..1e9b1a94 100644 --- a/arch/cortex-m/aerugo-samv71-hal/src/hal.rs +++ b/arch/cortex-m/aerugo-samv71-hal/src/hal.rs @@ -4,8 +4,7 @@ use aerugo_hal::SystemInstant; use aerugo_hal::{AerugoHal, SystemHardwareConfig}; use bare_metal::CriticalSection; -use cortex_m::asm; - +use crate::cortex_m; use crate::error::HalError; use crate::system_peripherals::SystemPeripherals; use crate::user_peripherals::UserPeripherals; @@ -348,7 +347,7 @@ fn configure_timer_pmc(pmc: &PMC) { // Wait until PCK6 is ready while pmc.sr.read().pckrdy6().bit_is_clear() { - asm::nop(); + cortex_m::asm::nop(); } } diff --git a/arch/cortex-m/aerugo-samv71-hal/src/lib.rs b/arch/cortex-m/aerugo-samv71-hal/src/lib.rs index 01927766..7d9c62e4 100644 --- a/arch/cortex-m/aerugo-samv71-hal/src/lib.rs +++ b/arch/cortex-m/aerugo-samv71-hal/src/lib.rs @@ -14,4 +14,5 @@ pub mod user_peripherals; pub use hal::Hal; pub use samv71_hal as drivers; +pub use samv71_hal::cortex_m; pub use user_peripherals::UserPeripherals; diff --git a/arch/cortex-m/samv71-hal/Cargo.toml b/arch/cortex-m/samv71-hal/Cargo.toml index ecf80bdb..e427a3ff 100644 --- a/arch/cortex-m/samv71-hal/Cargo.toml +++ b/arch/cortex-m/samv71-hal/Cargo.toml @@ -10,6 +10,7 @@ license.workspace = true description = "Crate containing drivers for SAMV71 peripherals" [dependencies] +cortex-m = "0.7.7" embedded-hal = "0.2.7" fugit = "0.3.7" samv71q21-pac = { version = "0.1.0", path = "../samv71q21-pac" } diff --git a/arch/cortex-m/samv71-hal/src/lib.rs b/arch/cortex-m/samv71-hal/src/lib.rs index 08ce60dd..4e9bdd14 100644 --- a/arch/cortex-m/samv71-hal/src/lib.rs +++ b/arch/cortex-m/samv71-hal/src/lib.rs @@ -5,6 +5,7 @@ #![warn(clippy::missing_docs_in_private_items)] #![warn(rustdoc::missing_crate_level_docs)] +pub use cortex_m; pub use embedded_hal; pub use fugit as time; pub use samv71q21_pac as pac; From 2f6fc5a7310918c1f33d459f72da6fa9acc50568 Mon Sep 17 00:00:00 2001 From: Wojciech Olech Date: Thu, 31 Aug 2023 15:10:37 +0200 Subject: [PATCH 5/6] HAL: Imports cleaned up --- arch/cortex-m/aerugo-samv71-hal/src/hal.rs | 3 +-- arch/cortex-m/aerugo-samv71-hal/src/system_peripherals.rs | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/cortex-m/aerugo-samv71-hal/src/hal.rs b/arch/cortex-m/aerugo-samv71-hal/src/hal.rs index 1e9b1a94..86208a09 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::SystemInstant; -use aerugo_hal::{AerugoHal, SystemHardwareConfig}; +use aerugo_hal::{AerugoHal, SystemHardwareConfig, SystemInstant}; use bare_metal::CriticalSection; use crate::cortex_m; diff --git a/arch/cortex-m/aerugo-samv71-hal/src/system_peripherals.rs b/arch/cortex-m/aerugo-samv71-hal/src/system_peripherals.rs index 966c4c46..b7cd6de9 100644 --- a/arch/cortex-m/aerugo-samv71-hal/src/system_peripherals.rs +++ b/arch/cortex-m/aerugo-samv71-hal/src/system_peripherals.rs @@ -1,7 +1,6 @@ //! Module representing peripherals internally used by Aerugo. use samv71_hal::pac::{PMC, TC0}; - use samv71_hal::{ timer::{Ch0, Ch1, Ch2, Channel, Timer, Waveform}, watchdog::Watchdog, From ee40460d437d0b56646bee94cc8c95ce323214ba Mon Sep 17 00:00:00 2001 From: Wojciech Olech Date: Thu, 31 Aug 2023 17:11:48 +0200 Subject: [PATCH 6/6] AerugoHal: Removed 'System' prefix from time types --- aerugo-hal/src/lib.rs | 6 +++--- arch/cortex-m/aerugo-samv71-hal/src/hal.rs | 6 +++--- arch/x86/aerugo-x86-hal/src/hal.rs | 6 +++--- src/aerugo.rs | 10 +++++----- src/api/runtime_api.rs | 10 +++++----- src/lib.rs | 2 +- src/tasklet.rs | 10 +++++----- src/tasklet/tasklet_ptr.rs | 6 +++--- src/tasklet/tasklet_vtable.rs | 10 +++++----- src/time_source.rs | 16 ++++++++-------- 10 files changed, 41 insertions(+), 41 deletions(-) diff --git a/aerugo-hal/src/lib.rs b/aerugo-hal/src/lib.rs index a4b6d23d..eaf5070d 100644 --- a/aerugo-hal/src/lib.rs +++ b/aerugo-hal/src/lib.rs @@ -20,9 +20,9 @@ pub use fugit as time; /// Aerugo requires a timer with frequency of 1MHz to measure time with microsecond precision. pub const SYSTEM_TIMER_FREQUENCY: u32 = 1_000_000; /// Type representing Aerugo timestamp. -pub type SystemInstant = time::TimerInstantU64; +pub type Instant = time::TimerInstantU64; /// Type representing Aerugo duration. -pub type SystemDuration = time::TimerDurationU64; +pub type Duration = time::TimerDurationU64; /// System HAL trait. pub trait AerugoHal { @@ -38,7 +38,7 @@ pub trait AerugoHal { fn configure_hardware(config: SystemHardwareConfig) -> Result<(), Self::Error>; /// Gets current system time timestamp. - fn get_system_time() -> SystemInstant; + fn get_system_time() -> Instant; /// Feeds the system watchdog. fn feed_watchdog(); diff --git a/arch/cortex-m/aerugo-samv71-hal/src/hal.rs b/arch/cortex-m/aerugo-samv71-hal/src/hal.rs index 86208a09..22acf89f 100644 --- a/arch/cortex-m/aerugo-samv71-hal/src/hal.rs +++ b/arch/cortex-m/aerugo-samv71-hal/src/hal.rs @@ -1,6 +1,6 @@ //! System HAL implementation for Cortex-M SAMV71 target. -use aerugo_hal::{AerugoHal, SystemHardwareConfig, SystemInstant}; +use aerugo_hal::{AerugoHal, Instant, SystemHardwareConfig}; use bare_metal::CriticalSection; use crate::cortex_m; @@ -193,7 +193,7 @@ impl AerugoHal for Hal { result } - fn get_system_time() -> SystemInstant { + fn get_system_time() -> Instant { // SAFETY: This is safe, because this is a single-core system, and no other references to // system peripherals should exist during this call. let peripherals = unsafe { @@ -220,7 +220,7 @@ impl AerugoHal for Hal { let time_ch0 = ch0.counter_value(); // Timer's clock is 1MHz, so returned value is in microseconds. - SystemInstant::from_ticks(as_48bit_unsigned(time_ch0, time_ch1, time_ch2)) + Instant::from_ticks(as_48bit_unsigned(time_ch0, time_ch1, time_ch2)) } fn feed_watchdog() { diff --git a/arch/x86/aerugo-x86-hal/src/hal.rs b/arch/x86/aerugo-x86-hal/src/hal.rs index f8eb020d..0527cde9 100644 --- a/arch/x86/aerugo-x86-hal/src/hal.rs +++ b/arch/x86/aerugo-x86-hal/src/hal.rs @@ -3,7 +3,7 @@ use std::convert::TryInto; use std::time::SystemTime; -use aerugo_hal::SystemInstant; +use aerugo_hal::Instant; use aerugo_hal::{AerugoHal, SystemHardwareConfig}; use bare_metal::CriticalSection; use once_cell::sync::Lazy; @@ -32,8 +32,8 @@ impl AerugoHal for Hal { Ok(()) } - fn get_system_time() -> SystemInstant { - SystemInstant::from_ticks( + fn get_system_time() -> Instant { + Instant::from_ticks( TIME_START .elapsed() .expect("{}") diff --git a/src/aerugo.rs b/src/aerugo.rs index cac461a9..0ef531a7 100644 --- a/src/aerugo.rs +++ b/src/aerugo.rs @@ -24,7 +24,7 @@ use crate::message_queue::{MessageQueueHandle, MessageQueueStorage}; use crate::tasklet::{StepFn, TaskletConfig, TaskletHandle, TaskletId, TaskletPtr, TaskletStorage}; use crate::time_manager::TimeManager; use crate::time_source::TimeSource; -use crate::{SystemDuration, SystemInstant}; +use crate::{Duration, Instant}; /// Core system. /// @@ -711,14 +711,14 @@ impl RuntimeApi for Aerugo { EVENT_MANAGER.clear() } - fn get_system_time(&'static self) -> SystemInstant { + fn get_system_time(&'static self) -> Instant { match self.time_source.time_since_user_offset() { Some(system_time) => system_time, None => TimeSource::time_since_init(), } } - fn set_system_time_offset(&'static self, offset: SystemDuration) { + fn set_system_time_offset(&'static self, offset: Duration) { // SAFETY: This is safe, because it's called from non-IRQ context, and // system time cannot be accessed from IRQ context unsafe { @@ -728,13 +728,13 @@ impl RuntimeApi for Aerugo { /// Returns time elapsed between system initialization and start of the scheduler. /// If called before [`Aerugo::start`](crate::Aerugo::start), returns `None`. - fn get_startup_time(&'static self) -> Option { + fn get_startup_time(&'static self) -> Option { self.time_source.startup_duration() } /// Returns time elapsed since scheduler's start. /// If called before [`Aerugo::start`](crate::Aerugo::start), returns `None`. - fn get_time_since_startup(&'static self) -> Option { + fn get_time_since_startup(&'static self) -> Option { self.time_source.time_since_start() } diff --git a/src/api/runtime_api.rs b/src/api/runtime_api.rs index 454e4e11..dc135d66 100644 --- a/src/api/runtime_api.rs +++ b/src/api/runtime_api.rs @@ -8,7 +8,7 @@ use crate::api::RuntimeError; use crate::event::EventId; use crate::execution_monitoring::ExecutionStats; use crate::tasklet::TaskletId; -use crate::{SystemDuration, SystemInstant}; +use crate::{Duration, Instant}; /// System runtime API. pub trait RuntimeApi { @@ -34,21 +34,21 @@ pub trait RuntimeApi { fn clear_event_queue(&'static self); /// Gets current system time timestamp. - fn get_system_time(&'static self) -> SystemInstant; + fn get_system_time(&'static self) -> Instant; /// Sets system time offset. /// /// # Parameters /// * `offset` - Time offset. - fn set_system_time_offset(&'static self, offset: SystemDuration); + fn set_system_time_offset(&'static self, offset: Duration); /// Returns time elapsed between system initialization and start of the scheduler. /// If called before scheduler's start, should return `None`. - fn get_startup_time(&'static self) -> Option; + fn get_startup_time(&'static self) -> Option; /// Returns time elapsed since scheduler's start. /// If called before scheduler's start, should return `None`. - fn get_time_since_startup(&'static self) -> Option; + fn get_time_since_startup(&'static self) -> Option; /// Returns an iterator to the list with IDs of registered tasklets. fn query_tasks(&'static self) -> core::slice::Iter; diff --git a/src/lib.rs b/src/lib.rs index 44ff46b0..55f7d56a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ pub use self::boolean_condition::{ pub use self::event::{EventEnabler, EventId}; pub use self::message_queue::{MessageQueueHandle, MessageQueueStorage}; pub use self::tasklet::{TaskletConfig, TaskletStorage}; -pub use aerugo_hal::{time, SystemDuration, SystemHardwareConfig, SystemInstant}; +pub use aerugo_hal::{time, Duration, Instant, SystemHardwareConfig}; #[cfg(feature = "use-aerugo-cortex-m")] pub(crate) use aerugo_cortex_m as arch; diff --git a/src/tasklet.rs b/src/tasklet.rs index 8336b4e0..badceb5a 100644 --- a/src/tasklet.rs +++ b/src/tasklet.rs @@ -32,7 +32,7 @@ use crate::arch::Mutex; use crate::boolean_condition::BooleanConditionSet; use crate::data_provider::DataProvider; use crate::internal_cell::InternalCell; -use crate::SystemInstant; +use crate::Instant; /// Type of function that is executed by the tasklet in its step. pub(crate) type StepFn = fn(T, &mut C, &'static dyn RuntimeApi); @@ -55,7 +55,7 @@ pub(crate) struct Tasklet { /// Tasklet status. status: Mutex, /// Last execution time. - last_execution_time: Mutex, + last_execution_time: Mutex, /// Step function. step_fn: StepFn, /// Context data. @@ -78,7 +78,7 @@ impl Tasklet { name: config.name, priority: config.priority, status: Mutex::new(TaskletStatus::Sleeping), - last_execution_time: Mutex::new(SystemInstant::from_ticks(0)), + last_execution_time: Mutex::new(Instant::from_ticks(0)), step_fn, context: InternalCell::new(context), condition_set, @@ -110,7 +110,7 @@ impl Tasklet { } /// Returns last execution time. - pub(crate) fn get_last_execution_time(&self) -> SystemInstant { + pub(crate) fn get_last_execution_time(&self) -> Instant { self.last_execution_time.lock(|t| *t) } @@ -118,7 +118,7 @@ impl Tasklet { /// /// # Parameters /// * `time` - Last execution time. - pub(crate) fn set_last_execution_time(&self, time: SystemInstant) { + pub(crate) fn set_last_execution_time(&self, time: Instant) { self.last_execution_time.lock(|t| *t = time) } diff --git a/src/tasklet/tasklet_ptr.rs b/src/tasklet/tasklet_ptr.rs index 71a6722d..ecb7c417 100644 --- a/src/tasklet/tasklet_ptr.rs +++ b/src/tasklet/tasklet_ptr.rs @@ -14,7 +14,7 @@ use core::cmp::Ordering; use crate::tasklet::{tasklet_vtable, Tasklet, TaskletStatus, TaskletVTable}; -use crate::SystemInstant; +use crate::Instant; /// Raw tasklet pointer. #[derive(Clone)] @@ -70,13 +70,13 @@ impl TaskletPtr { /// See: [get_last_execution_time](crate::tasklet::Tasklet::get_last_execution_time()) #[inline(always)] - pub(crate) fn get_last_execution_time(&self) -> SystemInstant { + pub(crate) fn get_last_execution_time(&self) -> Instant { (self.vtable.get_last_execution_time)(self.ptr) } /// See: [set_last_execution_time](crate::tasklet::Tasklet::set_last_execution_time()) #[inline(always)] - pub(crate) fn set_last_execution_time(&self, time: SystemInstant) { + pub(crate) fn set_last_execution_time(&self, time: Instant) { (self.vtable.set_last_execution_time)(self.ptr, time) } diff --git a/src/tasklet/tasklet_vtable.rs b/src/tasklet/tasklet_vtable.rs index 6fb0e6aa..c68c9c56 100644 --- a/src/tasklet/tasklet_vtable.rs +++ b/src/tasklet/tasklet_vtable.rs @@ -7,7 +7,7 @@ //! For more information look at `TaskletPtr` structure. use crate::tasklet::{Tasklet, TaskletStatus}; -use crate::SystemInstant; +use crate::Instant; /// Hand-made tasklet virtual table. pub(crate) struct TaskletVTable { @@ -20,9 +20,9 @@ pub(crate) struct TaskletVTable { /// Pointer to [set_status](set_status()) function. pub(crate) set_status: fn(*const (), TaskletStatus), /// Pointer to [get_last_execution_time](get_last_execution_time()) function. - pub(crate) get_last_execution_time: fn(*const ()) -> SystemInstant, + pub(crate) get_last_execution_time: fn(*const ()) -> Instant, /// Pointer to [set_last_execution_time](set_last_execution_time()) function. - pub(crate) set_last_execution_time: fn(*const (), SystemInstant), + pub(crate) set_last_execution_time: fn(*const (), Instant), /// Pointer to [has_work](has_work()) function. pub(crate) has_work: fn(*const ()) -> bool, /// Pointer to [is_active](is_active()) function. @@ -104,7 +104,7 @@ fn set_status( #[inline(always)] fn get_last_execution_time( ptr: *const (), -) -> SystemInstant { +) -> Instant { // SAFETY: This is safe, because `Tasklet` is the only structure that implements `Task` trait, // and so is the only type that we store in the `*const ()`. let tasklet = unsafe { &*(ptr as *const Tasklet) }; @@ -117,7 +117,7 @@ fn get_last_execution_time( #[inline(always)] fn set_last_execution_time( ptr: *const (), - time: SystemInstant, + time: Instant, ) { // SAFETY: This is safe, because `Tasklet` is the only structure that implements `Task` trait, // and so is the only type that we store in the `*const ()`. diff --git a/src/time_source.rs b/src/time_source.rs index 8164a3af..974a3b6d 100644 --- a/src/time_source.rs +++ b/src/time_source.rs @@ -3,7 +3,7 @@ use crate::hal::Hal; use crate::internal_cell::InternalCell; -use crate::{SystemDuration, SystemInstant}; +use crate::{Duration, Instant}; use aerugo_hal::AerugoHal; /// Time source, responsible for creating timestamps. @@ -18,9 +18,9 @@ use aerugo_hal::AerugoHal; /// unless it's explicitly guaranteed by design that mutations will not occur during interrupt's execution. pub struct TimeSource { /// Time since system's scheduler start. - system_start_offset: InternalCell>, + system_start_offset: InternalCell>, /// User-defined offset. - user_offset: InternalCell>, + user_offset: InternalCell>, } impl TimeSource { @@ -34,7 +34,7 @@ impl TimeSource { /// Returns time since system initialization (call to [`Aerugo::initialize`](crate::Aerugo::initialize), start of the hardware timer) #[inline(always)] - pub fn time_since_init() -> SystemInstant { + pub fn time_since_init() -> Instant { Hal::get_system_time() } @@ -44,7 +44,7 @@ impl TimeSource { /// This is safe as long as it's used in single-core context, and `TimeSource` does not pass interrupt boundary. /// Calling [`TimeSource::mark_system_start`] in parallel with this function (interrupt is treated as different thread) /// is an undefined behavior. - pub fn time_since_start(&self) -> Option { + pub fn time_since_start(&self) -> Option { match unsafe { *self.system_start_offset.as_ref() } { Some(start_offset) => TimeSource::time_since_init().checked_sub_duration(start_offset), None => None, @@ -57,7 +57,7 @@ impl TimeSource { /// This is safe as long as it's used in single-core context, and `TimeSource` does not pass interrupt boundary. /// Calling [`TimeSource::set_user_offset`] in parallel with this function (interrupt is treated as different thread) /// is an undefined behavior. - pub fn time_since_user_offset(&self) -> Option { + pub fn time_since_user_offset(&self) -> Option { match unsafe { *self.user_offset.as_ref() } { Some(user_offset) => TimeSource::time_since_init().checked_add_duration(user_offset), None => None, @@ -78,7 +78,7 @@ impl TimeSource { /// /// # Parameters /// * `duration` - Duration to offset the time source with. - pub(crate) unsafe fn set_user_offset(&self, duration: SystemDuration) { + pub(crate) unsafe fn set_user_offset(&self, duration: Duration) { let offset_ref = unsafe { self.user_offset.as_mut_ref() }; offset_ref.replace(duration); } @@ -89,7 +89,7 @@ impl TimeSource { /// This is safe as long as it's used in single-core context, and `TimeSource` does not pass interrupt boundary. /// Calling [`TimeSource::mark_system_start`] in parallel with this function (interrupt is treated as different /// thread) is an undefined behavior. - pub fn startup_duration(&self) -> Option { + pub fn startup_duration(&self) -> Option { unsafe { *self.system_start_offset.as_ref() } }