From b9d055b3867d735cb42ab269c04534366b0ad6cd Mon Sep 17 00:00:00 2001 From: Ralph Ursprung Date: Sun, 2 Jul 2023 13:21:12 +0200 Subject: [PATCH 1/2] update to `embedded-hal=1.0.0-alpha.10` there were various breaking changes in alpha.9 and alpha.10 which have been applied here. see [their changelog][embedded-hal-1.0.0-alpha.10] for further details. important: this is completely untested beyond "it compiles", please test this carefully! compilation has been tested with the `stm32f401` feature, the CI will show whether it'll also compile for all others. note that this does only the bare minimum to make it build against the new version - it does not implement any of the newly added APIs. this can be done in subsequent commits. this fixes #602 [embedded-hal-1.0.0-alpha.10]: https://github.com/rust-embedded/embedded-hal/blob/v1.0.0-alpha.10/embedded-hal/CHANGELOG.md --- CHANGELOG.md | 4 ++++ Cargo.toml | 3 ++- src/dwt.rs | 10 +++------ src/fmpi2c/hal_1.rs | 30 ++------------------------ src/gpio/dynamic.rs | 7 ++++++ src/gpio/hal_1.rs | 52 ++------------------------------------------- src/i2c.rs | 2 +- src/i2c/hal_1.rs | 31 ++------------------------- src/serial/hal_1.rs | 7 ++---- src/spi/hal_1.rs | 11 ++++------ src/timer/hal_1.rs | 23 ++++++-------------- 11 files changed, 36 insertions(+), 144 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfa5c315..6720742f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - move gpio impls in subdir, remove unused `From` impls + - Bump `embedded-hal` to `1.0.0-alpha.10`. See [their changelog][embedded-hal-1.0.0-alpha.10] for further details. Note that this included breaking changes to the previous alpha APIs. [#663] + +[#663]: https://github.com/stm32-rs/stm32f4xx-hal/pull/663 +[embedded-hal-1.0.0-alpha.10]: https://github.com/rust-embedded/embedded-hal/blob/v1.0.0-alpha.10/embedded-hal/CHANGELOG.md ## [v0.16.2] - 2023-06-27 diff --git a/Cargo.toml b/Cargo.toml index 18c03e92..207e02b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ embedded-dma = "0.2.0" bare-metal = { version = "1" } void = { default-features = false, version = "1.0.2" } embedded-hal = { features = ["unproven"], version = "0.2.7" } +embedded-hal-nb = "1.0.0-alpha.2" display-interface = { version = "0.4.1", optional = true } fugit = "0.3.6" fugit-timer = "0.1.3" @@ -53,7 +54,7 @@ version = "0.3.14" default-features = false [dependencies.embedded-hal-one] -version = "=1.0.0-alpha.8" +version = "=1.0.0-alpha.10" package = "embedded-hal" [dependencies.stm32_i2s_v12x] diff --git a/src/dwt.rs b/src/dwt.rs index ee79a3da..ed847150 100644 --- a/src/dwt.rs +++ b/src/dwt.rs @@ -114,23 +114,19 @@ impl> embedded_hal::blocking::delay::DelayMs for Delay { } } -impl embedded_hal_one::delay::blocking::DelayUs for Delay { - type Error = core::convert::Infallible; - - fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> { +impl embedded_hal_one::delay::DelayUs for Delay { + fn delay_us(&mut self, us: u32) { // Convert us to ticks let start = DWT::cycle_count(); let ticks = (us as u64 * self.clock.raw() as u64) / 1_000_000; Delay::delay_ticks(start, ticks); - Ok(()) } - fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { + fn delay_ms(&mut self, ms: u32) { // Convert ms to ticks let start = DWT::cycle_count(); let ticks = (ms as u64 * self.clock.raw() as u64) / 1_000; Delay::delay_ticks(start, ticks); - Ok(()) } } diff --git a/src/fmpi2c/hal_1.rs b/src/fmpi2c/hal_1.rs index fe253230..0f217194 100644 --- a/src/fmpi2c/hal_1.rs +++ b/src/fmpi2c/hal_1.rs @@ -9,9 +9,9 @@ impl ErrorType for super::FMPI2c { mod blocking { use super::super::{fmpi2c1, FMPI2c, Instance}; use core::ops::Deref; - use embedded_hal_one::i2c::blocking::Operation; + use embedded_hal_one::i2c::Operation; - impl embedded_hal_one::i2c::blocking::I2c for FMPI2c + impl embedded_hal_one::i2c::I2c for FMPI2c where I2C: Deref, { @@ -23,13 +23,6 @@ mod blocking { self.write(addr, bytes) } - fn write_iter(&mut self, _addr: u8, _bytes: B) -> Result<(), Self::Error> - where - B: IntoIterator, - { - todo!() - } - fn write_read( &mut self, addr: u8, @@ -39,18 +32,6 @@ mod blocking { self.write_read(addr, bytes, buffer) } - fn write_iter_read( - &mut self, - _addr: u8, - _bytes: B, - _buffer: &mut [u8], - ) -> Result<(), Self::Error> - where - B: IntoIterator, - { - todo!() - } - fn transaction( &mut self, _addr: u8, @@ -58,12 +39,5 @@ mod blocking { ) -> Result<(), Self::Error> { todo!() } - - fn transaction_iter<'a, O>(&mut self, _addr: u8, _operations: O) -> Result<(), Self::Error> - where - O: IntoIterator>, - { - todo!() - } } } diff --git a/src/gpio/dynamic.rs b/src/gpio/dynamic.rs index d95c3b44..3733324c 100644 --- a/src/gpio/dynamic.rs +++ b/src/gpio/dynamic.rs @@ -1,4 +1,5 @@ use super::*; +use embedded_hal_one::digital::ErrorKind; /// Pin type with dynamic mode /// @@ -30,6 +31,12 @@ pub enum PinModeError { IncorrectMode, } +impl embedded_hal_one::digital::Error for PinModeError { + fn kind(&self) -> ErrorKind { + ErrorKind::Other + } +} + impl Dynamic { /// Is pin in readable mode pub fn is_input(&self) -> bool { diff --git a/src/gpio/hal_1.rs b/src/gpio/hal_1.rs index 09afaacb..701def76 100644 --- a/src/gpio/hal_1.rs +++ b/src/gpio/hal_1.rs @@ -1,23 +1,14 @@ use core::convert::Infallible; use super::{ - dynamic::PinModeError, marker, DynamicPin, ErasedPin, Input, OpenDrain, Output, - PartiallyErasedPin, Pin, PinMode, + dynamic::PinModeError, marker, DynamicPin, ErasedPin, Output, PartiallyErasedPin, Pin, }; pub use embedded_hal_one::digital::PinState; use embedded_hal_one::digital::{ - blocking::{InputPin, IoPin, OutputPin, StatefulOutputPin, ToggleableOutputPin}, - ErrorType, + ErrorType, InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin, }; -fn into_state(state: PinState) -> super::PinState { - match state { - PinState::Low => super::PinState::Low, - PinState::High => super::PinState::High, - } -} - // Implementations for `Pin` impl ErrorType for Pin { type Error = Infallible; @@ -72,45 +63,6 @@ where } } -impl IoPin for Pin> { - type Error = Infallible; - fn into_input_pin(self) -> Result { - Ok(self) - } - fn into_output_pin(mut self, state: PinState) -> Result { - self.set_state(into_state(state)); - Ok(self) - } -} - -impl IoPin, Self> for Pin> -where - Output: PinMode, -{ - type Error = Infallible; - fn into_input_pin(self) -> Result, Self::Error> { - Ok(self.into_input()) - } - fn into_output_pin(mut self, state: PinState) -> Result { - self.set_state(into_state(state)); - Ok(self) - } -} - -impl IoPin>> for Pin -where - Output: PinMode, -{ - type Error = Infallible; - fn into_input_pin(self) -> Result { - Ok(self) - } - fn into_output_pin(mut self, state: PinState) -> Result>, Self::Error> { - self._set_state(into_state(state)); - Ok(self.into_mode()) - } -} - // Implementations for `ErasedPin` impl ErrorType for ErasedPin { type Error = core::convert::Infallible; diff --git a/src/i2c.rs b/src/i2c.rs index 0be7d0f1..ca98ecde 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -6,7 +6,7 @@ use crate::rcc::{Enable, Reset}; use crate::gpio; use crate::rcc::Clocks; -use embedded_hal_one::i2c::blocking::Operation; +use embedded_hal_one::i2c::Operation; use fugit::{HertzU32 as Hertz, RateExtU32}; mod hal_02; diff --git a/src/i2c/hal_1.rs b/src/i2c/hal_1.rs index 97e7a2ca..2142358e 100644 --- a/src/i2c/hal_1.rs +++ b/src/i2c/hal_1.rs @@ -18,9 +18,9 @@ impl ErrorType for super::I2c { mod blocking { use super::super::{I2c, Instance}; - use embedded_hal_one::i2c::blocking::Operation; + use embedded_hal_one::i2c::Operation; - impl embedded_hal_one::i2c::blocking::I2c for I2c { + impl embedded_hal_one::i2c::I2c for I2c { fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { self.read(addr, buffer) } @@ -29,13 +29,6 @@ mod blocking { self.write(addr, bytes) } - fn write_iter(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> - where - B: IntoIterator, - { - self.write_iter(addr, bytes) - } - fn write_read( &mut self, addr: u8, @@ -45,18 +38,6 @@ mod blocking { self.write_read(addr, bytes, buffer) } - fn write_iter_read( - &mut self, - addr: u8, - bytes: B, - buffer: &mut [u8], - ) -> Result<(), Self::Error> - where - B: IntoIterator, - { - self.write_iter_read(addr, bytes, buffer) - } - fn transaction( &mut self, addr: u8, @@ -64,13 +45,5 @@ mod blocking { ) -> Result<(), Self::Error> { self.transaction_slice(addr, operations) } - - fn transaction_iter<'a, O>(&mut self, addr: u8, operations: O) -> Result<(), Self::Error> - where - O: IntoIterator>, - { - let it = operations.into_iter(); - self.transaction(addr, it) - } } } diff --git a/src/serial/hal_1.rs b/src/serial/hal_1.rs index f1afa43f..e34087b7 100644 --- a/src/serial/hal_1.rs +++ b/src/serial/hal_1.rs @@ -18,10 +18,7 @@ mod nb { use core::ops::Deref; use super::super::{Error, Instance, RegisterBlockImpl, Rx, Serial, Tx}; - use embedded_hal_one::serial::{ - nb::{Read, Write}, - ErrorType, - }; + use embedded_hal_nb::serial::{ErrorType, Read, Write}; impl Read for Serial where @@ -107,7 +104,7 @@ mod blocking { use super::super::{Instance, RegisterBlockImpl, Serial, Tx}; use super::ErrorType; - use embedded_hal_one::serial::blocking::Write; + use embedded_hal_one::serial::Write; impl Write for Serial where diff --git a/src/spi/hal_1.rs b/src/spi/hal_1.rs index abcfe4da..52f35973 100644 --- a/src/spi/hal_1.rs +++ b/src/spi/hal_1.rs @@ -45,7 +45,7 @@ impl ErrorType for super::Spi mod nb { use super::super::{Error, FrameSize, Instance, Spi}; - use embedded_hal_one::spi::nb::FullDuplex; + use embedded_hal_nb::spi::FullDuplex; impl FullDuplex for Spi where @@ -63,10 +63,7 @@ mod nb { mod blocking { use super::super::{FrameSize, Instance, Spi}; - use embedded_hal_one::spi::{ - blocking::{SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite}, - nb::FullDuplex, - }; + use embedded_hal_one::spi::{SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite}; impl SpiBus for Spi where @@ -112,8 +109,8 @@ mod blocking { { fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> { for word in words { - nb::block!(>::write(self, W::default()))?; - *word = nb::block!(>::read(self))?; + nb::block!(self.write_nonblocking(W::default()))?; + *word = nb::block!(self.read_nonblocking())?; } Ok(()) diff --git a/src/timer/hal_1.rs b/src/timer/hal_1.rs index acc30650..e1864476 100644 --- a/src/timer/hal_1.rs +++ b/src/timer/hal_1.rs @@ -3,36 +3,27 @@ //! TIM2 and TIM5 are a general purpose 32-bit auto-reload up/downcounter with //! a 16-bit prescaler. -use core::convert::Infallible; -use embedded_hal_one::delay::blocking::DelayUs; +use embedded_hal_one::delay::DelayUs; -use super::{Delay, Error, Instance, SysDelay}; +use super::{Delay, Instance, SysDelay}; use fugit::ExtU32; impl DelayUs for SysDelay { - type Error = Infallible; - - fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> { + fn delay_us(&mut self, us: u32) { self.delay(us.micros()); - - Ok(()) } - fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { - self.delay_us(ms * 1_000) + fn delay_ms(&mut self, ms: u32) { + self.delay_us(ms * 1_000); } } impl DelayUs for Delay { - type Error = Error; - - fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> { + fn delay_us(&mut self, us: u32) { self.delay(us.micros()); - Ok(()) } - fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { + fn delay_ms(&mut self, ms: u32) { self.delay(ms.millis()); - Ok(()) } } From 6fa9e558e2e3f2f927f18fec3b648322acbed692 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sun, 2 Jul 2023 13:50:57 +0200 Subject: [PATCH 2/2] implement `embedded-hal=1.0.0-alpha.10` `SetDutyCycle` --- src/timer/hal_1.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/timer/hal_1.rs b/src/timer/hal_1.rs index e1864476..3f835473 100644 --- a/src/timer/hal_1.rs +++ b/src/timer/hal_1.rs @@ -3,9 +3,10 @@ //! TIM2 and TIM5 are a general purpose 32-bit auto-reload up/downcounter with //! a 16-bit prescaler. +use core::convert::Infallible; use embedded_hal_one::delay::DelayUs; -use super::{Delay, Instance, SysDelay}; +use super::{Delay, Instance, PwmChannel, SysDelay, WithPwm}; use fugit::ExtU32; impl DelayUs for SysDelay { @@ -27,3 +28,19 @@ impl DelayUs for Delay { self.delay(ms.millis()); } } + +impl embedded_hal_one::pwm::ErrorType for PwmChannel { + type Error = Infallible; +} + +impl embedded_hal_one::pwm::SetDutyCycle + for PwmChannel +{ + fn get_max_duty_cycle(&self) -> u16 { + self.get_max_duty() + } + fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> { + self.set_duty(duty); + Ok(()) + } +}