rust-embedded / rust-embedded/cortex-m

Missing `pub` in register bitfields

Open
#478 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Rust
Stars
1k
Forks
202
Avg merge
6d 2h
Merged PRs (30d)
2

Description

In general, the definitions for a lot of the registers defined in SCB, SAU, etc., are rather deficient, but my current complaint is the Fault Status registers.

The ones defined in SCB are just defined as RW<u32>, so if you want to check individual bits, you have to define your own bitfield (or do the bit twiddling manually). Disappointing, but usable.

In SAU, the situation seems a little better: there are bitfield macros for Sfsr and Sfar! That sounds great, until you try to use them:

#[exception]
unsafe fn SecureFault() {
    let peripherals = cortex_m::Peripherals::steal();
    let sfsr = peripherals.SAU.sfsr.read();
    if sfsr.invep() {
        log::error!("INVEP");
    }
}

which gets you

error[E0624]: method `invep` is private
   --> <snip>/src/main.rs:122:13
    |
122 |       if sfsr.invep() {
    |               ^^^^^ private method
    |
   ::: <snip>/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-0.7.7/src/peripheral/sau.rs:81:1
    |
81  | / bitfield! {
82  | |     /// Secure Fault Status Register description
83  | |     #[repr(C)]
84  | |     #[derive(Copy, Clone)]
...   |
93  | |     lserr, _: 7;
94  | | }
    | |_- private method defined here

so you end up having to do the same thing (define your own bitfield or do the bit twiddling manually), only now you have to pull the value out of the existing bitfield with sfsr.0.

The sau crate doesn't do anything with this register, yet this bitfield definition makes it useless outside of the crate.

Fix

The register definitions could use a lot of love 🙁, but the obvious fix here is to make the fields in the bitfield pub, e.g.:

bitfield! {
    /// Secure Fault Status Register description
    #[repr(C)]
    #[derive(Copy, Clone)]
    pub struct Sfsr(u32);
    pub invep, _: 0;
    pub invis, _: 1;
    pub inver, _: 2;
    pub auviol, _: 3;
    pub invtran, _: 4;
    pub lsperr, _: 5;
    pub sfarvalid, _: 6;
    pub lserr, _: 7;
}

// Ditto for all other `bitfield`s

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the SAU Sfsr and Sfar bitfield declarations referenced in the issue, then inspect the other register bitfields in SCB, SAU, and related peripherals for the same visibility pattern. Verify that callers outside the crate can access the named status-bit methods, and confirm the register definitions remain usable through the existing read API.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.