From 5f7df5720d5a43f3c7e1aeea509bcf62e149870a Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Wed, 19 Jan 2022 22:52:41 +0000 Subject: stm32_gpio: provide port and pin numbers Accessing Pin.n and Pin.get_portnum() will give you values suitable for use with EXTI and similar places. Signed-off-by: Karl Palsson --- gpio/stm32_gpio.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'gpio/stm32_gpio.h') diff --git a/gpio/stm32_gpio.h b/gpio/stm32_gpio.h index 6a428d4..7d47b92 100644 --- a/gpio/stm32_gpio.h +++ b/gpio/stm32_gpio.h @@ -32,10 +32,10 @@ class STM32_GPIO_v1_t : public mmio_ptr { class Pin { private: const STM32_GPIO_v1_t& g; - int n; public: - constexpr Pin(const STM32_GPIO_v1_t& gpio, int pin) : g(gpio), n(pin) {} + const int n; + constexpr Pin(const STM32_GPIO_v1_t& gpio, const int pin) : g(gpio), n(pin) {} enum Mode { Input = 0x4, @@ -76,6 +76,10 @@ class STM32_GPIO_v1_t : public mmio_ptr { void toggle() { set(!(g->ODR & (1 << n))); } + + unsigned get_portnum() { + return (((*(uint32_t*)&g) >>10) & 0xf) - 2; + } }; class PinArray { @@ -121,10 +125,10 @@ class STM32_GPIO_v2_t : public mmio_ptr { class Pin { private: const STM32_GPIO_v2_t& g; - int n; public: - constexpr Pin(const STM32_GPIO_v2_t& gpio, int pin) : g(gpio), n(pin) {} + const int n; + constexpr Pin(const STM32_GPIO_v2_t& gpio, const int pin) : g(gpio), n(pin) {} enum Mode { Input, @@ -202,6 +206,10 @@ class STM32_GPIO_v2_t : public mmio_ptr { void toggle() { set(!(g->ODR & (1 << n))); } + + unsigned get_portnum() { + return (((*(uint32_t*)&g) >>10) & 0xf); + } }; class PinArray { -- cgit v1.2.3