Skip to content

Commit

Permalink
gpio: Implement and test embedded-hal 1
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Jan 20, 2024
1 parent 476f519 commit 4d75a97
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
60 changes: 60 additions & 0 deletions src/gpio/impl_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::*;

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

impl ErrorType for InputGPIO {
type Error = Infallible;
}

impl InputPin for InputGPIO {
fn is_high(&mut self) -> Result<bool, Infallible> {
Ok(unsafe { gpio_read(self.to_c()) } != 0)
}

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

impl ErrorType for OutputGPIO {
type Error = Infallible;
}

impl OutputPin for OutputGPIO {
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 ErrorType for InOutGPIO {
type Error = Infallible;
}

impl InputPin for InOutGPIO {
fn is_high(&mut self) -> Result<bool, Infallible> {
Ok(unsafe { gpio_read(self.to_c()) } != 0)
}

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

impl OutputPin for InOutGPIO {
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(())
}
}
1 change: 1 addition & 0 deletions src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! the [embedded_hal_0_2::digital::v2] traits.
mod impl_0_2;
mod impl_1;

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

Expand Down
2 changes: 1 addition & 1 deletion tests/gpio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ panic = "abort"
[dependencies]
riot-wrappers = { version = "*", features = [ "set_panic_handler", "panic_handler_format" ] }
riot-sys = "*"
embedded-hal = "0.2.4"
embedded-hal = "1"
8 changes: 5 additions & 3 deletions tests/gpio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use riot_wrappers::gpio::{InputMode, OutputMode, GPIO};
use riot_wrappers::println;
use riot_wrappers::riot_main;

use embedded_hal_0_2::digital::v2::{InputPin, OutputPin, PinState};
use embedded_hal::digital::{InputPin, OutputPin, PinState};

riot_main!(main);

Expand All @@ -22,14 +22,16 @@ fn main() {
.expect("Out pin does not exist")
.configure_as_output(OutputMode::Out)
.expect("Out pin could not be configured");
let p_in = GPIO::from_port_and_pin(in_port, in_pin)
let mut p_in = GPIO::from_port_and_pin(in_port, in_pin)
.expect("In pin does not exist")
.configure_as_input(in_mode)
.expect("In pin could not be configured");

loop {
let value = p_in.is_high().unwrap();
println!("Read GPIO value {}, writing it to the out port", value);
p_out.set_state(if value { PinState::High } else { PinState::Low });
p_out
.set_state(if value { PinState::High } else { PinState::Low })
.unwrap();
}
}

0 comments on commit 4d75a97

Please sign in to comment.