Skip to main content

bmi323_driver/
driver.rs

1use core::marker::PhantomData;
2
3use embedded_hal::i2c::I2c;
4use embedded_hal::spi::SpiDevice;
5use embedded_hal_async::i2c::I2c as AsyncI2c;
6use embedded_hal_async::spi::SpiDevice as AsyncSpiDevice;
7
8use crate::registers::TransportKind;
9use crate::{
10    AccelRange, AsyncI2cTransport, AsyncSpiTransport, GyroRange, SyncI2cTransport, SyncSpiTransport,
11};
12
13/// Blocking BMI323 driver.
14///
15/// Create this with [`Bmi323::new_i2c`] or [`Bmi323::new_spi`].
16pub struct Bmi323<T> {
17    pub(crate) transport: T,
18    pub(crate) kind: TransportKind,
19    pub(crate) accel_range: AccelRange,
20    pub(crate) gyro_range: GyroRange,
21}
22
23/// Async BMI323 driver.
24///
25/// Create this with [`Bmi323Async::new_i2c`] or [`Bmi323Async::new_spi`].
26pub struct Bmi323Async<T> {
27    pub(crate) transport: T,
28    pub(crate) kind: TransportKind,
29    pub(crate) accel_range: AccelRange,
30    pub(crate) gyro_range: GyroRange,
31    pub(crate) _not_sync: PhantomData<fn() -> ()>,
32}
33
34impl<I2C> Bmi323<SyncI2cTransport<I2C>>
35where
36    I2C: I2c,
37{
38    /// Create a blocking BMI323 driver over an I2C bus.
39    ///
40    /// `address` is the 7-bit BMI323 I2C address selected by hardware.
41    pub fn new_i2c(i2c: I2C, address: u8) -> Self {
42        Self {
43            transport: SyncI2cTransport { bus: i2c, address },
44            kind: TransportKind::I2c,
45            accel_range: AccelRange::G8,
46            gyro_range: GyroRange::Dps2000,
47        }
48    }
49
50    /// Consume the driver and return ownership of the underlying I2C bus.
51    pub fn destroy(self) -> I2C {
52        self.transport.bus
53    }
54}
55
56impl<SPI> Bmi323<SyncSpiTransport<SPI>>
57where
58    SPI: SpiDevice<u8>,
59{
60    /// Create a blocking BMI323 driver over an SPI device.
61    pub fn new_spi(spi: SPI) -> Self {
62        Self {
63            transport: SyncSpiTransport { bus: spi },
64            kind: TransportKind::Spi,
65            accel_range: AccelRange::G8,
66            gyro_range: GyroRange::Dps2000,
67        }
68    }
69
70    /// Consume the driver and return ownership of the underlying SPI device.
71    pub fn destroy(self) -> SPI {
72        self.transport.bus
73    }
74}
75
76impl<I2C> Bmi323Async<AsyncI2cTransport<I2C>>
77where
78    I2C: AsyncI2c,
79{
80    /// Create an async BMI323 driver over an I2C bus.
81    ///
82    /// `address` is the 7-bit BMI323 I2C address selected by hardware.
83    pub fn new_i2c(i2c: I2C, address: u8) -> Self {
84        Self {
85            transport: AsyncI2cTransport { bus: i2c, address },
86            kind: TransportKind::I2c,
87            accel_range: AccelRange::G8,
88            gyro_range: GyroRange::Dps2000,
89            _not_sync: PhantomData,
90        }
91    }
92
93    /// Consume the driver and return ownership of the underlying I2C bus.
94    pub fn destroy(self) -> I2C {
95        self.transport.bus
96    }
97}
98
99impl<SPI> Bmi323Async<AsyncSpiTransport<SPI>>
100where
101    SPI: AsyncSpiDevice<u8>,
102{
103    /// Create an async BMI323 driver over an SPI device.
104    pub fn new_spi(spi: SPI) -> Self {
105        Self {
106            transport: AsyncSpiTransport { bus: spi },
107            kind: TransportKind::Spi,
108            accel_range: AccelRange::G8,
109            gyro_range: GyroRange::Dps2000,
110            _not_sync: PhantomData,
111        }
112    }
113
114    /// Consume the driver and return ownership of the underlying SPI device.
115    pub fn destroy(self) -> SPI {
116        self.transport.bus
117    }
118}