Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing pub in register bitfields #478

Open
BigPeteB opened this issue Jun 2, 2023 · 1 comment
Open

Missing pub in register bitfields #478

BigPeteB opened this issue Jun 2, 2023 · 1 comment

Comments

@BigPeteB
Copy link

BigPeteB commented Jun 2, 2023

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
@jonathanpallant
Copy link
Contributor

Or to write an SVD file and use svd2rust...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants