Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update embedded-hal to 1.0 #73

Merged
merged 11 commits into from
Jan 26, 2024
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ license = "MIT OR Apache-2.0"


[dependencies]
embedded-hal = { version = "0.2.4", features = ["unproven"] }
embedded-hal-0-2 = { package = "embedded-hal", version = "0.2.4", features = ["unproven"] }
embedded-hal = "1"
switch-hal = "0.4.0"
nb = "0.1.1"
riot-sys = "0.7.8"
Expand Down
4 changes: 2 additions & 2 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ pub struct ADC {
pub resolution: riot_sys::adc_res_t,
}

impl embedded_hal::adc::Channel<ADC> for ADCLine {
impl embedded_hal_0_2::adc::Channel<ADC> for ADCLine {
type ID = riot_sys::adc_t;
fn channel() -> Self::ID {
unimplemented!("See https://github.com/rust-embedded/embedded-hal/issues/110")
}
}

impl embedded_hal::adc::OneShot<ADC, i32, ADCLine> for ADC {
impl embedded_hal_0_2::adc::OneShot<ADC, i32, ADCLine> for ADC {
type Error = Never;

fn read(&mut self, pin: &mut ADCLine) -> nb::Result<i32, Never> {
Expand Down
73 changes: 73 additions & 0 deletions src/gpio/impl_0_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use super::*;

use embedded_hal_0_2::digital::v2::{InputPin, OutputPin, ToggleableOutputPin};

impl InputPin for InputGPIO {
type Error = Never;

fn is_high(&self) -> Result<bool, Never> {
Ok(unsafe { gpio_read(self.to_c()) } != 0)
}

fn is_low(&self) -> Result<bool, Never> {
Ok(unsafe { gpio_read(self.to_c()) } == 0)
}
}

impl OutputPin for OutputGPIO {
type Error = Never;

fn set_high(&mut self) -> Result<(), Never> {
unsafe { gpio_set(self.to_c()) };
Ok(())
}

fn set_low(&mut self) -> Result<(), Never> {
unsafe { gpio_clear(self.to_c()) };
Ok(())
}
}

impl ToggleableOutputPin for OutputGPIO {
type Error = Never;

fn toggle(&mut self) -> Result<(), Never> {
unsafe { gpio_toggle(self.to_c()) };
Ok(())
}
}

impl InputPin for InOutGPIO {
type Error = Never;

fn is_high(&self) -> Result<bool, Never> {
Ok(unsafe { gpio_read(self.to_c()) } != 0)
}

fn is_low(&self) -> Result<bool, Never> {
Ok(unsafe { gpio_read(self.to_c()) } == 0)
}
}

impl OutputPin for InOutGPIO {
type Error = Never;

fn set_high(&mut self) -> Result<(), Never> {
unsafe { gpio_set(self.to_c()) };
Ok(())
}

fn set_low(&mut self) -> Result<(), Never> {
unsafe { gpio_clear(self.to_c()) };
Ok(())
}
}

impl ToggleableOutputPin for InOutGPIO {
type Error = Never;

fn toggle(&mut self) -> Result<(), Never> {
unsafe { gpio_toggle(self.to_c()) };
Ok(())
}
}
64 changes: 64 additions & 0 deletions src/gpio/impl_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use super::*;

use core::convert::Infallible;
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, PinState};

impl ErrorType for InputGPIO {
type Error = Infallible;
}

impl InputPin for InputGPIO {
fn is_high(&mut self) -> Result<bool, Infallible> {
Ok(InputGPIO::is_high(self))
}

fn is_low(&mut self) -> Result<bool, Infallible> {
Ok(InputGPIO::is_low(self))
}
}

impl ErrorType for OutputGPIO {
type Error = Infallible;
}

impl OutputPin for OutputGPIO {
fn set_high(&mut self) -> Result<(), Infallible> {
Ok(OutputGPIO::set_high(self))
}

fn set_low(&mut self) -> Result<(), Infallible> {
Ok(OutputGPIO::set_low(self))
}

fn set_state(&mut self, state: PinState) -> Result<(), Infallible> {
Ok(OutputGPIO::set_state(self, state.into()))
}
}

impl ErrorType for InOutGPIO {
type Error = Infallible;
}

impl InputPin for InOutGPIO {
fn is_high(&mut self) -> Result<bool, Infallible> {
Ok(InOutGPIO::is_high(self))
}

fn is_low(&mut self) -> Result<bool, Infallible> {
Ok(InOutGPIO::is_low(self))
}
}

impl OutputPin for InOutGPIO {
fn set_high(&mut self) -> Result<(), Infallible> {
Ok(InOutGPIO::set_high(self))
}

fn set_low(&mut self) -> Result<(), Infallible> {
Ok(InOutGPIO::set_low(self))
}

fn set_state(&mut self, state: PinState) -> Result<(), Infallible> {
Ok(InOutGPIO::set_state(self, state.into()))
}
}
74 changes: 23 additions & 51 deletions src/gpio.rs → src/gpio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Access to [RIOT's GPIO pins](http://doc.riot-os.org/group__drivers__periph__gpio.html)
//!
//! The various configured GPIO types ([InputGPIO], [OutputGPIO], [InOutGPIO]) can be used through
//! the [embedded_hal::digital::v2] traits.
//! 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.

use riot_sys::{gpio_clear, gpio_mode_t, gpio_read, gpio_set, gpio_t, gpio_toggle};
mod impl_0_2;
mod impl_1;

use embedded_hal::digital::v2::{InputPin, OutputPin, ToggleableOutputPin};
use riot_sys::{gpio_clear, gpio_mode_t, gpio_read, gpio_set, gpio_t, gpio_toggle, gpio_write};

use crate::error::NegativeErrorExt;
use crate::Never;
Expand Down Expand Up @@ -142,28 +144,17 @@ impl OutputGPIO {
pub fn deconfigured(self) -> GPIO {
self.0
}
}

impl OutputPin for OutputGPIO {
type Error = Never;

fn set_high(&mut self) -> Result<(), Never> {
pub fn set_high(&mut self) {
unsafe { gpio_set(self.to_c()) };
Ok(())
}

fn set_low(&mut self) -> Result<(), Never> {
pub fn set_low(&mut self) {
unsafe { gpio_clear(self.to_c()) };
Ok(())
}
}

impl ToggleableOutputPin for OutputGPIO {
type Error = Never;

fn toggle(&mut self) -> Result<(), Never> {
unsafe { gpio_toggle(self.to_c()) };
Ok(())
pub fn set_state(&mut self, state: bool) {
unsafe { gpio_write(self.to_c(), state as _) };
}
}

Expand All @@ -180,17 +171,13 @@ impl InputGPIO {
pub fn deconfigured(self) -> GPIO {
self.0
}
}

impl InputPin for InputGPIO {
type Error = Never;

fn is_high(&self) -> Result<bool, Never> {
Ok(unsafe { gpio_read(self.to_c()) } != 0)
pub fn is_high(&self) -> bool {
unsafe { gpio_read(self.to_c()) != 0 }
}

fn is_low(&self) -> Result<bool, Never> {
Ok(unsafe { gpio_read(self.to_c()) } == 0)
pub fn is_low(&self) -> bool {
unsafe { gpio_read(self.to_c()) == 0 }
}
}

Expand All @@ -207,39 +194,24 @@ impl InOutGPIO {
pub fn deconfigured(self) -> GPIO {
self.0
}
}

impl InputPin for InOutGPIO {
type Error = Never;

fn is_high(&self) -> Result<bool, Never> {
Ok(unsafe { gpio_read(self.to_c()) } != 0)
pub fn set_high(&mut self) {
unsafe { gpio_set(self.to_c()) };
}

fn is_low(&self) -> Result<bool, Never> {
Ok(unsafe { gpio_read(self.to_c()) } == 0)
pub fn set_low(&mut self) {
unsafe { gpio_clear(self.to_c()) };
}
}

impl OutputPin for InOutGPIO {
type Error = Never;

fn set_high(&mut self) -> Result<(), Never> {
unsafe { gpio_set(self.to_c()) };
Ok(())
pub fn set_state(&mut self, state: bool) {
unsafe { gpio_write(self.to_c(), state as _) };
}

fn set_low(&mut self) -> Result<(), Never> {
unsafe { gpio_clear(self.to_c()) };
Ok(())
pub fn is_high(&self) -> bool {
unsafe { gpio_read(self.to_c()) != 0 }
}
}

impl ToggleableOutputPin for InOutGPIO {
type Error = Never;

fn toggle(&mut self) -> Result<(), Never> {
unsafe { gpio_toggle(self.to_c()) };
Ok(())
pub fn is_low(&self) -> bool {
unsafe { gpio_read(self.to_c()) == 0 }
}
}
33 changes: 8 additions & 25 deletions src/i2c.rs → src/i2c/impl_0_2.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
//! Controlling the I²C bus
//! 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::blocking;
use riot_sys::i2c_t;
use embedded_hal_0_2::blocking;

/// An I²C master backed by RIOT's [I2C implementation]
///
/// [I2C implementation]: http://doc.riot-os.org/group__drivers__periph__i2c.html
///
/// Actual transactions on this are performed through the [embedded_hal::blocking::i2c] traits
/// implemented by this.
#[derive(Debug)]
pub struct I2CDevice {
dev: i2c_t,
}
use super::*;

impl I2CDevice {
/// Create a new I2CDevice from a RIOT descriptor
///
/// As all transactions on the bus are gated by acquire / release steps implied in the
/// individual reads or writes, multiple copies of the same device can safely coexist.
pub fn new(dev: i2c_t) -> Self {
I2CDevice { dev }
}
}
use riot_sys::libc;
use riot_sys::{i2c_acquire, i2c_read_bytes, i2c_release, i2c_write_bytes};

#[derive(Debug)]
#[non_exhaustive]
Expand All @@ -32,9 +18,6 @@ pub enum Error {
ReadError(i32),
}

use riot_sys::libc;
use riot_sys::{i2c_acquire, i2c_read_bytes, i2c_release, i2c_write_bytes};

impl blocking::i2c::WriteRead for I2CDevice {
type Error = Error;

Expand Down
Loading
Loading