From ee158e3c398cfbff4ffbc89c735b436a78130822 Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 12 Oct 2023 22:35:48 +0200 Subject: [PATCH 1/5] gpio: Add initializer based on pin and port numbers Follow-up-for: https://github.com/RIOT-OS/rust-riot-sys/pull/17 --- src/gpio.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/gpio.rs b/src/gpio.rs index 38272405..ca418123 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -85,14 +85,16 @@ impl GPIO { } } - // using a generic GPIO_PIN is probably best done by making GPIO_INIT a static inline (given - // it's already fixed to types at tests/periph_gpio/main.c) - // /// Create a GPIO out of thin air - // #[cfg(riot_module_nrf5x_common_periph)] - // pub unsafe fn new(port: u8, pin: u8) -> Self { - // // EXPANDED cpu/nrf5x_common/include/periph_cpu_common.h:50 - // GPIO(((port << 5) | pin).into()) - // } + /// Create a GPIO from its port and pin numbers + /// + /// ``` + /// let pin_c8 = GPIO::from_port_and_pin(3, 8); + /// ``` + /// + /// See [from_c] for safety constraints. + pub fn from_port_and_pin(port: u32, pin: u32) -> Option { + Self::from_c(unsafe { riot_sys::macro_GPIO_PIN(port, pin) }) + } pub fn configure_as_output( self, From fab3dbbdbbabd744ebf2e2b7926c9e0f07eaef6d Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 12 Oct 2023 23:16:41 +0200 Subject: [PATCH 2/5] doc: Point out inconsistencies --- src/adc.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/adc.rs b/src/adc.rs index bfe1698a..3675d5ca 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -6,6 +6,10 @@ impl ADCLine { /// Initialize an ADC line and get it as a handle. This is declared as unsafe as it may only /// be called once. (A safe abstraction would need to check which RIOT devices have been /// initialized already). + /// + /// This being unsafe is inconsistent with other subsystem wrappers that chose to not declare + /// this unsafe; that inconsistency is tracked in + /// and so far unresolved. pub unsafe fn init(line: riot_sys::adc_t) -> Result { let success = riot_sys::adc_init(line); match success { From e587536c20152741cd354ade5b1607aefa7f0eaf Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 12 Oct 2023 23:23:44 +0200 Subject: [PATCH 3/5] SPI: Add initializer based on bus number Follow-up-for: https://github.com/RIOT-OS/rust-riot-sys/pull/17 --- src/spi.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/spi.rs b/src/spi.rs index b932749b..84ffa48c 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -10,7 +10,7 @@ use riot_sys::{ spi_transfer_bytes, }; -pub struct SPIDevice(pub spi_t); +pub struct SPIDevice(#[deprecated(note = "Use constructor instead")] pub spi_t); pub struct AcquiredSPI<'a> { device: &'a mut SPIDevice, @@ -24,6 +24,17 @@ impl<'a> Drop for AcquiredSPI<'a> { } impl SPIDevice { + /// Create an SPI device from an `spi_t` + pub fn from_c(bus: spi_t) -> Self { + Self(bus) + } + + /// Create an SPI device from the number it is assigned on the board + pub fn from_number(bus: u32) -> Self { + let bus = unsafe { riot_sys::macro_SPI_DEV(bus) }; + Self::from_c(bus) + } + pub fn acquire<'a>( &'a mut self, cs: spi_cs_t, From 2643368980826df45cb38ddc3dbf39665efaa950 Mon Sep 17 00:00:00 2001 From: chrysn Date: Fri, 13 Oct 2023 13:09:59 +0200 Subject: [PATCH 4/5] 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 }); + } +} From ddf926579c56af820db498414ebe3fd82369662f Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 12 Oct 2023 23:16:55 +0200 Subject: [PATCH 5/5] ADC: Add initializer based on line number Follow-up-for: https://github.com/RIOT-OS/rust-riot-sys/pull/17 --- src/adc.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/adc.rs b/src/adc.rs index 3675d5ca..df043957 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -17,6 +17,14 @@ impl ADCLine { e => Err(e), } } + + /// Initialize an ADC line identified by the line number it is assigned on the board + /// + /// Safety: See [init] + pub unsafe fn from_number(line: u32) -> Result { + let line = riot_sys::macro_ADC_LINE(line); + Self::init(line) + } } /// A configured representation of the single operating-system level ADC that RIOT exposes via its