diff options
Diffstat (limited to 'embassy-embedded-hal/src')
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 60 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 58 |
2 files changed, 118 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 | } |
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index d54ca6bfa..b31efd64b 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |||
| @@ -1,4 +1,23 @@ | |||
| 1 | //! Blocking shared SPI bus | 1 | //! Blocking shared SPI bus |
| 2 | //! | ||
| 3 | //! # Example (nrf52) | ||
| 4 | //! | ||
| 5 | //! ```rust | ||
| 6 | //! use embassy_embedded_hal::shared_bus::blocking::spi::SpiBusDevice; | ||
| 7 | //! use embassy::blocking_mutex::{NoopMutex, raw::NoopRawMutex}; | ||
| 8 | //! | ||
| 9 | //! static SPI_BUS: Forever<NoopMutex<RefCell<Spim<SPI3>>>> = Forever::new(); | ||
| 10 | //! let irq = interrupt::take!(SPIM3); | ||
| 11 | //! let spi = Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, Config::default()); | ||
| 12 | //! let spi_bus = NoopMutex::new(RefCell::new(spi)); | ||
| 13 | //! let spi_bus = SPI_BUS.put(spi_bus); | ||
| 14 | //! | ||
| 15 | //! // Device 1, using embedded-hal compatible driver for ST7735 LCD display | ||
| 16 | //! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); | ||
| 17 | //! let spi_dev1 = SpiBusDevice::new(spi_bus, cs_pin1); | ||
| 18 | //! let display1 = ST7735::new(spi_dev1, dc1, rst1, Default::default(), false, 160, 128); | ||
| 19 | //! ``` | ||
| 20 | |||
| 2 | use core::cell::RefCell; | 21 | use core::cell::RefCell; |
| 3 | 22 | ||
| 4 | use embassy::blocking_mutex::raw::RawMutex; | 23 | use embassy::blocking_mutex::raw::RawMutex; |
| @@ -57,6 +76,7 @@ where | |||
| 57 | } | 76 | } |
| 58 | } | 77 | } |
| 59 | 78 | ||
| 79 | <<<<<<< HEAD | ||
| 60 | pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { | 80 | pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { |
| 61 | bus: &'a Mutex<M, RefCell<BUS>>, | 81 | bus: &'a Mutex<M, RefCell<BUS>>, |
| 62 | cs: CS, | 82 | cs: CS, |
| @@ -101,6 +121,44 @@ where | |||
| 101 | flush_res.map_err(SpiBusDeviceError::Spi)?; | 121 | flush_res.map_err(SpiBusDeviceError::Spi)?; |
| 102 | cs_res.map_err(SpiBusDeviceError::Cs)?; | 122 | cs_res.map_err(SpiBusDeviceError::Cs)?; |
| 103 | 123 | ||
| 124 | ======= | ||
| 125 | impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer<u8> for SpiBusDevice<'_, M, BUS, CS> | ||
| 126 | where | ||
| 127 | M: RawMutex, | ||
| 128 | BUS: embedded_hal_02::blocking::spi::Transfer<u8, Error = BusErr>, | ||
| 129 | CS: OutputPin<Error = CsErr>, | ||
| 130 | { | ||
| 131 | type Error = SpiBusDeviceError<BusErr, CsErr>; | ||
| 132 | fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { | ||
| 133 | self.bus.lock(|bus| { | ||
| 134 | let mut bus = bus.borrow_mut(); | ||
| 135 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 136 | let f_res = bus.transfer(words); | ||
| 137 | let cs_res = self.cs.set_high(); | ||
| 138 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 139 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 140 | Ok(f_res) | ||
| 141 | }) | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Write<u8> for SpiBusDevice<'_, M, BUS, CS> | ||
| 146 | where | ||
| 147 | M: RawMutex, | ||
| 148 | BUS: embedded_hal_02::blocking::spi::Write<u8, Error = BusErr>, | ||
| 149 | CS: OutputPin<Error = CsErr>, | ||
| 150 | { | ||
| 151 | type Error = SpiBusDeviceError<BusErr, CsErr>; | ||
| 152 | |||
| 153 | fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { | ||
| 154 | self.bus.lock(|bus| { | ||
| 155 | let mut bus = bus.borrow_mut(); | ||
| 156 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 157 | let f_res = bus.write(words); | ||
| 158 | let cs_res = self.cs.set_high(); | ||
| 159 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 160 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 161 | >>>>>>> master | ||
| 104 | Ok(f_res) | 162 | Ok(f_res) |
| 105 | }) | 163 | }) |
| 106 | } | 164 | } |
