From 26c7236872f98991be39c07a75edb5bce4f54be0 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 3 May 2026 12:43:45 +0200 Subject: [PATCH] add back optional old cortex-m interrupt number impl generation --- .github/workflows/ci.yml | 1 + CHANGELOG.md | 6 +++--- src/config.rs | 2 ++ src/generate/interrupt.rs | 11 +++++++++++ src/main.rs | 9 +++++++++ svd2rust-regress/src/svd_test.rs | 16 +++++++++------- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e74402cc..e2baf5cf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,6 +83,7 @@ jobs: - { vendor: Spansion, options: "-- --atomics" } - { vendor: STMicro } - { vendor: STMicro, options: "-- --atomics" } + - { vendor: STMicro, options: "-- --add-cortex-m-int-num" } - { vendor: STMicro, options: "-- --strict -f enum_value::p: --max-cluster-size --atomics --atomics-feature atomics --impl-debug --impl-defmt defmt", diff --git a/CHANGELOG.md b/CHANGELOG.md index 6957fbfe..247c8527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +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`. +- Generated `cortex-m` PACs now implement the `cortex_m_types::InterruptNumber` trait instead of + the `cortex-m::interrupt::InterruptNumber` trait. A new `add-cortex-m-int-num` option + which allows adding the generation of the old trait. - Bump MSRV of generated code to 1.81 ## [v0.37.1] - 2025-10-17 diff --git a/src/config.rs b/src/config.rs index 820c8763..d275615a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -38,6 +38,8 @@ pub struct Config { pub interrupt_link_section: Option, pub reexport_core_peripherals: bool, pub reexport_interrupt: bool, + /// Also generating interrupt number trait implementation for `cortex-m` trait. + pub add_cortex_m_int_num: bool, pub ident_formats: IdentFormats, pub ident_formats_theme: Option, pub field_names_for_enums: bool, diff --git a/src/generate/interrupt.rs b/src/generate/interrupt.rs index 157914c6..3888ee46 100644 --- a/src/generate/interrupt.rs +++ b/src/generate/interrupt.rs @@ -340,6 +340,17 @@ pub fn render( } } }); + + if config.add_cortex_m_int_num { + root.extend(quote! { + unsafe impl cortex_m::interrupt::InterruptNumber for Interrupt { + #[inline(always)] + fn number(#self_token) -> u16 { + #self_token as u16 + } + } + }); + } } Target::XtensaLX => { root.extend(quote! { diff --git a/src/main.rs b/src/main.rs index 07de10cb..9c4c6565 100755 --- a/src/main.rs +++ b/src/main.rs @@ -279,6 +279,15 @@ Allowed cases are `unchanged` (''), `pascal` ('p'), `constant` ('c') and `snake` .action(ArgAction::SetTrue) .help("Reexport interrupt macro from cortex-m-rt like crates"), ) + .arg( + Arg::new("add_cortex_m_int_num") + .long("add-cortex-m-int-num") + .alias("add_cortex_m_int_num") + .action(ArgAction::SetTrue) + .help("Add the generation for the cortex-m InterruptNumber trait impl") + .long_help("Add the generation for the cortex-m InterruptNumber trait impl. +The generated crate will have a dependency on cortex-m.") + ) .arg( Arg::new("base_address_shift") .short('b') diff --git a/svd2rust-regress/src/svd_test.rs b/svd2rust-regress/src/svd_test.rs index ca15cccc..ba214004 100644 --- a/svd2rust-regress/src/svd_test.rs +++ b/svd2rust-regress/src/svd_test.rs @@ -18,6 +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_LEGACY_INT_NUM: &[&str] = &["cortex-m = { version = \"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\""]; @@ -429,15 +430,16 @@ impl TestCase { Target::XtensaLX => [].iter(), Target::None => unreachable!(), }) - .chain(if let Some(opts) = opts { + .chain(opts.as_ref().map_or(Vec::new().into_iter(), |opts| { + let mut fragments = Vec::new(); if opts.iter().any(|v| v.contains("atomics")) { - CRATES_ATOMICS.iter() - } else { - [].iter() + fragments.extend(CRATES_ATOMICS); } - } else { - [].iter() - }) + if opts.iter().any(|v| v.contains("add-cortex-m-int-num")) { + fragments.extend(CRATES_CORTEX_M_LEGACY_INT_NUM); + } + fragments.into_iter() + })) .chain(PROFILE_ALL.iter()) .chain(FEATURES_ALL.iter()) .chain(match &self.arch {