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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![deny(warnings)]
#![no_std]
use embedded_hal::digital::v2::OutputPin;
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Channel {
A = 0,
B = 1,
C = 2,
D = 3,
E = 4,
F = 5,
G = 6,
H = 7,
NOMSG = 8,
BROADCAST = 9,
}
impl Channel {
pub fn from_index(index: u8) -> Channel {
match index {
0 => Channel::A,
1 => Channel::B,
2 => Channel::C,
3 => Channel::D,
4 => Channel::E,
5 => Channel::F,
6 => Channel::G,
7 => Channel::H,
_ => panic!("Unsupported index for dac8568 channel select"),
}
}
}
pub enum ControlType {
WriteToInputRegister = 0,
UpdateRegister = 1,
WriteToChannelAndUpdateAllRegisters = 2,
WriteToChannelAndUpdateSingleRegister = 3,
PowerDownComm = 4,
WriteToClearCodeRegister = 5,
WriteToLDACRegister = 6,
SoftwareReset = 7,
}
#[derive(Copy, Clone)]
pub struct Message {
prefix: u8, control: u8, address: u8, data: u16, feature: u8, }
impl Message {
pub fn get_internal_reference_message(internal: bool) -> Message {
Message {
prefix: 0x00,
control: 0x08,
address: 0x00,
data: 0x0000,
feature: if internal { 0x01 } else { 0x00 },
}
}
pub fn get_voltage_message(channel: Channel, value: u16, is_inverted: bool) -> Message {
let output = if is_inverted { u16::MAX - value } else { value };
Message {
prefix: 0,
control: ControlType::WriteToChannelAndUpdateSingleRegister as u8,
address: channel as u8,
data: output,
feature: 0,
}
}
pub fn get_software_reset_message() -> Message {
Message {
prefix: 0b0001,
control: 0b1100,
address: 0x00,
data: 0x00,
feature: 0x00,
}
}
pub fn get_payload_word(&self) -> u32 {
let mut payload: u32 = 0x00;
payload |= (self.prefix as u32) << 28;
payload |= (self.control as u32) << 24;
payload |= (self.address as u32) << 20;
payload |= (self.data as u32) << 4;
payload |= self.feature as u32;
payload
}
pub fn get_payload_bytes(&self) -> [u8; 4] {
self.get_payload_word().to_be_bytes()
}
}
pub struct Dac<SPI, SYNC> {
spi: SPI,
sync: SYNC,
is_inverted: bool,
}
#[derive(Copy, Clone, Debug)]
#[non_exhaustive]
pub enum DacError {
BusWriteError,
}
impl<SPI, SYNC> Dac<SPI, SYNC>
where
SPI: embedded_hal::blocking::spi::Write<u8>,
SYNC: OutputPin,
{
pub fn new(spi: SPI, sync: SYNC) -> Self {
Self {
spi,
sync,
is_inverted: false,
}
}
pub fn release(self) -> (SPI, SYNC) {
(self.spi, self.sync)
}
pub fn set_inverted_output(&mut self, state: bool) {
self.is_inverted = state;
}
pub fn set_voltage(&mut self, channel: Channel, voltage: u16) -> Result<(), DacError> {
let message = Message::get_voltage_message(channel, voltage, self.is_inverted);
self.write(message)
}
pub fn use_internal_reference(&mut self) -> Result<(), DacError> {
let message = Message::get_internal_reference_message(true);
self.write(message)
}
pub fn use_external_reference(&mut self) -> Result<(), DacError> {
let message = Message::get_internal_reference_message(false);
self.write(message)
}
pub fn reset(&mut self) -> Result<(), DacError> {
let message = Message::get_software_reset_message();
self.write(message)
}
fn write(&mut self, message: Message) -> Result<(), DacError> {
let command: [u8; 4] = message.get_payload_bytes();
self.sync.set_low().unwrap_or_default();
let result = self.spi.write(&command);
self.sync.set_high().unwrap_or_default();
match result {
Ok(v) => Ok(v),
Err(_e) => Err(DacError::BusWriteError),
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn inverts_signal() {
let message = super::Message::get_voltage_message(super::Channel::A, 0, false);
assert_eq!(message.data, 0);
let message = super::Message::get_voltage_message(super::Channel::A, 0, true);
assert_eq!(message.data, u16::MAX);
}
}