Skip to content

Commit

Permalink
Merge pull request #55 from diondokter/cfg-read-all-fix
Browse files Browse the repository at this point in the history
Fix cfg problem in `read_all_registers`
  • Loading branch information
diondokter authored Jan 2, 2025
2 parents 7b722fd + 7c09ae2 commit e1b3773
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
86 changes: 86 additions & 0 deletions device-driver/tests/cfg-register.rs
Original file line number Diff line number Diff line change
@@ -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();
}
9 changes: 7 additions & 2 deletions generation/src/lir/token_transform/block_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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());
}
}))
Expand Down Expand Up @@ -328,8 +331,9 @@ mod tests {
where
I: ::device_driver::RegisterInterface<AddressType = u8>,
{
#[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.
Expand All @@ -352,8 +356,9 @@ mod tests {
where
I: ::device_driver::AsyncRegisterInterface<AddressType = u8>,
{
#[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
Expand Down

0 comments on commit e1b3773

Please sign in to comment.