Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update embedded_hal 1 to alpha.10 #653

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Cargo.toml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

due to semver and the way cargo handles semver-based dependencies these updates are not necessary (the Cargo.lock will automatically contain the newer versions if you build it fresh or run cargo update). forcing the update here is only necessary if you rely on a feature (or bugfix) introduced in the newer version. is this the case here?

Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ bare-metal = { version = "1" }
void = { default-features = false, version = "1.0.2" }
embedded-hal = { features = ["unproven"], version = "0.2.7" }
display-interface = { version = "0.4.1", optional = true }
fugit = "0.3.6"
fugit = "0.3.7"
fugit-timer = "0.1.3"
rtic-monotonic = { version = "1.0", optional = true }
systick-monotonic = { version = "1.0", optional = true }
bitflags = "2.2"
bitflags = "2.3"
embedded-storage = "0.2"

[dependencies.time]
version = "0.3.14"
version = "0.3.21"
default-features = false

[dependencies.embedded-hal-one]
version = "=1.0.0-alpha.8"
version = "=1.0.0-alpha.10"
package = "embedded-hal"

[dependencies.embedded-hal-nb]
version = "=1.0.0-alpha.2"

[dependencies.stm32_i2s_v12x]
version = "0.5.0"
optional = true
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!()
}
}
}
60 changes: 10 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,
Error, ErrorKind, 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 Expand Up @@ -219,6 +171,14 @@ where
}
}

impl Error for PinModeError {
fn kind(&self) -> ErrorKind {
match self {
Self::IncorrectMode => ErrorKind::Other,
}
}
}

// Implementations for `DynamicPin
impl<const P: char, const N: u8> ErrorType for DynamicPin<P, N> {
type Error = PinModeError;
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)
}
}
}
8 changes: 3 additions & 5 deletions src/serial/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ 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::{Read, Write};
use embedded_hal_one::serial::ErrorType;

impl<USART: Instance, WORD: Copy> Read<WORD> for Serial<USART, WORD>
where
Expand Down Expand Up @@ -107,7 +105,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
11 changes: 4 additions & 7 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,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<SPI, const BIDI: bool, W: FrameSize + 'static> SpiBus<W> for Spi<SPI, BIDI, W>
where
Expand Down Expand Up @@ -112,8 +109,8 @@ mod blocking {
{
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
for word in words {
nb::block!(<Self as FullDuplex<W>>::write(self, W::default()))?;
*word = nb::block!(<Self as FullDuplex<W>>::read(self))?;
nb::block!(self.write_nonblocking(W::default()))?;
*word = nb::block!(self.read_nonblocking())?;
}

Ok(())
Expand Down
35 changes: 21 additions & 14 deletions src/timer/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@
//! a 16-bit prescaler.

use core::convert::Infallible;
use embedded_hal_one::delay::blocking::DelayUs;

use super::{Delay, Error, Instance, SysDelay};
use embedded_hal_one::delay::DelayUs;

use super::{Delay, Instance, SysDelay, WithPwm, PwmChannel};
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> {
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(())
}
}

impl<TIM: Instance + WithPwm, const C: u8> embedded_hal_one::pwm::ErrorType for PwmChannel<TIM, C> {
type Error = Infallible;
}

impl<TIM: Instance + WithPwm, const C: u8> embedded_hal_one::pwm::SetDutyCycle for PwmChannel<TIM, C> {
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(())
}
}