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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#![no_std]
use embedded_hal as hal;
use hal::blocking::delay::DelayMs;
use hal::digital::v2::OutputPin;
#[cfg(feature = "rttdebug")]
use panic_rtt_core::rprintln;
mod interface;
pub use interface::{I2cInterface, SensorInterface, SpiInterface};
#[derive(Debug)]
pub enum Error<CommE, PinE> {
Comm(CommE),
Pin(PinE),
UnknownChipId,
Unresponsive,
}
pub struct Builder {}
impl Builder {
pub fn new_i2c<I2C, CommE>(&self, i2c: I2C, address: u8) -> ICM20689<I2cInterface<I2C>>
where
I2C: hal::blocking::i2c::Write<Error = CommE>
+ hal::blocking::i2c::Read<Error = CommE>
+ hal::blocking::i2c::WriteRead<Error = CommE>,
CommE: core::fmt::Debug,
{
let iface = interface::I2cInterface::new(i2c, address);
ICM20689::new_with_interface(iface)
}
pub fn new_spi<SPI, CSN, CommE, PinE>(spi: SPI, csn: CSN) -> ICM20689<SpiInterface<SPI, CSN>>
where
SPI: hal::blocking::spi::Transfer<u8, Error = CommE>
+ hal::blocking::spi::Write<u8, Error = CommE>,
CSN: OutputPin<Error = PinE>,
CommE: core::fmt::Debug,
PinE: core::fmt::Debug,
{
let iface = interface::SpiInterface::new(spi, csn);
ICM20689::new_with_interface(iface)
}
}
pub struct ICM20689<SI> {
pub(crate) si: SI,
pub(crate) gyro_scale: f32,
pub(crate) accel_scale: f32,
}
impl<SI, CommE, PinE> ICM20689<SI>
where
SI: SensorInterface<InterfaceError = Error<CommE, PinE>>,
{
pub(crate) fn new_with_interface(sensor_interface: SI) -> Self {
Self {
si: sensor_interface,
gyro_scale: 0.0,
accel_scale: 0.0
}
}
pub fn check_identity(
&mut self,
delay_source: &mut impl DelayMs<u8>,
) -> Result<bool, SI::InterfaceError> {
for _ in 0..5 {
let chip_id = self.si.register_read(REG_WHO_AM_I)?;
match chip_id {
ICM20602_WAI | ICM20608_WAI | ICM20689_WAI => {
#[cfg(feature = "rttdebug")]
rprintln!("found device: 0x{:0x} ", chip_id);
return Ok(true);
}
_ => {
#[cfg(feature = "rttdebug")]
rprintln!("bogus whoami: 0x{:0x} ", chip_id);
}
}
delay_source.delay_ms(10);
}
Ok(false)
}
pub fn soft_reset(
&mut self,
delay_source: &mut impl DelayMs<u8>,
) -> Result<(), SI::InterfaceError> {
const I2C_IF_DIS: u8 = 1 << 4;
const PWR_DEVICE_RESET: u8 = 1 << 7;
const CLKSEL_AUTO: u8 = 0x01;
const SENSOR_ENABLE_ALL: u8 = 0x00;
self.si.register_write(REG_PWR_MGMT_1, PWR_DEVICE_RESET)?;
delay_source.delay_ms(110);
let mut reset_success = false;
for _ in 0..10 {
if let Ok(reg_val) = self.si.register_read(REG_PWR_MGMT_1) {
if reg_val & PWR_DEVICE_RESET == 0 {
reset_success = true;
break;
}
}
delay_source.delay_ms(10);
}
if !reset_success {
#[cfg(feature = "rttdebug")]
rprintln!("couldn't read REG_PWR_MGMT_1");
return Err(Error::Unresponsive);
}
if self.si.using_spi() {
self.si.register_write(REG_USER_CTRL, I2C_IF_DIS)?;
}
self.si.register_write(REG_PWR_MGMT_1, CLKSEL_AUTO)?;
self.si.register_write(REG_PWR_MGMT_2, SENSOR_ENABLE_ALL)?;
delay_source.delay_ms(200);
Ok(())
}
pub fn setup(&mut self, delay_source: &mut impl DelayMs<u8>) -> Result<(), SI::InterfaceError> {
const FIFO_RST: u8 = 1 << 2;
const DMP_RST: u8 = 1 << 3;
self.soft_reset(delay_source)?;
let supported = self.check_identity(delay_source)?;
if !supported {
return Err(Error::UnknownChipId);
}
self.si.register_write(REG_INT_ENABLE, 0x00)?;
self.si.register_write(REG_FIFO_EN, 0x7C)?;
let ctrl_flags = FIFO_RST | DMP_RST;
self.si.register_write(REG_USER_CTRL, ctrl_flags)?;
self.set_accel_range(AccelRange::default())?;
self.set_gyro_range(GyroRange::default())?;
Ok(())
}
pub fn set_accel_range(&mut self, range: AccelRange) -> Result<(), SI::InterfaceError> {
self.accel_scale = range.scale();
self.si.register_write(REG_ACCEL_CONFIG, (range as u8) << 3)
}
pub fn set_gyro_range(&mut self, range: GyroRange) -> Result<(), SI::InterfaceError> {
self.gyro_scale = range.scale();
self.si.register_write(REG_GYRO_CONFIG, (range as u8) << 2)
}
pub fn get_raw_accel(&mut self) -> Result<[i16; 3], SI::InterfaceError> {
self.si.read_vec3_i16(REG_ACCEL_START)
}
pub fn get_raw_gyro(&mut self) -> Result<[i16; 3], SI::InterfaceError> {
self.si.read_vec3_i16(REG_GYRO_START)
}
pub fn get_scaled_accel(&mut self) -> Result<[f32; 3], SI::InterfaceError> {
let raw_accel = self.get_raw_accel()?;
Ok([
self.accel_scale * (raw_accel[0] as f32),
self.accel_scale * (raw_accel[1] as f32),
self.accel_scale * (raw_accel[2] as f32),
])
}
pub fn get_scaled_gyro(&mut self) -> Result<[f32; 3], SI::InterfaceError> {
let raw_gyro = self.get_raw_gyro()?;
Ok([
self.gyro_scale * (raw_gyro[0] as f32),
self.gyro_scale * (raw_gyro[1] as f32),
self.gyro_scale * (raw_gyro[2] as f32),
])
}
}
const REG_USER_CTRL: u8 = 0x6A;
const REG_PWR_MGMT_1: u8 = 0x6B;
const REG_PWR_MGMT_2: u8 = 0x6C;
const REG_GYRO_CONFIG: u8 = 0x1B;
const REG_ACCEL_CONFIG: u8 = 0x1C;
const REG_FIFO_EN: u8 = 0x23;
const REG_INT_ENABLE: u8 = 0x38;
const REG_ACCEL_XOUT_H: u8 = 0x3B;
const REG_ACCEL_START: u8 = REG_ACCEL_XOUT_H;
const REG_GYRO_XOUT_H: u8 = 0x43;
const REG_GYRO_START: u8 = REG_GYRO_XOUT_H;
const REG_WHO_AM_I: u8 = 0x75;
const ICM20602_WAI: u8 = 0x12;
const ICM20608_WAI: u8 = 0xAF;
const ICM20689_WAI: u8 = 0x98;
#[repr(u8)]
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
pub enum GyroRange {
Range_250dps = 0b00,
Range_500dps = 0b01,
Range_1000dps = 0b10,
Range_2000dps = 0b11,
}
impl Default for GyroRange {
fn default() -> Self {
GyroRange::Range_2000dps
}
}
impl GyroRange {
const RADIANS_PER_DEGREE: f32 = core::f32::consts::PI / 180.0;
pub(crate) fn scale(&self) -> f32 {
Self::RADIANS_PER_DEGREE * self.resolution()
}
pub(crate) fn resolution(&self) -> f32 {
match self {
GyroRange::Range_250dps => 250.0 / 32768.0,
GyroRange::Range_500dps => 500.0 / 32768.0,
GyroRange::Range_1000dps => 1000.0 / 32768.0,
GyroRange::Range_2000dps => 2000.0 / 32768.0,
}
}
}
#[repr(u8)]
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug)]
pub enum AccelRange {
Range_2g = 0b00,
Range_4g = 0b01,
Range_8g = 0b10,
Range_16g = 0b11,
}
impl Default for AccelRange {
fn default() -> Self {
AccelRange::Range_8g
}
}
impl AccelRange {
const EARTH_GRAVITY_ACCEL: f32 = 9.807;
pub(crate) fn scale(&self) -> f32 {
Self::EARTH_GRAVITY_ACCEL * self.resolution()
}
pub(crate) fn resolution(&self) -> f32 {
match self {
Self::Range_2g => 2.0 / 32768.0,
Self::Range_4g => 4.0 / 32768.0,
Self::Range_8g => 8.0 / 32768.0,
Self::Range_16g => 16.0 / 32768.0,
}
}
}