2021-12-17 12:50:48 +01:00
|
|
|
use core::future::Future;
|
2022-01-26 22:39:06 +01:00
|
|
|
use embedded_hal_02::blocking;
|
|
|
|
use embedded_hal_02::serial;
|
2021-12-17 12:50:48 +01:00
|
|
|
|
|
|
|
/// BlockingAsync is a wrapper that implements async traits using blocking peripherals. This allows
|
|
|
|
/// driver writers to depend on the async traits while still supporting embedded-hal peripheral implementations.
|
|
|
|
///
|
|
|
|
/// BlockingAsync will implement any async trait that maps to embedded-hal traits implemented for the wrapped driver.
|
|
|
|
///
|
|
|
|
/// Driver users are then free to choose which implementation that is available to them.
|
|
|
|
pub struct BlockingAsync<T> {
|
|
|
|
wrapped: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> BlockingAsync<T> {
|
|
|
|
/// Create a new instance of a wrapper for a given peripheral.
|
|
|
|
pub fn new(wrapped: T) -> Self {
|
|
|
|
Self { wrapped }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2022-01-26 22:39:06 +01:00
|
|
|
// I2C implementations
|
2021-12-17 12:50:48 +01:00
|
|
|
//
|
2022-01-26 22:39:06 +01:00
|
|
|
impl<T, E> embedded_hal_1::i2c::ErrorType for BlockingAsync<T>
|
2021-12-17 12:50:48 +01:00
|
|
|
where
|
2022-01-26 22:39:06 +01:00
|
|
|
E: embedded_hal_1::i2c::Error + 'static,
|
2021-12-17 12:50:48 +01:00
|
|
|
T: blocking::i2c::WriteRead<Error = E>
|
|
|
|
+ blocking::i2c::Read<Error = E>
|
|
|
|
+ blocking::i2c::Write<Error = E>,
|
|
|
|
{
|
|
|
|
type Error = E;
|
2022-01-26 22:39:06 +01:00
|
|
|
}
|
2021-12-17 12:50:48 +01:00
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
impl<T, E> embedded_hal_async::i2c::I2c for BlockingAsync<T>
|
|
|
|
where
|
|
|
|
E: embedded_hal_1::i2c::Error + 'static,
|
|
|
|
T: blocking::i2c::WriteRead<Error = E>
|
|
|
|
+ blocking::i2c::Read<Error = E>
|
|
|
|
+ blocking::i2c::Write<Error = E>,
|
|
|
|
{
|
|
|
|
type WriteFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
type ReadFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
type WriteReadFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
2021-12-17 12:50:48 +01:00
|
|
|
|
|
|
|
fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
|
|
|
async move { self.wrapped.read(address, buffer) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
async move { self.wrapped.write(address, bytes) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_read<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
address: u8,
|
|
|
|
bytes: &'a [u8],
|
|
|
|
buffer: &'a mut [u8],
|
|
|
|
) -> Self::WriteReadFuture<'a> {
|
|
|
|
async move { self.wrapped.write_read(address, bytes, buffer) }
|
|
|
|
}
|
2022-01-26 22:39:06 +01:00
|
|
|
|
|
|
|
type TransactionFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
|
|
|
|
fn transaction<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
address: u8,
|
|
|
|
operations: &mut [embedded_hal_async::i2c::Operation<'a>],
|
|
|
|
) -> Self::TransactionFuture<'a> {
|
|
|
|
let _ = address;
|
|
|
|
let _ = operations;
|
|
|
|
async move { todo!() }
|
|
|
|
}
|
2021-12-17 12:50:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// SPI implementatinos
|
|
|
|
//
|
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
impl<T, E> embedded_hal_async::spi::ErrorType for BlockingAsync<T>
|
2021-12-17 12:50:48 +01:00
|
|
|
where
|
2022-01-26 22:39:06 +01:00
|
|
|
E: embedded_hal_1::spi::Error,
|
|
|
|
T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
|
2021-12-17 12:50:48 +01:00
|
|
|
{
|
|
|
|
type Error = E;
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
impl<T, E> embedded_hal_async::spi::ReadWrite<u8> for BlockingAsync<T>
|
2021-12-17 12:50:48 +01:00
|
|
|
where
|
2022-01-26 22:39:06 +01:00
|
|
|
E: embedded_hal_1::spi::Error + 'static,
|
|
|
|
T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
|
2021-12-17 12:50:48 +01:00
|
|
|
{
|
2022-01-26 22:39:06 +01:00
|
|
|
type TransferFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
2021-12-17 12:50:48 +01:00
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Self::TransferFuture<'a> {
|
2021-12-17 12:50:48 +01:00
|
|
|
async move {
|
|
|
|
// Ensure we write the expected bytes
|
|
|
|
for i in 0..core::cmp::min(read.len(), write.len()) {
|
|
|
|
read[i] = write[i].clone();
|
|
|
|
}
|
|
|
|
self.wrapped.transfer(read)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 22:39:06 +01:00
|
|
|
|
|
|
|
type TransferInPlaceFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
|
|
|
|
fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> {
|
|
|
|
async move { todo!() }
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
|
|
|
|
fn transaction<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
_: &'a mut [embedded_hal_async::spi::Operation<'a, u8>],
|
|
|
|
) -> Self::TransactionFuture<'a> {
|
|
|
|
async move { todo!() }
|
|
|
|
}
|
2021-12-17 12:50:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
impl<T, E> embedded_hal_async::spi::Write<u8> for BlockingAsync<T>
|
2021-12-17 12:50:48 +01:00
|
|
|
where
|
2022-01-26 22:39:06 +01:00
|
|
|
E: embedded_hal_1::spi::Error + 'static,
|
|
|
|
T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
|
2021-12-17 12:50:48 +01:00
|
|
|
{
|
2022-01-26 22:39:06 +01:00
|
|
|
type WriteFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
|
|
|
|
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
async move {
|
|
|
|
self.wrapped.write(data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type WriteTransactionFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
2021-12-17 12:50:48 +01:00
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
fn write_transaction<'a>(&'a mut self, _: &'a [&'a [u8]]) -> Self::WriteTransactionFuture<'a> {
|
|
|
|
async move { todo!() }
|
2021-12-17 12:50:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
impl<T, E> embedded_hal_async::spi::Read<u8> for BlockingAsync<T>
|
2021-12-17 12:50:48 +01:00
|
|
|
where
|
2022-01-26 22:39:06 +01:00
|
|
|
E: embedded_hal_1::spi::Error + 'static,
|
|
|
|
T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
|
2021-12-17 12:50:48 +01:00
|
|
|
{
|
2022-01-26 22:39:06 +01:00
|
|
|
type ReadFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
2021-12-17 12:50:48 +01:00
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
2021-12-17 12:50:48 +01:00
|
|
|
async move {
|
|
|
|
self.wrapped.transfer(data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 22:39:06 +01:00
|
|
|
|
|
|
|
type ReadTransactionFuture<'a>
|
|
|
|
where
|
|
|
|
Self: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
|
|
|
|
fn read_transaction<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
_: &'a mut [&'a mut [u8]],
|
|
|
|
) -> Self::ReadTransactionFuture<'a> {
|
|
|
|
async move { todo!() }
|
|
|
|
}
|
2021-12-17 12:50:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uart implementatinos
|
2022-01-26 22:39:06 +01:00
|
|
|
impl<T, E> embedded_hal_1::serial::ErrorType for BlockingAsync<T>
|
|
|
|
where
|
|
|
|
T: serial::Read<u8, Error = E>,
|
|
|
|
E: embedded_hal_1::serial::Error + 'static,
|
|
|
|
{
|
|
|
|
type Error = E;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> embedded_hal_async::serial::Read for BlockingAsync<T>
|
2021-12-17 12:50:48 +01:00
|
|
|
where
|
2022-01-26 22:39:06 +01:00
|
|
|
T: serial::Read<u8, Error = E>,
|
|
|
|
E: embedded_hal_1::serial::Error + 'static,
|
2021-12-17 12:50:48 +01:00
|
|
|
{
|
2022-01-26 22:39:06 +01:00
|
|
|
type ReadFuture<'a>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
2021-12-17 12:50:48 +01:00
|
|
|
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
|
|
|
async move {
|
|
|
|
let mut pos = 0;
|
|
|
|
while pos < buf.len() {
|
|
|
|
match self.wrapped.read() {
|
|
|
|
Err(nb::Error::WouldBlock) => {}
|
2022-01-26 22:39:06 +01:00
|
|
|
Err(nb::Error::Other(e)) => return Err(e),
|
2021-12-17 12:50:48 +01:00
|
|
|
Ok(b) => {
|
|
|
|
buf[pos] = b;
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:39:06 +01:00
|
|
|
impl<T, E> embedded_hal_async::serial::Write for BlockingAsync<T>
|
2021-12-17 12:50:48 +01:00
|
|
|
where
|
2022-01-26 22:39:06 +01:00
|
|
|
T: blocking::serial::Write<u8, Error = E> + serial::Read<u8, Error = E>,
|
|
|
|
E: embedded_hal_1::serial::Error + 'static,
|
2021-12-17 12:50:48 +01:00
|
|
|
{
|
2022-01-26 22:39:06 +01:00
|
|
|
type WriteFuture<'a>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
2021-12-17 12:50:48 +01:00
|
|
|
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
|
2022-01-26 22:39:06 +01:00
|
|
|
async move { self.wrapped.bwrite_all(buf) }
|
|
|
|
}
|
|
|
|
|
|
|
|
type FlushFuture<'a>
|
|
|
|
where
|
|
|
|
T: 'a,
|
|
|
|
= impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
|
|
|
|
async move { self.wrapped.bflush() }
|
2021-12-17 12:50:48 +01:00
|
|
|
}
|
|
|
|
}
|