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
//! This module contains the TIA related functions.
use embedded_hal::i2c::I2c;
use embedded_hal::i2c::SevenBitAddress;
use crate::{
device::AFE4404,
errors::AfeError,
modes::{ThreeLedsMode, TwoLedsMode},
};
pub use configuration::{CapacitorConfiguration, ResistorConfiguration};
mod configuration;
pub mod low_level;
pub mod values;
impl<I2C> AFE4404<I2C, ThreeLedsMode>
where
I2C: I2c<SevenBitAddress>,
{
/// Sets the tia resistors value.
///
/// # Notes
///
/// This function automatically rounds the resistors value to the closest actual value.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// Setting a resistor value outside the range 10-2000 kOhm will result in an error.
pub fn set_tia_resistors(
&mut self,
configuration: &ResistorConfiguration<ThreeLedsMode>,
) -> Result<ResistorConfiguration<ThreeLedsMode>, AfeError<I2C::Error>> {
let r20h_prev = self.registers.r20h.read()?;
let r21h_prev = self.registers.r21h.read()?;
let values = [
Self::from_resistor(*configuration.resistor1())?,
Self::from_resistor(*configuration.resistor2())?,
];
let separate_resistor: bool =
(values[0] != values[1]) || (r21h_prev.tia_cf() != r20h_prev.tia_cf_sep());
self.registers.r20h.write(
r20h_prev
.with_ensepgain(separate_resistor)
.with_tia_gain_sep(values[1].1),
)?;
self.registers
.r21h
.write(r21h_prev.with_tia_gain(values[0].1))?;
Ok(ResistorConfiguration::<ThreeLedsMode>::new(
values[0].0,
values[1].0,
))
}
/// Gets the tia resistors value.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error or if the [`AFE4404`] contains invalid data.
pub fn get_tia_resistors(
&mut self,
) -> Result<ResistorConfiguration<ThreeLedsMode>, AfeError<I2C::Error>> {
Ok(ResistorConfiguration::<ThreeLedsMode>::new(
self.get_tia_resistor1()?,
self.get_tia_resistor2()?,
))
}
/// Sets the tia capacitors value.
///
/// # Notes
///
/// This function automatically rounds the capacitors value to the closest actual value.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// Setting a capacitor value outside the range 2.5-25 pF will result in an error.
pub fn set_tia_capacitors(
&mut self,
configuration: &CapacitorConfiguration<ThreeLedsMode>,
) -> Result<CapacitorConfiguration<ThreeLedsMode>, AfeError<I2C::Error>> {
let r20h_prev = self.registers.r20h.read()?;
let r21h_prev = self.registers.r21h.read()?;
let values = [
Self::from_capacitor(*configuration.capacitor1())?,
Self::from_capacitor(*configuration.capacitor2())?,
];
let separate_capacitor: bool =
(values[0] != values[1]) || (r21h_prev.tia_gain() != r20h_prev.tia_gain_sep());
self.registers.r20h.write(
r20h_prev
.with_ensepgain(separate_capacitor)
.with_tia_cf_sep(values[1].1),
)?;
self.registers
.r21h
.write(r21h_prev.with_tia_cf(values[0].1))?;
Ok(CapacitorConfiguration::<ThreeLedsMode>::new(
values[0].0,
values[1].0,
))
}
/// Gets the tia capacitors value.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error or if the [`AFE4404`] contains invalid data.
pub fn get_tia_capacitors(
&mut self,
) -> Result<CapacitorConfiguration<ThreeLedsMode>, AfeError<I2C::Error>> {
Ok(CapacitorConfiguration::<ThreeLedsMode>::new(
self.get_tia_capacitor1()?,
self.get_tia_capacitor2()?,
))
}
}
impl<I2C> AFE4404<I2C, TwoLedsMode>
where
I2C: I2c<SevenBitAddress>,
{
/// Sets the tia resistors value.
///
/// # Notes
///
/// This function automatically rounds the resistors value to the closest actual value.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// Setting a resistor value outside the range 10-2000 kOhm will result in an error.
pub fn set_tia_resistors(
&mut self,
configuration: &ResistorConfiguration<TwoLedsMode>,
) -> Result<ResistorConfiguration<TwoLedsMode>, AfeError<I2C::Error>> {
let r20h_prev = self.registers.r20h.read()?;
let r21h_prev = self.registers.r21h.read()?;
let values = [
Self::from_resistor(*configuration.resistor1())?,
Self::from_resistor(*configuration.resistor2())?,
];
let separate_resistor: bool =
(values[0] != values[1]) || (r21h_prev.tia_cf() != r20h_prev.tia_cf_sep());
self.registers.r20h.write(
r20h_prev
.with_ensepgain(separate_resistor)
.with_tia_gain_sep(values[1].1),
)?;
self.registers
.r21h
.write(r21h_prev.with_tia_gain(values[0].1))?;
Ok(ResistorConfiguration::<TwoLedsMode>::new(
values[0].0,
values[1].0,
))
}
/// Gets the tia resistors value.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error or if the [`AFE4404`] contains invalid data.
pub fn get_tia_resistors(
&mut self,
) -> Result<ResistorConfiguration<TwoLedsMode>, AfeError<I2C::Error>> {
Ok(ResistorConfiguration::<TwoLedsMode>::new(
self.get_tia_resistor1()?,
self.get_tia_resistor2()?,
))
}
/// Sets the tia capacitors value.
///
/// # Notes
///
/// This function automatically rounds the capacitors value to the closest actual value.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error.
/// Setting a capacitor value outside the range 2.5-25 pF will result in an error.
pub fn set_tia_capacitors(
&mut self,
configuration: &CapacitorConfiguration<TwoLedsMode>,
) -> Result<CapacitorConfiguration<TwoLedsMode>, AfeError<I2C::Error>> {
let r20h_prev = self.registers.r20h.read()?;
let r21h_prev = self.registers.r21h.read()?;
let values = [
Self::from_capacitor(*configuration.capacitor1())?,
Self::from_capacitor(*configuration.capacitor2())?,
];
let separate_capacitor: bool =
(values[0] != values[1]) || (r21h_prev.tia_gain() != r20h_prev.tia_gain_sep());
self.registers.r20h.write(
r20h_prev
.with_ensepgain(separate_capacitor)
.with_tia_cf_sep(values[1].1),
)?;
self.registers
.r21h
.write(r21h_prev.with_tia_cf(values[0].1))?;
Ok(CapacitorConfiguration::<TwoLedsMode>::new(
values[0].0,
values[1].0,
))
}
/// Gets the tia capacitors value.
///
/// # Errors
///
/// This function returns an error if the I2C bus encounters an error or if the [`AFE4404`] contains invalid data.
pub fn get_tia_capacitors(
&mut self,
) -> Result<CapacitorConfiguration<TwoLedsMode>, AfeError<I2C::Error>> {
Ok(CapacitorConfiguration::<TwoLedsMode>::new(
self.get_tia_capacitor1()?,
self.get_tia_capacitor2()?,
))
}
}