diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c6e14d..e2ddc7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Unreleased - Make defmt impls for fieldsets a bit more efficient by using type hints +- Fixed cfg problem in the `read_all_registers` function ### 1.0.0 (28-12-24) diff --git a/device-driver/tests/cfg-register.rs b/device-driver/tests/cfg-register.rs new file mode 100644 index 0000000..25c855c --- /dev/null +++ b/device-driver/tests/cfg-register.rs @@ -0,0 +1,86 @@ +use device_driver::RegisterInterface; + +pub struct DeviceInterface { + device_memory: [u8; 128], +} + +impl Default for DeviceInterface { + fn default() -> Self { + Self::new() + } +} + +impl DeviceInterface { + pub const fn new() -> Self { + Self { + device_memory: [0; 128], + } + } +} + +impl RegisterInterface for DeviceInterface { + type Error = (); + type AddressType = u8; + + fn write_register( + &mut self, + address: Self::AddressType, + _size_bits: u32, + data: &[u8], + ) -> Result<(), Self::Error> { + self.device_memory[address as usize..][..data.len()].copy_from_slice(data); + + Ok(()) + } + + fn read_register( + &mut self, + address: Self::AddressType, + _size_bits: u32, + data: &mut [u8], + ) -> Result<(), Self::Error> { + data.copy_from_slice(&self.device_memory[address as usize..][..data.len()]); + Ok(()) + } +} + +device_driver::create_device!( + device_name: MyTestDevice, + dsl: { + config { + type RegisterAddressType = u8; + } + #[cfg(not(windows))] + register Foo1 { + const ADDRESS = 0; + const SIZE_BITS = 8; + const RESET_VALUE = 20; + + value: bool = 0, + generated: uint as try enum Gen { + A = 0, + } = 1..2, + }, + #[cfg(windows)] + register Foo2 { + const ADDRESS = 1; + const SIZE_BITS = 8; + const RESET_VALUE = 10; + + value: bool = 0, + generated: uint as try enum Gen { + A = 1, + } = 1..2, + }, + } +); + +#[test] +fn test_basic_read_modify_write() { + let mut device = MyTestDevice::new(DeviceInterface::new()); + device + .read_all_registers(|addr, name, val| { + println!("@{addr}: {name} = {val:?}"); + }) + .unwrap(); +} diff --git a/generation/src/lir/token_transform/block_transform.rs b/generation/src/lir/token_transform/block_transform.rs index d56d273..b0f3b1d 100644 --- a/generation/src/lir/token_transform/block_transform.rs +++ b/generation/src/lir/token_transform/block_transform.rs @@ -58,6 +58,7 @@ pub fn generate_block( } => { let register_name = &m.name; let address = &m.address; + let cfg_attr = &m.cfg_attr; let read_function = match use_async { true => quote! { read_async().await }, @@ -81,7 +82,9 @@ pub fn generate_block( }; let index = Literal::i64_unsuffixed(index); quote! { + #cfg_attr let reg = self.#register_name(#index_param).#read_function?; + #cfg_attr callback(#address + #index * #stride, #register_display_name, reg.into()); } })) @@ -328,8 +331,9 @@ mod tests { where I: ::device_driver::RegisterInterface, { + #[cfg(unix)] let reg = self.my_register1().read()?; - callback(5 + 0 * 0, \"my_register1\", reg.into()); + #[cfg(unix)] callback(5 + 0 * 0, \"my_register1\", reg.into()); Ok(()) } /// Read all readable register values in this block from the device. @@ -352,8 +356,9 @@ mod tests { where I: ::device_driver::AsyncRegisterInterface, { + #[cfg(unix)] let reg = self.my_register1().read_async().await?; - callback(5 + 0 * 0, \"my_register1\", reg.into()); + #[cfg(unix)] callback(5 + 0 * 0, \"my_register1\", reg.into()); Ok(()) } ///42 is the answer