From 2643368980826df45cb38ddc3dbf39665efaa950 Mon Sep 17 00:00:00 2001 From: chrysn Date: Fri, 13 Oct 2023 13:09:59 +0200 Subject: [PATCH] gpio: Add test --- tests/gpio/Cargo.toml | 17 +++++++++++++++++ tests/gpio/Makefile | 8 ++++++++ tests/gpio/src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 tests/gpio/Cargo.toml create mode 100644 tests/gpio/Makefile create mode 100644 tests/gpio/src/lib.rs diff --git a/tests/gpio/Cargo.toml b/tests/gpio/Cargo.toml new file mode 100644 index 00000000..ffd6e853 --- /dev/null +++ b/tests/gpio/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "riot-wrappers-test-gpio" +version = "0.1.0" +authors = ["Christian Amsüss "] +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" diff --git a/tests/gpio/Makefile b/tests/gpio/Makefile new file mode 100644 index 00000000..2210600b --- /dev/null +++ b/tests/gpio/Makefile @@ -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 diff --git a/tests/gpio/src/lib.rs b/tests/gpio/src/lib.rs new file mode 100644 index 00000000..b1d13f04 --- /dev/null +++ b/tests/gpio/src/lib.rs @@ -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 }); + } +}