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 250a2c7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 26 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
15 changes: 12 additions & 3 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::rcu::AHB;
use core::convert::Infallible;
use core::marker::PhantomData;
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};
use embedded_hal_02::digital::v2::toggleable;

/// Extension trait to split a GPIO peripheral in independent pins and registers
pub trait GpioExt {
Expand Down Expand Up @@ -175,7 +174,8 @@ pub struct Pin<MODE> {
unsafe impl<MODE> Sync for Pin<MODE> {}
unsafe impl<MODE> Send for Pin<MODE> {}

impl<MODE> toggleable::Default for Pin<Output<MODE>> {}
#[cfg(feature = "embedded-hal-02")]
impl<MODE> embedded_hal_02::digital::v2::toggleable::Default for Pin<Output<MODE>> {}

impl<MODE> ErrorType for Pin<MODE> {
type Error = Infallible;
Expand All @@ -197,6 +197,7 @@ impl<MODE> OutputPin for Pin<Output<MODE>> {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<MODE> embedded_hal_02::digital::v2::OutputPin for Pin<Output<MODE>> {
type Error = Infallible;

Expand Down Expand Up @@ -225,6 +226,7 @@ impl<MODE> StatefulOutputPin for Pin<Output<MODE>> {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<MODE> embedded_hal_02::digital::v2::StatefulOutputPin for Pin<Output<MODE>> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
self.is_set_low().map(|b| !b)
Expand All @@ -245,6 +247,7 @@ impl<MODE> InputPin for Pin<Input<MODE>> {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<MODE> embedded_hal_02::digital::v2::InputPin for Pin<Input<MODE>> {
type Error = Infallible;

Expand All @@ -267,6 +270,7 @@ impl InputPin for Pin<Output<OpenDrain>> {
}
}

#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::digital::v2::InputPin for Pin<Output<OpenDrain>> {
type Error = Infallible;

Expand Down Expand Up @@ -492,7 +496,8 @@ macro_rules! gpio_core {
}
}

impl<MODE> toggleable::Default for $PXi<Output<MODE>> {}
#[cfg(feature = "embedded-hal-02")]
impl<MODE> embedded_hal_02::digital::v2::toggleable::Default for $PXi<Output<MODE>> {}

impl<MODE> ErrorType for $PXi<MODE> {
type Error = Infallible;
Expand All @@ -508,6 +513,7 @@ macro_rules! gpio_core {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<MODE> embedded_hal_02::digital::v2::OutputPin for $PXi<Output<MODE>> {
type Error = Infallible;

Expand All @@ -530,6 +536,7 @@ macro_rules! gpio_core {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<MODE> embedded_hal_02::digital::v2::StatefulOutputPin for $PXi<Output<MODE>> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
self.is_set_low().map(|b| !b)
Expand All @@ -550,6 +557,7 @@ macro_rules! gpio_core {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<MODE> embedded_hal_02::digital::v2::InputPin for $PXi<Input<MODE>> {
type Error = Infallible;

Expand All @@ -572,6 +580,7 @@ macro_rules! gpio_core {
}
}

#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::digital::v2::InputPin for $PXi<Output<OpenDrain>> {
type Error = Infallible;

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
4 changes: 4 additions & 0 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN: TxPin<USART>, RXPIN> W
}
}

#[cfg(feature = "embedded-hal-02")]
impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN: TxPin<USART>, RXPIN>
embedded_hal_02::serial::Write<u8> for Serial<USART, TXPIN, RXPIN>
{
Expand Down Expand Up @@ -411,6 +412,7 @@ impl<USART: Deref<Target = usart0::RegisterBlock>> WriteReady for Tx<USART> {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<USART: Deref<Target = usart0::RegisterBlock>> embedded_hal_02::serial::Write<u8>
for Tx<USART>
{
Expand Down Expand Up @@ -476,6 +478,7 @@ impl<USART: Deref<Target = usart0::RegisterBlock>> ReadReady for Rx<USART> {
}
}

#[cfg(feature = "embedded-hal-02")]
impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN, RXPIN: RxPin<USART>>
embedded_hal_02::serial::Read<u8> for Serial<USART, TXPIN, RXPIN>
{
Expand All @@ -486,6 +489,7 @@ impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN, RXPIN: RxPin<USART>>
}
}

#[cfg(feature = "embedded-hal-02")]
impl<USART: Deref<Target = usart0::RegisterBlock>> embedded_hal_02::serial::Read<u8> for Rx<USART> {
type Error = Error;

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
7 changes: 4 additions & 3 deletions src/watchdog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
pac::{DBG, FWDGT},
time::MilliSeconds,
};
use embedded_hal_02::watchdog::{Watchdog, WatchdogEnable};

/// Wraps the Free Watchdog (FWDGT) peripheral
pub struct FreeWatchdog {
Expand Down Expand Up @@ -105,15 +104,17 @@ impl FreeWatchdog {
}
}

impl WatchdogEnable for FreeWatchdog {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::watchdog::WatchdogEnable for FreeWatchdog {
type Time = MilliSeconds;

fn start<T: Into<Self::Time>>(&mut self, period: T) {
self.start(period);
}
}

impl Watchdog for FreeWatchdog {
#[cfg(feature = "embedded-hal-02")]
impl embedded_hal_02::watchdog::Watchdog for FreeWatchdog {
fn feed(&mut self) {
self.feed();
}
Expand Down

0 comments on commit 250a2c7

Please sign in to comment.