Skip to content

Commit

Permalink
feat(rtic-v2-rtc-monotonics): Addresses the remaining issue raised ab…
Browse files Browse the repository at this point in the history
…out 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`.
  • Loading branch information
Dan Whitman committed Jan 13, 2025
1 parent 0b8aff5 commit c11b7d1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
9 changes: 9 additions & 0 deletions hal/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
if prio >= 1 && prio <= 8 {
Some(unsafe { core::mem::transmute::<u8, Self>(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).
Expand Down
2 changes: 1 addition & 1 deletion hal/src/rtc/rtic/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down
18 changes: 8 additions & 10 deletions hal/src/rtc/rtic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c11b7d1

Please sign in to comment.