Skip to content

Commit

Permalink
update to embedded-hal=1.0.0-alpha.10
Browse files Browse the repository at this point in the history
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 stm32-rs#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
  • Loading branch information
rursprung committed Jul 2, 2023
1 parent 6ab795a commit 008fbeb
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 142 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed

- 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

### Changed
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand Down
10 changes: 3 additions & 7 deletions src/dwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,19 @@ impl<T: Into<u64>> embedded_hal::blocking::delay::DelayMs<T> 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(())
}
}

Expand Down
30 changes: 2 additions & 28 deletions src/fmpi2c/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ impl<I2C: Instance> ErrorType for super::FMPI2c<I2C> {
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<I2C: Instance> embedded_hal_one::i2c::blocking::I2c for FMPI2c<I2C>
impl<I2C: Instance> embedded_hal_one::i2c::I2c for FMPI2c<I2C>
where
I2C: Deref<Target = fmpi2c1::RegisterBlock>,
{
Expand All @@ -23,13 +23,6 @@ mod blocking {
self.write(addr, bytes)
}

fn write_iter<B>(&mut self, _addr: u8, _bytes: B) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
todo!()
}

fn write_read(
&mut self,
addr: u8,
Expand All @@ -39,31 +32,12 @@ mod blocking {
self.write_read(addr, bytes, buffer)
}

fn write_iter_read<B>(
&mut self,
_addr: u8,
_bytes: B,
_buffer: &mut [u8],
) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
todo!()
}

fn transaction(
&mut self,
_addr: u8,
_operations: &mut [Operation<'_>],
) -> Result<(), Self::Error> {
todo!()
}

fn transaction_iter<'a, O>(&mut self, _addr: u8, _operations: O) -> Result<(), Self::Error>
where
O: IntoIterator<Item = Operation<'a>>,
{
todo!()
}
}
}
7 changes: 7 additions & 0 deletions src/gpio/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use embedded_hal_one::digital::ErrorKind;

/// Pin type with dynamic mode
///
Expand Down Expand Up @@ -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 {
Expand Down
52 changes: 2 additions & 50 deletions src/gpio/hal_1.rs
Original file line number Diff line number Diff line change
@@ -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<const P: char, const N: u8, MODE> ErrorType for Pin<P, N, MODE> {
type Error = Infallible;
Expand Down Expand Up @@ -72,45 +63,6 @@ where
}
}

impl<const P: char, const N: u8> IoPin<Self, Self> for Pin<P, N, Output<OpenDrain>> {
type Error = Infallible;
fn into_input_pin(self) -> Result<Self, Self::Error> {
Ok(self)
}
fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
self.set_state(into_state(state));
Ok(self)
}
}

impl<const P: char, const N: u8, Otype> IoPin<Pin<P, N, Input>, Self> for Pin<P, N, Output<Otype>>
where
Output<Otype>: PinMode,
{
type Error = Infallible;
fn into_input_pin(self) -> Result<Pin<P, N, Input>, Self::Error> {
Ok(self.into_input())
}
fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
self.set_state(into_state(state));
Ok(self)
}
}

impl<const P: char, const N: u8, Otype> IoPin<Self, Pin<P, N, Output<Otype>>> for Pin<P, N, Input>
where
Output<Otype>: PinMode,
{
type Error = Infallible;
fn into_input_pin(self) -> Result<Self, Self::Error> {
Ok(self)
}
fn into_output_pin(mut self, state: PinState) -> Result<Pin<P, N, Output<Otype>>, Self::Error> {
self._set_state(into_state(state));
Ok(self.into_mode())
}
}

// Implementations for `ErasedPin`
impl<MODE> ErrorType for ErasedPin<MODE> {
type Error = core::convert::Infallible;
Expand Down
2 changes: 1 addition & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 2 additions & 29 deletions src/i2c/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ impl<I2C: super::Instance> ErrorType for super::I2c<I2C> {

mod blocking {
use super::super::{I2c, Instance};
use embedded_hal_one::i2c::blocking::Operation;
use embedded_hal_one::i2c::Operation;

impl<I2C: Instance> embedded_hal_one::i2c::blocking::I2c for I2c<I2C> {
impl<I2C: Instance> embedded_hal_one::i2c::I2c for I2c<I2C> {
fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
self.read(addr, buffer)
}
Expand All @@ -29,13 +29,6 @@ mod blocking {
self.write(addr, bytes)
}

fn write_iter<B>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
self.write_iter(addr, bytes)
}

fn write_read(
&mut self,
addr: u8,
Expand All @@ -45,32 +38,12 @@ mod blocking {
self.write_read(addr, bytes, buffer)
}

fn write_iter_read<B>(
&mut self,
addr: u8,
bytes: B,
buffer: &mut [u8],
) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
self.write_iter_read(addr, bytes, buffer)
}

fn transaction(
&mut self,
addr: u8,
operations: &mut [Operation<'_>],
) -> 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<Item = Operation<'a>>,
{
let it = operations.into_iter();
self.transaction(addr, it)
}
}
}
7 changes: 2 additions & 5 deletions src/serial/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<USART: Instance, WORD: Copy> Read<WORD> for Serial<USART, WORD>
where
Expand Down Expand Up @@ -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<USART: Instance, WORD: Copy> Write<WORD> for Serial<USART, WORD>
where
Expand Down
8 changes: 3 additions & 5 deletions src/spi/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<SPI: Instance, const BIDI: bool, W> ErrorType for super::Spi<SPI, BIDI, W>

mod nb {
use super::super::{Error, FrameSize, Instance, Spi};
use embedded_hal_one::spi::nb::FullDuplex;
use embedded_hal_nb::spi::FullDuplex;

impl<SPI, const BIDI: bool, W: FrameSize> FullDuplex<W> for Spi<SPI, BIDI, W>
where
Expand All @@ -63,10 +63,8 @@ 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_nb::spi::FullDuplex;
use embedded_hal_one::spi::{SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite};

impl<SPI, const BIDI: bool, W: FrameSize + 'static> SpiBus<W> for Spi<SPI, BIDI, W>
where
Expand Down
23 changes: 7 additions & 16 deletions src/timer/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TIM: Instance, const FREQ: u32> DelayUs for Delay<TIM, FREQ> {
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(())
}
}

0 comments on commit 008fbeb

Please sign in to comment.