diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-07-06 14:59:29 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-07-06 14:59:29 +0000 |
| commit | 6baddaf53982b75149cb7e91280c571f7fe2e7bc (patch) | |
| tree | 24ab8956f759f1eb148c7d88673a93a6f342ad62 /embassy-embedded-hal/src | |
| parent | 5fef527764f1b694a9213050f4a187339dcc049b (diff) | |
| parent | 455374b7f9da0ebd55849e47d7800e313197cb33 (diff) | |
Merge #845
845: Add blocking shared bus for i2c and SPI r=Dirbaio a=kalkyl
Blocking versions of the shared buses
Co-authored-by: Henrik AlseĢr <[email protected]>
Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'embassy-embedded-hal/src')
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 84 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/mod.rs | 3 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 57 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/mod.rs | 6 |
4 files changed, 148 insertions, 2 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs new file mode 100644 index 000000000..2c762fe14 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | //! Blocking shared I2C bus | ||
| 2 | use core::cell::RefCell; | ||
| 3 | |||
| 4 | use embassy::blocking_mutex::raw::RawMutex; | ||
| 5 | use embassy::blocking_mutex::Mutex; | ||
| 6 | use embedded_hal_1::i2c::blocking::{I2c, Operation}; | ||
| 7 | use embedded_hal_1::i2c::ErrorType; | ||
| 8 | |||
| 9 | use crate::shared_bus::i2c::I2cBusDeviceError; | ||
| 10 | |||
| 11 | pub struct I2cBusDevice<'a, M: RawMutex, BUS> { | ||
| 12 | bus: &'a Mutex<M, RefCell<BUS>>, | ||
| 13 | } | ||
| 14 | |||
| 15 | impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { | ||
| 16 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>) -> Self { | ||
| 17 | Self { bus } | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | impl<'a, M: RawMutex, BUS> ErrorType for I2cBusDevice<'a, M, BUS> | ||
| 22 | where | ||
| 23 | BUS: ErrorType, | ||
| 24 | { | ||
| 25 | type Error = I2cBusDeviceError<BUS::Error>; | ||
| 26 | } | ||
| 27 | |||
| 28 | impl<M, BUS> I2c for I2cBusDevice<'_, M, BUS> | ||
| 29 | where | ||
| 30 | M: RawMutex, | ||
| 31 | BUS: I2c, | ||
| 32 | { | ||
| 33 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 34 | self.bus | ||
| 35 | .lock(|bus| bus.borrow_mut().read(address, buffer).map_err(I2cBusDeviceError::I2c)) | ||
| 36 | } | ||
| 37 | |||
| 38 | fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 39 | self.bus | ||
| 40 | .lock(|bus| bus.borrow_mut().write(address, bytes).map_err(I2cBusDeviceError::I2c)) | ||
| 41 | } | ||
| 42 | |||
| 43 | fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 44 | self.bus.lock(|bus| { | ||
| 45 | bus.borrow_mut() | ||
| 46 | .write_read(address, wr_buffer, rd_buffer) | ||
| 47 | .map_err(I2cBusDeviceError::I2c) | ||
| 48 | }) | ||
| 49 | } | ||
| 50 | |||
| 51 | fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> { | ||
| 52 | let _ = address; | ||
| 53 | let _ = operations; | ||
| 54 | todo!() | ||
| 55 | } | ||
| 56 | |||
| 57 | fn write_iter<B: IntoIterator<Item = u8>>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> { | ||
| 58 | let _ = addr; | ||
| 59 | let _ = bytes; | ||
| 60 | todo!() | ||
| 61 | } | ||
| 62 | |||
| 63 | fn write_iter_read<B: IntoIterator<Item = u8>>( | ||
| 64 | &mut self, | ||
| 65 | addr: u8, | ||
| 66 | bytes: B, | ||
| 67 | buffer: &mut [u8], | ||
| 68 | ) -> Result<(), Self::Error> { | ||
| 69 | let _ = addr; | ||
| 70 | let _ = bytes; | ||
| 71 | let _ = buffer; | ||
| 72 | todo!() | ||
| 73 | } | ||
| 74 | |||
| 75 | fn transaction_iter<'a, O: IntoIterator<Item = Operation<'a>>>( | ||
| 76 | &mut self, | ||
| 77 | address: u8, | ||
| 78 | operations: O, | ||
| 79 | ) -> Result<(), Self::Error> { | ||
| 80 | let _ = address; | ||
| 81 | let _ = operations; | ||
| 82 | todo!() | ||
| 83 | } | ||
| 84 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/mod.rs b/embassy-embedded-hal/src/shared_bus/blocking/mod.rs new file mode 100644 index 000000000..c2063ed2e --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/blocking/mod.rs | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | //! Blocking shared bus implementations for embedded-hal | ||
| 2 | pub mod i2c; | ||
| 3 | pub mod spi; | ||
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs new file mode 100644 index 000000000..c08bcbf62 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | //! Blocking shared SPI bus | ||
| 2 | use core::cell::RefCell; | ||
| 3 | |||
| 4 | use embassy::blocking_mutex::raw::RawMutex; | ||
| 5 | use embassy::blocking_mutex::Mutex; | ||
| 6 | use embedded_hal_1::digital::blocking::OutputPin; | ||
| 7 | use embedded_hal_1::spi; | ||
| 8 | use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; | ||
| 9 | |||
| 10 | use crate::shared_bus::spi::SpiBusDeviceError; | ||
| 11 | |||
| 12 | pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { | ||
| 13 | bus: &'a Mutex<M, RefCell<BUS>>, | ||
| 14 | cs: CS, | ||
| 15 | } | ||
| 16 | |||
| 17 | impl<'a, M: RawMutex, BUS, CS> SpiBusDevice<'a, M, BUS, CS> { | ||
| 18 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, cs: CS) -> Self { | ||
| 19 | Self { bus, cs } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | impl<'a, M: RawMutex, BUS, CS> spi::ErrorType for SpiBusDevice<'a, M, BUS, CS> | ||
| 24 | where | ||
| 25 | BUS: spi::ErrorType, | ||
| 26 | CS: OutputPin, | ||
| 27 | { | ||
| 28 | type Error = SpiBusDeviceError<BUS::Error, CS::Error>; | ||
| 29 | } | ||
| 30 | |||
| 31 | impl<BUS, M, CS> SpiDevice for SpiBusDevice<'_, M, BUS, CS> | ||
| 32 | where | ||
| 33 | M: RawMutex, | ||
| 34 | BUS: SpiBusFlush, | ||
| 35 | CS: OutputPin, | ||
| 36 | { | ||
| 37 | type Bus = BUS; | ||
| 38 | |||
| 39 | fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> { | ||
| 40 | self.bus.lock(|bus| { | ||
| 41 | let mut bus = bus.borrow_mut(); | ||
| 42 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 43 | |||
| 44 | let f_res = f(&mut bus); | ||
| 45 | |||
| 46 | // On failure, it's important to still flush and deassert CS. | ||
| 47 | let flush_res = bus.flush(); | ||
| 48 | let cs_res = self.cs.set_high(); | ||
| 49 | |||
| 50 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 51 | flush_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 52 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 53 | |||
| 54 | Ok(f_res) | ||
| 55 | }) | ||
| 56 | } | ||
| 57 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index bd4fd2c31..cd748cac8 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs | |||
| @@ -1,4 +1,6 @@ | |||
| 1 | //! Shared bus implementations for embedded-hal-async | 1 | //! Shared bus implementations |
| 2 | 2 | pub mod blocking; | |
| 3 | /// Shared i2c bus implementation for embedded-hal-async | ||
| 3 | pub mod i2c; | 4 | pub mod i2c; |
| 5 | /// Shared SPI bus implementation for embedded-hal-async | ||
| 4 | pub mod spi; | 6 | pub mod spi; |
