Add blocking shared bus for i2c and SPI
This commit is contained in:
parent
5fef527764
commit
264b32d71b
4 changed files with 145 additions and 2 deletions
69
embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
Normal file
69
embassy-embedded-hal/src/shared_bus/blocking/i2c.rs
Normal file
|
@ -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<BUS> {
|
||||||
|
I2c(BUS),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BUS> i2c::Error for I2cBusDeviceError<BUS>
|
||||||
|
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<BUS>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, BUS> I2cBusDevice<'a, BUS> {
|
||||||
|
pub fn new(bus: &'a RefCell<BUS>) -> Self {
|
||||||
|
Self { bus }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, BUS> i2c::ErrorType for I2cBusDevice<'a, BUS>
|
||||||
|
where
|
||||||
|
BUS: i2c::ErrorType,
|
||||||
|
{
|
||||||
|
type Error = I2cBusDeviceError<BUS::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M, BUS> 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!()
|
||||||
|
}
|
||||||
|
}
|
3
embassy-embedded-hal/src/shared_bus/blocking/mod.rs
Normal file
3
embassy-embedded-hal/src/shared_bus/blocking/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
//! Blocking shared bus implementations for embedded-hal
|
||||||
|
pub mod i2c;
|
||||||
|
pub mod spi;
|
69
embassy-embedded-hal/src/shared_bus/blocking/spi.rs
Normal file
69
embassy-embedded-hal/src/shared_bus/blocking/spi.rs
Normal file
|
@ -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<BUS, CS> {
|
||||||
|
Spi(BUS),
|
||||||
|
Cs(CS),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BUS, CS> spi::Error for SpiBusDeviceError<BUS, CS>
|
||||||
|
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<BUS>,
|
||||||
|
cs: CS,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, BUS, CS> SpiBusDevice<'a, BUS, CS> {
|
||||||
|
pub fn new(bus: &'a RefCell<BUS>, 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<BUS::Error, CS::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BUS, CS> spi::SpiDevice for SpiBusDevice<'_, BUS, CS>
|
||||||
|
where
|
||||||
|
BUS: spi::SpiBusFlush,
|
||||||
|
CS: OutputPin,
|
||||||
|
{
|
||||||
|
type Bus = BUS;
|
||||||
|
fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
pub mod i2c;
|
||||||
|
/// Shared SPI bus implementation for embedded-hal-async
|
||||||
pub mod spi;
|
pub mod spi;
|
||||||
|
|
Loading…
Reference in a new issue