1use embedded_hal_async::{delay::DelayNs, i2c::*};
34
35use super::{defs::*, Bmi323, Error};
36
37impl<I, D, W, E> Bmi323<I, D, W>
38where
39 I: I2c<SevenBitAddress, Error = E>,
40 D: DelayNs,
41{
42 pub async fn set_int_map(&mut self, map: IntMap) -> Result<(), Error<E>> {
43 self.write(Reg::IntMap1, map).await
44 }
45
46 pub async fn set_int_latch(&mut self, latch: bool) -> Result<(), Error<E>> {
53 let mut r: IntConfReg = self.read(Reg::IntConf).await?;
55 r.latch = latch;
56 self.write(Reg::IntConf, r).await
57 }
58
59 pub async fn set_int_pins(&mut self, pin1: IntConfig, pin2: IntConfig) -> Result<(), Error<E>> {
60 let mut r: IoIntCtrl = self.read(Reg::IoIntCtrl).await?;
61 r.int1_level = pin1.level;
62 r.int1_output = pin1.output;
63 r.int1_enable = pin1.enable;
64 r.int2_level = pin2.level;
65 r.int2_output = pin2.output;
66 r.int2_enable = pin2.enable;
67 self.write(Reg::IoIntCtrl, r).await
68 }
69
70 pub async fn set_int_config(&mut self, latch: bool, pin1: IntConfig, pin2: IntConfig) -> Result<(), Error<E>> {
72 self.set_int_latch(latch).await?;
73 self.set_int_pins(pin1, pin2).await
74 }
75
76 pub async fn get_int_config(&mut self) -> Result<(IntConfig, IntConfig), Error<E>> {
77 let reg: IoIntCtrl = self.read(Reg::IoIntCtrl).await?;
78 let p1 = IntConfig { output: reg.int1_output, level: reg.int1_level, enable: reg.int1_enable };
79 let p2 = IntConfig { output: reg.int2_output, level: reg.int2_level, enable: reg.int2_enable };
80 Ok((p1, p2))
81 }
82
83 pub async fn get_int1_status(&mut self) -> Result<IntStatus, Error<E>> {
85 self.read(Reg::IntStatusInt1).await
86 }
87
88 pub async fn get_int2_status(&mut self) -> Result<IntStatus, Error<E>> {
90 self.read(Reg::IntStatusInt2).await
91 }
92
93 pub async fn get_feature_event_ext(&mut self) -> Result<FeatureEventExt, Error<E>> {
95 self.read(Reg::FeatureEventExt).await
96 }
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
101#[cfg_attr(feature = "defmt", derive(defmt::Format))]
102#[packbits::pack(bytes = 4)]
103pub struct IntMap {
104 #[bits(2)]
105 pub no_motion: IntPin,
106 #[bits(2)]
107 pub any_motion: IntPin,
108 #[bits(2)]
109 pub flat: IntPin,
110 #[bits(2)]
111 pub orientation: IntPin,
112 #[bits(2)]
113 pub step_detector: IntPin,
114 #[bits(2)]
115 pub step_counter: IntPin,
116 #[bits(2)]
117 pub sig_motion: IntPin,
118 #[bits(2)]
119 pub tilt: IntPin,
120 #[bits(2)]
121 pub tap: IntPin,
122 #[skip(2)]
123 #[bits(2)]
124 pub err_status: IntPin,
125 #[bits(2)]
126 pub temp_data_ready: IntPin,
127 #[bits(2)]
128 pub gyro_data_ready: IntPin,
129 #[bits(2)]
130 pub accel_data_ready: IntPin,
131 #[bits(2)]
132 pub fifo_watermark: IntPin,
133 #[bits(2)]
134 pub fifo_full: IntPin,
135}
136
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
139#[cfg_attr(feature = "defmt", derive(defmt::Format))]
140pub enum OrientationPl {
141 PortraitUpRight,
142 LandscapeLeft,
143 PortraitUpDown,
144 LandscapeRight,
145}
146
147impl From<OrientationPl> for u8 {
148 fn from(value: OrientationPl) -> Self {
149 value as u8
150 }
151}
152
153impl TryFrom<u8> for OrientationPl {
154 type Error = ();
155 fn try_from(v: u8) -> Result<Self, Self::Error> {
156 match v {
157 0x0 => Ok(OrientationPl::PortraitUpRight),
158 0x1 => Ok(OrientationPl::LandscapeLeft),
159 0x2 => Ok(OrientationPl::PortraitUpDown),
160 0x3 => Ok(OrientationPl::LandscapeRight),
161 _ => Err(()),
162 }
163 }
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq)]
167#[cfg_attr(feature = "defmt", derive(defmt::Format))]
168pub enum Face {
169 Up,
170 Down,
171}
172
173impl From<Face> for u8 {
174 fn from(value: Face) -> Self {
175 value as u8
176 }
177}
178
179impl TryFrom<u8> for Face {
180 type Error = ();
181 fn try_from(v: u8) -> Result<Self, Self::Error> {
182 match v {
183 0x0 => Ok(Face::Up),
184 0x1 => Ok(Face::Down),
185 _ => Err(()),
186 }
187 }
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq)]
191#[cfg_attr(feature = "defmt", derive(defmt::Format))]
192#[packbits::pack(bytes = 2)]
193pub struct FeatureEventExt {
194 #[bits(2)]
195 pub pl: OrientationPl,
196 #[bits(1)]
197 pub face: Face,
198 #[bits(1)]
199 pub s_tap: bool,
200 #[bits(1)]
201 pub d_tap: bool,
202 #[bits(1)]
203 pub t_tap: bool,
204}
205
206#[derive(Debug, Clone, Copy, PartialEq, Eq)]
207#[cfg_attr(feature = "defmt", derive(defmt::Format))]
208pub struct IntConfig {
209 pub output: OutputMode,
210 pub level: ActiveLevel,
211 pub enable: bool,
212}
213
214impl Default for IntConfig {
215 fn default() -> Self {
216 Self { output: OutputMode::OpenDrain, level: ActiveLevel::ActiveHigh, enable: false }
217 }
218}
219
220#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
221#[cfg_attr(feature = "defmt", derive(defmt::Format))]
222pub enum IntPin {
223 #[default]
224 None = 0x0,
225 Int1 = 0x1,
226 Int2 = 0x2,
227 }
229
230impl From<IntPin> for u8 {
231 fn from(value: IntPin) -> Self {
232 value as u8
233 }
234}
235
236impl TryFrom<u8> for IntPin {
237 type Error = ();
238 fn try_from(v: u8) -> Result<Self, Self::Error> {
239 match v {
240 0x0 => Ok(IntPin::None),
241 0x1 => Ok(IntPin::Int1),
242 0x2 => Ok(IntPin::Int2),
243 0x3 => Ok(IntPin::None), _ => Err(()),
245 }
246 }
247}
248
249#[derive(Debug, Clone, Copy, PartialEq, Eq)]
252#[cfg_attr(feature = "defmt", derive(defmt::Format))]
253pub enum LatchMode {
254 NonLatch = 0,
255 Latch = 1,
256}
257
258#[derive(Debug, Clone, Copy, PartialEq, Eq)]
260#[cfg_attr(feature = "defmt", derive(defmt::Format))]
261pub enum OutputMode {
262 PushPull = 0,
263 OpenDrain = 1,
264}
265
266impl From<OutputMode> for u8 {
267 fn from(value: OutputMode) -> Self {
268 value as u8
269 }
270}
271
272impl TryFrom<u8> for OutputMode {
273 type Error = ();
274 fn try_from(v: u8) -> Result<Self, Self::Error> {
275 match v & 0x01 {
276 0 => Ok(OutputMode::PushPull),
277 1 => Ok(OutputMode::OpenDrain),
278 _ => Err(()),
279 }
280 }
281}
282
283#[derive(Debug, Clone, Copy, PartialEq, Eq)]
285#[cfg_attr(feature = "defmt", derive(defmt::Format))]
286pub enum ActiveLevel {
287 ActiveLow = 0,
288 ActiveHigh = 1,
289}
290
291impl From<ActiveLevel> for u8 {
292 fn from(value: ActiveLevel) -> Self {
293 value as u8
294 }
295}
296
297impl TryFrom<u8> for ActiveLevel {
298 type Error = ();
299 fn try_from(v: u8) -> Result<Self, Self::Error> {
300 match v & 0x01 {
301 0 => Ok(ActiveLevel::ActiveLow),
302 1 => Ok(ActiveLevel::ActiveHigh),
303 _ => Err(()),
304 }
305 }
306}
307
308#[derive(Debug, Clone, Copy, PartialEq, Eq)]
309#[cfg_attr(feature = "defmt", derive(defmt::Format))]
310#[packbits::pack(bytes = 2)]
311struct IntConfReg {
312 #[bits(1)]
313 pub latch: bool,
314}
315
316#[derive(Debug, Clone, Copy, PartialEq, Eq)]
317#[cfg_attr(feature = "defmt", derive(defmt::Format))]
318#[packbits::pack(bytes = 2)]
319struct IoIntCtrl {
320 #[bits(1)]
322 pub int1_level: ActiveLevel,
323 #[bits(1)]
324 pub int1_output: OutputMode,
325 #[bits(1)]
326 pub int1_enable: bool,
327 #[skip(5)]
328 #[bits(1)]
330 pub int2_level: ActiveLevel,
331 #[bits(1)]
332 pub int2_output: OutputMode,
333 #[bits(1)]
334 pub int2_enable: bool,
335}
336
337#[derive(Debug, Clone, Copy, PartialEq, Eq)]
338#[cfg_attr(feature = "defmt", derive(defmt::Format))]
339#[packbits::pack(bytes = 2)]
340pub struct IntStatus {
341 #[bits(1)]
342 pub no_motion: bool,
343 #[bits(1)]
344 pub any_motion: bool,
345 #[bits(1)]
346 pub flat: bool,
347 #[bits(1)]
348 pub orientation: bool,
349 #[bits(1)]
350 pub step_detector: bool,
351 #[bits(1)]
352 pub step_counter: bool,
353 #[bits(1)]
354 pub sig_motion: bool,
355 #[bits(1)]
356 pub tilt: bool,
357 #[bits(1)]
358 pub tap: bool,
359 #[skip(1)]
360 #[bits(1)]
361 pub err_status: bool,
362 #[bits(1)]
363 pub temp_data_ready: bool,
364 #[bits(1)]
365 pub gyro_data_ready: bool,
366 #[bits(1)]
367 pub accel_data_ready: bool,
368 #[bits(1)]
369 pub fifo_watermark: bool,
370 #[bits(1)]
371 pub fifo_full: bool,
372}