From 4ced5c2cc1f81724a2c9934a48141b5fea96565b Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 25 Apr 2026 11:33:39 +0200 Subject: [PATCH 1/3] use cortex-m-types crate for the InterruptNumber trait --- src/generate/interrupt.rs | 43 ++++++++++++++++++++++++-------- src/lib.rs | 2 +- svd2rust-regress/src/svd_test.rs | 2 +- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/generate/interrupt.rs b/src/generate/interrupt.rs index 1e9420fb..157914c6 100644 --- a/src/generate/interrupt.rs +++ b/src/generate/interrupt.rs @@ -120,7 +120,7 @@ pub fn render( (quote!(#[no_mangle]), quote!(extern)) }; - let n = util::unsuffixed(pos); + let num_of_interrupts = util::unsuffixed(pos); match target { Target::CortexM => { for name in &names { @@ -154,7 +154,7 @@ pub fn render( #[doc(hidden)] #link_section_attr #nomangle - pub static __INTERRUPTS: [Vector; #n] = [ + pub static __INTERRUPTS: [Vector; #num_of_interrupts] = [ #elements ]; }); @@ -193,7 +193,7 @@ pub fn render( #nomangle #[used] pub static __INTERRUPTS: - [Vector; #n] = [ + [Vector; #num_of_interrupts] = [ #elements ]; }); @@ -228,7 +228,7 @@ pub fn render( #[doc(hidden)] #link_section_attr #nomangle - pub static __EXTERNAL_INTERRUPTS: [Vector; #n] = [ + pub static __EXTERNAL_INTERRUPTS: [Vector; #num_of_interrupts] = [ #elements ]; }); @@ -263,7 +263,7 @@ pub fn render( #[doc(hidden)] #link_section_attr #nomangle - pub static __INTERRUPTS: [Vector; #n] = [ + pub static __INTERRUPTS: [Vector; #num_of_interrupts] = [ #elements ]; }); @@ -273,10 +273,10 @@ pub fn render( } let self_token = quote!(self); - let (enum_repr, nr_expr) = if variants.is_empty() { - (quote!(), quote!(match #self_token {})) + let enum_repr = if variants.is_empty() { + quote!() } else { - (quote!(#[repr(u16)]), quote!(#self_token as u16)) + quote!(#[repr(u16)]) }; let defmt = config @@ -309,13 +309,34 @@ pub fn render( match target { Target::CortexM => { + let num_value: usize = num_of_interrupts.base10_parse()?; + let max_interrupt_number = num_value.saturating_sub(1); + root.extend(quote! { #interrupt_enum - unsafe impl cortex_m::interrupt::InterruptNumber for Interrupt { + unsafe impl cortex_m_types::InterruptNumber for Interrupt { + const MAX_INTERRUPT_NUMBER: usize = #max_interrupt_number; + #[inline(always)] - fn number(#self_token) -> u16 { - #nr_expr + fn number(#self_token) -> usize { + #self_token as usize + } + + /// Tries to convert a number to a valid interrupt. + #[inline] + fn from_number(value: usize) -> cortex_m_types::result::Result { + if value > Self::MAX_INTERRUPT_NUMBER { + return Err(cortex_m_types::result::Error::IndexOutOfBounds { + index: value, + min: 0, + max: Self::MAX_INTERRUPT_NUMBER, + }); + } + match value { + #from_arms + _ => Err(cortex_m_types::result::Error::InvalidVariant(value)), + } } } }); diff --git a/src/lib.rs b/src/lib.rs index 1e3f370f..770993ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,7 @@ //! ``` toml //! [dependencies] //! critical-section = { version = "1.0", optional = true } -//! cortex-m = "0.7.6" +//! cortex-m-types = "0.1" //! cortex-m-rt = { version = "0.6.13", optional = true } //! vcell = "0.1.2" //! diff --git a/svd2rust-regress/src/svd_test.rs b/svd2rust-regress/src/svd_test.rs index 5be57029..ca15cccc 100644 --- a/svd2rust-regress/src/svd_test.rs +++ b/svd2rust-regress/src/svd_test.rs @@ -18,7 +18,7 @@ const CRATES_ALL: &[&str] = &[ const CRATES_MSP430: &[&str] = &["msp430 = \"0.4.0\"", "msp430-rt = \"0.4.0\""]; const CRATES_ATOMICS: &[&str] = &["portable-atomic = { version = \"1\", default-features = false }"]; -const CRATES_CORTEX_M: &[&str] = &["cortex-m = \"0.7.6\"", "cortex-m-rt = \"0.7\""]; +const CRATES_CORTEX_M: &[&str] = &["cortex-m-types = \"0.1\"", "cortex-m-rt = \"0.7\""]; const CRATES_RISCV: &[&str] = &["riscv = \"0.12.1\"", "riscv-rt = \"0.13.0\""]; const CRATES_MIPS: &[&str] = &["mips-mcu = \"0.1.0\""]; const PROFILE_ALL: &[&str] = &["[profile.dev]", "incremental = false"]; From 33f16a5e87a6c84352ba16cc353455c3d79dfbfd Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 25 Apr 2026 11:39:18 +0200 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d1e2974..ff9b4733 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/). - Use marker struct instead of address in `Periph` with `PeripheralSpec` trait - Add `--skip-peripherals-struct` flag to skip generating the `Peripherals` struct, its `take`/`steal` impl and the `DEVICE_PERIPHERALS` static +- Generated `cortex-m` PACs now implement the `cortex_m_types::InterruptNumber` trait instead + of `cortex-m::interrupt::InterruptNumber` to avoid a hard dependency on `cortex-m` for PACs. + PACs should now use the `cortex-m-types` dependency instead of `cortex-m`. ## [v0.37.1] - 2025-10-17 From 665d7842448b485d7c786d5f960c24f6aec34be1 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 25 Apr 2026 11:50:09 +0200 Subject: [PATCH 3/3] bump MSRV to v1.81 --- .github/workflows/ci.yml | 4 ++-- CHANGELOG.md | 1 + README.md | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 645436ad..e74402cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -125,11 +125,11 @@ jobs: cargo install svd2rust --path . # Install the MSRV toolchain - - uses: dtolnay/rust-toolchain@1.76.0 + - uses: dtolnay/rust-toolchain@1.81.0 - name: Run reression tool with MSRV # The MSRV only applies to the generated crate. The regress tool should still be run with # stable. - run: cargo +stable regress tests --toolchain 1.76.0 -m Nordic -- --strict --atomics + run: cargo +stable regress tests --toolchain 1.81.0 -m Nordic -- --strict --atomics ci-docs-clippy: runs-on: ubuntu-latest diff --git a/CHANGELOG.md b/CHANGELOG.md index ff9b4733..6957fbfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). - Generated `cortex-m` PACs now implement the `cortex_m_types::InterruptNumber` trait instead of `cortex-m::interrupt::InterruptNumber` to avoid a hard dependency on `cortex-m` for PACs. PACs should now use the `cortex-m-types` dependency instead of `cortex-m`. +- Bump MSRV of generated code to 1.81 ## [v0.37.1] - 2025-10-17 diff --git a/README.md b/README.md index d0bf6fb5..830e1f67 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,9 @@ This project is developed and maintained by the [Tools team][team]. ## Minimum Supported Rust Version (MSRV) -The **generated code** is guaranteed to compile on stable Rust 1.76.0 and up. +The **generated code** is guaranteed to compile on stable Rust 1.81.0 and up. -If you encounter compilation errors on any stable version newer than 1.76.0, please open an issue. +If you encounter compilation errors on any stable version newer than 1.81.0, please open an issue. # Testing Locally