From 96fd778797ae5dffa81e766398f48ea8576e3e1c Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 12 Oct 2023 22:35:48 +0200 Subject: [PATCH] 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..3a1b3e1f 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: usize, pin: usize) -> Option { + Self::from_c(unsafe { riot_sys::macro_GPIO_PIN(port, pin) }) + } pub fn configure_as_output( self,