diondokter / device-driver

A toolkit to create better Rust device drivers, faster
https://diondokter.github.io/device-driver/
Apache License 2.0
96 stars 4 forks source link

Added #[generate(Debug)] feature #10

Closed diondokter closed 3 years ago

diondokter commented 3 years ago

Closes #8

You can now do this:

implement_registers!(
    MyDevice.registers<u8> = {
        // (comments must go first)
        #[generate(Debug)]              // <- new!
        id(RO, 0, 3) = MSB {
            manufacturer: u16:LE as Manufacturer = RW 0..16,
            version: u8:NE = RO 16..20,
            edition: u8:BE = RO 20..24,
        },
    }
);

Then we can:

let id = device.registers().id().read()?;
println!("{:?}", id);
id::R { raw: 0x010065, manufacturer: Ok(CarmineCrystal), version: 6, edition: 5 }

The generate attribute is optional. If you don't add it to the register, the output is now better too:

id::R { raw: 0x010065 }

When opting in, all field types must impl Debug.

This attribute syntax can be used later for other things as well.

diondokter commented 3 years ago

I'm now just noticing it... The field values that are just a number are in decimal. All other numbers are Hex. Will this be a problem? It's only the raw bit values that are hex and they're all prepended with 0x, so I think that's fine. In the end you're supposed to leave numbers as numbers. Anywhere where the bits are important (so you'd want to see the hex fmt) should be encoded in enums anyways.

diondokter commented 3 years ago

Good you spotted that!

korken89 commented 3 years ago

Now it works when I test it!