aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/i2c.rs
diff options
context:
space:
mode:
authorHenrik Alsér <[email protected]>2022-07-08 15:47:47 +0200
committerHenrik Alsér <[email protected]>2022-07-08 15:47:47 +0200
commitf30ff9cadcf575100a4e08c972d2f161172a3fc9 (patch)
tree3c19664e1e72a483a1d2d85b176cf760694642f4 /embassy-embedded-hal/src/shared_bus/i2c.rs
parent6baddaf53982b75149cb7e91280c571f7fe2e7bc (diff)
Shared buses with SetConfig
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/i2c.rs')
-rw-r--r--embassy-embedded-hal/src/shared_bus/i2c.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs
index e8131288a..0e964773c 100644
--- a/embassy-embedded-hal/src/shared_bus/i2c.rs
+++ b/embassy-embedded-hal/src/shared_bus/i2c.rs
@@ -29,6 +29,8 @@ use embassy::blocking_mutex::raw::RawMutex;
29use embassy::mutex::Mutex; 29use embassy::mutex::Mutex;
30use embedded_hal_async::i2c; 30use embedded_hal_async::i2c;
31 31
32use crate::SetConfig;
33
32#[derive(Copy, Clone, Eq, PartialEq, Debug)] 34#[derive(Copy, Clone, Eq, PartialEq, Debug)]
33pub enum I2cBusDeviceError<BUS> { 35pub enum I2cBusDeviceError<BUS> {
34 I2c(BUS), 36 I2c(BUS),
@@ -116,3 +118,79 @@ where
116 async move { todo!() } 118 async move { todo!() }
117 } 119 }
118} 120}
121
122pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> {
123 bus: &'a Mutex<M, BUS>,
124 config: C,
125}
126
127impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> {
128 pub fn new(bus: &'a Mutex<M, BUS>, config: C) -> Self {
129 Self { bus, config }
130 }
131}
132
133impl<'a, M: RawMutex, BUS, C> i2c::ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C>
134where
135 BUS: i2c::ErrorType,
136{
137 type Error = I2cBusDeviceError<BUS::Error>;
138}
139
140impl<M, BUS, C> i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS, C>
141where
142 M: RawMutex + 'static,
143 BUS: i2c::I2c + SetConfig<C> + 'static,
144{
145 type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
146
147 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
148 async move {
149 let mut bus = self.bus.lock().await;
150 bus.set_config(&self.config);
151 bus.read(address, buffer).await.map_err(I2cBusDeviceError::I2c)?;
152 Ok(())
153 }
154 }
155
156 type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
157
158 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
159 async move {
160 let mut bus = self.bus.lock().await;
161 bus.set_config(&self.config);
162 bus.write(address, bytes).await.map_err(I2cBusDeviceError::I2c)?;
163 Ok(())
164 }
165 }
166
167 type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
168
169 fn write_read<'a>(
170 &'a mut self,
171 address: u8,
172 wr_buffer: &'a [u8],
173 rd_buffer: &'a mut [u8],
174 ) -> Self::WriteReadFuture<'a> {
175 async move {
176 let mut bus = self.bus.lock().await;
177 bus.set_config(&self.config);
178 bus.write_read(address, wr_buffer, rd_buffer)
179 .await
180 .map_err(I2cBusDeviceError::I2c)?;
181 Ok(())
182 }
183 }
184
185 type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a;
186
187 fn transaction<'a, 'b>(
188 &'a mut self,
189 address: u8,
190 operations: &'a mut [embedded_hal_async::i2c::Operation<'b>],
191 ) -> Self::TransactionFuture<'a, 'b> {
192 let _ = address;
193 let _ = operations;
194 async move { todo!() }
195 }
196}