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

(read|modify|write)_register should take/return typed structures rather than u8 #10

Open
dsvensson opened this issue May 28, 2018 · 4 comments

Comments

@dsvensson
Copy link
Owner

The following code:

        self.modify_register(config::Register::MDMCFG2, |r| {
            MDMCFG2(r).modify().sync_mode(mode.value()).bits()
        })?;

... should ideally not mention MDMCFG2 in the body, nor modify():

        self.modify_register(config::Register::MDMCFG2, |r| {
            r.sync_mode(mode.value()).bits()
        })?;

And similarily, calling self.read_register(config::Register.:MDMCFG2) should return a MDMCFG2 rather than an u8.

Can probably be fixed by introducing some good traits. Figure out how.

@dsvensson
Copy link
Owner Author

This probably requires some broader macro that replaces the per-register-type enum with macro expansion as there's not really a good link between the register value wrappers and the register address enums.

@dsvensson
Copy link
Owner Author

dsvensson commented Dec 30, 2024

Finally took a stab at this.

self.modify_register(Config::MCSM0, |r| {
    MCSM0(r).modify().fs_autocal(autocal.into()).bits() // untyped in/out u8
})?;

becomes:

self.modify_register(config::MCSM0, |r /* inferred as &mut MCSM0<W> which exposes writable api */ | {
    r.fs_autocal(autocal.into()) // fully type safe builder in/out
})?;

Register categories declared as:

registers!(Config, {
    Read => Any, /* category specific access behavior of registers, Any|Burst|Single|Reject */
    Write => Any,
}, {
    #[doc = "Main Radio Cntrl State Machine config"]
    MCSM0(addr: 0x18, reset: 0b0000_0100) {
        #[doc = "Automatically calibrate when going to RX or TX, or back to IDLE"]
        fs_autocal @ 4..6, /* bit fields */
        ....
    },
    MCSM1(...) { .. }
    ....
}

wdyt? @Andrei-Basarab @SebKuzminsky

Going to be a bit of an OCD grind to transform the declarations, but think it looks pretty neat with register address and doc near the field declarations, and away with the noise around the access functions.

@SebKuzminsky
Copy link
Contributor

SebKuzminsky commented Dec 30, 2024

// fully type safe builder

I like the sound of that :-)

Is your prototype code available for inspection anywhere?

I volunteer to help with the OCD grind part of this update if you want.

Oh and while we're talking about the register macros, it would be super handy if register! implemented Debug...

How did you make the decision to implement the register bitfields yourself, instead of using an existing no_std bitfield crate like bilge or any of the others?

@Andrei-Basarab
Copy link
Contributor

@dsvensson If you can provide a working example of the change you're thinking of implementing, I can look into that and help with the change.
As I understand, this change is only "under the hood" and will not change the driver's interface, right?

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

No branches or pull requests

3 participants