From 2191ddfc582fc486b7f87ca5c0c3ac4bc42c3361 Mon Sep 17 00:00:00 2001 From: chrysn Date: Mon, 19 Aug 2024 10:13:06 +0200 Subject: [PATCH] Drop support for embedded-hal 0.2 except where 1.0 has no traits for it (ADC) SPI is not dropped either because the 1.0 trait includes acquisition in the trait (which makes it not trivial to port). Closes: https://github.com/RIOT-OS/rust-riot-wrappers/issues/65 --- src/gpio/impl_0_2.rs | 73 ----------------------------- src/gpio/mod.rs | 1 - src/i2c/impl_0_2.rs | 106 ------------------------------------------- src/i2c/mod.rs | 1 - src/led.rs | 32 ++----------- src/ztimer/mod.rs | 12 ----- 6 files changed, 3 insertions(+), 222 deletions(-) delete mode 100644 src/gpio/impl_0_2.rs delete mode 100644 src/i2c/impl_0_2.rs diff --git a/src/gpio/impl_0_2.rs b/src/gpio/impl_0_2.rs deleted file mode 100644 index ebf2e2c..0000000 --- a/src/gpio/impl_0_2.rs +++ /dev/null @@ -1,73 +0,0 @@ -use super::*; - -use embedded_hal_0_2::digital::v2::{InputPin, OutputPin, ToggleableOutputPin}; - -impl InputPin for InputGPIO { - type Error = Infallible; - - fn is_high(&self) -> Result { - Ok(unsafe { gpio_read(self.to_c()) } != 0) - } - - fn is_low(&self) -> Result { - Ok(unsafe { gpio_read(self.to_c()) } == 0) - } -} - -impl OutputPin for OutputGPIO { - type Error = Infallible; - - fn set_high(&mut self) -> Result<(), Infallible> { - unsafe { gpio_set(self.to_c()) }; - Ok(()) - } - - fn set_low(&mut self) -> Result<(), Infallible> { - unsafe { gpio_clear(self.to_c()) }; - Ok(()) - } -} - -impl ToggleableOutputPin for OutputGPIO { - type Error = Infallible; - - fn toggle(&mut self) -> Result<(), Infallible> { - unsafe { gpio_toggle(self.to_c()) }; - Ok(()) - } -} - -impl InputPin for InOutGPIO { - type Error = Infallible; - - fn is_high(&self) -> Result { - Ok(unsafe { gpio_read(self.to_c()) } != 0) - } - - fn is_low(&self) -> Result { - Ok(unsafe { gpio_read(self.to_c()) } == 0) - } -} - -impl OutputPin for InOutGPIO { - type Error = Infallible; - - fn set_high(&mut self) -> Result<(), Infallible> { - unsafe { gpio_set(self.to_c()) }; - Ok(()) - } - - fn set_low(&mut self) -> Result<(), Infallible> { - unsafe { gpio_clear(self.to_c()) }; - Ok(()) - } -} - -impl ToggleableOutputPin for InOutGPIO { - type Error = Infallible; - - fn toggle(&mut self) -> Result<(), Infallible> { - unsafe { gpio_toggle(self.to_c()) }; - Ok(()) - } -} diff --git a/src/gpio/mod.rs b/src/gpio/mod.rs index b6a2c97..6e0bc4d 100644 --- a/src/gpio/mod.rs +++ b/src/gpio/mod.rs @@ -4,7 +4,6 @@ //! the [embedded_hal::digital::v2] traits. As recommended for infallible types, they also //! provide identically named direct methods, which (for input pins) also work on shared reference. -mod impl_0_2; mod impl_1; use riot_sys::{gpio_clear, gpio_mode_t, gpio_read, gpio_set, gpio_t, gpio_toggle, gpio_write}; diff --git a/src/i2c/impl_0_2.rs b/src/i2c/impl_0_2.rs deleted file mode 100644 index b3c0e98..0000000 --- a/src/i2c/impl_0_2.rs +++ /dev/null @@ -1,106 +0,0 @@ -//! Implementation of embedded-hal 0.2's I2C for [I2CDevice] -//! -//! As the implementation is on the [I2CDevice directly], all that is in this module is the -//! suitable [Error] type. - -use embedded_hal_0_2::blocking; - -use super::*; - -use riot_sys::libc; -use riot_sys::{i2c_acquire, i2c_read_bytes, i2c_release, i2c_write_bytes}; - -#[derive(Debug)] -#[non_exhaustive] -pub enum Error { - AcquireError, - WriteError(i32), - ReadError(i32), -} - -impl blocking::i2c::WriteRead for I2CDevice { - type Error = Error; - - fn write_read( - &mut self, - address: u8, - bytes: &[u8], - buffer: &mut [u8], - ) -> Result<(), Self::Error> { - unsafe { i2c_acquire(self.dev) }; - let err = unsafe { - i2c_write_bytes( - self.dev, - address as u16, - bytes.as_ptr() as *const libc::c_void, - bytes.len() as _, - 0, - ) - }; - if err != 0 { - unsafe { i2c_release(self.dev) }; - return Err(Error::WriteError(err)); - } - let err = unsafe { - i2c_read_bytes( - self.dev, - address as u16, - buffer.as_ptr() as *mut libc::c_void, - buffer.len() as _, - 0, - ) - }; - if err != 0 { - unsafe { i2c_release(self.dev) }; - return Err(Error::ReadError(err)); - } - unsafe { i2c_release(self.dev) }; - Ok(()) - } -} - -impl blocking::i2c::Write for I2CDevice { - type Error = Error; - - fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { - unsafe { i2c_acquire(self.dev) }; - let err = unsafe { - i2c_write_bytes( - self.dev, - address as u16, - bytes.as_ptr() as *const libc::c_void, - bytes.len() as _, - 0, - ) - }; - if err != 0 { - unsafe { i2c_release(self.dev) }; - return Err(Error::WriteError(err)); - } - unsafe { i2c_release(self.dev) }; - Ok(()) - } -} - -impl blocking::i2c::Read for I2CDevice { - type Error = Error; - - fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { - unsafe { i2c_acquire(self.dev) }; - let err = unsafe { - i2c_read_bytes( - self.dev, - address as u16, - buffer.as_ptr() as *mut libc::c_void, - buffer.len() as _, - 0, - ) - }; - if err != 0 { - unsafe { i2c_release(self.dev) }; - return Err(Error::ReadError(err)); - } - unsafe { i2c_release(self.dev) }; - Ok(()) - } -} diff --git a/src/i2c/mod.rs b/src/i2c/mod.rs index 2526e38..93afc04 100644 --- a/src/i2c/mod.rs +++ b/src/i2c/mod.rs @@ -1,6 +1,5 @@ //! Controlling the I²C bus -pub mod impl_0_2; pub mod impl_1; use riot_sys::i2c_t; diff --git a/src/led.rs b/src/led.rs index 2f3cfb2..c61cd1b 100644 --- a/src/led.rs +++ b/src/led.rs @@ -7,10 +7,6 @@ use core::convert::Infallible; /// The preferred interface for turning a LED on and off is [switch_hal::OutputSwitch]. /// /// LEDs are accessible safely; any not implemented on a board are silently ignored. -/// -/// LEDs are wrapped into embedded-hal 0.2 GPIOs for compatibility reasons (but that integration is -/// discontinued with embedded-hal 1.0); GPIO is interpreted such that "high" is having the LED on, -/// and "low" is off. pub struct LED(()); impl LED { @@ -24,28 +20,6 @@ impl switch_hal::OutputSwitch for LED { type Error = Infallible; fn on(&mut self) -> Result<(), Self::Error> { - use embedded_hal_0_2::digital::v2::OutputPin; - self.set_high() - } - - fn off(&mut self) -> Result<(), Self::Error> { - use embedded_hal_0_2::digital::v2::OutputPin; - self.set_low() - } -} - -impl switch_hal::ToggleableOutputSwitch for LED { - type Error = Infallible; - - fn toggle(&mut self) -> Result<(), Self::Error> { - ::toggle(self) - } -} - -impl embedded_hal_0_2::digital::v2::OutputPin for LED { - type Error = Infallible; - - fn set_high(&mut self) -> Result<(), Infallible> { // unsafe: RIOT's LED functions can be called any time (and no-op on undefined LEDs) unsafe { match I { @@ -63,7 +37,7 @@ impl embedded_hal_0_2::digital::v2::OutputPin for LED { Ok(()) } - fn set_low(&mut self) -> Result<(), Infallible> { + fn off(&mut self) -> Result<(), Self::Error> { // unsafe: RIOT's LED functions can be called any time (and no-op on undefined LEDs) unsafe { match I { @@ -82,10 +56,10 @@ impl embedded_hal_0_2::digital::v2::OutputPin for LED { } } -impl embedded_hal_0_2::digital::v2::ToggleableOutputPin for LED { +impl switch_hal::ToggleableOutputSwitch for LED { type Error = Infallible; - fn toggle(&mut self) -> Result<(), Infallible> { + fn toggle(&mut self) -> Result<(), Self::Error> { // unsafe: RIOT's LED functions can be called any time (and no-op on undefined LEDs) unsafe { match I { diff --git a/src/ztimer/mod.rs b/src/ztimer/mod.rs index b6916f6..b984b1a 100644 --- a/src/ztimer/mod.rs +++ b/src/ztimer/mod.rs @@ -213,18 +213,6 @@ impl Clock<1000000> { } } -impl embedded_hal_0_2::blocking::delay::DelayMs for Clock<1000> { - fn delay_ms(&mut self, ms: u32) { - self.sleep_ticks(ms.into()); - } -} - -impl embedded_hal_0_2::blocking::delay::DelayUs for Clock<1000000> { - fn delay_us(&mut self, us: u32) { - self.sleep_ticks(us); - } -} - #[cfg(all(feature = "embedded-hal-async", riot_module_ztimer_usec))] /// Struct that provides the [embedded_hal_async::delay::DelayNs] trait ///