From 2983cec1f1da3c1c6114c6b106021ca541f91a6d Mon Sep 17 00:00:00 2001 From: onestacked Date: Sun, 5 May 2024 17:15:10 +0200 Subject: [PATCH 1/6] Put embedded-hal v0.2 dependency behind feature flag in embassy-time --- embassy-time/CHANGELOG.md | 4 +++ embassy-time/Cargo.toml | 7 ++++-- embassy-time/src/delay.rs | 52 ++++++++++++++++++++------------------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/embassy-time/CHANGELOG.md b/embassy-time/CHANGELOG.md index df093949fa..266a32337c 100644 --- a/embassy-time/CHANGELOG.md +++ b/embassy-time/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +- Put `embedded-hal v0.2` dependency behind a feature flag. + ## 0.3.0 - 2024-01-11 - Update `embedded-hal-async` to `1.0.0` diff --git a/embassy-time/Cargo.toml b/embassy-time/Cargo.toml index ca7ad2d09a..92544996e9 100644 --- a/embassy-time/Cargo.toml +++ b/embassy-time/Cargo.toml @@ -40,6 +40,9 @@ mock-driver = ["tick-hz-1_000_000"] ## To use this you must have a time driver provided. generic-queue = [] +#! Support legacy for embedded hal 0.2 traits +embedded-hal-02 = ["dep:embedded-hal-02"] + #! The following features set how many timers are used for the generic queue. At most one #! `generic-queue-*` feature can be enabled. If none is enabled, a default of 64 timers is used. #! @@ -404,8 +407,8 @@ embassy-time-queue-driver = { version = "0.1.0", path = "../embassy-time-queue-d defmt = { version = "0.3", optional = true } log = { version = "0.4.14", optional = true } -embedded-hal-02 = { package = "embedded-hal", version = "0.2.6" } -embedded-hal-1 = { package = "embedded-hal", version = "1.0" } +embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", optional = true } +embedded-hal = "1.0" embedded-hal-async = { version = "1.0" } futures-util = { version = "0.3.17", default-features = false } diff --git a/embassy-time/src/delay.rs b/embassy-time/src/delay.rs index f77859d4af..1260040f5f 100644 --- a/embassy-time/src/delay.rs +++ b/embassy-time/src/delay.rs @@ -16,7 +16,7 @@ pub fn block_for(duration: Duration) { #[derive(Clone)] pub struct Delay; -impl embedded_hal_1::delay::DelayNs for Delay { +impl embedded_hal::delay::DelayNs for Delay { fn delay_ns(&mut self, ns: u32) { block_for(Duration::from_nanos(ns as u64)) } @@ -43,39 +43,41 @@ impl embedded_hal_async::delay::DelayNs for Delay { Timer::after_millis(ms as _).await } } - -impl embedded_hal_02::blocking::delay::DelayMs for Delay { - fn delay_ms(&mut self, ms: u8) { - block_for(Duration::from_millis(ms as u64)) +#[cfg(feature = "embedded_hal_02")] +mod embedded_hal_02 { + impl embedded_hal_02::blocking::delay::DelayMs for Delay { + fn delay_ms(&mut self, ms: u8) { + block_for(Duration::from_millis(ms as u64)) + } } -} -impl embedded_hal_02::blocking::delay::DelayMs for Delay { - fn delay_ms(&mut self, ms: u16) { - block_for(Duration::from_millis(ms as u64)) + impl embedded_hal_02::blocking::delay::DelayMs for Delay { + fn delay_ms(&mut self, ms: u16) { + block_for(Duration::from_millis(ms as u64)) + } } -} -impl embedded_hal_02::blocking::delay::DelayMs for Delay { - fn delay_ms(&mut self, ms: u32) { - block_for(Duration::from_millis(ms as u64)) + impl embedded_hal_02::blocking::delay::DelayMs for Delay { + fn delay_ms(&mut self, ms: u32) { + block_for(Duration::from_millis(ms as u64)) + } } -} -impl embedded_hal_02::blocking::delay::DelayUs for Delay { - fn delay_us(&mut self, us: u8) { - block_for(Duration::from_micros(us as u64)) + impl embedded_hal_02::blocking::delay::DelayUs for Delay { + fn delay_us(&mut self, us: u8) { + block_for(Duration::from_micros(us as u64)) + } } -} -impl embedded_hal_02::blocking::delay::DelayUs for Delay { - fn delay_us(&mut self, us: u16) { - block_for(Duration::from_micros(us as u64)) + impl embedded_hal_02::blocking::delay::DelayUs for Delay { + fn delay_us(&mut self, us: u16) { + block_for(Duration::from_micros(us as u64)) + } } -} -impl embedded_hal_02::blocking::delay::DelayUs for Delay { - fn delay_us(&mut self, us: u32) { - block_for(Duration::from_micros(us as u64)) + impl embedded_hal_02::blocking::delay::DelayUs for Delay { + fn delay_us(&mut self, us: u32) { + block_for(Duration::from_micros(us as u64)) + } } } From 303f3e25afab534ea720cf4e998b544d57551098 Mon Sep 17 00:00:00 2001 From: onestacked Date: Sun, 5 May 2024 17:33:48 +0200 Subject: [PATCH 2/6] Make embedded-hal-02 dependency optional in embassy-embedded-hal --- embassy-embedded-hal/Cargo.toml | 7 +- .../src/adapter/blocking_async.rs | 220 ++++++++++++------ .../src/adapter/yielding_async.rs | 6 +- .../src/shared_bus/asynch/spi.rs | 4 +- .../src/shared_bus/blocking/i2c.rs | 75 +++--- .../src/shared_bus/blocking/spi.rs | 83 +++---- embassy-embedded-hal/src/shared_bus/mod.rs | 2 +- 7 files changed, 246 insertions(+), 151 deletions(-) diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index e89179740a..81cc21a35a 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml @@ -26,14 +26,17 @@ std = [] time = ["dep:embassy-time"] default = ["time"] +#! Support legacy for embedded hal 0.2 traits +embedded-hal-02 = ["dep:embedded-hal-02"] + [dependencies] embassy-futures = { version = "0.1.0", path = "../embassy-futures" } embassy-sync = { version = "0.5.0", path = "../embassy-sync" } embassy-time = { version = "0.3.0", path = "../embassy-time", optional = true } embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [ "unproven", -] } -embedded-hal-1 = { package = "embedded-hal", version = "1.0" } +], optional = true } +embedded-hal = "1.0" embedded-hal-async = { version = "1.0" } embedded-storage = "0.3.1" embedded-storage-async = { version = "0.4.1" } diff --git a/embassy-embedded-hal/src/adapter/blocking_async.rs b/embassy-embedded-hal/src/adapter/blocking_async.rs index bafc31583d..4a2db3661e 100644 --- a/embassy-embedded-hal/src/adapter/blocking_async.rs +++ b/embassy-embedded-hal/src/adapter/blocking_async.rs @@ -1,5 +1,3 @@ -use embedded_hal_02::blocking; - /// Wrapper that implements async traits using blocking implementations. /// /// This allows driver writers to depend on the async traits while still supporting embedded-hal peripheral implementations. @@ -17,92 +15,180 @@ impl BlockingAsync { Self { wrapped } } } +#[cfg(feature = "embedded_hal_02")] +mod embedded_hal_02 { + use super::BlockingAsync; + use embedded_hal_02::blocking; + // + // I2C implementations + // + impl embedded_hal::i2c::ErrorType for BlockingAsync + where + E: embedded_hal::i2c::Error + 'static, + T: blocking::i2c::WriteRead + blocking::i2c::Read + blocking::i2c::Write, + { + type Error = E; + } -// -// 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::i2c::Error + 'static, + T: blocking::i2c::WriteRead + blocking::i2c::Read + blocking::i2c::Write, + { + async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.read(address, read) + } -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, -{ - async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { - self.wrapped.read(address, read) - } + async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { + self.wrapped.write(address, write) + } + + async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.write_read(address, write, read) + } - async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { - self.wrapped.write(address, write) + async fn transaction( + &mut self, + address: u8, + operations: &mut [embedded_hal::i2c::Operation<'_>], + ) -> Result<(), Self::Error> { + let _ = address; + let _ = operations; + todo!() + } } - async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { - self.wrapped.write_read(address, write, read) + // + // SPI implementatinos + // + + impl embedded_hal_async::spi::ErrorType for BlockingAsync + where + E: embedded_hal::spi::Error, + T: blocking::spi::Transfer + blocking::spi::Write, + { + type Error = E; } - async fn transaction( - &mut self, - address: u8, - operations: &mut [embedded_hal_1::i2c::Operation<'_>], - ) -> Result<(), Self::Error> { - let _ = address; - let _ = operations; - todo!() + impl embedded_hal_async::spi::SpiBus for BlockingAsync + where + E: embedded_hal::spi::Error + 'static, + T: blocking::spi::Transfer + blocking::spi::Write, + { + async fn flush(&mut self) -> Result<(), Self::Error> { + Ok(()) + } + + async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> { + self.wrapped.write(data)?; + Ok(()) + } + + async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.transfer(data)?; + Ok(()) + } + + async fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> { + // 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(()) + } + + async fn transfer_in_place(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.transfer(data)?; + Ok(()) + } } } +#[cfg(not(feature = "embedded_hal_02"))] +mod embedded_hal { + use super::BlockingAsync; + + // + // I2C implementations + // + impl embedded_hal::i2c::ErrorType for BlockingAsync + where + E: embedded_hal::i2c::Error + 'static, + T: embedded_hal::i2c::I2c + embedded_hal_async::i2c::ErrorType, + { + type Error = E; + } -// -// SPI implementatinos -// + impl embedded_hal_async::i2c::I2c for BlockingAsync + where + E: embedded_hal::i2c::Error + 'static, + T: embedded_hal::i2c::I2c + embedded_hal_async::i2c::ErrorType, + { + async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.read(address, read) + } -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; -} + async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { + self.wrapped.write(address, write) + } -impl embedded_hal_async::spi::SpiBus for BlockingAsync -where - E: embedded_hal_1::spi::Error + 'static, - T: blocking::spi::Transfer + blocking::spi::Write, -{ - async fn flush(&mut self) -> Result<(), Self::Error> { - Ok(()) - } + async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.write_read(address, write, read) + } - async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> { - self.wrapped.write(data)?; - Ok(()) + async fn transaction( + &mut self, + address: u8, + operations: &mut [embedded_hal::i2c::Operation<'_>], + ) -> Result<(), Self::Error> { + let _ = address; + let _ = operations; + todo!() + } } - async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { - self.wrapped.transfer(data)?; - Ok(()) + // + // SPI implementatinos + // + + impl embedded_hal_async::spi::ErrorType for BlockingAsync + where + E: embedded_hal::spi::Error, + T: embedded_hal::spi::SpiBus + embedded_hal_async::spi::ErrorType, + { + type Error = E; } - async fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> { - // Ensure we write the expected bytes - for i in 0..core::cmp::min(read.len(), write.len()) { - read[i] = write[i].clone(); + impl embedded_hal_async::spi::SpiBus for BlockingAsync + where + E: embedded_hal::spi::Error + 'static, + T: embedded_hal::spi::SpiBus + embedded_hal_async::spi::ErrorType, + { + async fn flush(&mut self) -> Result<(), Self::Error> { + Ok(()) } - self.wrapped.transfer(read)?; - Ok(()) - } - async fn transfer_in_place(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { - self.wrapped.transfer(data)?; - Ok(()) + async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> { + self.wrapped.write(data)?; + Ok(()) + } + + async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.read(data)?; + Ok(()) + } + + async fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> { + self.wrapped.transfer(read, write)?; + Ok(()) + } + + async fn transfer_in_place(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.transfer_in_place(data)?; + Ok(()) + } } } - /// NOR flash wrapper use embedded_storage::nor_flash::{ErrorType, MultiwriteNorFlash, NorFlash, ReadNorFlash}; use embedded_storage_async::nor_flash::{ diff --git a/embassy-embedded-hal/src/adapter/yielding_async.rs b/embassy-embedded-hal/src/adapter/yielding_async.rs index fe9c9c3418..363ab51bc7 100644 --- a/embassy-embedded-hal/src/adapter/yielding_async.rs +++ b/embassy-embedded-hal/src/adapter/yielding_async.rs @@ -18,9 +18,9 @@ impl YieldingAsync { // // I2C implementations // -impl embedded_hal_1::i2c::ErrorType for YieldingAsync +impl embedded_hal::i2c::ErrorType for YieldingAsync where - T: embedded_hal_1::i2c::ErrorType, + T: embedded_hal::i2c::ErrorType, { type Error = T::Error; } @@ -50,7 +50,7 @@ where async fn transaction( &mut self, address: u8, - operations: &mut [embedded_hal_1::i2c::Operation<'_>], + operations: &mut [embedded_hal::i2c::Operation<'_>], ) -> Result<(), Self::Error> { self.wrapped.transaction(address, operations).await?; yield_now().await; diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs index 9890f218d1..1009b915cb 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs @@ -27,8 +27,8 @@ use embassy_sync::blocking_mutex::raw::RawMutex; use embassy_sync::mutex::Mutex; -use embedded_hal_1::digital::OutputPin; -use embedded_hal_1::spi::Operation; +use embedded_hal::digital::OutputPin; +use embedded_hal::spi::Operation; use embedded_hal_async::spi; use crate::shared_bus::SpiDeviceError; diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 627767c8a1..8b0b72d792 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -19,7 +19,7 @@ use core::cell::RefCell; use embassy_sync::blocking_mutex::raw::RawMutex; use embassy_sync::blocking_mutex::Mutex; -use embedded_hal_1::i2c::{ErrorType, I2c, Operation}; +use embedded_hal::i2c::{ErrorType, I2c, Operation}; use crate::shared_bus::I2cDeviceError; use crate::SetConfig; @@ -75,45 +75,48 @@ where } } -impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cDevice<'_, M, BUS> -where - M: RawMutex, - BUS: embedded_hal_02::blocking::i2c::Write, -{ - type Error = I2cDeviceError; - - fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> Result<(), Self::Error> { - self.bus - .lock(|bus| bus.borrow_mut().write(addr, bytes).map_err(I2cDeviceError::I2c)) +#[cfg(feature = "embedded_hal_02")] +mod embedded_hal_02 { + impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cDevice<'_, M, BUS> + where + M: RawMutex, + BUS: embedded_hal_02::blocking::i2c::Write, + { + type Error = I2cDeviceError; + + fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> Result<(), Self::Error> { + self.bus + .lock(|bus| bus.borrow_mut().write(addr, bytes).map_err(I2cDeviceError::I2c)) + } } -} - -impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cDevice<'_, M, BUS> -where - M: RawMutex, - BUS: embedded_hal_02::blocking::i2c::Read, -{ - type Error = I2cDeviceError; - fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> Result<(), Self::Error> { - self.bus - .lock(|bus| bus.borrow_mut().read(addr, bytes).map_err(I2cDeviceError::I2c)) + impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cDevice<'_, M, BUS> + where + M: RawMutex, + BUS: embedded_hal_02::blocking::i2c::Read, + { + type Error = I2cDeviceError; + + fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> Result<(), Self::Error> { + self.bus + .lock(|bus| bus.borrow_mut().read(addr, bytes).map_err(I2cDeviceError::I2c)) + } } -} - -impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cDevice<'_, M, BUS> -where - M: RawMutex, - BUS: embedded_hal_02::blocking::i2c::WriteRead, -{ - type Error = I2cDeviceError; - fn write_read<'w>(&mut self, addr: u8, bytes: &'w [u8], buffer: &'w mut [u8]) -> Result<(), Self::Error> { - self.bus.lock(|bus| { - bus.borrow_mut() - .write_read(addr, bytes, buffer) - .map_err(I2cDeviceError::I2c) - }) + impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cDevice<'_, M, BUS> + where + M: RawMutex, + BUS: embedded_hal_02::blocking::i2c::WriteRead, + { + type Error = I2cDeviceError; + + fn write_read<'w>(&mut self, addr: u8, bytes: &'w [u8], buffer: &'w mut [u8]) -> Result<(), Self::Error> { + self.bus.lock(|bus| { + bus.borrow_mut() + .write_read(addr, bytes, buffer) + .map_err(I2cDeviceError::I2c) + }) + } } } diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index 801899f9fb..6034fe32a2 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -21,8 +21,8 @@ use core::cell::RefCell; use embassy_sync::blocking_mutex::raw::RawMutex; use embassy_sync::blocking_mutex::Mutex; -use embedded_hal_1::digital::OutputPin; -use embedded_hal_1::spi::{self, Operation, SpiBus}; +use embedded_hal::digital::OutputPin; +use embedded_hal::spi::{self, Operation, SpiBus}; use crate::shared_bus::SpiDeviceError; use crate::SetConfig; @@ -48,7 +48,7 @@ where type Error = SpiDeviceError; } -impl embedded_hal_1::spi::SpiDevice for SpiDevice<'_, M, BUS, CS> +impl embedded_hal::spi::SpiDevice for SpiDevice<'_, M, BUS, CS> where M: RawMutex, BUS: SpiBus, @@ -90,44 +90,47 @@ where } } -impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer for SpiDevice<'_, M, BUS, CS> -where - M: RawMutex, - BUS: embedded_hal_02::blocking::spi::Transfer, - CS: OutputPin, -{ - type Error = SpiDeviceError; - fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { - self.bus.lock(|bus| { - let mut bus = bus.borrow_mut(); - self.cs.set_low().map_err(SpiDeviceError::Cs)?; - let op_res = bus.transfer(words); - let cs_res = self.cs.set_high(); - let op_res = op_res.map_err(SpiDeviceError::Spi)?; - cs_res.map_err(SpiDeviceError::Cs)?; - Ok(op_res) - }) +#[cfg(feature = "embedded_hal_02")] +mod embedded_hal_02 { + impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer for SpiDevice<'_, M, BUS, CS> + where + M: RawMutex, + BUS: embedded_hal_02::blocking::spi::Transfer, + CS: OutputPin, + { + type Error = SpiDeviceError; + fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { + self.bus.lock(|bus| { + let mut bus = bus.borrow_mut(); + self.cs.set_low().map_err(SpiDeviceError::Cs)?; + let op_res = bus.transfer(words); + let cs_res = self.cs.set_high(); + let op_res = op_res.map_err(SpiDeviceError::Spi)?; + cs_res.map_err(SpiDeviceError::Cs)?; + Ok(op_res) + }) + } } -} -impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Write for SpiDevice<'_, M, BUS, CS> -where - M: RawMutex, - BUS: embedded_hal_02::blocking::spi::Write, - CS: OutputPin, -{ - type Error = SpiDeviceError; - - fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { - self.bus.lock(|bus| { - let mut bus = bus.borrow_mut(); - self.cs.set_low().map_err(SpiDeviceError::Cs)?; - let op_res = bus.write(words); - let cs_res = self.cs.set_high(); - let op_res = op_res.map_err(SpiDeviceError::Spi)?; - cs_res.map_err(SpiDeviceError::Cs)?; - Ok(op_res) - }) + impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Write for SpiDevice<'_, M, BUS, CS> + where + M: RawMutex, + BUS: embedded_hal_02::blocking::spi::Write, + CS: OutputPin, + { + type Error = SpiDeviceError; + + fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { + self.bus.lock(|bus| { + let mut bus = bus.borrow_mut(); + self.cs.set_low().map_err(SpiDeviceError::Cs)?; + let op_res = bus.write(words); + let cs_res = self.cs.set_high(); + let op_res = op_res.map_err(SpiDeviceError::Spi)?; + cs_res.map_err(SpiDeviceError::Cs)?; + Ok(op_res) + }) + } } } @@ -163,7 +166,7 @@ where type Error = SpiDeviceError; } -impl embedded_hal_1::spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS> +impl embedded_hal::spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS> where M: RawMutex, BUS: SpiBus + SetConfig, diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index d835306bc5..19df92316c 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs @@ -1,7 +1,7 @@ //! Shared bus implementations use core::fmt::Debug; -use embedded_hal_1::{i2c, spi}; +use embedded_hal::{i2c, spi}; pub mod asynch; pub mod blocking; From 7b933ac3a3919916501601efb7bd61781b1e5862 Mon Sep 17 00:00:00 2001 From: onestacked Date: Sun, 5 May 2024 19:24:39 +0200 Subject: [PATCH 3/6] Formatting --- embassy-embedded-hal/src/adapter/blocking_async.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/embassy-embedded-hal/src/adapter/blocking_async.rs b/embassy-embedded-hal/src/adapter/blocking_async.rs index 4a2db3661e..ea62a63052 100644 --- a/embassy-embedded-hal/src/adapter/blocking_async.rs +++ b/embassy-embedded-hal/src/adapter/blocking_async.rs @@ -17,8 +17,9 @@ impl BlockingAsync { } #[cfg(feature = "embedded_hal_02")] mod embedded_hal_02 { - use super::BlockingAsync; use embedded_hal_02::blocking; + + use super::BlockingAsync; // // I2C implementations // From 62d01e4a9cfe17010def219b1848f5ec20b433d1 Mon Sep 17 00:00:00 2001 From: onestacked Date: Sun, 5 May 2024 19:49:42 +0200 Subject: [PATCH 4/6] Enable embedded-hal-02 by default --- embassy-embedded-hal/Cargo.toml | 2 +- embassy-time/Cargo.toml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index 81cc21a35a..af0ba256a3 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml @@ -24,7 +24,7 @@ features = ["std"] [features] std = [] time = ["dep:embassy-time"] -default = ["time"] +default = ["time", "embedded-hal-02"] #! Support legacy for embedded hal 0.2 traits embedded-hal-02 = ["dep:embedded-hal-02"] diff --git a/embassy-time/Cargo.toml b/embassy-time/Cargo.toml index 92544996e9..bc6221c81f 100644 --- a/embassy-time/Cargo.toml +++ b/embassy-time/Cargo.toml @@ -24,6 +24,7 @@ target = "x86_64-unknown-linux-gnu" features = ["defmt", "std"] [features] +default = ["embedded-hal-02"] std = ["tick-hz-1_000_000", "critical-section/std"] wasm = ["dep:wasm-bindgen", "dep:js-sys", "dep:wasm-timer", "tick-hz-1_000_000"] From 4f1556a0eba967ab30d5fe9e534b2698ee682662 Mon Sep 17 00:00:00 2001 From: onestacked Date: Sun, 5 May 2024 20:32:10 +0200 Subject: [PATCH 5/6] Fix feature flag --- embassy-embedded-hal/src/adapter/blocking_async.rs | 4 ++-- embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 8 +++++++- embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 9 ++++++++- embassy-time/src/delay.rs | 4 +++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/embassy-embedded-hal/src/adapter/blocking_async.rs b/embassy-embedded-hal/src/adapter/blocking_async.rs index ea62a63052..043d2ede74 100644 --- a/embassy-embedded-hal/src/adapter/blocking_async.rs +++ b/embassy-embedded-hal/src/adapter/blocking_async.rs @@ -15,7 +15,7 @@ impl BlockingAsync { Self { wrapped } } } -#[cfg(feature = "embedded_hal_02")] +#[cfg(feature = "embedded-hal-02")] mod embedded_hal_02 { use embedded_hal_02::blocking; @@ -105,7 +105,7 @@ mod embedded_hal_02 { } } } -#[cfg(not(feature = "embedded_hal_02"))] +#[cfg(not(feature = "embedded-hal-02"))] mod embedded_hal { use super::BlockingAsync; diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 8b0b72d792..873b6f07d4 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -75,8 +75,14 @@ where } } -#[cfg(feature = "embedded_hal_02")] +#[cfg(feature = "embedded-hal-02")] mod embedded_hal_02 { + use embassy_sync::blocking_mutex::raw::RawMutex; + + use crate::shared_bus::I2cDeviceError; + + use super::I2cDevice; + impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cDevice<'_, M, BUS> where M: RawMutex, diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index 6034fe32a2..d5e3a84c8d 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -90,8 +90,15 @@ where } } -#[cfg(feature = "embedded_hal_02")] +#[cfg(feature = "embedded-hal-02")] mod embedded_hal_02 { + use embassy_sync::blocking_mutex::raw::RawMutex; + use embedded_hal_02::digital::v2::OutputPin; + + use crate::shared_bus::SpiDeviceError; + + use super::SpiDevice; + impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer for SpiDevice<'_, M, BUS, CS> where M: RawMutex, diff --git a/embassy-time/src/delay.rs b/embassy-time/src/delay.rs index 1260040f5f..9e822fdc97 100644 --- a/embassy-time/src/delay.rs +++ b/embassy-time/src/delay.rs @@ -43,8 +43,10 @@ impl embedded_hal_async::delay::DelayNs for Delay { Timer::after_millis(ms as _).await } } -#[cfg(feature = "embedded_hal_02")] +#[cfg(feature = "embedded-hal-02")] mod embedded_hal_02 { + use crate::{block_for, Delay, Duration}; + impl embedded_hal_02::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, ms: u8) { block_for(Duration::from_millis(ms as u64)) From 58728ca704a712da9389f189f2b3f81969429327 Mon Sep 17 00:00:00 2001 From: onestacked Date: Sun, 5 May 2024 20:34:18 +0200 Subject: [PATCH 6/6] Formatting --- embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 3 +-- embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 873b6f07d4..5512ae1a67 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -79,9 +79,8 @@ where mod embedded_hal_02 { use embassy_sync::blocking_mutex::raw::RawMutex; - use crate::shared_bus::I2cDeviceError; - use super::I2cDevice; + use crate::shared_bus::I2cDeviceError; impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cDevice<'_, M, BUS> where diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index d5e3a84c8d..d995125112 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -95,9 +95,8 @@ mod embedded_hal_02 { use embassy_sync::blocking_mutex::raw::RawMutex; use embedded_hal_02::digital::v2::OutputPin; - use crate::shared_bus::SpiDeviceError; - use super::SpiDevice; + use crate::shared_bus::SpiDeviceError; impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer for SpiDevice<'_, M, BUS, CS> where