Skip to content

Commit

Permalink
Make embedded-hal 0.2.7 support optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwandor committed Jan 29, 2024
1 parent ce79293 commit bc255e3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ embedded-dma = "0.2.0"
embedded-hal = "1.0.0"
embedded-hal-02 = { package = "embedded-hal", version = "0.2.7", features = [
"unproven",
] }
], optional = true }
embedded-io = "0.6.1"
gd32f1 = { version = "0.8.0", features = ["critical-section"] }
nb = "1.1.0"
Expand Down
4 changes: 2 additions & 2 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::pac::{
};
use crate::rcu::{Clocks, Enable, Reset, APB2};
use core::{
convert::Infallible,
marker::PhantomData,
sync::atomic::{self, Ordering},
};
Expand Down Expand Up @@ -494,12 +493,13 @@ impl Adc {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<WORD, PIN> embedded_hal_02::adc::OneShot<ADC, WORD, PIN> for Adc
where
WORD: From<u16>,
PIN: Channel<ADC, ID = u8>,
{
type Error = Infallible;
type Error = core::convert::Infallible;

fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
let res = self.read_channel(pin);
Expand Down
19 changes: 12 additions & 7 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::rcu::Clocks;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use embedded_hal::delay::DelayNs;
use embedded_hal_02::blocking::delay::{DelayMs, DelayUs};

/// System timer (SysTick) as a delay provider
pub struct Delay {
Expand Down Expand Up @@ -60,37 +59,43 @@ impl DelayNs for Delay {
}
}

impl DelayMs<u32> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
DelayNs::delay_ms(self, ms);
}
}

impl DelayMs<u16> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
DelayNs::delay_ms(self, ms.into());
}
}

impl DelayMs<u8> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
DelayNs::delay_ms(self, ms.into());
}
}

impl DelayUs<u32> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayUs<u32> for Delay {
fn delay_us(&mut self, us: u32) {
DelayNs::delay_us(self, us)
}
}

impl DelayUs<u16> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayUs<u16> for Delay {
fn delay_us(&mut self, us: u16) {
DelayNs::delay_us(self, us.into())
}
}

impl DelayUs<u8> for Delay {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::blocking::delay::DelayUs<u8> for Delay {
fn delay_us(&mut self, us: u8) {
DelayNs::delay_us(self, us.into())
}
Expand Down
3 changes: 3 additions & 0 deletions src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl<TIMER, PIN> SetDutyCycle for PwmChannel<TIMER, PIN> {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<TIMER, PIN> embedded_hal_02::PwmPin for PwmChannel<TIMER, PIN> {
type Duty = u16;

Expand Down Expand Up @@ -263,6 +264,7 @@ impl<TIMER, PIN> PwmChannelComplementary<TIMER, PIN> {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<TIMER, PIN> embedded_hal_02::PwmPin for PwmChannelComplementary<TIMER, PIN> {
type Duty = u16;

Expand Down Expand Up @@ -643,6 +645,7 @@ macro_rules! hal {
)?
}

#[cfg(feature = "embedded-hal-02")]
impl<PINS> embedded_hal_02::Pwm for Pwm<$TIMERX, PINS>
where
PINS: Pins<$TIMERX>,
Expand Down
24 changes: 14 additions & 10 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use crate::time::Hertz;
use cast::{u16, u32, u64};
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m::peripheral::SYST;
use embedded_hal_02::timer::{Cancel, CountDown, Periodic};
use void::Void;

/// Interrupt events
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -164,7 +162,8 @@ impl CountDownTimer<SYST> {
}
}

impl CountDown for CountDownTimer<SYST> {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::timer::CountDown for CountDownTimer<SYST> {
type Time = Hertz;

fn start<T>(&mut self, timeout: T)
Expand All @@ -174,7 +173,7 @@ impl CountDown for CountDownTimer<SYST> {
self.start(timeout);
}

fn wait(&mut self) -> nb::Result<(), Void> {
fn wait(&mut self) -> nb::Result<(), void::Void> {
if self.has_elapsed() {
Ok(())
} else {
Expand All @@ -183,15 +182,17 @@ impl CountDown for CountDownTimer<SYST> {
}
}

impl Cancel for CountDownTimer<SYST> {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::timer::Cancel for CountDownTimer<SYST> {
type Error = Error;

fn cancel(&mut self) -> Result<(), Self::Error> {
self.cancel()
}
}

impl Periodic for CountDownTimer<SYST> {}
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::timer::Periodic for CountDownTimer<SYST> {}

/// Helper methods used by other parts of the HAL, such as PWM.
pub(crate) trait TimerExt {
Expand Down Expand Up @@ -378,7 +379,8 @@ macro_rules! hal {
}
}

impl CountDown for CountDownTimer<$TIMERX> {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::timer::CountDown for CountDownTimer<$TIMERX> {
type Time = Hertz;

fn start<T>(&mut self, timeout: T)
Expand All @@ -388,7 +390,7 @@ macro_rules! hal {
self.start(timeout);
}

fn wait(&mut self) -> nb::Result<(), Void> {
fn wait(&mut self) -> nb::Result<(), void::Void> {
if !self.is_pending(Event::Update) {
Err(nb::Error::WouldBlock)
} else {
Expand All @@ -398,15 +400,17 @@ macro_rules! hal {
}
}

impl Cancel for CountDownTimer<$TIMERX> {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::timer::Cancel for CountDownTimer<$TIMERX> {
type Error = Error;

fn cancel(&mut self) -> Result<(), Self::Error> {
self.cancel()
}
}

impl Periodic for CountDownTimer<$TIMERX> {}
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::timer::Periodic for CountDownTimer<$TIMERX> {}
};
}

Expand Down

0 comments on commit bc255e3

Please sign in to comment.