/
test-macro-driver.rs
158 lines (132 loc) · 4.17 KB
/
test-macro-driver.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use bitvec::array::BitArray;
use device_driver::{AsyncRegisterDevice, Register, RegisterDevice};
pub struct TestDevice {
device_memory: [u8; 128],
}
impl RegisterDevice for TestDevice {
type Error = ();
type AddressType = u8;
fn write_register<R, const SIZE_BYTES: usize>(
&mut self,
data: &BitArray<[u8; SIZE_BYTES]>,
) -> Result<(), Self::Error>
where
R: Register<SIZE_BYTES, AddressType = Self::AddressType>,
{
self.device_memory[R::ADDRESS as usize..][..SIZE_BYTES]
.copy_from_slice(data.as_raw_slice());
Ok(())
}
fn read_register<R, const SIZE_BYTES: usize>(
&mut self,
data: &mut BitArray<[u8; SIZE_BYTES]>,
) -> Result<(), Self::Error>
where
R: Register<SIZE_BYTES, AddressType = Self::AddressType>,
{
data.as_raw_mut_slice()
.copy_from_slice(&self.device_memory[R::ADDRESS as usize..][..SIZE_BYTES]);
Ok(())
}
}
impl AsyncRegisterDevice for TestDevice {
type Error = ();
type AddressType = u8;
async fn write_register<R, const SIZE_BYTES: usize>(
&mut self,
data: &BitArray<[u8; SIZE_BYTES]>,
) -> Result<(), Self::Error>
where
R: Register<SIZE_BYTES, AddressType = Self::AddressType>,
{
self.device_memory[R::ADDRESS as usize..][..SIZE_BYTES]
.copy_from_slice(data.as_raw_slice());
Ok(())
}
async fn read_register<R, const SIZE_BYTES: usize>(
&mut self,
data: &mut BitArray<[u8; SIZE_BYTES]>,
) -> Result<(), Self::Error>
where
R: Register<SIZE_BYTES, AddressType = Self::AddressType>,
{
data.as_raw_mut_slice()
.copy_from_slice(&self.device_memory[R::ADDRESS as usize..][..SIZE_BYTES]);
Ok(())
}
}
impl Default for TestDevice {
fn default() -> Self {
Self::new()
}
}
impl TestDevice {
pub fn new() -> Self {
// Normally we'd take like a SPI here or something
Self {
device_memory: [0; 128],
}
}
}
pub mod registers {
use super::*;
device_driver_macros::implement_registers!(
impl TestDevice {
register Id {
type RWType = ReadOnly;
const ADDRESS: u8 = 12;
const SIZE_BITS: usize = 24;
const RESET_VALUE: [u8] = [0, 0, 5];
manufacturer: u16 as Manufacturer = 0..16,
version: u8 = 16..20,
edition: u8 as enum Edition {
One = 1,
Two,
/// Test!
Five = 5,
Rest = "default",
} = 20..24,
},
/// Baudrate register
register Baudrate {
type RWType = RW;
const ADDRESS: u8 = 42;
const SIZE_BITS: usize = 16;
/// Baudrate value
value: u16 = 0..16,
},
register Foo {
type RWType = RW;
const ADDRESS: u8 = 0;
const SIZE_BITS: usize = 16;
const RESET_VALUE: u16 = 0x1234;
value: u16 = 0..16,
}
}
);
}
#[derive(Debug, num_enum::IntoPrimitive, num_enum::TryFromPrimitive)]
#[repr(u16)]
pub enum Manufacturer {
MegaCorpX,
Indy,
}
fn main() {
let mut test_device = TestDevice::new();
println!("{:?}", test_device.id().read().unwrap());
test_device.baudrate().write(|w| w.value(12)).unwrap();
write_baud(&mut test_device);
assert_eq!(test_device.baudrate().read().unwrap().value(), 12);
test_device.foo().clear().unwrap();
assert_eq!(test_device.foo().read().unwrap().value(), 0x1234);
test_device.foo().write(|w| w.value(5)).unwrap();
assert_eq!(test_device.foo().read().unwrap().value(), 5);
test_device.foo().write_with_zero(|w| w).unwrap();
assert_eq!(test_device.foo().read().unwrap().value(), 0);
test_device.foo().write(|w| w).unwrap();
assert_eq!(test_device.foo().read().unwrap().value(), 0x1234);
}
#[inline(never)]
fn write_baud(device: &mut TestDevice) {
device.baudrate().write(|w| w.value(12)).unwrap();
}