icm42670 0.2.0

An embedded-hal driver for the ICM-42670 6-axis IMU
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use crate::{
    error::SensorError,
    register::{Bank0, Register},
};

pub(crate) trait Bitfield {
    const BITMASK: u8;
    type Reg: Register;
    const REGISTER: Self::Reg;

    /// Bit value of a discriminant, shifted to the correct position if
    /// necessary
    fn bits(self) -> u8;
}

/// I²C slave addresses, determined by the logic level of pin `AP_AD0`
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Address {
    /// `AP_AD0` pin == 0
    Primary   = 0x68,
    /// `AP_AD0` pin == 1
    Secondary = 0x69,
}

/// Configurable ranges of the Accelerometer
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AccelRange {
    /// ±2G
    G2  = 3,
    /// ±4G
    G4  = 2,
    /// ±8G
    G8  = 1,
    /// ±16G
    G16 = 0,
}

impl AccelRange {
    /// Sensitivity scale factor
    pub fn scale_factor(&self) -> f32 {
        use AccelRange::*;

        // Values taken from Table 2 of the data sheet
        match self {
            G2 => 16_384.0,
            G4 => 8_192.0,
            G8 => 4_096.0,
            G16 => 2_048.0,
        }
    }
}

impl Bitfield for AccelRange {
    const BITMASK: u8 = 0b0110_0000;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::ACCEL_CONFIG0;

    fn bits(self) -> u8 {
        // `ACCEL_UI_FS_SEL` occupies bits 6:5 in the register
        (self as u8) << 5
    }
}

impl Default for AccelRange {
    fn default() -> Self {
        Self::G16
    }
}

impl TryFrom<u8> for AccelRange {
    type Error = SensorError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use AccelRange::*;

        match value {
            0 => Ok(G16),
            1 => Ok(G8),
            2 => Ok(G4),
            3 => Ok(G2),
            _ => Err(SensorError::InvalidDiscriminant),
        }
    }
}

/// Configurable ranges of the Gyroscope
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GyroRange {
    /// ±250 deg/sec
    Deg250  = 3,
    /// ±500 deg/sec
    Deg500  = 2,
    /// ±1000 deg/sec
    Deg1000 = 1,
    /// ±2000 deg/sec
    Deg2000 = 0,
}

impl GyroRange {
    /// Sensitivity scale factor
    pub fn scale_factor(&self) -> f32 {
        use GyroRange::*;

        // Values taken from Table 1 of the data sheet
        match self {
            Deg250 => 131.0,
            Deg500 => 65.5,
            Deg1000 => 32.8,
            Deg2000 => 16.4,
        }
    }
}

impl Bitfield for GyroRange {
    const BITMASK: u8 = 0b0110_0000;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::GYRO_CONFIG0;

    fn bits(self) -> u8 {
        // `GYRO_UI_FS_SEL` occupies bits 6:5 in the register
        (self as u8) << 5
    }
}

impl Default for GyroRange {
    fn default() -> Self {
        Self::Deg2000
    }
}

impl TryFrom<u8> for GyroRange {
    type Error = SensorError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use GyroRange::*;

        match value {
            0 => Ok(Deg2000),
            1 => Ok(Deg1000),
            2 => Ok(Deg500),
            3 => Ok(Deg250),
            _ => Err(SensorError::InvalidDiscriminant),
        }
    }
}

/// Configurable power modes of the IMU
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PowerMode {
    /// Gyroscope: OFF, Accelerometer: OFF
    Sleep           = 0b0000,
    /// Gyroscope: DRIVE ON, Accelerometer: OFF
    Standby         = 0b0100,
    /// Gyroscope: OFF, Accelerometer: DUTY-CYCLED
    AccelLowPower   = 0b0010,
    /// Gyroscope: OFF, Accelerometer: ON
    AccelLowNoise   = 0b0011,
    /// Gyroscope: ON, Accelerometer: OFF
    GyroLowNoise    = 0b1100,
    /// Gyroscope: ON, Accelerometer: ON
    SixAxisLowNoise = 0b1111,
}

impl Bitfield for PowerMode {
    const BITMASK: u8 = 0b0000_1111;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::PWR_MGMT0;

    fn bits(self) -> u8 {
        // `GYRO_MODE` occupies bits 3:2 in the register
        // `ACCEL_MODE` occupies bits 1:0 in the register
        self as u8
    }
}

impl Default for PowerMode {
    fn default() -> Self {
        PowerMode::Sleep
    }
}

impl TryFrom<u8> for PowerMode {
    type Error = SensorError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use PowerMode::*;

        match value {
            0b0000 => Ok(Sleep),
            0b0100 => Ok(Standby),
            0b0010 => Ok(AccelLowPower),
            0b0011 => Ok(AccelLowNoise),
            0b1100 => Ok(GyroLowNoise),
            0b1111 => Ok(SixAxisLowNoise),
            _ => Err(SensorError::InvalidDiscriminant),
        }
    }
}

/// Accelerometer ODR selection values
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AccelOdr {
    /// 1.6 kHz (LN mode)
    Hz1600   = 0b0101,
    /// 800 Hz (LN mode
    Hz800    = 0b0110,
    /// 400 Hz (LP or LN mode)
    Hz400    = 0b0111,
    /// 200 Hz (LP or LN mode)
    Hz200    = 0b1000,
    /// 100 Hz (LP or LN mode)
    Hz100    = 0b1001,
    /// 50 Hz (LP or LN mode)
    Hz50     = 0b1010,
    /// 25 Hz (LP or LN mode)
    Hz25     = 0b1011,
    /// 12.5 Hz (LP or LN mode)
    Hz12_5   = 0b1100,
    /// 6.25 Hz (LP mode)
    Hz6_25   = 0b1101,
    /// 3.125 Hz (LP mode)
    Hz3_125  = 0b1110,
    /// 1.5625 Hz (LP mode
    Hz1_5625 = 0b1111,
}

impl AccelOdr {
    pub fn as_f32(self) -> f32 {
        use AccelOdr::*;

        match self {
            Hz1600 => 1600.0,
            Hz800 => 800.0,
            Hz400 => 400.0,
            Hz200 => 200.0,
            Hz100 => 100.0,
            Hz50 => 50.0,
            Hz25 => 25.0,
            Hz12_5 => 12.5,
            Hz6_25 => 6.25,
            Hz3_125 => 3.125,
            Hz1_5625 => 1.5625,
        }
    }
}

impl Bitfield for AccelOdr {
    const BITMASK: u8 = 0b0000_1111;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::ACCEL_CONFIG0;

    fn bits(self) -> u8 {
        // `ACCEL_ODR` occupies bits 3:0 in the register
        self as u8
    }
}

impl Default for AccelOdr {
    fn default() -> Self {
        Self::Hz800
    }
}

impl TryFrom<u8> for AccelOdr {
    type Error = SensorError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use AccelOdr::*;

        match value {
            0b0101 => Ok(Hz1600),
            0b0110 => Ok(Hz800),
            0b0111 => Ok(Hz400),
            0b1000 => Ok(Hz200),
            0b1001 => Ok(Hz100),
            0b1010 => Ok(Hz50),
            0b1011 => Ok(Hz25),
            0b1100 => Ok(Hz12_5),
            0b1101 => Ok(Hz6_25),
            0b1110 => Ok(Hz3_125),
            0b1111 => Ok(Hz1_5625),
            _ => Err(SensorError::InvalidDiscriminant),
        }
    }
}

/// Acceleration Low Power Averaging
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AccLpAvg {
    X2  = 0b000,
    X4  = 0b001,
    X8  = 0b010,
    X16 = 0b011,
    X32 = 0b100,
    X64 = 0b101,
}

impl Bitfield for AccLpAvg {
    const BITMASK: u8 = 0b0111_0000;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::ACCEL_CONFIG1;

    fn bits(self) -> u8 {
        (self as u8) << 4
    }
}

/// Acceleration Digital Low Pass Filter options
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AccelDlpfBw {
    Bypassed = 0b000,
    Hz180    = 0b001,
    Hz121    = 0b010,
    Hz73     = 0b011,
    Hz53     = 0b100,
    Hz34     = 0b101,
    Hz25     = 0b110,
    Hz16     = 0b111,
}

impl Bitfield for AccelDlpfBw {
    const BITMASK: u8 = 0b0000_0111;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::ACCEL_CONFIG1;

    fn bits(self) -> u8 {
        self as u8
    }
}

/// Temperature DLPF (Digital Low Pass Filter) Bandwidth
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TempDlpfBw {
    Bypassed = 0b000,
    Hz180    = 0b001,
    Hz72     = 0b010,
    Hz34     = 0b011,
    Hz16     = 0b100,
    Hz8      = 0b101,
    Hz4      = 0b110,
}
impl Bitfield for TempDlpfBw {
    const BITMASK: u8 = 0b0111_0000;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::TEMP_CONFIG0;

    fn bits(self) -> u8 {
        (self as u8) << 4
    }
}

/// Gyroscope UI low pass filter bandwidth
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GyroLpFiltBw {
    Bypassed = 0b000,
    Hz180    = 0b001,
    Hz121    = 0b010,
    Hz73     = 0b011,
    Hz53     = 0b100,
    Hz34     = 0b101,
    Hz25     = 0b110,
    Hz16     = 0b111,
}
impl Bitfield for GyroLpFiltBw {
    const BITMASK: u8 = 0b0000_0111;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::GYRO_CONFIG1;

    fn bits(self) -> u8 {
        self as u8
    }
}
/// Gyroscope ODR selection values
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GyroOdr {
    /// 1.6k Hz
    Hz1600 = 0b0101,
    /// 800 Hz
    Hz800  = 0b0110,
    /// 400 Hz
    Hz400  = 0b0111,
    /// 200 Hz
    Hz200  = 0b1000,
    /// 100 Hz
    Hz100  = 0b1001,
    /// 50 Hz
    Hz50   = 0b1010,
    /// 25 Hz
    Hz25   = 0b1011,
    /// 12.5 Hz
    Hz12_5 = 0b1100,
}

impl GyroOdr {
    pub fn as_f32(self) -> f32 {
        use GyroOdr::*;

        match self {
            Hz1600 => 1600.0,
            Hz800 => 800.0,
            Hz400 => 400.0,
            Hz200 => 200.0,
            Hz100 => 100.0,
            Hz50 => 50.0,
            Hz25 => 25.0,
            Hz12_5 => 12.5,
        }
    }
}

impl Bitfield for GyroOdr {
    const BITMASK: u8 = 0b0000_1111;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::GYRO_CONFIG0;

    fn bits(self) -> u8 {
        // `GYRO_ODR` occupies bits 3:0 in the register
        self as u8
    }
}

impl Default for GyroOdr {
    fn default() -> Self {
        Self::Hz800
    }
}

impl TryFrom<u8> for GyroOdr {
    type Error = SensorError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use GyroOdr::*;

        match value {
            0b0101 => Ok(Hz1600),
            0b0110 => Ok(Hz800),
            0b0111 => Ok(Hz400),
            0b1000 => Ok(Hz200),
            0b1001 => Ok(Hz100),
            0b1010 => Ok(Hz50),
            0b1011 => Ok(Hz25),
            0b1100 => Ok(Hz12_5),
            _ => Err(SensorError::InvalidDiscriminant),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum SoftReset {
    Enabled   = 0b0,
    _Disabled = 0b1,
}

impl Bitfield for SoftReset {
    const BITMASK: u8 = 0b0001_0000;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::SIGNAL_PATH_RESET;

    fn bits(self) -> u8 {
        (self as u8) << 4
    }
}

#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum MClkReady {
    Running    = 0b0,
    NotRunning = 0b1,
}

impl Bitfield for MClkReady {
    const BITMASK: u8 = 0b0000_1000;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::MCLK_RDY;

    fn bits(self) -> u8 {
        (self as u8) << 3
    }
}

#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum SpiWireCount {
    ThreeWire = 0b0,
    FourWire  = 0b1,
}

impl Bitfield for SpiWireCount {
    const BITMASK: u8 = 0b0000_0100;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::DEVICE_CONFIG;

    fn bits(self) -> u8 {
        (self as u8) << 2
    }
}

#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum SpiMode {
    Mode0And3 = 0b0,
    Mode1And2 = 0b1,
}

impl Bitfield for SpiMode {
    const BITMASK: u8 = 0b0000_0001;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::DEVICE_CONFIG;

    fn bits(self) -> u8 {
        self as u8
    }
}

/// Controls slew rate for output pin 14 when device is in I3CSM DDR protocol.
/// While in I3CSM operation, the device automatically switches to use
/// I3C_DDR_SLEW_RATE after receiving ENTHDR0 ccc command from the host.
/// The device automatically switches back to I3C_SDR_SLEW_RATE after the
/// host issues HDR_EXIT pattern.
///
/// This register field should not be programmed in I3C/DDR mode.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum I3CDdrSlewRate {
    /// Min 20ns; Typ 40ns; Max 60ns
    M20T40M60 = 0b000,
    /// Min 12ns; Typ 24ns; Max 36ns
    M12T24M36 = 0b001,
    /// Min 6ns; Typ 12ns; Max 19ns
    M6T12M19  = 0b010,
    /// Min 4ns; Typ 8ns; Max 14ns
    M4T8M14   = 0b011,
    /// Min 2ns; Typ 4ns; Max 8ns
    M2T4M8    = 0b100,
    /// Max 2ns
    M2        = 0b101,
}

impl Bitfield for I3CDdrSlewRate {
    const BITMASK: u8 = 0b0011_1000;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::DRIVE_CONFIG1;

    fn bits(self) -> u8 {
        (self as u8) << 3
    }
}

/// Controls slew rate for output pin 14 in I3CSM SDR protocol.
/// After device reset, I2C_SLEW_RATE is used by default. If I3CSM feature is
/// enabled, the device automatically switches to use I3C_SDR_SLEW_RATE
/// after receiving 0x7E+W message (an I3CSM broadcast message).
///
/// This register field should not be programmed in I3C/DDR mode.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum I3CSdrSlewRate {
    /// Min 20ns; Typ 40ns; Max 60ns
    M20T40M60 = 0b000,
    /// Min 12ns; Typ 24ns; Max 36ns
    M12T24M36 = 0b001,
    /// Min 6ns; Typ 12ns; Max 19ns
    M6T12M19  = 0b010,
    /// Min 4ns; Typ 8ns; Max 14ns
    M4T8M14   = 0b011,
    /// Min 2ns; Typ 4ns; Max 8ns
    M2T4M8    = 0b100,
    /// Max 2ns
    M2        = 0b101,
}

impl Bitfield for I3CSdrSlewRate {
    const BITMASK: u8 = 0b0000_0111;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::DRIVE_CONFIG1;

    fn bits(self) -> u8 {
        self as u8
    }
}

/// Controls slew rate for output pin 14 in I2C mode.
/// After device reset, the I2C_SLEW_RATE is used by default. If the 1st write
/// operation from host is an SPI transaction, the device automatically switches
/// to SPI_SLEW_RATE. If I3CSM feature is enabled, the device automatically
/// switches to I3C_SDR_SLEW_RATE after receiving 0x7E+W message (an I3C
/// broadcast message).
///
/// This register field should not be programmed in I3C/DDR mode.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum I2CSlewRate {
    /// Min 20ns; Typ 40ns; Max 60ns
    M20T40M60 = 0b000,
    /// Min 12ns; Typ 24ns; Max 36ns
    M12T24M36 = 0b001,
    /// Min 6ns; Typ 12ns; Max 19ns
    M6T12M19  = 0b010,
    /// Min 4ns; Typ 8ns; Max 14ns
    M4T8M14   = 0b011,
    /// Min 2ns; Typ 4ns; Max 8ns
    M2T4M8    = 0b100,
    /// Max 2ns
    M2        = 0b101,
}

impl Bitfield for I2CSlewRate {
    const BITMASK: u8 = 0b0011_1000;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::DRIVE_CONFIG2;

    fn bits(self) -> u8 {
        (self as u8) << 3
    }
}

/// Configure drive strength for all output pins in all modes (SPI3, SPI4, I2C,
/// I3CSM) excluding pin 14.
///
/// This register field should not be programmed in I3C/DDR mode.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum AllSlewRate {
    /// Min 20ns; Typ 40ns; Max 60ns
    M20T40M60 = 0b000,
    /// Min 12ns; Typ 24ns; Max 36ns
    M12T24M36 = 0b001,
    /// Min 6ns; Typ 12ns; Max 19ns
    M6T12M19  = 0b010,
    /// Min 4ns; Typ 8ns; Max 14ns
    M4T8M14   = 0b011,
    /// Min 2ns; Typ 4ns; Max 8ns
    M2T4M8    = 0b100,
    /// Max 2ns
    M2        = 0b101,
}

impl Bitfield for AllSlewRate {
    const BITMASK: u8 = 0b0000_0111;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::DRIVE_CONFIG2;

    fn bits(self) -> u8 {
        self as u8
    }
}

/// Controls slew rate for output pin 14 in SPI 3-wire mode. In SPI 4-wire mode
/// this register controls the slew rate of pin 1 as it is used as an output in
/// SPI 4- wire mode only. After chip reset, the I2C_SLEW_RATE is used by
/// default for pin 14 pin. If the 1st write operation from the host is an
/// SPI3/4 transaction, the device automatically switches to SPI_SLEW_RATE.
///
/// This register field should not be programmed in I3C/DDR mode.
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum SpiSlewRate {
    /// Min 20ns; Typ 40ns; Max 60ns
    M20T40M60 = 0b000,
    /// Min 12ns; Typ 24ns; Max 36ns
    M12T24M36 = 0b001,
    /// Min 6ns; Typ 12ns; Max 19ns
    M6T12M19  = 0b010,
    /// Min 4ns; Typ 8ns; Max 14ns
    M4T8M14   = 0b011,
    /// Min 2ns; Typ 4ns; Max 8ns
    M2T4M8    = 0b100,
    /// Max 2ns
    M2        = 0b101,
}

impl Bitfield for SpiSlewRate {
    const BITMASK: u8 = 0b0000_0111;
    type Reg = Bank0;
    const REGISTER: Self::Reg = Self::Reg::DRIVE_CONFIG2;

    fn bits(self) -> u8 {
        self as u8
    }
}