-
Notifications
You must be signed in to change notification settings - Fork 4
/
test-json-driver.rs
190 lines (151 loc) · 4.82 KB
/
test-json-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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::ops::Range;
use bitvec::array::BitArray;
use device_driver::{
AsyncBufferDevice, AsyncCommandDevice, AsyncRegisterDevice, BufferDevice, CommandDevice,
Register, RegisterDevice,
};
pub struct TestDevice {
device_memory: [u8; 128],
last_command: u32,
last_buffer: u32,
}
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 CommandDevice for TestDevice {
type Error = ();
fn dispatch_command(&mut self, id: u32) -> Result<(), Self::Error> {
self.last_command = id;
Ok(())
}
}
impl AsyncCommandDevice for TestDevice {
type Error = ();
async fn dispatch_command(&mut self, id: u32) -> Result<(), Self::Error> {
self.last_command = id;
Ok(())
}
}
const BUFFER_RANGE: Range<usize> = 64..128;
impl BufferDevice for TestDevice {
fn write(&mut self, id: u32, buf: &[u8]) -> Result<usize, embedded_io::ErrorKind> {
self.last_buffer = id;
if buf.len() > BUFFER_RANGE.len() {
return Err(embedded_io::ErrorKind::InvalidData);
}
self.device_memory[BUFFER_RANGE][..buf.len()].copy_from_slice(buf);
Ok(buf.len())
}
fn read(&mut self, id: u32, buf: &mut [u8]) -> Result<usize, embedded_io::ErrorKind> {
self.last_buffer = id;
let max = buf.len().min(BUFFER_RANGE.len());
buf[..max].copy_from_slice(&self.device_memory[BUFFER_RANGE][..max]);
Ok(max)
}
}
impl AsyncBufferDevice for TestDevice {
async fn write(&mut self, id: u32, buf: &[u8]) -> Result<usize, embedded_io::ErrorKind> {
self.last_buffer = id;
if buf.len() > BUFFER_RANGE.len() {
return Err(embedded_io::ErrorKind::InvalidData);
}
self.device_memory[BUFFER_RANGE][..buf.len()].copy_from_slice(buf);
Ok(buf.len())
}
async fn read(&mut self, id: u32, buf: &mut [u8]) -> Result<usize, embedded_io::ErrorKind> {
self.last_buffer = id;
let max = buf.len().min(BUFFER_RANGE.len());
buf[..max].copy_from_slice(&self.device_memory[BUFFER_RANGE][..max]);
Ok(max)
}
}
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],
last_command: u32::MAX,
last_buffer: u32::MAX,
}
}
}
pub mod registers {
use super::*;
#[device_driver_macros::implement_device_from_file(json = "test-files/json_syntax.json")]
impl TestDevice {}
}
#[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.sleep().dispatch().unwrap();
assert_eq!(test_device.last_command, 0);
test_device.burn().dispatch().unwrap();
assert_eq!(test_device.last_command, 0xDEAD);
}
#[inline(never)]
fn write_baud(device: &mut TestDevice) {
device.baudrate().write(|w| w.value(12)).unwrap();
}