diff options
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/blocking/i2c.rs')
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 6f5f07051..12c2a1f4b 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | |||
| @@ -1,4 +1,21 @@ | |||
| 1 | //! Blocking shared I2C bus | 1 | //! Blocking shared I2C bus |
| 2 | //! | ||
| 3 | //! # Example (nrf52) | ||
| 4 | //! | ||
| 5 | //! ```rust | ||
| 6 | //! use embassy_embedded_hal::shared_bus::blocking::i2c::I2cBusDevice; | ||
| 7 | //! use embassy::blocking_mutex::{NoopMutex, raw::NoopRawMutex}; | ||
| 8 | //! | ||
| 9 | //! static I2C_BUS: Forever<NoopMutex<RefCell<Twim<TWISPI0>>>> = Forever::new(); | ||
| 10 | //! let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | ||
| 11 | //! let i2c = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, Config::default()); | ||
| 12 | //! let i2c_bus = NoopMutex::new(RefCell::new(i2c)); | ||
| 13 | //! let i2c_bus = I2C_BUS.put(i2c_bus); | ||
| 14 | //! | ||
| 15 | //! let i2c_dev1 = I2cBusDevice::new(i2c_bus); | ||
| 16 | //! let mpu = Mpu6050::new(i2c_dev1); | ||
| 17 | //! ``` | ||
| 18 | |||
| 2 | use core::cell::RefCell; | 19 | use core::cell::RefCell; |
| 3 | 20 | ||
| 4 | use embassy::blocking_mutex::raw::RawMutex; | 21 | use embassy::blocking_mutex::raw::RawMutex; |
| @@ -84,6 +101,7 @@ where | |||
| 84 | } | 101 | } |
| 85 | } | 102 | } |
| 86 | 103 | ||
| 104 | <<<<<<< HEAD | ||
| 87 | pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { | 105 | pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { |
| 88 | bus: &'a Mutex<M, RefCell<BUS>>, | 106 | bus: &'a Mutex<M, RefCell<BUS>>, |
| 89 | config: C, | 107 | config: C, |
| @@ -165,4 +183,46 @@ where | |||
| 165 | let _ = operations; | 183 | let _ = operations; |
| 166 | todo!() | 184 | todo!() |
| 167 | } | 185 | } |
| 186 | ======= | ||
| 187 | impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cBusDevice<'_, M, BUS> | ||
| 188 | where | ||
| 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 | |||
| 200 | impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cBusDevice<'_, M, BUS> | ||
| 201 | where | ||
| 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 | |||
| 213 | impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cBusDevice<'_, M, BUS> | ||
| 214 | where | ||
| 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 | >>>>>>> master | ||
| 168 | } | 228 | } |
