Skip to content

update NVIC to use derive-mmio#655

Open
robamu wants to merge 1 commit intomasterfrom
rewrite-nvic-to-use-derive-mmio
Open

update NVIC to use derive-mmio#655
robamu wants to merge 1 commit intomasterfrom
rewrite-nvic-to-use-derive-mmio

Conversation

@robamu
Copy link
Copy Markdown

@robamu robamu commented Apr 27, 2026

The primary advantage is that derive-mmio does not create shared references to MMIO like VolatileCell / volatile_register does.

@robamu robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch 2 times, most recently from 0d40861 to 12368e7 Compare April 27, 2026 14:01
@robamu robamu requested review from adamgreig, Copilot and thejpster and removed request for Copilot April 27, 2026 14:35
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR rewrites the NVIC peripheral implementation to use derive-mmio so MMIO access doesn’t rely on creating shared references (as with VolatileCell / volatile-register).

Changes:

  • Introduces derive-mmio and converts the NVIC RegisterBlock to a #[derive(derive_mmio::Mmio)] MMIO model.
  • Refactors crate::peripheral::NVIC into an owning wrapper around nvic::MmioRegisterBlock<'static> and updates NVIC methods accordingly.
  • Updates peripheral address layout tests to use derive-mmio pointer helpers instead of taking &*NVIC::PTR.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
cortex-m/src/peripheral/nvic.rs Replaces volatile-register register block with derive-mmio and rewrites NVIC operations to use generated MMIO accessors / raw volatile byte access for IPR on non-armv6m.
cortex-m/src/peripheral/mod.rs Changes NVIC from a ZST marker with PTR to a wrapper holding MmioRegisterBlock<'static>, and updates Peripherals::steal() construction.
cortex-m/src/peripheral/test.rs Updates NVIC address assertions to use derive-mmio pointer accessors and NVIC::steal().
cortex-m/Cargo.toml Adds derive-mmio = "0.6" dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 541 to 551
impl NVIC {
/// Pointer to the register block
pub const PTR: *const nvic::RegisterBlock = 0xE000_E100 as *const _;

/// Returns a pointer to the register block
#[inline(always)]
#[deprecated(since = "0.7.5", note = "Use the associated constant `PTR` instead")]
pub const fn ptr() -> *const nvic::RegisterBlock {
Self::PTR
/// Unsafely steal an instance of the NVIC.
///
/// # Safety
///
/// This potentially allows to create multiple instances of the NVIC register block, which
/// might only be valid in certain multi-core environments.
#[inline]
pub unsafe fn steal() -> Self {
NVIC(unsafe { nvic::RegisterBlock::new_mmio_fixed() })
}
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change removes the long-standing NVIC::PTR / NVIC::ptr() raw-pointer API and changes NVIC from a ZST marker to an owning wrapper. That’s a breaking public API change (and is inconsistent with the other peripherals in this module that still expose PTR). Consider keeping PTR (as a raw pointer/usize base address) and the deprecated ptr() for compatibility, even if you steer users to steal()/derive-mmio accessors.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a breaking change anyway. Not sure how useful it would be to keep the old PTR API for the new type.

Comment thread cortex-m/src/peripheral/mod.rs
Comment thread cortex-m/src/peripheral/nvic.rs
// SAFETY:
// - atomic stateless write; IPR doesn't store any state
// - InterruptNumber is an unsafe trait, we can assume correct implementation.
unsafe { core::ptr::write_volatile(ipr_ptr.offset(nr as isize), prio) }
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVIC::set_priority (non-armv6m path) uses ptr.offset(...) on an MMIO pointer. As with get_priority, this can be UB under strict provenance because offset requires staying within a single allocated object. Prefer wrapping_add/wrapping_offset (or address arithmetic from BASE_ADDRESS) before calling write_volatile.

Suggested change
unsafe { core::ptr::write_volatile(ipr_ptr.offset(nr as isize), prio) }
unsafe { core::ptr::write_volatile(ipr_ptr.wrapping_add(usize::from(nr)), prio) }

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

@robamu robamu Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be fine I think? The pointer is related to the IPR allocation and we assume the interrupt number is valid.

Comment thread cortex-m/src/peripheral/nvic.rs
Comment thread cortex-m/src/peripheral/nvic.rs Outdated
@robamu robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch from 12368e7 to 3539584 Compare April 27, 2026 16:13
@robamu
Copy link
Copy Markdown
Author

robamu commented Apr 27, 2026

I tested this PR with #655 with https://github.com/ferrous-systems/rust-training/tree/main/example-code/qemu-thumbv7em. Async and interrupt using examples were running fine. I'll probably add some tests to verify priority setting/reading as well because pointer magic was used there..

@robamu robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch from 3539584 to 2570853 Compare April 27, 2026 22:55
@robamu robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch from b797433 to 624d62f Compare April 28, 2026 09:27
@robamu robamu marked this pull request as ready for review April 28, 2026 09:28
@robamu robamu changed the title rewrite NVIC to use derive-mmio update NVIC to use derive-mmio Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants