device-driver 2.0.0

A toolkit to write better device drivers, faster
Documentation
use device_driver::{FieldsetMetadata, RegisterInterface, RegisterInterfaceBase};

device_driver::compile!(
    unstable_ddsl: "
        device MyTestDevice {
            default-byte-order: LE,
            register-address-type: u8,
            default-access: RW,

            register Foo {
                address: 0,
                reset: 0xFFFFFFFF,

                fields: fieldset FooFieldSet {
                    size-bytes: 4,

                    field value 31:0 -> uint
                }
            }
        }
    "
);

pub struct DeviceInterface;

impl RegisterInterfaceBase for DeviceInterface {
    type Error = ();
    type AddressType = u8;
}
impl RegisterInterface for DeviceInterface {
    fn write_register(
        &mut self,
        _address: Self::AddressType,
        _data: &mut [u8],
        _metadata: &FieldsetMetadata,
    ) -> Result<(), Self::Error> {
        unimplemented!()
    }

    fn read_register(
        &mut self,
        _address: Self::AddressType,
        _data: &mut [u8],
        _metadata: &FieldsetMetadata,
    ) -> Result<(), Self::Error> {
        unimplemented!()
    }
}

#[cfg(test)]
mod tests {
    use device_driver::Fieldset;

    use super::*;

    #[test]
    fn and() {
        let reset_foo = MyTestDevice::new(DeviceInterface).foo().reset_value();

        assert_eq!(reset_foo & FooFieldSet::ZERO, FooFieldSet::ZERO);

        let mut test_foo = FooFieldSet::ZERO;
        test_foo.set_value(0x12345678);

        assert_eq!(reset_foo & test_foo, test_foo);
        assert_eq!(test_foo & FooFieldSet::ZERO, FooFieldSet::ZERO);

        test_foo &= FooFieldSet::ZERO;

        assert_eq!(test_foo, FooFieldSet::ZERO);
    }

    #[test]
    fn or() {
        let reset_foo = MyTestDevice::new(DeviceInterface).foo().reset_value();

        assert_eq!(FooFieldSet::ZERO | reset_foo, reset_foo);

        let mut test_foo = FooFieldSet::ZERO;
        test_foo.set_value(0x12345678);

        assert_eq!(FooFieldSet::ZERO | test_foo, test_foo);
        assert_eq!(test_foo | reset_foo, reset_foo);

        test_foo |= reset_foo;

        assert_eq!(test_foo, reset_foo);
    }

    #[test]
    fn xor() {
        let reset_foo = MyTestDevice::new(DeviceInterface).foo().reset_value();

        assert_eq!(FooFieldSet::ZERO ^ reset_foo, reset_foo);
        assert_eq!(FooFieldSet::ZERO ^ reset_foo ^ reset_foo, FooFieldSet::ZERO);

        let mut test_foo = FooFieldSet::ZERO;
        test_foo.set_value(0x12345678);

        assert_eq!(FooFieldSet::ZERO ^ test_foo, test_foo);
        assert_eq!(test_foo ^ reset_foo, !test_foo);

        test_foo ^= test_foo;

        assert_eq!(test_foo, FooFieldSet::ZERO);
    }

    #[test]
    fn not() {
        let reset_foo = MyTestDevice::new(DeviceInterface).foo().reset_value();

        assert_eq!(!FooFieldSet::ZERO, reset_foo);
        assert_eq!(!reset_foo, FooFieldSet::ZERO);
    }
}