From b002be9e8f91ec151f6bd970d280c3f41411ad80 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 11 Jan 2024 02:52:18 +0100 Subject: [PATCH 1/2] Remove unused imports. --- src/gpio/epoll.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gpio/epoll.rs b/src/gpio/epoll.rs index d454d384..58adedda 100644 --- a/src/gpio/epoll.rs +++ b/src/gpio/epoll.rs @@ -9,7 +9,7 @@ use libc::{ self, c_int, c_void, EFD_NONBLOCK, EFD_SEMAPHORE, EPOLL_CTL_ADD, EPOLL_CTL_DEL, EPOLL_CTL_MOD, }; -pub use libc::{epoll_event, EPOLLERR, EPOLLET, EPOLLIN, EPOLLONESHOT, EPOLLOUT, EPOLLPRI}; +pub use libc::{epoll_event, EPOLLERR, EPOLLET, EPOLLIN, EPOLLPRI}; pub type Result = result::Result; From dfb095f4c54353b67f9045f23a0ad0a39b104a3e Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 11 Jan 2024 02:32:50 +0100 Subject: [PATCH 2/2] Update to `embedded-hal` 1.0. --- Cargo.toml | 16 +++--- src/gpio.rs | 6 ++- src/gpio/hal.rs | 107 ++++++++++++++++++--------------------- src/gpio/hal_unproven.rs | 58 ++++++++++----------- src/gpio/pin.rs | 54 ++++++++++++++++---- src/hal.rs | 62 +++++++++++------------ src/i2c.rs | 6 ++- src/i2c/hal.rs | 44 ++++++++-------- src/lib.rs | 6 ++- src/pwm.rs | 6 ++- src/pwm/hal.rs | 3 +- src/pwm/hal_unproven.rs | 2 +- src/spi.rs | 16 ++++-- src/spi/hal.rs | 69 +++++++++++++------------ src/uart.rs | 6 ++- src/uart/hal.rs | 41 +++++++-------- 16 files changed, 277 insertions(+), 225 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 129419b1..4511d7e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,10 +15,10 @@ keywords = ["raspberry", "pi", "embedded-hal", "embedded-hal-impl", "hal"] [dependencies] libc = "0.2" -nb = { version = "0.1.1", optional = true } +nb = { version = "0.1.3", optional = true } embedded-hal-0 = { version = "0.2.7", optional = true, package = "embedded-hal" } -embedded-hal = { version = "=1.0.0-rc.2", optional = true } -embedded-hal-nb = { version = "=1.0.0-rc.2", optional = true } +embedded-hal = { version = "1", optional = true } +embedded-hal-nb = { version = "1", optional = true } void = { version = "1.0.2", optional = true } spin_sleep = { version = "1.0.0", optional = true } @@ -27,12 +27,12 @@ simple-signal = "1.1.1" [features] default = [] +embedded-hal-0 = ["dep:embedded-hal-0", "dep:void", "embedded-hal-nb", "nb"] +embedded-hal = ["dep:embedded-hal", "dep:spin_sleep"] +embedded-hal-nb = ["dep:embedded-hal-nb", "embedded-hal"] hal = [ - "nb", + "embedded-hal-0", "embedded-hal", "embedded-hal-nb", - "embedded-hal-0", - "spin_sleep", - "void", ] -hal-unproven = ["nb", "embedded-hal-0/unproven", "hal"] +hal-unproven = ["embedded-hal-0/unproven", "hal"] diff --git a/src/gpio.rs b/src/gpio.rs index eea2d942..8aec782b 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -125,7 +125,11 @@ use std::time::Duration; mod epoll; mod gpiomem; -#[cfg(feature = "hal")] +#[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" +))] mod hal; #[cfg(feature = "hal-unproven")] mod hal_unproven; diff --git a/src/gpio/hal.rs b/src/gpio/hal.rs index 63aebb87..7e4a1685 100644 --- a/src/gpio/hal.rs +++ b/src/gpio/hal.rs @@ -1,78 +1,73 @@ use core::convert::Infallible; -use embedded_hal::digital::{ - ErrorType, InputPin as InputPinHal, OutputPin as OutputPinHal, - StatefulOutputPin as StatefulOutputPinHal, ToggleableOutputPin as ToggleableOutputPinHal, -}; - use super::{InputPin, IoPin, Level, OutputPin, Pin}; -/// `ErrorType` trait implementation for `embedded-hal` v1.0.0. -impl ErrorType for Pin { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::ErrorType for Pin { type Error = Infallible; } -/// `InputPin` trait implementation for `embedded-hal` v1.0.0. -impl InputPinHal for Pin { - fn is_high(&self) -> Result { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::InputPin for Pin { + fn is_high(&mut self) -> Result { Ok(Self::read(self) == Level::High) } - fn is_low(&self) -> Result { + fn is_low(&mut self) -> Result { Ok(Self::read(self) == Level::Low) } } -/// `ErrorType` trait implementation for `embedded-hal` v1.0.0. -impl ErrorType for InputPin { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::ErrorType for InputPin { type Error = Infallible; } -/// `InputPin` trait implementation for `embedded-hal` v1.0.0. -impl InputPinHal for InputPin { - fn is_high(&self) -> Result { - Ok(Self::is_high(self)) +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::InputPin for InputPin { + fn is_high(&mut self) -> Result { + Ok((*self).is_high()) } - fn is_low(&self) -> Result { - Ok(Self::is_low(self)) + fn is_low(&mut self) -> Result { + Ok((*self).is_high()) } } -/// `ErrorType` trait implementation for `embedded-hal` v1.0.0. -impl ErrorType for IoPin { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::ErrorType for IoPin { type Error = Infallible; } -/// `InputPin` trait implementation for `embedded-hal` v1.0.0. -impl InputPinHal for IoPin { - fn is_high(&self) -> Result { - Ok(Self::is_high(self)) +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::InputPin for IoPin { + fn is_high(&mut self) -> Result { + Ok((*self).is_high()) } - fn is_low(&self) -> Result { - Ok(Self::is_low(self)) + fn is_low(&mut self) -> Result { + Ok((*self).is_low()) } } -/// `ErrorType` trait implementation for `embedded-hal` v1.0.0. -impl ErrorType for OutputPin { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::ErrorType for OutputPin { type Error = Infallible; } -/// `InputPin` trait implementation for `embedded-hal` v1.0.0. -impl InputPinHal for OutputPin { - fn is_high(&self) -> Result { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::InputPin for OutputPin { + fn is_high(&mut self) -> Result { Ok(Self::is_set_high(self)) } - fn is_low(&self) -> Result { + fn is_low(&mut self) -> Result { Ok(Self::is_set_low(self)) } } -/// `OutputPin` trait implementation for `embedded-hal` v1.0.0. -impl OutputPinHal for OutputPin { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::OutputPin for OutputPin { fn set_low(&mut self) -> Result<(), Self::Error> { OutputPin::set_low(self); @@ -86,32 +81,29 @@ impl OutputPinHal for OutputPin { } } -/// `OutputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::OutputPin for OutputPin { type Error = Infallible; fn set_low(&mut self) -> Result<(), Self::Error> { - OutputPinHal::set_low(self) + embedded_hal::digital::OutputPin::set_low(self) } fn set_high(&mut self) -> Result<(), Self::Error> { - OutputPinHal::set_high(self) + embedded_hal::digital::OutputPin::set_high(self) } } -/// `StatefulOutputPin` trait implementation for `embedded-hal` v1.0.0. -impl StatefulOutputPinHal for OutputPin { - fn is_set_high(&self) -> Result { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::StatefulOutputPin for OutputPin { + fn is_set_high(&mut self) -> Result { Ok(OutputPin::is_set_high(self)) } - fn is_set_low(&self) -> Result { + fn is_set_low(&mut self) -> Result { Ok(OutputPin::is_set_low(self)) } -} -/// `ToggleableOutputPin` trait implementation for `embedded-hal` v1.0.0. -impl ToggleableOutputPinHal for OutputPin { fn toggle(&mut self) -> Result<(), Self::Error> { OutputPin::toggle(self); @@ -119,8 +111,8 @@ impl ToggleableOutputPinHal for OutputPin { } } -/// `OutputPin` trait implementation for `embedded-hal` v1.0.0. -impl OutputPinHal for IoPin { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::OutputPin for IoPin { fn set_low(&mut self) -> Result<(), Self::Error> { IoPin::set_low(self); @@ -134,32 +126,29 @@ impl OutputPinHal for IoPin { } } -/// `OutputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::OutputPin for IoPin { type Error = Infallible; fn set_low(&mut self) -> Result<(), Self::Error> { - OutputPinHal::set_low(self) + embedded_hal::digital::OutputPin::set_low(self) } fn set_high(&mut self) -> Result<(), Self::Error> { - OutputPinHal::set_high(self) + embedded_hal::digital::OutputPin::set_high(self) } } -/// `StatefulOutputPin` trait implementation for `embedded-hal` v1.0.0. -impl StatefulOutputPinHal for IoPin { - fn is_set_high(&self) -> Result { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::digital::StatefulOutputPin for IoPin { + fn is_set_high(&mut self) -> Result { Ok(IoPin::is_high(self)) } - fn is_set_low(&self) -> Result { + fn is_set_low(&mut self) -> Result { Ok(IoPin::is_low(self)) } -} -/// `ToggleableOutputPin` trait implementation for `embedded-hal` v1.0.0. -impl ToggleableOutputPinHal for IoPin { fn toggle(&mut self) -> Result<(), Self::Error> { IoPin::toggle(self); @@ -167,7 +156,7 @@ impl ToggleableOutputPinHal for IoPin { } } -/// `PwmPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::PwmPin for OutputPin { type Duty = f64; @@ -196,7 +185,7 @@ impl embedded_hal_0::PwmPin for OutputPin { } } -/// `PwmPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::PwmPin for IoPin { type Duty = f64; diff --git a/src/gpio/hal_unproven.rs b/src/gpio/hal_unproven.rs index a026f2f8..a530633e 100644 --- a/src/gpio/hal_unproven.rs +++ b/src/gpio/hal_unproven.rs @@ -1,109 +1,103 @@ use core::convert::Infallible; use std::time::Duration; -use embedded_hal::digital::{ - InputPin as InputPinHal, StatefulOutputPin as StatefulOutputPinHal, - ToggleableOutputPin as ToggleableOutputPinHal, -}; - -use super::{InputPin, IoPin, OutputPin, Pin}; -use crate::gpio::Mode; +use super::{InputPin, IoPin, Level, Mode, OutputPin, Pin}; const NANOS_PER_SEC: f64 = 1_000_000_000.0; -/// Unproven `InputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::InputPin for Pin { type Error = Infallible; fn is_high(&self) -> Result { - InputPinHal::is_high(self) + Ok((*self).read() == Level::High) } fn is_low(&self) -> Result { - InputPinHal::is_low(self) + Ok((*self).read() == Level::Low) } } -/// Unproven `InputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::InputPin for InputPin { type Error = Infallible; fn is_high(&self) -> Result { - InputPinHal::is_high(self) + Ok((*self).is_high()) } fn is_low(&self) -> Result { - InputPinHal::is_low(self) + Ok((*self).is_high()) } } -/// Unproven `InputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::InputPin for IoPin { type Error = Infallible; fn is_high(&self) -> Result { - InputPinHal::is_high(self) + Ok((*self).is_high()) } fn is_low(&self) -> Result { - InputPinHal::is_low(self) + Ok((*self).is_high()) } } -/// Unproven `InputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::InputPin for OutputPin { type Error = Infallible; fn is_high(&self) -> Result { - InputPinHal::is_high(self) + Ok((*self).is_set_high()) } fn is_low(&self) -> Result { - InputPinHal::is_low(self) + Ok((*self).is_set_low()) } } -/// Unproven `StatefulOutputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::StatefulOutputPin for IoPin { fn is_set_high(&self) -> Result { - StatefulOutputPinHal::is_set_high(self) + Ok((*self).is_high()) } fn is_set_low(&self) -> Result { - StatefulOutputPinHal::is_set_low(self) + Ok((*self).is_low()) } } -/// Unproven `StatefulOutputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::StatefulOutputPin for OutputPin { fn is_set_high(&self) -> Result { - StatefulOutputPinHal::is_set_high(self) + Ok((*self).is_set_high()) } fn is_set_low(&self) -> Result { - StatefulOutputPinHal::is_set_low(self) + Ok((*self).is_set_low()) } } -/// Unproven `ToggleableOutputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::ToggleableOutputPin for IoPin { type Error = Infallible; fn toggle(&mut self) -> Result<(), Self::Error> { - ToggleableOutputPinHal::toggle(self) + embedded_hal::digital::StatefulOutputPin::toggle(self) } } -/// Unproven `ToggleableOutputPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::ToggleableOutputPin for OutputPin { type Error = Infallible; fn toggle(&mut self) -> Result<(), Self::Error> { - ToggleableOutputPinHal::toggle(self) + embedded_hal::digital::StatefulOutputPin::toggle(self) } } -/// Unproven `Pwm` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::Pwm for OutputPin { type Duty = f64; type Channel = (); @@ -162,7 +156,7 @@ impl embedded_hal_0::Pwm for OutputPin { } } -/// Unproven `Pwm` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::Pwm for IoPin { type Duty = f64; type Channel = (); @@ -221,7 +215,7 @@ impl embedded_hal_0::Pwm for IoPin { } } -/// Unproven `IoPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::digital::v2::IoPin for IoPin { type Error = Infallible; diff --git a/src/gpio/pin.rs b/src/gpio/pin.rs index ee5820ca..eee5da9f 100644 --- a/src/gpio/pin.rs +++ b/src/gpio/pin.rs @@ -112,7 +112,11 @@ macro_rules! impl_output { } // Store frequency/duty cycle for the embedded-hal PwmPin implementation. - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] { let period_s = period.as_secs() as f64 + (f64::from(period.subsec_nanos()) / NANOS_PER_SEC); @@ -581,10 +585,18 @@ pub struct OutputPin { bias: Bias, pub(crate) soft_pwm: Option, // Stores the softpwm frequency. Used for embedded_hal::PwmPin. - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] pub(crate) frequency: f64, // Stores the softpwm duty cycle. Used for embedded_hal::PwmPin. - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] pub(crate) duty_cycle: f64, } @@ -605,9 +617,17 @@ impl OutputPin { reset_on_drop: true, bias: Bias::Off, soft_pwm: None, - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] frequency: 0.0, - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] duty_cycle: 0.0, } } @@ -674,10 +694,18 @@ pub struct IoPin { bias: Bias, pub(crate) soft_pwm: Option, // Stores the softpwm frequency. Used for embedded_hal::PwmPin. - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] pub(crate) frequency: f64, // Stores the softpwm duty cycle. Used for embedded_hal::PwmPin. - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] pub(crate) duty_cycle: f64, } @@ -699,9 +727,17 @@ impl IoPin { reset_on_drop: true, bias: Bias::Off, soft_pwm: None, - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] frequency: 0.0, - #[cfg(feature = "hal")] + #[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" + ))] duty_cycle: 0.0, } } diff --git a/src/hal.rs b/src/hal.rs index ba33fab7..1cf31f14 100644 --- a/src/hal.rs +++ b/src/hal.rs @@ -6,17 +6,14 @@ //! This module is only included when either the `hal` or `hal-unproven` feature //! flag is enabled. -use std::time::{Duration, Instant}; - -use embedded_hal::delay::DelayNs; -use spin_sleep::sleep; -use void::Void; +use std::time::Duration; +#[cfg(feature = "embedded-hal-0")] +use std::time::Instant; /// Implements the `embedded-hal` `DelayMs` and `DelayNs` traits. #[derive(Debug, Default)] pub struct Delay; -/// `Delay` trait implementation for `embedded-hal` v1.0.0. impl Delay { /// Constructs a new `Delay`. pub fn new() -> Delay { @@ -24,84 +21,84 @@ impl Delay { } } -/// `DelayMs` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, ms: u8) { - DelayNs::delay_ms(self, ms as u32); + embedded_hal::delay::DelayNs::delay_ms(self, ms as u32); } } -/// `DelayMs` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, ms: u16) { - DelayNs::delay_ms(self, ms as u32); + embedded_hal::delay::DelayNs::delay_ms(self, ms as u32); } } -/// `DelayMs` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, ms: u32) { - DelayNs::delay_ms(self, ms); + embedded_hal::delay::DelayNs::delay_ms(self, ms); } } -/// `DelayMs` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, mut ms: u64) { while ms > (u32::MAX as u64) { ms -= u32::MAX as u64; - DelayNs::delay_ms(self, u32::MAX); + embedded_hal::delay::DelayNs::delay_ms(self, u32::MAX); } - DelayNs::delay_ms(self, ms as u32); + embedded_hal::delay::DelayNs::delay_ms(self, ms as u32); } } -/// `DelayNs` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, us: u8) { - DelayNs::delay_us(self, us as u32); + embedded_hal::delay::DelayNs::delay_us(self, us as u32); } } -/// `DelayNs` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, us: u16) { - DelayNs::delay_us(self, us as u32); + embedded_hal::delay::DelayNs::delay_us(self, us as u32); } } -/// `DelayNs` trait implementation for `embedded-hal` v1.0.0. -impl DelayNs for Delay { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::delay::DelayNs for Delay { fn delay_ns(&mut self, ns: u32) { - sleep(Duration::from_nanos(ns.into())); + spin_sleep::sleep(Duration::from_nanos(ns.into())); } fn delay_us(&mut self, us: u32) { - sleep(Duration::from_micros(us.into())); + spin_sleep::sleep(Duration::from_micros(us.into())); } fn delay_ms(&mut self, ms: u32) { - sleep(Duration::from_millis(ms.into())); + spin_sleep::sleep(Duration::from_millis(ms.into())); } } -/// `DelayNs` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, us: u32) { - DelayNs::delay_us(self, us); + embedded_hal::delay::DelayNs::delay_us(self, us); } } -/// `DelayNs` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, mut us: u64) { while us > (u32::MAX as u64) { us -= u32::MAX as u64; - DelayNs::delay_us(self, u32::MAX); + embedded_hal::delay::DelayNs::delay_us(self, u32::MAX); } - DelayNs::delay_us(self, us as u32); + embedded_hal::delay::DelayNs::delay_us(self, us as u32); } } @@ -121,12 +118,14 @@ impl From for Duration { } /// Implements the `embedded-hal` `CountDown` trait. +#[cfg(feature = "embedded-hal-0")] #[derive(Debug, Copy, Clone)] pub struct Timer { start: Instant, duration: Duration, } +#[cfg(feature = "embedded-hal-0")] impl Timer { /// Constructs a new `Timer`. pub fn new() -> Self { @@ -137,13 +136,14 @@ impl Timer { } } +#[cfg(feature = "embedded-hal-0")] impl Default for Timer { fn default() -> Self { Timer::new() } } -/// `CountDown` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::timer::CountDown for Timer { type Time = Duration; @@ -157,7 +157,7 @@ impl embedded_hal_0::timer::CountDown for Timer { } /// Returns `Ok` if the timer has wrapped. - fn wait(&mut self) -> nb::Result<(), Void> { + fn wait(&mut self) -> nb::Result<(), void::Void> { if self.start.elapsed() >= self.duration { Ok(()) } else { diff --git a/src/i2c.rs b/src/i2c.rs index fb9bedf4..c17f66c7 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -126,7 +126,11 @@ use libc::c_ulong; use crate::system; use crate::system::{DeviceInfo, Model}; -#[cfg(feature = "hal")] +#[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" +))] mod hal; mod ioctl; diff --git a/src/i2c/hal.rs b/src/i2c/hal.rs index 6a6dfe22..54505830 100644 --- a/src/i2c/hal.rs +++ b/src/i2c/hal.rs @@ -1,26 +1,24 @@ -use embedded_hal::i2c::{self, ErrorType, I2c as I2cHal, Operation as I2cOperation}; - use super::{Error, I2c}; -/// `Write` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::i2c::Write for I2c { type Error = Error; fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { - I2cHal::write(self, address, bytes) + embedded_hal::i2c::I2c::write(self, address, bytes) } } -/// `Read` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::i2c::Read for I2c { type Error = Error; fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { - I2cHal::read(self, address, buffer) + embedded_hal::i2c::I2c::read(self, address, buffer) } } -/// `WriteRead` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::i2c::WriteRead for I2c { type Error = Error; @@ -30,45 +28,47 @@ impl embedded_hal_0::blocking::i2c::WriteRead for I2c { bytes: &[u8], buffer: &mut [u8], ) -> Result<(), Self::Error> { - I2cHal::write_read(self, address, bytes, buffer) + embedded_hal::i2c::I2c::write_read(self, address, bytes, buffer) } } -impl ErrorType for I2c { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::i2c::ErrorType for I2c { type Error = Error; } -impl i2c::Error for Error { - fn kind(&self) -> i2c::ErrorKind { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::i2c::Error for Error { + fn kind(&self) -> embedded_hal::i2c::ErrorKind { if let Error::Io(e) = self { use std::io::ErrorKind::*; match e.kind() { - /* ResourceBusy | */ InvalidData => i2c::ErrorKind::Bus, - WouldBlock => i2c::ErrorKind::ArbitrationLoss, - _ => i2c::ErrorKind::Other, + /* ResourceBusy | */ InvalidData => embedded_hal::i2c::ErrorKind::Bus, + WouldBlock => embedded_hal::i2c::ErrorKind::ArbitrationLoss, + _ => embedded_hal::i2c::ErrorKind::Other, } } else { - i2c::ErrorKind::Other + embedded_hal::i2c::ErrorKind::Other } } } -/// `I2c` trait implementation for `embedded-hal` v1.0.0. -impl I2cHal for I2c { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::i2c::I2c for I2c { fn transaction( &mut self, address: u8, - operations: &mut [I2cOperation], + operations: &mut [embedded_hal::i2c::Operation], ) -> Result<(), Self::Error> { self.set_slave_address(u16::from(address))?; for op in operations { match op { - I2cOperation::Read(buff) => { - I2c::read(self, buff)?; + embedded_hal::i2c::Operation::Read(buff) => { + self.read(buff)?; } - I2cOperation::Write(buff) => { - I2c::write(self, buff)?; + embedded_hal::i2c::Operation::Write(buff) => { + self.write(buff)?; } } } diff --git a/src/lib.rs b/src/lib.rs index 8f121ef4..27e2f9be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,11 @@ mod macros; pub mod gpio; -#[cfg(feature = "hal")] +#[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" +))] pub mod hal; pub mod i2c; pub mod pwm; diff --git a/src/pwm.rs b/src/pwm.rs index 0437a288..04e5d1c0 100644 --- a/src/pwm.rs +++ b/src/pwm.rs @@ -66,7 +66,11 @@ use std::io; use std::result; use std::time::Duration; -#[cfg(feature = "hal")] +#[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" +))] mod hal; #[cfg(feature = "hal-unproven")] mod hal_unproven; diff --git a/src/pwm/hal.rs b/src/pwm/hal.rs index ddbad28c..a2077646 100644 --- a/src/pwm/hal.rs +++ b/src/pwm/hal.rs @@ -1,6 +1,7 @@ +#[cfg(feature = "embedded-hal-0")] use super::Pwm; -/// `PwmPin` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::PwmPin for Pwm { type Duty = f64; diff --git a/src/pwm/hal_unproven.rs b/src/pwm/hal_unproven.rs index 4a493c26..f72a86db 100644 --- a/src/pwm/hal_unproven.rs +++ b/src/pwm/hal_unproven.rs @@ -2,7 +2,7 @@ use std::time::Duration; use super::Pwm; -/// Unproven `Pwm` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::Pwm for Pwm { type Duty = f64; type Channel = (); diff --git a/src/spi.rs b/src/spi.rs index 48864696..2f7bdbd1 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -142,13 +142,21 @@ use std::marker::PhantomData; use std::os::unix::io::AsRawFd; use std::result; -#[cfg(feature = "hal")] +#[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" +))] mod hal; mod ioctl; mod segment; pub use self::segment::Segment; -#[cfg(feature = "hal")] +#[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" +))] pub use hal::SimpleHalSpiDevice; /// Errors that can occur when accessing the SPI peripheral. @@ -414,7 +422,7 @@ impl fmt::Display for BitOrder { pub struct Spi { spidev: File, // Stores the last read value. Used for embedded_hal::spi::FullDuplex. - #[cfg(feature = "hal")] + #[cfg(any(feature = "embedded-hal-0", feature = "embedded-hal-nb"))] last_read: Option, // The not_sync field is a workaround to force !Sync. Spi isn't safe for // Sync because of ioctl() and the underlying drivers. This avoids needing @@ -459,7 +467,7 @@ impl Spi { let spi = Spi { spidev, - #[cfg(feature = "hal")] + #[cfg(any(feature = "embedded-hal-0", feature = "embedded-hal-nb"))] last_read: None, not_sync: PhantomData, }; diff --git a/src/spi/hal.rs b/src/spi/hal.rs index c28bba8b..7918d6cf 100644 --- a/src/spi/hal.rs +++ b/src/spi/hal.rs @@ -1,24 +1,21 @@ -use embedded_hal::{ - delay::DelayNs, - spi::{self, ErrorType, Operation, SpiBus, SpiDevice}, -}; -use embedded_hal_nb::spi::FullDuplex; use std::io; use super::{super::hal::Delay, Error, Spi}; -impl ErrorType for Spi { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::spi::ErrorType for Spi { type Error = Error; } -impl spi::Error for Error { - fn kind(&self) -> spi::ErrorKind { - spi::ErrorKind::Other +#[cfg(feature = "embedded-hal")] +impl embedded_hal::spi::Error for Error { + fn kind(&self) -> embedded_hal::spi::ErrorKind { + embedded_hal::spi::ErrorKind::Other } } -/// `SpiBus` trait implementation for `embedded-hal` v1.0.0. -impl SpiBus for Spi { +#[cfg(feature = "embedded-hal")] +impl embedded_hal::spi::SpiBus for Spi { fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> { Spi::read(self, words)?; Ok(()) @@ -44,37 +41,37 @@ impl SpiBus for Spi { } } -/// `Transfer` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::spi::Transfer for Spi { type Error = Error; fn transfer<'a>(&mut self, buffer: &'a mut [u8]) -> Result<&'a [u8], Self::Error> { let write_buffer = buffer.to_vec(); - SpiBus::transfer(self, buffer, &write_buffer)?; + embedded_hal::spi::SpiBus::transfer(self, buffer, &write_buffer)?; Ok(buffer) } } -/// `Write` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::blocking::spi::Write for Spi { type Error = Error; fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { - SpiBus::write(self, buffer) + embedded_hal::spi::SpiBus::write(self, buffer) } } -/// `FullDuplex` trait implementation for `embedded-hal` v1.0.0 -impl FullDuplex for Spi { - fn read(&mut self) -> nb::Result { +#[cfg(feature = "embedded-hal-nb")] +impl embedded_hal_nb::spi::FullDuplex for Spi { + fn read(&mut self) -> embedded_hal_nb::nb::Result { if let Some(last_read) = self.last_read.take() { Ok(last_read) } else { - Err(nb::Error::WouldBlock) + Err(embedded_hal_nb::nb::Error::WouldBlock) } } - fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> { + fn write(&mut self, byte: u8) -> embedded_hal_nb::nb::Result<(), Self::Error> { let mut read_buffer: [u8; 1] = [0]; Spi::transfer(self, &mut read_buffer, &[byte])?; @@ -84,16 +81,16 @@ impl FullDuplex for Spi { } } -/// `FullDuplex` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::spi::FullDuplex for Spi { type Error = Error; fn read(&mut self) -> nb::Result { - FullDuplex::read(self) + embedded_hal_nb::spi::FullDuplex::read(self) } fn send(&mut self, byte: u8) -> nb::Result<(), Self::Error> { - FullDuplex::write(self, byte) + embedded_hal_nb::spi::FullDuplex::write(self, byte) } } @@ -108,17 +105,22 @@ pub struct SimpleHalSpiDevice { bus: B, } -impl> SimpleHalSpiDevice { +#[cfg(feature = "embedded-hal")] +impl> SimpleHalSpiDevice { pub fn new(bus: B) -> SimpleHalSpiDevice { SimpleHalSpiDevice { bus } } } -impl> SpiDevice for SimpleHalSpiDevice { - fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Error> { +#[cfg(feature = "embedded-hal")] +impl> embedded_hal::spi::SpiDevice for SimpleHalSpiDevice { + fn transaction( + &mut self, + operations: &mut [embedded_hal::spi::Operation<'_, u8>], + ) -> Result<(), Error> { for op in operations { match op { - Operation::Read(read) => { + embedded_hal::spi::Operation::Read(read) => { self.bus.read(read).map_err(|_| { Error::Io(io::Error::new( io::ErrorKind::Other, @@ -126,7 +128,7 @@ impl> SpiDevice for SimpleHalSpiDevice { )) })?; } - Operation::Write(write) => { + embedded_hal::spi::Operation::Write(write) => { self.bus.write(write).map_err(|_| { Error::Io(io::Error::new( io::ErrorKind::Other, @@ -134,7 +136,7 @@ impl> SpiDevice for SimpleHalSpiDevice { )) })?; } - Operation::Transfer(read, write) => { + embedded_hal::spi::Operation::Transfer(read, write) => { self.bus.transfer(read, write).map_err(|_| { Error::Io(io::Error::new( io::ErrorKind::Other, @@ -142,7 +144,7 @@ impl> SpiDevice for SimpleHalSpiDevice { )) })?; } - Operation::TransferInPlace(words) => { + embedded_hal::spi::Operation::TransferInPlace(words) => { self.bus.transfer_in_place(words).map_err(|_| { Error::Io(io::Error::new( io::ErrorKind::Other, @@ -150,8 +152,8 @@ impl> SpiDevice for SimpleHalSpiDevice { )) })?; } - Operation::DelayNs(us) => { - Delay::new().delay_us(*us); + embedded_hal::spi::Operation::DelayNs(us) => { + embedded_hal::delay::DelayNs::delay_us(&mut Delay::new(), *us); } } } @@ -159,6 +161,7 @@ impl> SpiDevice for SimpleHalSpiDevice { } } -impl> ErrorType for SimpleHalSpiDevice { +#[cfg(feature = "embedded-hal")] +impl> embedded_hal::spi::ErrorType for SimpleHalSpiDevice { type Error = Error; } diff --git a/src/uart.rs b/src/uart.rs index 97ac3a79..d5a9d14c 100644 --- a/src/uart.rs +++ b/src/uart.rs @@ -147,7 +147,11 @@ use libc::{TIOCM_CAR, TIOCM_CTS, TIOCM_DSR, TIOCM_DTR, TIOCM_RNG, TIOCM_RTS}; use crate::gpio::{self, Gpio, IoPin, Mode}; use crate::system::{self, DeviceInfo, Model}; -#[cfg(feature = "hal")] +#[cfg(any( + feature = "embedded-hal-0", + feature = "embedded-hal", + feature = "embedded-hal-nb" +))] mod hal; mod termios; diff --git a/src/uart/hal.rs b/src/uart/hal.rs index f5be015d..1ae0a342 100644 --- a/src/uart/hal.rs +++ b/src/uart/hal.rs @@ -1,64 +1,65 @@ -use embedded_hal_nb::serial::{self, ErrorType, Read, Write}; - +#[cfg(any(feature = "embedded-hal-0", feature = "embedded-hal-nb"))] use super::{Error, Queue, Uart}; -impl ErrorType for Uart { +#[cfg(feature = "embedded-hal-nb")] +impl embedded_hal_nb::serial::ErrorType for Uart { type Error = Error; } -impl serial::Error for Error { - fn kind(&self) -> serial::ErrorKind { - serial::ErrorKind::Other +#[cfg(feature = "embedded-hal-nb")] +impl embedded_hal_nb::serial::Error for Error { + fn kind(&self) -> embedded_hal_nb::serial::ErrorKind { + embedded_hal_nb::serial::ErrorKind::Other } } -/// `Read` trait implementation for `embedded-hal` v1.0.0. -impl Read for Uart { - fn read(&mut self) -> nb::Result { +#[cfg(feature = "embedded-hal-nb")] +impl embedded_hal_nb::serial::Read for Uart { + fn read(&mut self) -> embedded_hal_nb::nb::Result { let mut buffer = [0u8; 1]; if Uart::read(self, &mut buffer)? == 0 { - Err(nb::Error::WouldBlock) + Err(embedded_hal_nb::nb::Error::WouldBlock) } else { Ok(buffer[0]) } } } -/// `Read` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::serial::Read for Uart { type Error = Error; fn read(&mut self) -> nb::Result { - Read::read(self) + embedded_hal_nb::serial::Read::read(self) } } -/// `Write` trait implementation for `embedded-hal` v1.0.0. -impl Write for Uart { - fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { +#[cfg(feature = "embedded-hal-nb")] +impl embedded_hal_nb::serial::Write for Uart { + fn write(&mut self, word: u8) -> embedded_hal_nb::nb::Result<(), Self::Error> { if Uart::write(self, &[word])? == 0 { - Err(nb::Error::WouldBlock) + Err(embedded_hal_nb::nb::Error::WouldBlock) } else { Ok(()) } } - fn flush(&mut self) -> nb::Result<(), Self::Error> { + fn flush(&mut self) -> embedded_hal_nb::nb::Result<(), Self::Error> { Uart::flush(self, Queue::Output)?; Ok(()) } } -/// `Write` trait implementation for `embedded-hal` v0.2.7. +#[cfg(feature = "embedded-hal-0")] impl embedded_hal_0::serial::Write for Uart { type Error = Error; fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> { - Write::write(self, word) + embedded_hal_nb::serial::Write::write(self, word) } fn flush(&mut self) -> nb::Result<(), Self::Error> { - Write::flush(self) + embedded_hal_nb::serial::Write::flush(self) } }