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 | 84 |
1 files changed, 84 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 2c762fe14..6f5f07051 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | |||
| @@ -7,6 +7,7 @@ use embedded_hal_1::i2c::blocking::{I2c, Operation}; | |||
| 7 | use embedded_hal_1::i2c::ErrorType; | 7 | use embedded_hal_1::i2c::ErrorType; |
| 8 | 8 | ||
| 9 | use crate::shared_bus::i2c::I2cBusDeviceError; | 9 | use crate::shared_bus::i2c::I2cBusDeviceError; |
| 10 | use crate::SetConfig; | ||
| 10 | 11 | ||
| 11 | pub struct I2cBusDevice<'a, M: RawMutex, BUS> { | 12 | pub struct I2cBusDevice<'a, M: RawMutex, BUS> { |
| 12 | bus: &'a Mutex<M, RefCell<BUS>>, | 13 | bus: &'a Mutex<M, RefCell<BUS>>, |
| @@ -82,3 +83,86 @@ where | |||
| 82 | todo!() | 83 | todo!() |
| 83 | } | 84 | } |
| 84 | } | 85 | } |
| 86 | |||
| 87 | pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { | ||
| 88 | bus: &'a Mutex<M, RefCell<BUS>>, | ||
| 89 | config: C, | ||
| 90 | } | ||
| 91 | |||
| 92 | impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { | ||
| 93 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: C) -> Self { | ||
| 94 | Self { bus, config } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | impl<'a, M: RawMutex, BUS, C> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> | ||
| 99 | where | ||
| 100 | BUS: ErrorType, | ||
| 101 | { | ||
| 102 | type Error = I2cBusDeviceError<BUS::Error>; | ||
| 103 | } | ||
| 104 | |||
| 105 | impl<M, BUS, C> I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> | ||
| 106 | where | ||
| 107 | M: RawMutex, | ||
| 108 | BUS: I2c + SetConfig<C>, | ||
| 109 | { | ||
| 110 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 111 | self.bus.lock(|bus| { | ||
| 112 | let mut bus = bus.borrow_mut(); | ||
| 113 | bus.set_config(&self.config); | ||
| 114 | bus.read(address, buffer).map_err(I2cBusDeviceError::I2c) | ||
| 115 | }) | ||
| 116 | } | ||
| 117 | |||
| 118 | fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 119 | self.bus.lock(|bus| { | ||
| 120 | let mut bus = bus.borrow_mut(); | ||
| 121 | bus.set_config(&self.config); | ||
| 122 | bus.write(address, bytes).map_err(I2cBusDeviceError::I2c) | ||
| 123 | }) | ||
| 124 | } | ||
| 125 | |||
| 126 | fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 127 | self.bus.lock(|bus| { | ||
| 128 | let mut bus = bus.borrow_mut(); | ||
| 129 | bus.set_config(&self.config); | ||
| 130 | bus.write_read(address, wr_buffer, rd_buffer) | ||
| 131 | .map_err(I2cBusDeviceError::I2c) | ||
| 132 | }) | ||
| 133 | } | ||
| 134 | |||
| 135 | fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> { | ||
| 136 | let _ = address; | ||
| 137 | let _ = operations; | ||
| 138 | todo!() | ||
| 139 | } | ||
| 140 | |||
| 141 | fn write_iter<B: IntoIterator<Item = u8>>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> { | ||
| 142 | let _ = addr; | ||
| 143 | let _ = bytes; | ||
| 144 | todo!() | ||
| 145 | } | ||
| 146 | |||
| 147 | fn write_iter_read<B: IntoIterator<Item = u8>>( | ||
| 148 | &mut self, | ||
| 149 | addr: u8, | ||
| 150 | bytes: B, | ||
| 151 | buffer: &mut [u8], | ||
| 152 | ) -> Result<(), Self::Error> { | ||
| 153 | let _ = addr; | ||
| 154 | let _ = bytes; | ||
| 155 | let _ = buffer; | ||
| 156 | todo!() | ||
| 157 | } | ||
| 158 | |||
| 159 | fn transaction_iter<'a, O: IntoIterator<Item = Operation<'a>>>( | ||
| 160 | &mut self, | ||
| 161 | address: u8, | ||
| 162 | operations: O, | ||
| 163 | ) -> Result<(), Self::Error> { | ||
| 164 | let _ = address; | ||
| 165 | let _ = operations; | ||
| 166 | todo!() | ||
| 167 | } | ||
| 168 | } | ||
