Skip to content

Commit

Permalink
gpio: Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Oct 13, 2023
1 parent e587536 commit 2643368
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/gpio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "riot-wrappers-test-gpio"
version = "0.1.0"
authors = ["Christian Amsüss <chrysn@fsfe.org>"]
edition = "2021"
publish = false

[lib]
crate-type = ["staticlib"]

[profile.release]
panic = "abort"

[dependencies]
riot-wrappers = { version = "*", features = [ "set_panic_handler", "panic_handler_format" ] }
riot-sys = "*"
embedded-hal = "0.2.4"
8 changes: 8 additions & 0 deletions tests/gpio/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# name of your application
APPLICATION = riot-wrappers-test-gpio
APPLICATION_RUST_MODULE = riot_wrappers_test_gpio
BASELIBS += $(APPLICATION_RUST_MODULE).module
FEATURES_REQUIRED += rust_target
FEATURES_REQUIRED += periph_gpio

include $(RIOTBASE)/Makefile.include
35 changes: 35 additions & 0 deletions tests/gpio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![no_std]

use riot_wrappers::gpio::{InputMode, OutputMode, GPIO};
use riot_wrappers::println;
use riot_wrappers::riot_main;

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

riot_main!(main);

fn main() {
let (out_port, out_pin, in_port, in_pin, in_mode) = match riot_wrappers::BOARD {
// Won't work -- currently, native GPIO don't do anything (but let's not panic already)
"native" => (0, 0, 0, 1, InputMode::In),
// 0.17 is LED1, 0.13 is button 1
"nrf52dk" => (0, 17, 0, 13, InputMode::InPullUp),

// Better safe than drive pins that were not supposed to be driven
_ => panic!("For this board, no GPIO pins were deemed safe to reconfigure."),
};
let mut p_out = GPIO::from_port_and_pin(out_port, out_pin)
.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)
.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 });
}
}

0 comments on commit 2643368

Please sign in to comment.