1use crate::I2C_ADDRESS_AD0_LOW;
7
8use crate::Error;
9use device_driver::RegisterInterface;
10
11pub struct I2cInterface<I2C> {
13 i2c: I2C,
14 #[allow(dead_code)]
15 address: u8,
16}
17
18impl<I2C> I2cInterface<I2C> {
19 pub const fn default(i2c: I2C) -> Self {
33 Self {
34 i2c,
35 address: I2C_ADDRESS_AD0_LOW,
36 }
37 }
38
39 pub const fn alternative(i2c: I2C) -> Self {
52 Self {
53 i2c,
54 address: crate::I2C_ADDRESS_AD0_HIGH,
55 }
56 }
57
58 pub const fn new(i2c: I2C, address: u8) -> Self {
68 Self { i2c, address }
69 }
70
71 pub fn release(self) -> I2C {
73 self.i2c
74 }
75}
76
77impl<I2C, E> RegisterInterface for I2cInterface<I2C>
78where
79 I2C: embedded_hal::i2c::I2c<Error = E>,
80{
81 type Error = E;
82 type AddressType = u8;
83
84 fn read_register(
85 &mut self,
86 address: Self::AddressType,
87 size_bits: u32,
88 read_data: &mut [u8],
89 ) -> Result<(), Self::Error> {
90 let _ = size_bits; self.i2c.write_read(self.address, &[address], read_data)
92 }
93
94 fn write_register(
95 &mut self,
96 address: Self::AddressType,
97 size_bits: u32,
98 write_data: &[u8],
99 ) -> Result<(), Self::Error> {
100 let _ = size_bits; let mut buffer = [0u8; 33]; buffer[0] = address;
104 let len = write_data.len().min(32);
105 buffer[1..=len].copy_from_slice(&write_data[..len]);
106
107 self.i2c.write(self.address, &buffer[..=len])
108 }
109}
110
111#[cfg(feature = "async")]
112impl<I2C, E> device_driver::AsyncRegisterInterface for I2cInterface<I2C>
113where
114 I2C: embedded_hal_async::i2c::I2c<Error = E>,
115{
116 type Error = E;
117 type AddressType = u8;
118
119 async fn read_register(
120 &mut self,
121 address: Self::AddressType,
122 size_bits: u32,
123 read_data: &mut [u8],
124 ) -> Result<(), Self::Error> {
125 let _ = size_bits; self.i2c
127 .write_read(self.address, &[address], read_data)
128 .await
129 }
130
131 async fn write_register(
132 &mut self,
133 address: Self::AddressType,
134 size_bits: u32,
135 write_data: &[u8],
136 ) -> Result<(), Self::Error> {
137 let _ = size_bits; let mut buffer = [0u8; 33]; buffer[0] = address;
141 let len = write_data.len().min(32);
142 buffer[1..=len].copy_from_slice(&write_data[..len]);
143
144 self.i2c.write(self.address, &buffer[..=len]).await
145 }
146}
147
148pub struct SpiInterface<SPI> {
162 spi: SPI,
163}
164
165impl<SPI> SpiInterface<SPI> {
166 pub const fn new(spi: SPI) -> Self {
171 Self { spi }
172 }
173
174 pub fn release(self) -> SPI {
176 self.spi
177 }
178}
179
180impl<SPI, E> RegisterInterface for SpiInterface<SPI>
181where
182 SPI: embedded_hal::spi::SpiDevice<Error = E>,
183{
184 type Error = Error<E>;
185 type AddressType = u8;
186
187 fn read_register(
188 &mut self,
189 address: Self::AddressType,
190 size_bits: u32,
191 read_data: &mut [u8],
192 ) -> Result<(), Self::Error> {
193 let _ = size_bits; let read_address = address | 0x80;
196
197 let mut operations = [
198 embedded_hal::spi::Operation::Write(&[read_address]),
199 embedded_hal::spi::Operation::Read(read_data),
200 ];
201
202 self.spi.transaction(&mut operations).map_err(Error::Bus)
203 }
204
205 fn write_register(
206 &mut self,
207 address: Self::AddressType,
208 size_bits: u32,
209 write_data: &[u8],
210 ) -> Result<(), Self::Error> {
211 let _ = size_bits; let write_address = address & 0x7F;
214
215 let mut buffer = [0u8; 33];
217 buffer[0] = write_address;
218 let len = write_data.len().min(32);
219 buffer[1..=len].copy_from_slice(&write_data[..len]);
220
221 self.spi.write(&buffer[..=len]).map_err(Error::Bus)
222 }
223}
224
225#[cfg(feature = "async")]
226impl<SPI, E> device_driver::AsyncRegisterInterface for SpiInterface<SPI>
227where
228 SPI: embedded_hal_async::spi::SpiDevice<Error = E>,
229{
230 type Error = Error<E>;
231 type AddressType = u8;
232
233 async fn read_register(
234 &mut self,
235 address: Self::AddressType,
236 size_bits: u32,
237 read_data: &mut [u8],
238 ) -> Result<(), Self::Error> {
239 let _ = size_bits; let read_address = address | 0x80;
242
243 let mut operations = [
244 embedded_hal_async::spi::Operation::Write(&[read_address]),
245 embedded_hal_async::spi::Operation::Read(read_data),
246 ];
247
248 self.spi
249 .transaction(&mut operations)
250 .await
251 .map_err(Error::Bus)
252 }
253
254 async fn write_register(
255 &mut self,
256 address: Self::AddressType,
257 size_bits: u32,
258 write_data: &[u8],
259 ) -> Result<(), Self::Error> {
260 let _ = size_bits; let write_address = address & 0x7F;
263
264 let mut buffer = [0u8; 33];
266 buffer[0] = write_address;
267 let len = write_data.len().min(32);
268 buffer[1..=len].copy_from_slice(&write_data[..len]);
269
270 self.spi.write(&buffer[..=len]).await.map_err(Error::Bus)
271 }
272}