1#![allow(non_camel_case_types, clippy::unreadable_literal)]
3
4use accelerometer_hal::Error;
5use bitflags::{bitflags, Flags};
6use core::fmt::Debug;
7mod read;
8mod write;
9
10#[derive(Copy, Clone, Debug, Eq, PartialEq)]
14#[repr(u8)]
15pub enum Register {
16 CHIP_ID = 0x00,
18
19 XOUT1 = 0x02,
22
23 XOUT2 = 0x03,
26
27 YOUT1 = 0x04,
30
31 YOUT2 = 0x05,
34
35 ZOUT1 = 0x06,
38
39 ZOUT2 = 0x07,
42
43 INTSTS1 = 0x09,
45
46 INTSTS2 = 0x0A,
48
49 EVENTINFO1 = 0x0B,
51
52 RANGESEL = 0x0F,
54
55 BWSEL = 0x10,
57
58 POWMODE = 0x11,
60
61 DATASETUP = 0x13,
65
66 SWRST = 0x14,
69
70 INTEN1 = 0x16,
72
73 INTEN2 = 0x17,
75
76 INTMAP1 = 0x19,
78
79 INTMAP2 = 0x1A,
81
82 INTCFG1 = 0x20,
84
85 INTCFG2 = 0x21,
87
88 SLOPEDLY = 0x27,
90
91 SLOPETHD = 0x28,
93
94 SIGMOT1 = 0x29,
96
97 SIGMOT2 = 0x2A,
100
101 SIGMOT3 = 0x2B,
103
104 INTFCFG = 0x34,
106
107 OFSTCOMP1 = 0x36,
109
110 OFSTX = 0x38,
112
113 OFSTY = 0x39,
115
116 OFSTZ = 0x3A,
118}
119
120impl Register {
121 pub const fn addr(self) -> u8 {
123 self as u8
124 }
125
126 pub const fn read_only(self) -> bool {
128 matches!(
129 self,
130 Self::CHIP_ID
131 | Self::XOUT1
132 | Self::XOUT2
133 | Self::YOUT1
134 | Self::YOUT2
135 | Self::ZOUT1
136 | Self::ZOUT2
137 | Self::INTSTS1
138 | Self::INTSTS2
139 | Self::EVENTINFO1,
140 )
141 }
142}
143
144macro_rules! reg_from_flags {
148 ($f:ident) => {
149 impl From<$f> for Register {
150 fn from(_value: $f) -> Self {
151 Self::$f
152 }
153 }
154 };
155}
156
157reg_from_flags!(INTSTS1);
158reg_from_flags!(INTSTS2);
159reg_from_flags!(EVENTINFO1);
160reg_from_flags!(POWMODE);
161reg_from_flags!(DATASETUP);
162reg_from_flags!(INTEN1);
163reg_from_flags!(INTEN2);
164reg_from_flags!(INTMAP1);
165reg_from_flags!(INTMAP2);
166reg_from_flags!(INTCFG1);
167reg_from_flags!(INTCFG2);
168reg_from_flags!(SIGMOT2);
169reg_from_flags!(INTFCFG);
170reg_from_flags!(OFSTCOMP1);
171
172pub enum Axis {
174 X,
175 Y,
176 Z,
177}
178
179bitflags! {
180 #[derive(Copy, Clone)]
182 pub struct INTSTS1: u8 {
183 const SIG_MOT_STS = 0b1;
185
186 const ANY_MOT_STS = 0b100;
188 }
189
190 #[derive(Copy, Clone)]
192 pub struct INTSTS2: u8 {
193 const DATA_STS = 0b1000_0000;
195 }
196
197 #[derive(Copy, Clone)]
199 pub struct EVENTINFO1: u8 {
200 const SLP_1ST_X = 0b1;
202
203 const SLP_1ST_Y = 0b10;
205
206 const SLP_1ST_Z = 0b100;
208
209 const SLPSIGN_X = 0b1_0000;
211
212 const SLPSIGN_Y = 0b10_0000;
214
215 const SLPSIGN_Z = 0b100_0000;
217 }
218}
219
220#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
222#[repr(u8)]
223pub enum RangeSel {
224 #[default]
226 PM2G = 0b0011,
227
228 PM4G = 0b0101,
230
231 PM8G = 0b1000,
233
234 Undefined = 0b0,
236}
237
238impl From<u8> for RangeSel {
239 fn from(value: u8) -> Self {
240 match value & 0b1111 {
242 0b0011 => Self::PM2G,
243 0b0101 => Self::PM4G,
244 0b1000 => Self::PM8G,
245 _ => Self::Undefined,
246 }
247 }
248}
249
250impl From<RangeSel> for Register {
251 fn from(_value: RangeSel) -> Self {
252 Self::RANGESEL
253 }
254}
255
256impl From<RangeSel> for f32 {
257 fn from(value: RangeSel) -> Self {
258 match value {
259 RangeSel::PM2G => 2.0,
260 RangeSel::PM4G => 4.0,
261 RangeSel::PM8G => 8.0,
262 RangeSel::Undefined => 0.0,
263 }
264 }
265}
266
267#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
269#[repr(u8)]
270pub enum BwSel {
271 Hz7C81 = 0b1000,
273
274 Hz15C64 = 0b1001,
276
277 Hz31C25 = 0b1010,
279
280 Hz62C5 = 0b1011,
282
283 Hz125 = 0b1100,
285
286 Hz250 = 0b1101,
288
289 Hz500 = 0b1110,
291
292 #[default]
294 Hz1000 = 0b1111,
295}
296
297impl From<u8> for BwSel {
298 fn from(value: u8) -> Self {
299 match value & 0b00011111 {
301 0..=0b1000 => Self::Hz7C81, 0b1001 => Self::Hz15C64,
303 0b1010 => Self::Hz31C25,
304 0b1011 => Self::Hz62C5,
305 0b1100 => Self::Hz125,
306 0b1101 => Self::Hz250,
307 0b1110 => Self::Hz500,
308 _ => Self::Hz1000, }
310 }
311}
312
313impl From<BwSel> for f32 {
314 fn from(value: BwSel) -> Self {
315 match value {
316 BwSel::Hz7C81 => 7.81,
317 BwSel::Hz15C64 => 15.64,
318 BwSel::Hz31C25 => 31.25,
319 BwSel::Hz62C5 => 62.5,
320 BwSel::Hz125 => 125.0,
321 BwSel::Hz250 => 250.0,
322 BwSel::Hz500 => 500.0,
323 BwSel::Hz1000 => 1000.0,
324 }
325 }
326}
327
328impl From<BwSel> for Register {
329 fn from(_value: BwSel) -> Self {
330 Self::BWSEL
331 }
332}
333
334#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
336#[repr(u8)]
337pub enum SleepDuration {
338 #[default]
340 Ms0C5 = 0b0101_0,
341
342 Ms1 = 0b0110_0,
344
345 Ms2 = 0b0111_0,
347
348 Ms4 = 0b1000_0,
350
351 Ms6 = 0b1001_0,
353
354 Ms10 = 0b1010_0,
356
357 Ms25 = 0b1011_0,
359
360 Ms50 = 0b1100_0,
362
363 Ms100 = 0b1101_0,
365
366 Ms500 = 0b1110_0,
368
369 Ms1000 = 0b1111_0,
371}
372
373impl From<u8> for SleepDuration {
374 fn from(value: u8) -> Self {
375 match value & 0b1111_0 {
377 0b0110_0 => Self::Ms1,
378 0b0111_0 => Self::Ms2,
379 0b1000_0 => Self::Ms4,
380 0b1001_0 => Self::Ms6,
381 0b1010_0 => Self::Ms10,
382 0b1011_0 => Self::Ms25,
383 0b1100_0 => Self::Ms50,
384 0b1101_0 => Self::Ms100,
385 0b1110_0 => Self::Ms500,
386 0b1111_0 => Self::Ms1000,
387 _ => Self::Ms0C5, }
389 }
390}
391
392impl From<SleepDuration> for Register {
393 fn from(_value: SleepDuration) -> Self {
394 Self::POWMODE
395 }
396}
397
398bitflags! {
399 #[derive(Copy, Clone)]
401 pub struct POWMODE: u8 {
402 const LOWPOWER = 0b01000000;
404
405 const SUSPEND = 0b10000000;
407 }
408
409 #[derive(Copy, Clone)]
411 pub struct DATASETUP: u8 {
412 const PROTECT_DIS = 0b01000000;
414
415 const DATA_SEL = 0b10000000;
417 }
418
419 #[derive(Copy, Clone)]
421 pub struct INTEN1: u8 {
422 const SLP_EN_X = 0b00000001;
425
426 const SLP_EN_Y = 0b00000010;
429
430 const SLP_EN_Z = 0b00000100;
433 }
434
435 #[derive(Copy, Clone)]
437 pub struct INTEN2: u8 {
438 const DATA_EN = 0b00001000;
441 }
442
443 #[derive(Copy, Clone)]
445 pub struct INTMAP1: u8 {
446 const SIGMOT2INT1 = 0b00000001;
449
450 const ANYMOT2INT1 = 0b0000100;
453 }
454
455 #[derive(Copy, Clone)]
457 pub struct INTMAP2: u8 {
458 const DATA2INT1 = 0b00000001;
461 }
462
463 #[derive(Copy, Clone)]
465 pub struct INTCFG1: u8 {
466 const INT1_LV = 0b00000001;
468
469 const INT1_OD = 0b00000010;
471 }
472}
473
474#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
476#[repr(u8)]
477pub enum IntLatch {
478 #[default]
479 NON_LATCHED = 0b0000,
480 LATCHED = 0b0111,
481
482 TEMP250US = 0b1001,
484
485 TEMP500US = 0b1010,
487
488 TEMP1MS = 0b1011,
490
491 TEMP12C5MS = 0b1100,
493
494 TEMP25MS = 0b1101,
496
497 TEMP50MS = 0b1110,
499
500 TEMP250MS = 0b0001,
502
503 TEMP500MS = 0b0010,
505
506 TEMP1S = 0b0011,
508
509 TEMP2S = 0b0100,
511
512 TEMP4S = 0b0101,
514
515 TEMP8S = 0b0110,
517}
518
519impl From<u8> for IntLatch {
520 fn from(value: u8) -> Self {
521 match value & 0b1111 {
522 0b0111 | 0b1111 => Self::LATCHED,
523 0b0001 => Self::TEMP250MS,
524 0b0010 => Self::TEMP500MS,
525 0b0011 => Self::TEMP1S,
526 0b0100 => Self::TEMP2S,
527 0b0101 => Self::TEMP4S,
528 0b0110 => Self::TEMP8S,
529 0b1001 => Self::TEMP250US,
530 0b1010 => Self::TEMP500US,
531 0b1011 => Self::TEMP1MS,
532 0b1100 => Self::TEMP12C5MS,
533 0b1101 => Self::TEMP25MS,
534 0b1110 => Self::TEMP50MS,
535 _ => Self::NON_LATCHED,
536 }
537 }
538}
539
540impl From<IntLatch> for Register {
541 fn from(_value: IntLatch) -> Self {
542 Self::INTCFG2
543 }
544}
545
546bitflags! {
547 #[derive(Copy, Clone)]
549 pub struct INTCFG2: u8 {
550 const INT_RST = 0b10000000;
551 }
552
553 #[derive(Copy, Clone)]
555 pub struct SIGMOT2: u8 {
556 const SIG_MOT_EN = 0b00000010;
558
559 const ANY_MOT_EN = 0b00000100;
561 }
562
563 #[derive(Copy, Clone)]
565 pub struct INTFCFG: u8 {
566 const I2C_WDT_SEL = 0b00000010;
568
569 const I2C_WDT_EN = 0b00000100;
571 }
572
573 #[derive(Copy, Clone)]
575 pub struct OFSTCOMP1: u8 {
576 const OFST_RST = 0b10000000;
577 }
578}
579
580pub trait Read<I2C, E> {
582 fn chip_id(&mut self) -> Result<u8, E>;
583 fn axis_lsb(&mut self, axis: &Axis) -> Result<u8, E>;
584 fn axis_msb(&mut self, axis: &Axis) -> Result<u8, E>;
585 fn axis_newdata(&mut self, axis: &Axis) -> Result<bool, E>;
586
587 fn read_flags<F: Flags<Bits = u8> + Into<Register>>(&mut self, flags: F) -> Result<F, E>;
588 fn read_mode<M: From<u8> + Into<Register>>(&mut self, mode: M) -> Result<M, E>;
589}
590
591pub trait Write<I2C, E>
593where
594 E: Debug,
595{
596 fn set_range(&mut self, range: RangeSel) -> Result<&mut Self, Error<E>>;
597 fn set_bandwidth(&mut self, bandwidth: BwSel) -> Result<&mut Self, Error<E>>;
598 fn set_sleep_duration(&mut self, sleep_duration: SleepDuration) -> Result<&mut Self, Error<E>>;
599 fn set_int_latch(&mut self, latch: IntLatch) -> Result<&mut Self, Error<E>>;
600
601 fn set_flags<F: Flags<Bits = u8> + Into<Register> + Copy>(
602 &mut self,
603 flags: F,
604 ) -> Result<&mut Self, Error<E>>;
605
606 fn reset_all(&mut self) -> Result<&mut Self, Error<E>>;
607}