aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
diff options
context:
space:
mode:
authorHenrik Alsér <[email protected]>2022-07-09 00:00:55 +0200
committerHenrik Alsér <[email protected]>2022-07-09 00:00:55 +0200
commitd637510b44ec8e58581f3e22f8398b53145503dc (patch)
treeb1a062c6b032947323248c6945fee87110bfc10d /embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
parent15384d27bb181bf48e580ca4a1c4fd848ecb1720 (diff)
Associated type
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/blocking/i2c.rs')
-rw-r--r--embassy-embedded-hal/src/shared_bus/blocking/i2c.rs101
1 files changed, 51 insertions, 50 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
index e8dd0f248..0c8338c73 100644
--- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
+++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
@@ -101,28 +101,71 @@ where
101 } 101 }
102} 102}
103 103
104pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { 104impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cBusDevice<'_, M, BUS>
105where
106 M: RawMutex,
107 BUS: embedded_hal_02::blocking::i2c::Write<Error = E>,
108{
109 type Error = I2cBusDeviceError<E>;
110
111 fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> Result<(), Self::Error> {
112 self.bus
113 .lock(|bus| bus.borrow_mut().write(addr, bytes).map_err(I2cBusDeviceError::I2c))
114 }
115}
116
117impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cBusDevice<'_, M, BUS>
118where
119 M: RawMutex,
120 BUS: embedded_hal_02::blocking::i2c::Read<Error = E>,
121{
122 type Error = I2cBusDeviceError<E>;
123
124 fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> Result<(), Self::Error> {
125 self.bus
126 .lock(|bus| bus.borrow_mut().read(addr, bytes).map_err(I2cBusDeviceError::I2c))
127 }
128}
129
130impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cBusDevice<'_, M, BUS>
131where
132 M: RawMutex,
133 BUS: embedded_hal_02::blocking::i2c::WriteRead<Error = E>,
134{
135 type Error = I2cBusDeviceError<E>;
136
137 fn write_read<'w>(&mut self, addr: u8, bytes: &'w [u8], buffer: &'w mut [u8]) -> Result<(), Self::Error> {
138 self.bus.lock(|bus| {
139 bus.borrow_mut()
140 .write_read(addr, bytes, buffer)
141 .map_err(I2cBusDeviceError::I2c)
142 })
143 }
144}
145
146pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> {
105 bus: &'a Mutex<M, RefCell<BUS>>, 147 bus: &'a Mutex<M, RefCell<BUS>>,
106 config: C, 148 config: BUS::Config,
107} 149}
108 150
109impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { 151impl<'a, M: RawMutex, BUS: SetConfig> I2cBusDeviceWithConfig<'a, M, BUS> {
110 pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: C) -> Self { 152 pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: BUS::Config) -> Self {
111 Self { bus, config } 153 Self { bus, config }
112 } 154 }
113} 155}
114 156
115impl<'a, M: RawMutex, BUS, C> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> 157impl<'a, M, BUS> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS>
116where 158where
117 BUS: ErrorType, 159 M: RawMutex,
160 BUS: ErrorType + SetConfig,
118{ 161{
119 type Error = I2cBusDeviceError<BUS::Error>; 162 type Error = I2cBusDeviceError<BUS::Error>;
120} 163}
121 164
122impl<M, BUS, C> I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> 165impl<M, BUS> I2c for I2cBusDeviceWithConfig<'_, M, BUS>
123where 166where
124 M: RawMutex, 167 M: RawMutex,
125 BUS: I2c + SetConfig<C>, 168 BUS: I2c + SetConfig,
126{ 169{
127 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { 170 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
128 self.bus.lock(|bus| { 171 self.bus.lock(|bus| {
@@ -183,45 +226,3 @@ where
183 todo!() 226 todo!()
184 } 227 }
185} 228}
186
187impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cBusDevice<'_, M, BUS>
188where
189 M: RawMutex,
190 BUS: embedded_hal_02::blocking::i2c::Write<Error = E>,
191{
192 type Error = I2cBusDeviceError<E>;
193
194 fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> Result<(), Self::Error> {
195 self.bus
196 .lock(|bus| bus.borrow_mut().write(addr, bytes).map_err(I2cBusDeviceError::I2c))
197 }
198}
199
200impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cBusDevice<'_, M, BUS>
201where
202 M: RawMutex,
203 BUS: embedded_hal_02::blocking::i2c::Read<Error = E>,
204{
205 type Error = I2cBusDeviceError<E>;
206
207 fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> Result<(), Self::Error> {
208 self.bus
209 .lock(|bus| bus.borrow_mut().read(addr, bytes).map_err(I2cBusDeviceError::I2c))
210 }
211}
212
213impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cBusDevice<'_, M, BUS>
214where
215 M: RawMutex,
216 BUS: embedded_hal_02::blocking::i2c::WriteRead<Error = E>,
217{
218 type Error = I2cBusDeviceError<E>;
219
220 fn write_read<'w>(&mut self, addr: u8, bytes: &'w [u8], buffer: &'w mut [u8]) -> Result<(), Self::Error> {
221 self.bus.lock(|bus| {
222 bus.borrow_mut()
223 .write_read(addr, bytes, buffer)
224 .map_err(I2cBusDeviceError::I2c)
225 })
226 }
227}