From c11b7d1e12f2ce92485bf326d1a5716b5809557d Mon Sep 17 00:00:00 2001 From: Dan Whitman Date: Mon, 13 Jan 2025 12:20:27 -0500 Subject: [PATCH] feat(rtic-v2-rtc-monotonics): Addresses the remaining issue raised about merging the PR (#804) * Adds the `from_numeric` method to `interrupt::Priority` to create a variant from a numeric priority. * Removes the `rtc::rtic::cortex_logical2hw` function, instead using `interrupt::Priority::logical2hw`. --- hal/src/interrupt.rs | 9 +++++++++ hal/src/rtc/rtic/backends.rs | 2 +- hal/src/rtc/rtic/mod.rs | 18 ++++++++---------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/hal/src/interrupt.rs b/hal/src/interrupt.rs index ec79802b0b3..607bec95b41 100644 --- a/hal/src/interrupt.rs +++ b/hal/src/interrupt.rs @@ -42,6 +42,15 @@ pub enum Priority { } impl Priority { + /// Creates the `Priority` from a numeric priority if possible. + pub const fn from_numeric(prio: u8) -> Option { + if prio >= 1 && prio <= 8 { + Some(unsafe { core::mem::transmute::(prio) }) + } else { + None + } + } + /// Convert a logical priority (where higher priority number = higher /// priority level) to a hardware priority level (where lower priority /// number = higher priority level). diff --git a/hal/src/rtc/rtic/backends.rs b/hal/src/rtc/rtic/backends.rs index 82bb23e32d5..88a00078e48 100644 --- a/hal/src/rtc/rtic/backends.rs +++ b/hal/src/rtc/rtic/backends.rs @@ -82,7 +82,7 @@ macro_rules! __internal_backend_methods { // plus we are not using any external shared resources so we won't impact // basepri/source masking based critical sections. unsafe { - $crate::rtc::rtic::set_monotonic_prio(pac::NVIC_PRIO_BITS, pac::Interrupt::RTC); + $crate::rtc::rtic::set_monotonic_prio(pac::Interrupt::RTC); pac::NVIC::unmask(pac::Interrupt::RTC); } }); diff --git a/hal/src/rtc/rtic/mod.rs b/hal/src/rtc/rtic/mod.rs index 8ddeba16ca0..242c4fb8122 100644 --- a/hal/src/rtc/rtic/mod.rs +++ b/hal/src/rtc/rtic/mod.rs @@ -177,6 +177,7 @@ pub mod v1 { mod backends; use super::modes::{mode0::RtcMode0, mode1::RtcMode1, RtcMode}; +use crate::interrupt::{Priority, NVIC_PRIO_BITS}; use atsamd_hal_macros::hal_cfg; /// Types used to specify the RTC clock rate at compile time when creating the @@ -314,24 +315,21 @@ macro_rules! rtc_monotonic { }; } -/// This function was copied from the private function in `rtic-monotonics`, -/// so that should be used when the monotonics move there. -const fn cortex_logical2hw(logical: u8, nvic_prio_bits: u8) -> u8 { - ((1 << nvic_prio_bits) - logical) << (8 - nvic_prio_bits) -} - -/// This function was modified from the private function in `rtic-monotonics`. +/// This function was modified from the private function in `rtic-monotonics`, +/// part of the [`rtic`](https://github.com/rtic-rs/rtic) project. /// /// Note that this depends on the static variable `RTIC_ASYNC_MAX_LOGICAL_PRIO` /// defined as part of RTIC. Refer to the example in the [`rtic` /// module](crate::rtc::rtic) documentation for more details. -unsafe fn set_monotonic_prio(prio_bits: u8, interrupt: impl cortex_m::interrupt::InterruptNumber) { +/// +/// See LICENSE-MIT and LICENSE-APACHE for the licenses. +unsafe fn set_monotonic_prio(interrupt: impl cortex_m::interrupt::InterruptNumber) { extern "C" { static RTIC_ASYNC_MAX_LOGICAL_PRIO: u8; } - let max_prio = RTIC_ASYNC_MAX_LOGICAL_PRIO.max(1).min(1 << prio_bits); - let hw_prio = cortex_logical2hw(max_prio, prio_bits); + let max_prio = RTIC_ASYNC_MAX_LOGICAL_PRIO.clamp(1, 1 << NVIC_PRIO_BITS); + let hw_prio = Priority::from_numeric(max_prio).unwrap().logical2hw(); // We take ownership of the entire IRQ and all settings to it, we only change // settings for the IRQ we control.