Skip to content

Commit

Permalink
Refactoring e-h 0.2 traits
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Aug 6, 2024
1 parent 5166da8 commit 1632cfd
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 99 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
fugit = "0.3.5"
embedded-hal = { version = "0.2.6", features = ["unproven"] }
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "1" }
embedded-dma = "0.2.0"
cortex-m = { version = "^0.7.7", features = ["critical-section-single-core"] }
defmt = { version = ">=0.2.0,<0.4", optional = true }
Expand Down
13 changes: 6 additions & 7 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
//! - [Using ADC1 and ADC2 in parallel](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/adc12_parallel.rs)
//! - [Using ADC1 through DMA](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/adc_dma.rs)

use crate::hal::adc::{Channel, OneShot};
use crate::hal::blocking::delay::DelayUs;
use embedded_hal_02::blocking::delay::DelayUs;

use core::convert::Infallible;
use core::marker::PhantomData;
Expand Down Expand Up @@ -189,7 +188,7 @@ impl AdcCalLinear {
macro_rules! adc_pins {
($ADC:ident, $($input:ty => $chan:expr),+ $(,)*) => {
$(
impl Channel<$ADC> for $input {
impl embedded_hal_02::adc::Channel<$ADC> for $input {
type ID = u8;

fn channel() -> u8 {
Expand Down Expand Up @@ -823,7 +822,7 @@ macro_rules! adc_hal {
/// The value can be then read through the `read_sample` method.
// Refer to RM0433 Rev 7 - Chapter 25.4.16
pub fn start_conversion<PIN>(&mut self, _pin: &mut PIN)
where PIN: Channel<$ADC, ID = u8>,
where PIN: embedded_hal_02::adc::Channel<$ADC, ID = u8>,
{
let chan = PIN::channel();
assert!(chan <= 19);
Expand All @@ -841,7 +840,7 @@ macro_rules! adc_hal {
/// This method starts a conversion sequence with DMA
/// enabled. The DMA mode selected depends on the [`AdcDmaMode`] specified.
pub fn start_conversion_dma<PIN>(&mut self, _pin: &mut PIN, mode: AdcDmaMode)
where PIN: Channel<$ADC, ID = u8>,
where PIN: embedded_hal_02::adc::Channel<$ADC, ID = u8>,
{
let chan = PIN::channel();
assert!(chan <= 19);
Expand Down Expand Up @@ -1112,10 +1111,10 @@ macro_rules! adc_hal {
}
}

impl<WORD, PIN> OneShot<$ADC, WORD, PIN> for Adc<$ADC, Enabled>
impl<WORD, PIN> embedded_hal_02::adc::OneShot<$ADC, WORD, PIN> for Adc<$ADC, Enabled>
where
WORD: From<u32>,
PIN: Channel<$ADC, ID = u8>,
PIN: embedded_hal_02::adc::Channel<$ADC, ID = u8>,
{
type Error = ();

Expand Down
2 changes: 1 addition & 1 deletion src/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::marker::PhantomData;
use core::mem::MaybeUninit;

use crate::gpio::{self, Analog};
use crate::hal::blocking::delay::DelayUs;
use crate::hal_02::blocking::delay::DelayUs;
use crate::rcc::{rec, ResetEnable};
#[cfg(not(feature = "rm0455"))]
use crate::stm32::DAC as DAC1;
Expand Down
31 changes: 14 additions & 17 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@
use cast::u32;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use embedded_hal::{
blocking::delay::{DelayMs, DelayUs},
timer::CountDown,
};
use void::Void;

use crate::nb::block;
Expand Down Expand Up @@ -110,7 +106,7 @@ impl<'a> Countdown<'a> {
}
}

impl<'a> CountDown for Countdown<'a> {
impl<'a> embedded_hal_02::timer::CountDown for Countdown<'a> {
type Time = fugit::MicrosDurationU32;

fn start<T>(&mut self, count: T)
Expand Down Expand Up @@ -163,25 +159,26 @@ impl Delay {
}
}

impl DelayMs<u32> for Delay {
impl embedded_hal_02::blocking::delay::DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
use embedded_hal_02::blocking::delay::DelayUs;
self.delay_us(ms * 1_000);
}
}

impl DelayMs<u16> for Delay {
impl embedded_hal_02::blocking::delay::DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
self.delay_ms(u32(ms));
}
}

impl DelayMs<u8> for Delay {
impl embedded_hal_02::blocking::delay::DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
self.delay_ms(u32(ms));
}
}

impl DelayUs<u32> for Delay {
impl embedded_hal_02::blocking::delay::DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
const MAX_RVR: u32 = 0x00FF_FFFF;
Expand Down Expand Up @@ -220,13 +217,13 @@ impl DelayUs<u32> for Delay {
}
}

impl DelayUs<u16> for Delay {
impl embedded_hal_02::blocking::delay::DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
self.delay_us(u32(us))
}
}

impl DelayUs<u8> for Delay {
impl embedded_hal_02::blocking::delay::DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
self.delay_us(u32(us))
}
Expand All @@ -251,9 +248,9 @@ macro_rules! impl_delay_from_count_down_timer {
($(($Delay:ident, $delay:ident, $num:expr)),+) => {
$(

impl<T> $Delay<u32> for DelayFromCountDownTimer<T>
impl<T> embedded_hal_02::blocking::delay::$Delay<u32> for DelayFromCountDownTimer<T>
where
T: CountDown<Time = Hertz>,
T: embedded_hal_02::timer::CountDown<Time = Hertz>,
{
fn $delay(&mut self, t: u32) {
let mut time_left = t;
Expand All @@ -280,18 +277,18 @@ macro_rules! impl_delay_from_count_down_timer {
}
}

impl<T> $Delay<u16> for DelayFromCountDownTimer<T>
impl<T> embedded_hal_02::blocking::delay::$Delay<u16> for DelayFromCountDownTimer<T>
where
T: CountDown<Time = Hertz>,
T: embedded_hal_02::timer::CountDown<Time = Hertz>,
{
fn $delay(&mut self, t: u16) {
self.$delay(t as u32);
}
}

impl<T> $Delay<u8> for DelayFromCountDownTimer<T>
impl<T> embedded_hal_02::blocking::delay::$Delay<u8> for DelayFromCountDownTimer<T>
where
T: CountDown<Time = Hertz>,
T: embedded_hal_02::timer::CountDown<Time = Hertz>,
{
fn $delay(&mut self, t: u8) {
self.$delay(t as u32);
Expand Down
2 changes: 1 addition & 1 deletion src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod dynamic;
pub use dynamic::{Dynamic, DynamicPin};
mod hal_02;

pub use embedded_hal::digital::v2::PinState;
pub use embedded_hal_02::digital::v2::PinState;

use core::fmt;

Expand Down
56 changes: 35 additions & 21 deletions src/gpio/hal_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ use super::{
Output, PartiallyErasedPin, Pin, PinMode, PinState,
};

use embedded_hal::digital::v2::{
InputPin, IoPin, OutputPin, StatefulOutputPin, ToggleableOutputPin,
};

// Implementations for `Pin`

impl<const P: char, const N: u8, MODE> OutputPin for Pin<P, N, Output<MODE>> {
impl<const P: char, const N: u8, MODE> embedded_hal_02::digital::v2::OutputPin
for Pin<P, N, Output<MODE>>
{
type Error = Infallible;

#[inline(always)]
Expand All @@ -27,7 +25,8 @@ impl<const P: char, const N: u8, MODE> OutputPin for Pin<P, N, Output<MODE>> {
}
}

impl<const P: char, const N: u8, MODE> StatefulOutputPin
impl<const P: char, const N: u8, MODE>
embedded_hal_02::digital::v2::StatefulOutputPin
for Pin<P, N, Output<MODE>>
{
#[inline(always)]
Expand All @@ -41,7 +40,8 @@ impl<const P: char, const N: u8, MODE> StatefulOutputPin
}
}

impl<const P: char, const N: u8, MODE> ToggleableOutputPin
impl<const P: char, const N: u8, MODE>
embedded_hal_02::digital::v2::ToggleableOutputPin
for Pin<P, N, Output<MODE>>
{
type Error = Infallible;
Expand All @@ -53,7 +53,8 @@ impl<const P: char, const N: u8, MODE> ToggleableOutputPin
}
}

impl<const P: char, const N: u8, MODE> InputPin for Pin<P, N, MODE>
impl<const P: char, const N: u8, MODE> embedded_hal_02::digital::v2::InputPin
for Pin<P, N, MODE>
where
MODE: marker::Readable,
{
Expand All @@ -70,7 +71,7 @@ where
}
}

impl<const P: char, const N: u8> IoPin<Self, Self>
impl<const P: char, const N: u8> embedded_hal_02::digital::v2::IoPin<Self, Self>
for Pin<P, N, Output<OpenDrain>>
{
type Error = Infallible;
Expand All @@ -83,7 +84,8 @@ impl<const P: char, const N: u8> IoPin<Self, Self>
}
}

impl<const P: char, const N: u8, Otype> IoPin<Pin<P, N, Input>, Self>
impl<const P: char, const N: u8, Otype>
embedded_hal_02::digital::v2::IoPin<Pin<P, N, Input>, Self>
for Pin<P, N, Output<Otype>>
where
Output<Otype>: PinMode,
Expand All @@ -98,7 +100,8 @@ where
}
}

impl<const P: char, const N: u8, Otype> IoPin<Self, Pin<P, N, Output<Otype>>>
impl<const P: char, const N: u8, Otype>
embedded_hal_02::digital::v2::IoPin<Self, Pin<P, N, Output<Otype>>>
for Pin<P, N, Input>
where
Output<Otype>: PinMode,
Expand All @@ -118,7 +121,7 @@ where

// Implementations for `ErasedPin`

impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
impl<MODE> embedded_hal_02::digital::v2::OutputPin for ErasedPin<Output<MODE>> {
type Error = core::convert::Infallible;

#[inline(always)]
Expand All @@ -134,7 +137,9 @@ impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
}
}

impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
impl<MODE> embedded_hal_02::digital::v2::StatefulOutputPin
for ErasedPin<Output<MODE>>
{
#[inline(always)]
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
Expand All @@ -146,7 +151,9 @@ impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
}
}

impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
impl<MODE> embedded_hal_02::digital::v2::ToggleableOutputPin
for ErasedPin<Output<MODE>>
{
type Error = Infallible;

#[inline(always)]
Expand All @@ -156,7 +163,7 @@ impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
}
}

impl<MODE> InputPin for ErasedPin<MODE>
impl<MODE> embedded_hal_02::digital::v2::InputPin for ErasedPin<MODE>
where
MODE: marker::Readable,
{
Expand All @@ -175,7 +182,9 @@ where

// Implementations for `PartiallyErasedPin`

impl<const P: char, MODE> OutputPin for PartiallyErasedPin<P, Output<MODE>> {
impl<const P: char, MODE> embedded_hal_02::digital::v2::OutputPin
for PartiallyErasedPin<P, Output<MODE>>
{
type Error = Infallible;

#[inline(always)]
Expand All @@ -191,7 +200,7 @@ impl<const P: char, MODE> OutputPin for PartiallyErasedPin<P, Output<MODE>> {
}
}

impl<const P: char, MODE> StatefulOutputPin
impl<const P: char, MODE> embedded_hal_02::digital::v2::StatefulOutputPin
for PartiallyErasedPin<P, Output<MODE>>
{
#[inline(always)]
Expand All @@ -205,7 +214,7 @@ impl<const P: char, MODE> StatefulOutputPin
}
}

impl<const P: char, MODE> ToggleableOutputPin
impl<const P: char, MODE> embedded_hal_02::digital::v2::ToggleableOutputPin
for PartiallyErasedPin<P, Output<MODE>>
{
type Error = Infallible;
Expand All @@ -217,7 +226,8 @@ impl<const P: char, MODE> ToggleableOutputPin
}
}

impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, MODE>
impl<const P: char, MODE> embedded_hal_02::digital::v2::InputPin
for PartiallyErasedPin<P, MODE>
where
MODE: marker::Readable,
{
Expand All @@ -236,7 +246,9 @@ where

// Implementations for `DynamicPin`

impl<const P: char, const N: u8> OutputPin for DynamicPin<P, N> {
impl<const P: char, const N: u8> embedded_hal_02::digital::v2::OutputPin
for DynamicPin<P, N>
{
type Error = PinModeError;
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high()
Expand All @@ -246,7 +258,9 @@ impl<const P: char, const N: u8> OutputPin for DynamicPin<P, N> {
}
}

impl<const P: char, const N: u8> InputPin for DynamicPin<P, N> {
impl<const P: char, const N: u8> embedded_hal_02::digital::v2::InputPin
for DynamicPin<P, N>
{
type Error = PinModeError;
fn is_high(&self) -> Result<bool, Self::Error> {
self.is_high()
Expand Down
7 changes: 3 additions & 4 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use core::cmp;
use core::marker::PhantomData;

use crate::gpio::{self, Alternate, OpenDrain};
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
use crate::rcc::{rec, CoreClocks, ResetEnable};
use crate::stm32::{I2C1, I2C2, I2C3, I2C4};
use crate::time::Hertz;
Expand Down Expand Up @@ -557,7 +556,7 @@ macro_rules! i2c {
}
}

impl Write for I2c<$I2CX> {
impl embedded_hal_02::blocking::i2c::Write for I2c<$I2CX> {
type Error = Error;

fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
Expand Down Expand Up @@ -592,7 +591,7 @@ macro_rules! i2c {
}
}

impl WriteRead for I2c<$I2CX> {
impl embedded_hal_02::blocking::i2c::WriteRead for I2c<$I2CX> {
type Error = Error;

fn write_read(
Expand Down Expand Up @@ -641,7 +640,7 @@ macro_rules! i2c {
}
}

impl Read for I2c<$I2CX> {
impl embedded_hal_02::blocking::i2c::Read for I2c<$I2CX> {
type Error = Error;

fn read(
Expand Down
Loading

0 comments on commit 1632cfd

Please sign in to comment.