kxtj3_1057/
register.rs

1//! This module provides definitions for controlling the accelerometer sensor.
2
3use num_enum::TryFromPrimitive;
4
5/// Possible I²C slave addresses.
6#[derive(Copy, Clone, Debug, Eq, PartialEq)]
7#[repr(u8)]
8pub enum SlaveAddr {
9    /// Default slave address (`0x0E`)
10    Default = 0x0E,
11
12    /// Alternate slave address (`0x0F`)
13    Alternate = 0x0F,
14}
15
16impl SlaveAddr {
17    pub fn addr(self) -> u8 {
18        self as u8
19    }
20}
21
22/// Enumerate all device registers.
23#[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)]
24#[derive(Copy, Clone, Debug, Eq, PartialEq)]
25#[repr(u8)]
26pub enum Register {
27    WHOAMI = 0x0F,
28    DCST_RESP = 0x0C,
29    SOFT_REST = 0x7F,
30    XOUT_L = 0x06,
31    XOUT_H = 0x07,
32    YOUT_L = 0x08,
33    YOUT_H = 0x09,
34    ZOUT_L = 0x0A,
35    ZOUT_H = 0x0B,
36    STATUS_REG = 0x18,
37    INT_SOURCE1 = 0x16,
38    INT_SOURCE2 = 0x17,
39    INT_REL = 0x1A,
40    CTRL1 = 0x1B,
41    CTRL2 = 0x1D,
42    INT_CTRL1 = 0x1E,
43    INT_CTRL2 = 0x1F,
44    DATA_CTRL = 0x21,
45    WAKEUP_COUNTER = 0x29,
46    NA_COUNTER = 0x2A,
47    SELF_TEST = 0x3A,
48    WAKEUP_THRESHOLD_H = 0x6A,
49    WAKEUP_THRESHOLD_L = 0x6B,
50}
51
52impl Register {
53    /// Get register address
54    pub fn addr(self) -> u8 {
55        self as u8
56    }
57
58    /// Is the register read-only?
59    pub fn read_only(self) -> bool {
60        matches!(
61            self,
62            Register::WHOAMI
63                | Register::XOUT_L
64                | Register::XOUT_H
65                | Register::YOUT_H
66                | Register::YOUT_L
67                | Register::ZOUT_H
68                | Register::ZOUT_L
69                | Register::DCST_RESP
70                | Register::INT_REL
71                | Register::STATUS_REG
72                | Register::INT_SOURCE1
73                | Register::INT_SOURCE2
74        )
75    }
76}
77
78///The operating mode .
79#[derive(Copy, Clone, Debug, Eq, PartialEq)]
80#[repr(u8)]
81pub enum Mode {
82    /// High-resolution mode (12-bit or 14-bit data output) .
83    HighResolution,
84
85    /// Low-power mode (8-bit data output).
86    LowPower,
87
88    /// Stand-by mode
89    Standby,
90}
91
92/// The acceleration output data rate.
93#[allow(non_camel_case_types)]
94#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
95#[repr(u8)]
96pub enum DataRate {
97    Hz_0_781 = 0b1000,
98    Hz_1_563 = 0b1001,
99    Hz_3_125 = 0b1010,
100    Hz_6_25 = 0b1011,
101    Hz_12_5 = 0b0000,
102    Hz_25 = 0b0001,
103    Hz_50 = 0b0010,
104    Hz_100 = 0b0011,
105    Hz_200 = 0b0100,
106    Hz_400HZ = 0b0101,
107    Hz_800HZ = 0b0110,
108    Hz_1600HZ = 0b0111,
109}
110
111impl DataRate {
112    pub const fn bits(self) -> u8 {
113        self as u8
114    }
115    pub const fn sample_rate(self) -> f32 {
116        match self {
117            DataRate::Hz_0_781 => 0.781,
118            DataRate::Hz_1_563 => 1.563,
119            DataRate::Hz_3_125 => 3.125,
120            DataRate::Hz_6_25 => 6.25,
121            DataRate::Hz_12_5 => 12.5,
122            DataRate::Hz_25 => 25.0,
123            DataRate::Hz_50 => 50.0,
124            DataRate::Hz_100 => 100.0,
125            DataRate::Hz_200 => 200.0,
126            DataRate::Hz_400HZ => 400.0,
127            DataRate::Hz_800HZ => 800.0,
128            DataRate::Hz_1600HZ => 1600.0,
129        }
130    }
131}
132///The acceleration range.
133#[allow(non_camel_case_types)]
134#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
135#[repr(u8)]
136pub enum Range {
137    /// ±16g
138    G16_1 = 0b001,
139
140    /// ±16g
141    G16_2 = 0b011,
142
143    /// ±16g
144    G16_3 = 0b101,
145
146    /// ±16g available only in 14-bit mode
147    G16_14Bit = 0b111,
148
149    /// ±8g
150    G8 = 0b100,
151
152    /// ±8g available only in 14-bit mode
153    G8_14Bit = 0b110,
154
155    /// ±4g
156    G4 = 0b010,
157
158    /// ±2g (Default)
159    G2 = 0b000,
160}
161impl Range {
162    pub const fn bits(self) -> u8 {
163        self as u8
164    }
165}
166
167/// The output data rate for the wake-up function.
168#[allow(dead_code, non_camel_case_types)]
169#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
170#[repr(u8)]
171pub enum MotionDetectionDataRate {
172    Hz_0_781 = 0b000,
173    Hz_1_563 = 0b001,
174    Hz_3_125 = 0b010,
175    Hz_6_25 = 0b011,
176    Hz_12_5 = 0b100,
177    Hz_25 = 0b101,
178    Hz_50 = 0b110,
179    Hz_100 = 0b111,
180}
181impl MotionDetectionDataRate {
182    pub const fn bits(self) -> u8 {
183        self as u8
184    }
185    pub const fn sample_rate(self) -> f32 {
186        match self {
187            MotionDetectionDataRate::Hz_0_781 => 0.781,
188            MotionDetectionDataRate::Hz_1_563 => 1.563,
189            MotionDetectionDataRate::Hz_100 => 100.0,
190            MotionDetectionDataRate::Hz_50 => 50.0,
191            MotionDetectionDataRate::Hz_25 => 25.0,
192            MotionDetectionDataRate::Hz_12_5 => 12.5,
193            MotionDetectionDataRate::Hz_3_125 => 3.125,
194            MotionDetectionDataRate::Hz_6_25 => 6.25,
195        }
196    }
197}
198
199///The axis and direction of motion.
200#[allow(dead_code, non_camel_case_types)]
201#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
202#[repr(u8)]
203pub enum MotionDetectionAxis {
204    X_Negative = 0b0010_0000,
205    X_Positive = 0b0001_0000,
206    Y_Negative = 0b0000_1000,
207    Y_Positive = 0b0000_0100,
208    Z_Negative = 0b0000_0010,
209    Z_Positive = 0b0000_0001,
210    None = 0b0000_0000,
211}
212impl MotionDetectionAxis {
213    pub const fn bits(self) -> u8 {
214        self as u8
215    }
216}
217
218///The latch mode of motion interrupt.
219#[allow(dead_code, non_camel_case_types)]
220#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
221#[repr(u8)]
222pub enum MotionDetectionLatchMode {
223    Latched = 0,
224    UnLatched = 1,
225}
226impl MotionDetectionLatchMode {
227    pub const fn bits(self) -> u8 {
228        self as u8
229    }
230}
231///The polarity of the physical interrupt pin.
232#[allow(dead_code, non_camel_case_types)]
233#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
234#[repr(u8)]
235pub enum InterruptPinPolarity {
236    ActiveLow,
237    ActiveHigh,
238}
239
240///The response of the physical interrupt pin.
241#[allow(dead_code, non_camel_case_types)]
242#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
243#[repr(u8)]
244pub enum InterruptPinResponse {
245    /// The physical interrupt pin (INT) latches until it is cleared by reading INT_REL
246    Latched,
247    /// The physical interrupt pin (INT) will transmit one pulse with a period of 0.03 - 0.05ms
248    Pulsed,
249}
250
251// === WHO_AMI_I (0Fh) ===
252///`WHO_AM_I` device identification register
253pub(crate) const DEVICE_ID: u8 = 0x35;
254
255// === CTRL_REG1 (1Bh) ===
256pub(crate) const PC1_EN: u8 = 0b1000_0000;
257pub(crate) const WUFE_EN: u8 = 0b0000_0010;
258pub(crate) const DRDYE_EN: u8 = 0b0010_0000;
259pub(crate) const RES_EN: u8 = 0b0100_0000;
260pub(crate) const GSEL_MASK: u8 = 0b0001_1100;
261
262// === CTRL_REG2 (1Dh) ===
263pub(crate) const ODRW_MASK: u8 = 0b0000_0111;
264
265// === DATA_CTRL_REG (21h) ===
266pub(crate) const ODR_MASK: u8 = 0b0000_1111;
267
268// === INT_CTRL_REG1(1Eh)  ====
269pub(crate) const IEN_EN: u8 = 0b0010_0000;
270pub(crate) const IEA_EN: u8 = 0b0001_0000;
271pub(crate) const IEL_EN: u8 = 0b0000_1000;
272
273// === INT_CTRL_REG2(1Fh)  ====
274pub(crate) const ULMODE_EN: u8 = 0b1000_0000;
275pub(crate) const WUE_MASK: u8 = 0b0011_1111;