From ba61633846ea0ddcbe997de3103346e8aa0a5869 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 26 Apr 2026 13:49:10 +0200 Subject: [PATCH 1/2] use cortex-m-types in NVIC --- cortex-m/CHANGELOG.md | 14 ++++++++++++++ cortex-m/Cargo.toml | 1 + cortex-m/src/interrupt.rs | 20 +++++++++++++++----- cortex-m/src/peripheral/nvic.rs | 18 +++++++++--------- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/cortex-m/CHANGELOG.md b/cortex-m/CHANGELOG.md index 941655ce..e9255578 100644 --- a/cortex-m/CHANGELOG.md +++ b/cortex-m/CHANGELOG.md @@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed + +- `nvic` methods now take `cortex_m_types::InterruptNumber` which is re-exported + as `InterruptNumber`. PACs and HALs need to implement the new trait. + +### Removed + +- Removed `bare_metal::interrupt::Nr` blanket implementation of `InterruptNumber` +- Removed `bare_metal::interrupt::Nr` re-export inside `interrupt` + +### Deprecated + +- `interrupt::InterruptNumber`. Implement `cortex_m_types::InterruptNumber` instead. + ## [v0.7.10] - 2026-10-21 ### Removed diff --git a/cortex-m/Cargo.toml b/cortex-m/Cargo.toml index 832902d5..995f0119 100644 --- a/cortex-m/Cargo.toml +++ b/cortex-m/Cargo.toml @@ -20,6 +20,7 @@ bitfield = "0.13.2" eh0 = { package = "embedded-hal", version = "0.2.4" } eh1 = { package = "embedded-hal", version = "1.0.0" } cortex-m-macros = { path = "macros", version = "=0.7.8" } +cortex-m-types = { path = "../cortex-m-types", version = "0.1" } [dependencies.serde] version = "1" diff --git a/cortex-m/src/interrupt.rs b/cortex-m/src/interrupt.rs index bdf63ded..b00b9ac5 100644 --- a/cortex-m/src/interrupt.rs +++ b/cortex-m/src/interrupt.rs @@ -1,6 +1,6 @@ //! Interrupts -pub use bare_metal::{CriticalSection, Mutex, Nr}; +pub use bare_metal::{CriticalSection, Mutex}; #[cfg(cortex_m)] use core::arch::asm; #[cfg(cortex_m)] @@ -21,6 +21,10 @@ use cortex_m_macros::asm_cfg; /// and must always return the same value (do not change at runtime). /// /// These requirements ensure safe nesting of critical sections. +#[deprecated( + since = "0.8.0", + note = "Implement the cortex_m_types::InterruptNumber trait instead" +)] pub unsafe trait InterruptNumber: Copy { /// Return the interrupt number associated with this variant. /// @@ -28,12 +32,18 @@ pub unsafe trait InterruptNumber: Copy { fn number(self) -> u16; } -/// Implement InterruptNumber for the old bare_metal::Nr trait. -/// This implementation is for backwards compatibility only and will be removed in cortex-m 0.8. -unsafe impl InterruptNumber for T { +// This trait is only here for backwards compatibility. +#[allow(deprecated)] +unsafe impl InterruptNumber for T { #[inline] fn number(self) -> u16 { - self.nr() as u16 + const { + assert!( + T::MAX_INTERRUPT_NUMBER <= u16::MAX as usize, + "interrupt numbers must fit into a u16" + ) + }; + ::number(self) as u16 } } diff --git a/cortex-m/src/peripheral/nvic.rs b/cortex-m/src/peripheral/nvic.rs index 8f0b73d7..651051fa 100644 --- a/cortex-m/src/peripheral/nvic.rs +++ b/cortex-m/src/peripheral/nvic.rs @@ -1,10 +1,10 @@ //! Nested Vector Interrupt Controller +pub use cortex_m_types::InterruptNumber; use volatile_register::RW; #[cfg(not(armv6m))] use volatile_register::{RO, WO}; -use crate::interrupt::InterruptNumber; use crate::peripheral::NVIC; /// Register block @@ -101,7 +101,7 @@ impl NVIC { let nr = interrupt.number(); unsafe { - self.stir.write(u32::from(nr)); + self.stir.write(nr as u32); } } @@ -147,7 +147,7 @@ impl NVIC { { let nr = interrupt.number(); // NOTE(unsafe) atomic read with no side effects - unsafe { (*Self::PTR).ipr[usize::from(nr)].read() } + unsafe { (*Self::PTR).ipr[nr].read() } } #[cfg(armv6m)] @@ -233,7 +233,7 @@ impl NVIC { #[cfg(not(armv6m))] { let nr = interrupt.number(); - self.ipr[usize::from(nr)].write(prio) + self.ipr[nr].write(prio) } #[cfg(armv6m)] @@ -320,9 +320,9 @@ impl NVIC { /// /// The `&` helps remove an expensive bounds check - we know all valid interrupt numbers will be in range. #[inline] - const fn reg_index_and_mask(irq: u16) -> (usize, u32) { - debug_assert!((irq as usize) < (Self::BITMAP_WORDS * 32)); - let idx = (irq / 32) as usize & (Self::BITMAP_WORDS - 1); + const fn reg_index_and_mask(irq: usize) -> (usize, u32) { + debug_assert!(irq < (Self::BITMAP_WORDS * 32)); + let idx = (irq / 32) & (Self::BITMAP_WORDS - 1); let mask = 1 << (irq % 32); (idx, mask) } @@ -333,7 +333,7 @@ impl NVIC { where I: InterruptNumber, { - usize::from(interrupt.number()) / 4 + interrupt.number() / 4 } #[cfg(armv6m)] @@ -342,6 +342,6 @@ impl NVIC { where I: InterruptNumber, { - (usize::from(interrupt.number()) % 4) * 8 + (interrupt.number() % 4) * 8 } } From e84427921895ba6568ed50b5a2233317962c2c62 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 27 Apr 2026 14:11:59 +0200 Subject: [PATCH 2/2] remove bare-metal dependency --- cortex-m/Cargo.toml | 1 - cortex-m/src/interrupt.rs | 2 +- cortex-m/src/lib.rs | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/cortex-m/Cargo.toml b/cortex-m/Cargo.toml index 995f0119..72bebd2e 100644 --- a/cortex-m/Cargo.toml +++ b/cortex-m/Cargo.toml @@ -13,7 +13,6 @@ links = "cortex-m" # prevent multiple versions of this crate to be linked toget rust-version = "1.85" [dependencies] -bare-metal = { version = "0.2.4", features = ["const-fn"] } critical-section = "1.0.0" volatile-register = "0.2.2" bitfield = "0.13.2" diff --git a/cortex-m/src/interrupt.rs b/cortex-m/src/interrupt.rs index b00b9ac5..10b0ceba 100644 --- a/cortex-m/src/interrupt.rs +++ b/cortex-m/src/interrupt.rs @@ -1,11 +1,11 @@ //! Interrupts -pub use bare_metal::{CriticalSection, Mutex}; #[cfg(cortex_m)] use core::arch::asm; #[cfg(cortex_m)] use core::sync::atomic::{Ordering, compiler_fence}; use cortex_m_macros::asm_cfg; +use critical_section::CriticalSection; /// Trait for enums of external interrupt numbers. /// diff --git a/cortex-m/src/lib.rs b/cortex-m/src/lib.rs index a3ed32e4..5949e48e 100644 --- a/cortex-m/src/lib.rs +++ b/cortex-m/src/lib.rs @@ -92,7 +92,6 @@ #[cfg(all(feature = "cm7-r0p1", not(armv7em)))] compile_error!("The feature \"cm7-r0p1\" is only compatible with the armv7em target"); -extern crate bare_metal; extern crate volatile_register; #[macro_use]