-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cfg problem in
read_all_registers
- Loading branch information
1 parent
7b722fd
commit 7c09ae2
Showing
3 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters