From 264b32d71baf471d0d36a34cc48aa10d429bed04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Alse=CC=81r?= Date: Wed, 6 Jul 2022 02:35:46 +0200 Subject: [PATCH 1/4] Add blocking shared bus for i2c and SPI --- .../src/shared_bus/blocking/i2c.rs | 69 +++++++++++++++++++ .../src/shared_bus/blocking/mod.rs | 3 + .../src/shared_bus/blocking/spi.rs | 69 +++++++++++++++++++ embassy-embedded-hal/src/shared_bus/mod.rs | 6 +- 4 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 embassy-embedded-hal/src/shared_bus/blocking/i2c.rs create mode 100644 embassy-embedded-hal/src/shared_bus/blocking/mod.rs create mode 100644 embassy-embedded-hal/src/shared_bus/blocking/spi.rs 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 00000000..2a6ea6dc --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -0,0 +1,69 @@ +//! Blocking shared I2C bus +use core::cell::RefCell; +use core::fmt::Debug; +use core::future::Future; + +use embedded_hal_1::i2c; + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub enum I2cBusDeviceError { + I2c(BUS), +} + +impl i2c::Error for I2cBusDeviceError +where + BUS: i2c::Error + Debug, +{ + fn kind(&self) -> i2c::ErrorKind { + match self { + Self::I2c(e) => e.kind(), + } + } +} + +pub struct I2cBusDevice<'a, BUS> { + bus: &'a RefCell, +} + +impl<'a, BUS> I2cBusDevice<'a, BUS> { + pub fn new(bus: &'a RefCell) -> Self { + Self { bus } + } +} + +impl<'a, BUS> i2c::ErrorType for I2cBusDevice<'a, BUS> +where + BUS: i2c::ErrorType, +{ + type Error = I2cBusDeviceError; +} + +impl i2c::I2c for I2cBusDevice<'_, BUS> +where + BUS: i2c::I2c, +{ + fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + let mut bus = self.bus.borrow_mut(); + bus.read(address, buffer).map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + + fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { + let mut bus = self.bus.borrow_mut(); + bus.write(address, bytes).map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + + fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { + let mut bus = self.bus.borrow_mut(); + bus.write_read(address, wr_buffer, rd_buffer) + .map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + + fn transaction<'a>(&mut self, address: u8, operations: &mut [i2c::Operation<'a>]) -> Result<(), Self::Error> { + let _ = address; + let _ = operations; + todo!() + } +} 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 00000000..c2063ed2 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/blocking/mod.rs @@ -0,0 +1,3 @@ +//! Blocking shared bus implementations for embedded-hal +pub mod i2c; +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 00000000..0d01a590 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -0,0 +1,69 @@ +//! Blocking shared SPI bus +use core::cell::RefCell; +use core::fmt::Debug; + +use embedded_hal_1::digital::blocking::OutputPin; +use embedded_hal_1::spi; +use embedded_hal_1::spi::blocking::SpiDevice; + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub enum SpiBusDeviceError { + Spi(BUS), + Cs(CS), +} + +impl spi::Error for SpiBusDeviceError +where + BUS: spi::Error + Debug, + CS: Debug, +{ + fn kind(&self) -> spi::ErrorKind { + match self { + Self::Spi(e) => e.kind(), + Self::Cs(_) => spi::ErrorKind::Other, + } + } +} + +pub struct SpiBusDevice<'a, BUS, CS> { + bus: &'a RefCell, + cs: CS, +} + +impl<'a, BUS, CS> SpiBusDevice<'a, BUS, CS> { + pub fn new(bus: &'a RefCell, cs: CS) -> Self { + Self { bus, cs } + } +} + +impl<'a, BUS, CS> spi::ErrorType for SpiBusDevice<'a, BUS, CS> +where + BUS: spi::ErrorType, + CS: OutputPin, +{ + type Error = SpiBusDeviceError; +} + +impl spi::SpiDevice for SpiBusDevice<'_, BUS, CS> +where + BUS: spi::SpiBusFlush, + CS: OutputPin, +{ + type Bus = BUS; + fn transaction(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result) -> Result { + let mut bus = self.bus.borrow_mut(); + self.cs.set_low().map_err(SpiDeviceWithCsError::Cs)?; + + let f_res = f(&mut bus); + + // On failure, it's important to still flush and deassert CS. + let flush_res = bus.flush(); + let cs_res = self.cs.set_high(); + + let f_res = f_res.map_err(SpiDeviceWithCsError::Spi)?; + flush_res.map_err(SpiDeviceWithCsError::Spi)?; + cs_res.map_err(SpiDeviceWithCsError::Cs)?; + + Ok(f_res) + } +} diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index bd4fd2c3..cd748cac 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs @@ -1,4 +1,6 @@ -//! Shared bus implementations for embedded-hal-async - +//! Shared bus implementations +pub mod blocking; +/// Shared i2c bus implementation for embedded-hal-async pub mod i2c; +/// Shared SPI bus implementation for embedded-hal-async pub mod spi; From d3d82ad87d9f170e4858ae30143fe44ab5ddd0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Alse=CC=81r?= Date: Wed, 6 Jul 2022 03:31:21 +0200 Subject: [PATCH 2/4] Mutex --- .../src/shared_bus/blocking/i2c.rs | 91 +++++++++++-------- .../src/shared_bus/blocking/spi.rs | 14 +-- 2 files changed, 60 insertions(+), 45 deletions(-) diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 2a6ea6dc..2c762fe1 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -1,67 +1,82 @@ //! Blocking shared I2C bus use core::cell::RefCell; -use core::fmt::Debug; -use core::future::Future; -use embedded_hal_1::i2c; +use embassy::blocking_mutex::raw::RawMutex; +use embassy::blocking_mutex::Mutex; +use embedded_hal_1::i2c::blocking::{I2c, Operation}; +use embedded_hal_1::i2c::ErrorType; -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub enum I2cBusDeviceError { - I2c(BUS), +use crate::shared_bus::i2c::I2cBusDeviceError; + +pub struct I2cBusDevice<'a, M: RawMutex, BUS> { + bus: &'a Mutex>, } -impl i2c::Error for I2cBusDeviceError -where - BUS: i2c::Error + Debug, -{ - fn kind(&self) -> i2c::ErrorKind { - match self { - Self::I2c(e) => e.kind(), - } - } -} - -pub struct I2cBusDevice<'a, BUS> { - bus: &'a RefCell, -} - -impl<'a, BUS> I2cBusDevice<'a, BUS> { - pub fn new(bus: &'a RefCell) -> Self { +impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { + pub fn new(bus: &'a Mutex>) -> Self { Self { bus } } } -impl<'a, BUS> i2c::ErrorType for I2cBusDevice<'a, BUS> +impl<'a, M: RawMutex, BUS> ErrorType for I2cBusDevice<'a, M, BUS> where - BUS: i2c::ErrorType, + BUS: ErrorType, { type Error = I2cBusDeviceError; } -impl i2c::I2c for I2cBusDevice<'_, BUS> +impl I2c for I2cBusDevice<'_, M, BUS> where - BUS: i2c::I2c, + M: RawMutex, + BUS: I2c, { fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { - let mut bus = self.bus.borrow_mut(); - bus.read(address, buffer).map_err(I2cBusDeviceError::I2c)?; - Ok(()) + self.bus + .lock(|bus| bus.borrow_mut().read(address, buffer).map_err(I2cBusDeviceError::I2c)) } fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { - let mut bus = self.bus.borrow_mut(); - bus.write(address, bytes).map_err(I2cBusDeviceError::I2c)?; - Ok(()) + self.bus + .lock(|bus| bus.borrow_mut().write(address, bytes).map_err(I2cBusDeviceError::I2c)) } fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { - let mut bus = self.bus.borrow_mut(); - bus.write_read(address, wr_buffer, rd_buffer) - .map_err(I2cBusDeviceError::I2c)?; - Ok(()) + self.bus.lock(|bus| { + bus.borrow_mut() + .write_read(address, wr_buffer, rd_buffer) + .map_err(I2cBusDeviceError::I2c) + }) } - fn transaction<'a>(&mut self, address: u8, operations: &mut [i2c::Operation<'a>]) -> Result<(), Self::Error> { + fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> { + let _ = address; + let _ = operations; + todo!() + } + + fn write_iter>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> { + let _ = addr; + let _ = bytes; + todo!() + } + + fn write_iter_read>( + &mut self, + addr: u8, + bytes: B, + buffer: &mut [u8], + ) -> Result<(), Self::Error> { + let _ = addr; + let _ = bytes; + let _ = buffer; + todo!() + } + + fn transaction_iter<'a, O: IntoIterator>>( + &mut self, + address: u8, + operations: O, + ) -> Result<(), Self::Error> { let _ = address; let _ = operations; todo!() diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index 0d01a590..2583c699 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -4,7 +4,7 @@ use core::fmt::Debug; use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi; -use embedded_hal_1::spi::blocking::SpiDevice; +use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum SpiBusDeviceError { @@ -44,15 +44,15 @@ where type Error = SpiBusDeviceError; } -impl spi::SpiDevice for SpiBusDevice<'_, BUS, CS> +impl SpiDevice for SpiBusDevice<'_, BUS, CS> where - BUS: spi::SpiBusFlush, + BUS: SpiBusFlush, CS: OutputPin, { type Bus = BUS; fn transaction(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result) -> Result { let mut bus = self.bus.borrow_mut(); - self.cs.set_low().map_err(SpiDeviceWithCsError::Cs)?; + self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; let f_res = f(&mut bus); @@ -60,9 +60,9 @@ where let flush_res = bus.flush(); let cs_res = self.cs.set_high(); - let f_res = f_res.map_err(SpiDeviceWithCsError::Spi)?; - flush_res.map_err(SpiDeviceWithCsError::Spi)?; - cs_res.map_err(SpiDeviceWithCsError::Cs)?; + let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; + flush_res.map_err(SpiBusDeviceError::Spi)?; + cs_res.map_err(SpiBusDeviceError::Cs)?; Ok(f_res) } From ba2412ff7ea1eeb5eb1a09dad4399214f0e22d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Alse=CC=81r?= Date: Wed, 6 Jul 2022 03:47:55 +0200 Subject: [PATCH 3/4] Mutex for SPI --- .../src/shared_bus/blocking/spi.rs | 47 +++++++------------ 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index 2583c699..cf9b55db 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -1,42 +1,26 @@ //! Blocking shared SPI bus use core::cell::RefCell; -use core::fmt::Debug; +use embassy::blocking_mutex::raw::RawMutex; +use embassy::blocking_mutex::Mutex; use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi; use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub enum SpiBusDeviceError { - Spi(BUS), - Cs(CS), -} +use crate::shared_bus::spi::SpiBusDeviceError; -impl spi::Error for SpiBusDeviceError -where - BUS: spi::Error + Debug, - CS: Debug, -{ - fn kind(&self) -> spi::ErrorKind { - match self { - Self::Spi(e) => e.kind(), - Self::Cs(_) => spi::ErrorKind::Other, - } - } -} - -pub struct SpiBusDevice<'a, BUS, CS> { - bus: &'a RefCell, +pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { + bus: &'a Mutex>, cs: CS, } -impl<'a, BUS, CS> SpiBusDevice<'a, BUS, CS> { - pub fn new(bus: &'a RefCell, cs: CS) -> Self { +impl<'a, M: RawMutex, BUS, CS> SpiBusDevice<'a, M, BUS, CS> { + pub fn new(bus: &'a Mutex>, cs: CS) -> Self { Self { bus, cs } } } -impl<'a, BUS, CS> spi::ErrorType for SpiBusDevice<'a, BUS, CS> +impl<'a, M: RawMutex, BUS, CS> spi::ErrorType for SpiBusDevice<'a, M, BUS, CS> where BUS: spi::ErrorType, CS: OutputPin, @@ -44,20 +28,25 @@ where type Error = SpiBusDeviceError; } -impl SpiDevice for SpiBusDevice<'_, BUS, CS> +impl SpiDevice for SpiBusDevice<'_, M, BUS, CS> where + M: RawMutex, BUS: SpiBusFlush, CS: OutputPin, { type Bus = BUS; + fn transaction(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result) -> Result { - let mut bus = self.bus.borrow_mut(); self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; - let f_res = f(&mut bus); + let (f_res, flush_res) = self.bus.lock(|bus| { + let mut bus = bus.borrow_mut(); + let f_res = f(&mut bus); + // On failure, it's important to still flush and deassert CS. + let flush_res = bus.flush(); + (f_res, flush_res) + }); - // On failure, it's important to still flush and deassert CS. - let flush_res = bus.flush(); let cs_res = self.cs.set_high(); let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; From 455374b7f9da0ebd55849e47d7800e313197cb33 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 6 Jul 2022 16:57:29 +0200 Subject: [PATCH 4/4] spi shared bus: assert/deassert CS inside the lock. --- .../src/shared_bus/blocking/spi.rs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index cf9b55db..c08bcbf6 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -37,22 +37,21 @@ where type Bus = BUS; fn transaction(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result) -> Result { - self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; - - let (f_res, flush_res) = self.bus.lock(|bus| { + self.bus.lock(|bus| { let mut bus = bus.borrow_mut(); + self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; + let f_res = f(&mut bus); + // On failure, it's important to still flush and deassert CS. let flush_res = bus.flush(); - (f_res, flush_res) - }); + let cs_res = self.cs.set_high(); - let cs_res = self.cs.set_high(); + let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; + flush_res.map_err(SpiBusDeviceError::Spi)?; + cs_res.map_err(SpiBusDeviceError::Cs)?; - let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; - flush_res.map_err(SpiBusDeviceError::Spi)?; - cs_res.map_err(SpiBusDeviceError::Cs)?; - - Ok(f_res) + Ok(f_res) + }) } }