Skip to content

update NVIC to use derive-mmio - #655

Open
robamu wants to merge 2 commits into
mainfrom
rewrite-nvic-to-use-derive-mmio
Open

robamu wants to merge 2 commits into
mainfrom
rewrite-nvic-to-use-derive-mmio

Conversation

@robamu

@robamu robamu commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

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

This comment was marked as resolved.

@robamu
robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch from 12368e7 to 3539584 Compare April 27, 2026 16:13
@robamu

robamu commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor Author

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 2 times, most recently 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
@jonathanpallant

Copy link
Copy Markdown
Contributor

Marking as draft because I think these are breaking changes and we need to avoid those right now. Also it now has merge conflicts.

@jonathanpallant
jonathanpallant marked this pull request as draft July 21, 2026 12:52
@Sympatron

Copy link
Copy Markdown

I thought cortex-m was going to move to version 0.8 in the hopefully near future, since there are some outstanding breaking changes anyway. Or did I misunderstand that?

@jonathanpallant

Copy link
Copy Markdown
Contributor

Near future, for some value of near. But we don't have a stable branch for v0.7-only changes, so I'd like to keep non-0.7 things out of the main branch.

@Sympatron

Copy link
Copy Markdown

What about adding a v0.8 branch so that work on 0.8 can start? See #604 #651 #656.

@robamu
robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch 3 times, most recently from 303076e to 1b1ac81 Compare July 29, 2026 09:22
@robamu

robamu commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor Author

The NVIC field of the peripheral singleton is now a ZST type, using the new derive-mmio owned handles. I still kept the NVIC code to be a run-time created driver wrapping a regular MMIO handle. I think this solution is cleaner because it also allows providing Deref and DerefMut impls to the inner MMIO handle, reflecting that this is a low-level driver.

I also moved the NVIC declaration to the nvic module now. It is still re-exported inside the peripheral top module.

@robamu
robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch from 1b1ac81 to f627086 Compare July 29, 2026 09:25
@robamu
robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch 2 times, most recently from 89fbc8a to 7a1dbc8 Compare August 25, 2026 08:40
@robamu
robamu marked this pull request as ready for review August 25, 2026 08:40
@robamu
robamu force-pushed the rewrite-nvic-to-use-derive-mmio branch from 7a1dbc8 to 15f776a Compare November 12, 2026 09:30
pub struct RegisterBlock {
/// Interrupt Set-Enable
pub iser: [RW<u32>; NVIC::BITMAP_WORDS],
iser: [u32; NVIC::BITMAP_WORDS],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PureRead, Write - but not modify

(reading gives you the current enable state)


/// Interrupt Clear-Enable
pub icer: [RW<u32>; NVIC::BITMAP_WORDS],
icer: [u32; NVIC::BITMAP_WORDS],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think Write is fine here - you can read icer but it gives you the same value as iser. But certainly we should disable Modify.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah.. reading is very confusing. I agree with only adding Write


/// Interrupt Set-Pending
pub ispr: [RW<u32>; NVIC::BITMAP_WORDS],
ispr: [u32; NVIC::BITMAP_WORDS],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as iser.


/// Interrupt Clear-Pending
pub icpr: [RW<u32>; NVIC::BITMAP_WORDS],
icpr: [u32; NVIC::BITMAP_WORDS],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as icer

#[cfg(armv8m)]
/// Interrupt Target Non-secure (only present on Arm v8-M)
pub itns: [RW<u32>; NVIC::BITMAP_WORDS],
itns: [u32; NVIC::BITMAP_WORDS],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd expressly mark this as PureRead, Write, Modify to make it clear that it is a read-modify-write register, not a foo-set/foo-clear register pair.

Comment thread cortex-m/src/peripheral/nvic.rs Outdated
/// architecture.
#[cfg(not(armv6m))]
pub ipr: [RW<u8>; 496],
ipr: [u32; 124],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the reason for changing from 8-bit to 32-bit fields here? I think it makes sense to have this as an array of 8-bit values because it avoids a read-modify-write cycle if you only change one 8-bit value. As the (now deleted) comment notes, the hardware was designed for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Probably because i thought I have to use u32 here (can not mix word sizes in one reg block). I will implement it with u8.

Comment thread cortex-m/src/peripheral/nvic.rs Outdated
#[cfg(not(armv6m))]
{
let nr = interrupt.number();
let ipr_ptr = self.pointer_to_ipr_start() as *mut u8;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should be avoiding raw pointer operations here. Doesn't derive-mmio give array access functions we can use?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, if IPR uses [u8; 496] that is the better solution.

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.

5 participants