From e10fc2bada1c59420431f09a35f7aa09a5b45623 Mon Sep 17 00:00:00 2001 From: Henrik Alsér Date: Thu, 26 May 2022 18:54:58 +0200 Subject: Async shared bus for SPI & I2C + rename embassy-traits (#769) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Rename embassy-traits to embassy-embedded-hal * Rename embassy-traits to embassy-embedded-hal * Add shared bus for SPI and I2C * rustfmt * EHA alpha 1 * Rename embedded-traits in examples * rustfmt * rustfmt Co-authored-by: Henrik Alsér --- .vscode/settings.json | 8 +- embassy-embedded-hal/Cargo.toml | 17 ++ embassy-embedded-hal/src/adapter.rs | 248 +++++++++++++++++++++++++ embassy-embedded-hal/src/lib.rs | 6 + embassy-embedded-hal/src/shared_bus/i2c.rs | 120 ++++++++++++ embassy-embedded-hal/src/shared_bus/mod.rs | 4 + embassy-embedded-hal/src/shared_bus/spi.rs | 110 +++++++++++ embassy-traits/Cargo.toml | 16 -- embassy-traits/src/adapter.rs | 248 ------------------------- embassy-traits/src/lib.rs | 5 - examples/boot/nrf/Cargo.toml | 2 +- examples/boot/nrf/src/bin/a.rs | 2 +- examples/boot/stm32f3/Cargo.toml | 2 +- examples/boot/stm32f3/src/bin/a.rs | 2 +- examples/boot/stm32f7/Cargo.toml | 2 +- examples/boot/stm32f7/src/bin/a.rs | 2 +- examples/boot/stm32h7/Cargo.toml | 2 +- examples/boot/stm32h7/src/bin/a.rs | 2 +- examples/boot/stm32l0/Cargo.toml | 2 +- examples/boot/stm32l0/src/bin/a.rs | 2 +- examples/boot/stm32l1/Cargo.toml | 2 +- examples/boot/stm32l1/src/bin/a.rs | 2 +- examples/boot/stm32l4/Cargo.toml | 2 +- examples/boot/stm32l4/src/bin/a.rs | 2 +- examples/boot/stm32wl/Cargo.toml | 2 +- examples/boot/stm32wl/src/bin/a.rs | 2 +- examples/stm32l4/Cargo.toml | 2 +- examples/stm32l4/src/bin/i2c_blocking_async.rs | 2 +- examples/stm32l4/src/bin/spi_blocking_async.rs | 2 +- 29 files changed, 528 insertions(+), 292 deletions(-) create mode 100644 embassy-embedded-hal/Cargo.toml create mode 100644 embassy-embedded-hal/src/adapter.rs create mode 100644 embassy-embedded-hal/src/lib.rs create mode 100644 embassy-embedded-hal/src/shared_bus/i2c.rs create mode 100644 embassy-embedded-hal/src/shared_bus/mod.rs create mode 100644 embassy-embedded-hal/src/shared_bus/spi.rs delete mode 100644 embassy-traits/Cargo.toml delete mode 100644 embassy-traits/src/adapter.rs delete mode 100644 embassy-traits/src/lib.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 79433a7c9..5ce8e4e7d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,15 +1,11 @@ { "editor.formatOnSave": true, - "rust-analyzer.assist.importEnforceGranularity": true, - "rust-analyzer.assist.importGranularity": "module", "rust-analyzer.checkOnSave.allFeatures": false, "rust-analyzer.checkOnSave.allTargets": false, "rust-analyzer.checkOnSave.noDefaultFeatures": true, "rust-analyzer.cargo.allFeatures": false, "rust-analyzer.cargo.noDefaultFeatures": true, - "rust-analyzer.experimental.procAttrMacros": false, "rust-analyzer.procMacro.enable": true, - "rust-analyzer.cargo.runBuildScripts": true, "rust-analyzer.cargo.target": "thumbv7em-none-eabi", "rust-analyzer.cargo.features": [ // These are needed to prevent embassy-net from failing to build @@ -38,4 +34,8 @@ // "examples/stm32wl55/Cargo.toml", // "examples/wasm/Cargo.toml", ], + "rust-analyzer.imports.granularity.enforce": true, + "rust-analyzer.imports.granularity.group": "module", + "rust-analyzer.cargo.buildScripts.enable": true, + "rust-analyzer.procMacro.attributes.enable": false, } \ No newline at end of file diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml new file mode 100644 index 000000000..14b73dce5 --- /dev/null +++ b/embassy-embedded-hal/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "embassy-embedded-hal" +version = "0.1.0" +authors = ["Dario Nieuwenhuis "] +edition = "2018" + +[features] +std = [] + +[dependencies] +embassy = { version = "0.1.0", path = "../embassy" } +embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } +embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } +embedded-hal-async = { version = "0.1.0-alpha.1" } +embedded-storage = "0.3.0" +embedded-storage-async = "0.3.0" +nb = "1.0.0" \ No newline at end of file diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter.rs new file mode 100644 index 000000000..033efb7ef --- /dev/null +++ b/embassy-embedded-hal/src/adapter.rs @@ -0,0 +1,248 @@ +use core::future::Future; +use embedded_hal_02::blocking; +use embedded_hal_02::serial; + +/// 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 { + wrapped: T, +} + +impl BlockingAsync { + /// Create a new instance of a wrapper for a given peripheral. + pub fn new(wrapped: T) -> Self { + Self { wrapped } + } +} + +// +// I2C implementations +// +impl embedded_hal_1::i2c::ErrorType for BlockingAsync +where + E: embedded_hal_1::i2c::Error + 'static, + T: blocking::i2c::WriteRead + + blocking::i2c::Read + + blocking::i2c::Write, +{ + type Error = E; +} + +impl embedded_hal_async::i2c::I2c for BlockingAsync +where + E: embedded_hal_1::i2c::Error + 'static, + T: blocking::i2c::WriteRead + + blocking::i2c::Read + + blocking::i2c::Write, +{ + type WriteFuture<'a> = impl Future> + 'a where Self: 'a; + type ReadFuture<'a> = impl Future> + 'a where Self: 'a; + type WriteReadFuture<'a> = impl Future> + 'a where Self: 'a; + + 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) } + } + + type TransactionFuture<'a, 'b> = impl Future> + 'a where Self: 'a, 'b: 'a; + + fn transaction<'a, 'b>( + &'a mut self, + address: u8, + operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], + ) -> Self::TransactionFuture<'a, 'b> { + let _ = address; + let _ = operations; + async move { todo!() } + } +} + +// +// SPI implementatinos +// + +impl embedded_hal_async::spi::ErrorType for BlockingAsync +where + E: embedded_hal_1::spi::Error, + T: blocking::spi::Transfer + blocking::spi::Write, +{ + type Error = E; +} + +impl embedded_hal_async::spi::SpiBus for BlockingAsync +where + E: embedded_hal_1::spi::Error + 'static, + T: blocking::spi::Transfer + blocking::spi::Write, +{ + type TransferFuture<'a> = impl Future> + 'a where Self: 'a; + + fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Self::TransferFuture<'a> { + 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(()) + } + } + + type TransferInPlaceFuture<'a> = impl Future> + 'a where Self: 'a; + + fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> { + async move { todo!() } + } +} + +impl embedded_hal_async::spi::SpiBusFlush for BlockingAsync +where + E: embedded_hal_1::spi::Error + 'static, + T: blocking::spi::Transfer + blocking::spi::Write, +{ + type FlushFuture<'a> = impl Future> + 'a where Self: 'a; + + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { + async move { Ok(()) } + } +} + +impl embedded_hal_async::spi::SpiBusWrite for BlockingAsync +where + E: embedded_hal_1::spi::Error + 'static, + T: blocking::spi::Transfer + blocking::spi::Write, +{ + type WriteFuture<'a> = impl Future> + 'a where Self: 'a; + + fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { + async move { + self.wrapped.write(data)?; + Ok(()) + } + } +} + +impl embedded_hal_async::spi::SpiBusRead for BlockingAsync +where + E: embedded_hal_1::spi::Error + 'static, + T: blocking::spi::Transfer + blocking::spi::Write, +{ + type ReadFuture<'a> = impl Future> + 'a where Self: 'a; + + fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { + async move { + self.wrapped.transfer(data)?; + Ok(()) + } + } +} + +// Uart implementatinos +impl embedded_hal_1::serial::ErrorType for BlockingAsync +where + T: serial::Read, + E: embedded_hal_1::serial::Error + 'static, +{ + type Error = E; +} + +#[cfg(feature = "_todo_embedded_hal_serial")] +impl embedded_hal_async::serial::Read for BlockingAsync +where + T: serial::Read, + E: embedded_hal_1::serial::Error + 'static, +{ + type ReadFuture<'a> = impl Future> + 'a where T: 'a; + 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) => {} + Err(nb::Error::Other(e)) => return Err(e), + Ok(b) => { + buf[pos] = b; + pos += 1; + } + } + } + Ok(()) + } + } +} + +#[cfg(feature = "_todo_embedded_hal_serial")] +impl embedded_hal_async::serial::Write for BlockingAsync +where + T: blocking::serial::Write + serial::Read, + E: embedded_hal_1::serial::Error + 'static, +{ + type WriteFuture<'a> = impl Future> + 'a where T: 'a; + fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { + async move { self.wrapped.bwrite_all(buf) } + } + + type FlushFuture<'a> = impl Future> + 'a where T: 'a; + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { + async move { self.wrapped.bflush() } + } +} + +/// NOR flash wrapper +use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; +use embedded_storage_async::nor_flash::{AsyncNorFlash, AsyncReadNorFlash}; + +impl ErrorType for BlockingAsync +where + T: ErrorType, +{ + type Error = T::Error; +} + +impl AsyncNorFlash for BlockingAsync +where + T: NorFlash, +{ + const WRITE_SIZE: usize = ::WRITE_SIZE; + const ERASE_SIZE: usize = ::ERASE_SIZE; + + type WriteFuture<'a> = impl Future> + 'a where Self: 'a; + fn write<'a>(&'a mut self, offset: u32, data: &'a [u8]) -> Self::WriteFuture<'a> { + async move { self.wrapped.write(offset, data) } + } + + type EraseFuture<'a> = impl Future> + 'a where Self: 'a; + fn erase<'a>(&'a mut self, from: u32, to: u32) -> Self::EraseFuture<'a> { + async move { self.wrapped.erase(from, to) } + } +} + +impl AsyncReadNorFlash for BlockingAsync +where + T: ReadNorFlash, +{ + const READ_SIZE: usize = ::READ_SIZE; + type ReadFuture<'a> = impl Future> + 'a where Self: 'a; + fn read<'a>(&'a mut self, address: u32, data: &'a mut [u8]) -> Self::ReadFuture<'a> { + async move { self.wrapped.read(address, data) } + } + + fn capacity(&self) -> usize { + self.wrapped.capacity() + } +} diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs new file mode 100644 index 000000000..27ffa7421 --- /dev/null +++ b/embassy-embedded-hal/src/lib.rs @@ -0,0 +1,6 @@ +#![cfg_attr(not(feature = "std"), no_std)] +#![feature(generic_associated_types)] +#![feature(type_alias_impl_trait)] + +pub mod adapter; +pub mod shared_bus; diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs new file mode 100644 index 000000000..5a180e897 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/i2c.rs @@ -0,0 +1,120 @@ +//! Asynchronous shared I2C bus +//! +//! # Example (nrf52) +//! +//! ```rust +//! use embassy_embedded_hal::shared_bus::i2c::I2cBusDevice; +//! use embassy::mutex::Mutex; +//! use embassy::blocking_mutex::raw::ThreadModeRawMutex; +//! +//! static I2C_BUS: Forever>> = Forever::new(); +//! let config = twim::Config::default(); +//! let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); +//! let i2c = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config); +//! let i2c_bus = Mutex::::new(i2c); +//! let i2c_bus = I2C_BUS.put(i2c_bus); +//! +//! // Device 1, using embedded-hal-async compatible driver for QMC5883L compass +//! let i2c_dev1 = I2cBusDevice::new(i2c_bus); +//! let compass = QMC5883L::new(i2c_dev1).await.unwrap(); +//! +//! // Device 2, using embedded-hal-async compatible driver for Mpu6050 accelerometer +//! let i2c_dev2 = I2cBusDevice::new(i2c_bus); +//! let mpu = Mpu6050::new(i2c_dev2); +//! ``` +use core::{fmt::Debug, future::Future}; +use embassy::blocking_mutex::raw::RawMutex; +use embassy::mutex::Mutex; +use embedded_hal_async::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, M: RawMutex, BUS> { + bus: &'a Mutex, +} + +impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { + pub fn new(bus: &'a Mutex) -> Self { + Self { bus } + } +} + +impl<'a, M: RawMutex, BUS> i2c::ErrorType for I2cBusDevice<'a, M, BUS> +where + BUS: i2c::ErrorType, +{ + type Error = I2cBusDeviceError; +} + +impl i2c::I2c for I2cBusDevice<'_, M, BUS> +where + M: RawMutex + 'static, + BUS: i2c::I2c + 'static, +{ + type ReadFuture<'a> = impl Future> + 'a where Self: 'a; + + fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { + async move { + let mut bus = self.bus.lock().await; + bus.read(address, buffer) + .await + .map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + } + + type WriteFuture<'a> = impl Future> + 'a where Self: 'a; + + fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { + async move { + let mut bus = self.bus.lock().await; + bus.write(address, bytes) + .await + .map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + } + + type WriteReadFuture<'a> = impl Future> + 'a where Self: 'a; + + fn write_read<'a>( + &'a mut self, + address: u8, + wr_buffer: &'a [u8], + rd_buffer: &'a mut [u8], + ) -> Self::WriteReadFuture<'a> { + async move { + let mut bus = self.bus.lock().await; + bus.write_read(address, wr_buffer, rd_buffer) + .await + .map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + } + + type TransactionFuture<'a, 'b> = impl Future> + 'a where Self: 'a, 'b: 'a; + + fn transaction<'a, 'b>( + &'a mut self, + address: u8, + operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], + ) -> Self::TransactionFuture<'a, 'b> { + let _ = address; + let _ = operations; + async move { todo!() } + } +} diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs new file mode 100644 index 000000000..bd4fd2c31 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/mod.rs @@ -0,0 +1,4 @@ +//! Shared bus implementations for embedded-hal-async + +pub mod i2c; +pub mod spi; diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/spi.rs new file mode 100644 index 000000000..3ec064ba7 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/spi.rs @@ -0,0 +1,110 @@ +//! Asynchronous shared SPI bus +//! +//! # Example (nrf52) +//! +//! ```rust +//! use embassy_embedded_hal::shared_bus::spi::SpiBusDevice; +//! use embassy::mutex::Mutex; +//! use embassy::blocking_mutex::raw::ThreadModeRawMutex; +//! +//! static SPI_BUS: Forever>> = Forever::new(); +//! let mut config = spim::Config::default(); +//! config.frequency = spim::Frequency::M32; +//! let irq = interrupt::take!(SPIM3); +//! let spi = spim::Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, config); +//! let spi_bus = Mutex::::new(spi); +//! let spi_bus = SPI_BUS.put(spi_bus); +//! +//! // Device 1, using embedded-hal-async compatible driver for ST7735 LCD display +//! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); +//! let spi_dev1 = SpiBusDevice::new(spi_bus, cs_pin1); +//! let display1 = ST7735::new(spi_dev1, dc1, rst1, Default::default(), 160, 128); +//! +//! // Device 2 +//! let cs_pin2 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); +//! let spi_dev2 = SpiBusDevice::new(spi_bus, cs_pin2); +//! let display2 = ST7735::new(spi_dev2, dc2, rst2, Default::default(), 160, 128); +//! ``` +use core::{fmt::Debug, future::Future}; +use embassy::blocking_mutex::raw::RawMutex; +use embassy::mutex::Mutex; + +use embedded_hal_1::digital::blocking::OutputPin; +use embedded_hal_1::spi::ErrorType; +use embedded_hal_async::spi; + +#[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, M: RawMutex, BUS, CS> { + bus: &'a Mutex, + cs: CS, +} + +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, M: RawMutex, BUS, CS> spi::ErrorType for SpiBusDevice<'a, M, BUS, CS> +where + BUS: spi::ErrorType, + CS: OutputPin, +{ + type Error = SpiBusDeviceError; +} + +impl spi::SpiDevice for SpiBusDevice<'_, M, BUS, CS> +where + M: RawMutex + 'static, + BUS: spi::SpiBusFlush + 'static, + CS: OutputPin, +{ + type Bus = BUS; + + type TransactionFuture<'a, R, F, Fut> = impl Future> + 'a + where + Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a, + Fut: Future::Error>> + 'a; + + fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut> + where + R: 'a, + F: FnOnce(*mut Self::Bus) -> Fut + 'a, + Fut: Future::Error>> + 'a, + { + async move { + let mut bus = self.bus.lock().await; + self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; + + let f_res = f(&mut *bus).await; + + // On failure, it's important to still flush and deassert CS. + let flush_res = bus.flush().await; + 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)?; + + Ok(f_res) + } + } +} diff --git a/embassy-traits/Cargo.toml b/embassy-traits/Cargo.toml deleted file mode 100644 index 4e0e7d22d..000000000 --- a/embassy-traits/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "embassy-traits" -version = "0.1.0" -authors = ["Dario Nieuwenhuis "] -edition = "2018" - -[features] -std = [] - -[dependencies] -embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } -embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } -embedded-hal-async = { version = "0.1.0-alpha.0" } -embedded-storage = "0.3.0" -embedded-storage-async = "0.3.0" -nb = "1.0.0" diff --git a/embassy-traits/src/adapter.rs b/embassy-traits/src/adapter.rs deleted file mode 100644 index 033efb7ef..000000000 --- a/embassy-traits/src/adapter.rs +++ /dev/null @@ -1,248 +0,0 @@ -use core::future::Future; -use embedded_hal_02::blocking; -use embedded_hal_02::serial; - -/// 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 { - wrapped: T, -} - -impl BlockingAsync { - /// Create a new instance of a wrapper for a given peripheral. - pub fn new(wrapped: T) -> Self { - Self { wrapped } - } -} - -// -// I2C implementations -// -impl embedded_hal_1::i2c::ErrorType for BlockingAsync -where - E: embedded_hal_1::i2c::Error + 'static, - T: blocking::i2c::WriteRead - + blocking::i2c::Read - + blocking::i2c::Write, -{ - type Error = E; -} - -impl embedded_hal_async::i2c::I2c for BlockingAsync -where - E: embedded_hal_1::i2c::Error + 'static, - T: blocking::i2c::WriteRead - + blocking::i2c::Read - + blocking::i2c::Write, -{ - type WriteFuture<'a> = impl Future> + 'a where Self: 'a; - type ReadFuture<'a> = impl Future> + 'a where Self: 'a; - type WriteReadFuture<'a> = impl Future> + 'a where Self: 'a; - - 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) } - } - - type TransactionFuture<'a, 'b> = impl Future> + 'a where Self: 'a, 'b: 'a; - - fn transaction<'a, 'b>( - &'a mut self, - address: u8, - operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], - ) -> Self::TransactionFuture<'a, 'b> { - let _ = address; - let _ = operations; - async move { todo!() } - } -} - -// -// SPI implementatinos -// - -impl embedded_hal_async::spi::ErrorType for BlockingAsync -where - E: embedded_hal_1::spi::Error, - T: blocking::spi::Transfer + blocking::spi::Write, -{ - type Error = E; -} - -impl embedded_hal_async::spi::SpiBus for BlockingAsync -where - E: embedded_hal_1::spi::Error + 'static, - T: blocking::spi::Transfer + blocking::spi::Write, -{ - type TransferFuture<'a> = impl Future> + 'a where Self: 'a; - - fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Self::TransferFuture<'a> { - 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(()) - } - } - - type TransferInPlaceFuture<'a> = impl Future> + 'a where Self: 'a; - - fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> { - async move { todo!() } - } -} - -impl embedded_hal_async::spi::SpiBusFlush for BlockingAsync -where - E: embedded_hal_1::spi::Error + 'static, - T: blocking::spi::Transfer + blocking::spi::Write, -{ - type FlushFuture<'a> = impl Future> + 'a where Self: 'a; - - fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { - async move { Ok(()) } - } -} - -impl embedded_hal_async::spi::SpiBusWrite for BlockingAsync -where - E: embedded_hal_1::spi::Error + 'static, - T: blocking::spi::Transfer + blocking::spi::Write, -{ - type WriteFuture<'a> = impl Future> + 'a where Self: 'a; - - fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { - async move { - self.wrapped.write(data)?; - Ok(()) - } - } -} - -impl embedded_hal_async::spi::SpiBusRead for BlockingAsync -where - E: embedded_hal_1::spi::Error + 'static, - T: blocking::spi::Transfer + blocking::spi::Write, -{ - type ReadFuture<'a> = impl Future> + 'a where Self: 'a; - - fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { - async move { - self.wrapped.transfer(data)?; - Ok(()) - } - } -} - -// Uart implementatinos -impl embedded_hal_1::serial::ErrorType for BlockingAsync -where - T: serial::Read, - E: embedded_hal_1::serial::Error + 'static, -{ - type Error = E; -} - -#[cfg(feature = "_todo_embedded_hal_serial")] -impl embedded_hal_async::serial::Read for BlockingAsync -where - T: serial::Read, - E: embedded_hal_1::serial::Error + 'static, -{ - type ReadFuture<'a> = impl Future> + 'a where T: 'a; - 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) => {} - Err(nb::Error::Other(e)) => return Err(e), - Ok(b) => { - buf[pos] = b; - pos += 1; - } - } - } - Ok(()) - } - } -} - -#[cfg(feature = "_todo_embedded_hal_serial")] -impl embedded_hal_async::serial::Write for BlockingAsync -where - T: blocking::serial::Write + serial::Read, - E: embedded_hal_1::serial::Error + 'static, -{ - type WriteFuture<'a> = impl Future> + 'a where T: 'a; - fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { - async move { self.wrapped.bwrite_all(buf) } - } - - type FlushFuture<'a> = impl Future> + 'a where T: 'a; - fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { - async move { self.wrapped.bflush() } - } -} - -/// NOR flash wrapper -use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; -use embedded_storage_async::nor_flash::{AsyncNorFlash, AsyncReadNorFlash}; - -impl ErrorType for BlockingAsync -where - T: ErrorType, -{ - type Error = T::Error; -} - -impl AsyncNorFlash for BlockingAsync -where - T: NorFlash, -{ - const WRITE_SIZE: usize = ::WRITE_SIZE; - const ERASE_SIZE: usize = ::ERASE_SIZE; - - type WriteFuture<'a> = impl Future> + 'a where Self: 'a; - fn write<'a>(&'a mut self, offset: u32, data: &'a [u8]) -> Self::WriteFuture<'a> { - async move { self.wrapped.write(offset, data) } - } - - type EraseFuture<'a> = impl Future> + 'a where Self: 'a; - fn erase<'a>(&'a mut self, from: u32, to: u32) -> Self::EraseFuture<'a> { - async move { self.wrapped.erase(from, to) } - } -} - -impl AsyncReadNorFlash for BlockingAsync -where - T: ReadNorFlash, -{ - const READ_SIZE: usize = ::READ_SIZE; - type ReadFuture<'a> = impl Future> + 'a where Self: 'a; - fn read<'a>(&'a mut self, address: u32, data: &'a mut [u8]) -> Self::ReadFuture<'a> { - async move { self.wrapped.read(address, data) } - } - - fn capacity(&self) -> usize { - self.wrapped.capacity() - } -} diff --git a/embassy-traits/src/lib.rs b/embassy-traits/src/lib.rs deleted file mode 100644 index 9c5c367a8..000000000 --- a/embassy-traits/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] -#![feature(generic_associated_types)] -#![feature(type_alias_impl_trait)] - -pub mod adapter; diff --git a/examples/boot/nrf/Cargo.toml b/examples/boot/nrf/Cargo.toml index da8333b7c..42bc35430 100644 --- a/examples/boot/nrf/Cargo.toml +++ b/examples/boot/nrf/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } embassy-nrf = { version = "0.1.0", path = "../../../embassy-nrf", features = ["time-driver-rtc1", "gpiote", "nightly", "nrf52840"] } embassy-boot-nrf = { version = "0.1.0", path = "../../../embassy-boot/nrf" } -embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../embassy-embedded-hal" } defmt = { version = "0.3", optional = true } defmt-rtt = { version = "0.3", optional = true } diff --git a/examples/boot/nrf/src/bin/a.rs b/examples/boot/nrf/src/bin/a.rs index 7368e36ce..d45d0ff34 100644 --- a/examples/boot/nrf/src/bin/a.rs +++ b/examples/boot/nrf/src/bin/a.rs @@ -5,13 +5,13 @@ #![feature(type_alias_impl_trait)] use embassy_boot_nrf::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_nrf::{ gpio::{Input, Pull}, gpio::{Level, Output, OutputDrive}, nvmc::Nvmc, Peripherals, }; -use embassy_traits::adapter::BlockingAsync; use panic_reset as _; static APP_B: &[u8] = include_bytes!("../../b.bin"); diff --git a/examples/boot/stm32f3/Cargo.toml b/examples/boot/stm32f3/Cargo.toml index d4ca600f8..fab6fd723 100644 --- a/examples/boot/stm32f3/Cargo.toml +++ b/examples/boot/stm32f3/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32f303re", "time-driver-any", "exti"] } embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } -embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../embassy-embedded-hal" } defmt = { version = "0.3", optional = true } defmt-rtt = { version = "0.3", optional = true } diff --git a/examples/boot/stm32f3/src/bin/a.rs b/examples/boot/stm32f3/src/bin/a.rs index db9262f43..9ad798389 100644 --- a/examples/boot/stm32f3/src/bin/a.rs +++ b/examples/boot/stm32f3/src/bin/a.rs @@ -3,11 +3,11 @@ #![feature(type_alias_impl_trait)] use embassy_boot_stm32::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::exti::ExtiInput; use embassy_stm32::flash::Flash; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use panic_reset as _; #[cfg(feature = "defmt-rtt")] diff --git a/examples/boot/stm32f7/Cargo.toml b/examples/boot/stm32f7/Cargo.toml index 857b287d5..14500b198 100644 --- a/examples/boot/stm32f7/Cargo.toml +++ b/examples/boot/stm32f7/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32f767zi", "time-driver-any", "exti"] } embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } -embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../embassy-embedded-hal" } defmt = { version = "0.3", optional = true } defmt-rtt = { version = "0.3", optional = true } diff --git a/examples/boot/stm32f7/src/bin/a.rs b/examples/boot/stm32f7/src/bin/a.rs index ca154f0af..b4f49d579 100644 --- a/examples/boot/stm32f7/src/bin/a.rs +++ b/examples/boot/stm32f7/src/bin/a.rs @@ -3,11 +3,11 @@ #![feature(type_alias_impl_trait)] use embassy_boot_stm32::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::exti::ExtiInput; use embassy_stm32::flash::Flash; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use panic_reset as _; #[cfg(feature = "defmt-rtt")] diff --git a/examples/boot/stm32h7/Cargo.toml b/examples/boot/stm32h7/Cargo.toml index 1fd03906f..3069277ed 100644 --- a/examples/boot/stm32h7/Cargo.toml +++ b/examples/boot/stm32h7/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32h743zi", "time-driver-any", "exti"] } embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } -embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../embassy-embedded-hal" } defmt = { version = "0.3", optional = true } defmt-rtt = { version = "0.3", optional = true } diff --git a/examples/boot/stm32h7/src/bin/a.rs b/examples/boot/stm32h7/src/bin/a.rs index 1f23a8bc2..1d196e8a5 100644 --- a/examples/boot/stm32h7/src/bin/a.rs +++ b/examples/boot/stm32h7/src/bin/a.rs @@ -3,11 +3,11 @@ #![feature(type_alias_impl_trait)] use embassy_boot_stm32::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::exti::ExtiInput; use embassy_stm32::flash::Flash; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use panic_reset as _; #[cfg(feature = "defmt-rtt")] diff --git a/examples/boot/stm32l0/Cargo.toml b/examples/boot/stm32l0/Cargo.toml index 5cb1add5b..13ca84367 100644 --- a/examples/boot/stm32l0/Cargo.toml +++ b/examples/boot/stm32l0/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32l072cz", "time-driver-any", "exti", "memory-x"] } embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32", features = ["thumbv6"] } -embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../embassy-embedded-hal" } defmt = { version = "0.3", optional = true } defmt-rtt = { version = "0.3", optional = true } diff --git a/examples/boot/stm32l0/src/bin/a.rs b/examples/boot/stm32l0/src/bin/a.rs index 9e603a226..d4b252bf0 100644 --- a/examples/boot/stm32l0/src/bin/a.rs +++ b/examples/boot/stm32l0/src/bin/a.rs @@ -4,11 +4,11 @@ use embassy::time::{Duration, Timer}; use embassy_boot_stm32::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::exti::ExtiInput; use embassy_stm32::flash::Flash; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use panic_reset as _; #[cfg(feature = "defmt-rtt")] diff --git a/examples/boot/stm32l1/Cargo.toml b/examples/boot/stm32l1/Cargo.toml index 9f97462f6..b0b6e3d25 100644 --- a/examples/boot/stm32l1/Cargo.toml +++ b/examples/boot/stm32l1/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32l151cb-a", "time-driver-any", "exti"] } embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } -embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../embassy-embedded-hal" } defmt = { version = "0.3", optional = true } defmt-rtt = { version = "0.3", optional = true } diff --git a/examples/boot/stm32l1/src/bin/a.rs b/examples/boot/stm32l1/src/bin/a.rs index 9e603a226..d4b252bf0 100644 --- a/examples/boot/stm32l1/src/bin/a.rs +++ b/examples/boot/stm32l1/src/bin/a.rs @@ -4,11 +4,11 @@ use embassy::time::{Duration, Timer}; use embassy_boot_stm32::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::exti::ExtiInput; use embassy_stm32::flash::Flash; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use panic_reset as _; #[cfg(feature = "defmt-rtt")] diff --git a/examples/boot/stm32l4/Cargo.toml b/examples/boot/stm32l4/Cargo.toml index 53424a666..705dbd0d6 100644 --- a/examples/boot/stm32l4/Cargo.toml +++ b/examples/boot/stm32l4/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32l475vg", "time-driver-any", "exti"] } embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } -embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../embassy-embedded-hal" } defmt = { version = "0.3", optional = true } defmt-rtt = { version = "0.3", optional = true } diff --git a/examples/boot/stm32l4/src/bin/a.rs b/examples/boot/stm32l4/src/bin/a.rs index 41684b2f0..23b1d98bb 100644 --- a/examples/boot/stm32l4/src/bin/a.rs +++ b/examples/boot/stm32l4/src/bin/a.rs @@ -3,11 +3,11 @@ #![feature(type_alias_impl_trait)] use embassy_boot_stm32::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::exti::ExtiInput; use embassy_stm32::flash::Flash; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use panic_reset as _; #[cfg(feature = "defmt-rtt")] diff --git a/examples/boot/stm32wl/Cargo.toml b/examples/boot/stm32wl/Cargo.toml index fb64886e6..41e6a112e 100644 --- a/examples/boot/stm32wl/Cargo.toml +++ b/examples/boot/stm32wl/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" embassy = { version = "0.1.0", path = "../../../embassy", features = ["nightly"] } embassy-stm32 = { version = "0.1.0", path = "../../../embassy-stm32", features = ["unstable-traits", "nightly", "stm32wl55jc-cm4", "time-driver-any", "exti"] } embassy-boot-stm32 = { version = "0.1.0", path = "../../../embassy-boot/stm32" } -embassy-traits = { version = "0.1.0", path = "../../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../../embassy-embedded-hal" } defmt = { version = "0.3", optional = true } defmt-rtt = { version = "0.3", optional = true } diff --git a/examples/boot/stm32wl/src/bin/a.rs b/examples/boot/stm32wl/src/bin/a.rs index b3e9efa75..1089eff1e 100644 --- a/examples/boot/stm32wl/src/bin/a.rs +++ b/examples/boot/stm32wl/src/bin/a.rs @@ -3,11 +3,11 @@ #![feature(type_alias_impl_trait)] use embassy_boot_stm32::FirmwareUpdater; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::exti::ExtiInput; use embassy_stm32::flash::Flash; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use panic_reset as _; #[cfg(feature = "defmt-rtt")] diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml index b3478f74e..afea5a5aa 100644 --- a/examples/stm32l4/Cargo.toml +++ b/examples/stm32l4/Cargo.toml @@ -9,7 +9,7 @@ resolver = "2" [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } -embassy-traits = { version = "0.1.0", path = "../../embassy-traits" } +embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits"] } defmt = "0.3" diff --git a/examples/stm32l4/src/bin/i2c_blocking_async.rs b/examples/stm32l4/src/bin/i2c_blocking_async.rs index 136aea849..eb74223b0 100644 --- a/examples/stm32l4/src/bin/i2c_blocking_async.rs +++ b/examples/stm32l4/src/bin/i2c_blocking_async.rs @@ -7,12 +7,12 @@ use panic_probe as _; use defmt::*; use embassy::executor::Spawner; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::dma::NoDma; use embassy_stm32::i2c::I2c; use embassy_stm32::interrupt; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use embedded_hal_async::i2c::I2c as I2cTrait; const ADDRESS: u8 = 0x5F; diff --git a/examples/stm32l4/src/bin/spi_blocking_async.rs b/examples/stm32l4/src/bin/spi_blocking_async.rs index 0398965d3..e1a400107 100644 --- a/examples/stm32l4/src/bin/spi_blocking_async.rs +++ b/examples/stm32l4/src/bin/spi_blocking_async.rs @@ -7,12 +7,12 @@ use panic_probe as _; use defmt::*; use embassy::executor::Spawner; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_stm32::dma::NoDma; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; use embassy_stm32::Peripherals; -use embassy_traits::adapter::BlockingAsync; use embedded_hal_async::spi::SpiBus; #[embassy::main] -- cgit