From f8b348721cc8400061733a68684fdffd63e6996f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucher?= <cfoucher@laas.fr> Date: Tue, 5 Sep 2023 09:44:11 +0000 Subject: [PATCH] Add ability to use GPIO API using Spin pin number. This MR adds the ability to use Spin pin number instead of Nucleo-style naming in GPIO API. --- zephyr/dts/bindings/gpio/spin-header.yaml | 4 +- .../zephyr/public_api/GpioApi.cpp | 160 +++++++++++++++--- .../zephyr/public_api/GpioApi.h | 101 ++++++----- 3 files changed, 198 insertions(+), 67 deletions(-) diff --git a/zephyr/dts/bindings/gpio/spin-header.yaml b/zephyr/dts/bindings/gpio/spin-header.yaml index c012f10..88c6b72 100644 --- a/zephyr/dts/bindings/gpio/spin-header.yaml +++ b/zephyr/dts/bindings/gpio/spin-header.yaml @@ -6,7 +6,7 @@ description: | The OwnTech Spin board provides 6 headers: * 3 around the external egde of the board, - * 4 within the board. + * 3 within the board. Proceeding counter-clockwise: * A 20-pin header on the external left of the board, numbered from 1 to 20 top to bottom. @@ -21,7 +21,7 @@ description: | * A 1-pin header on the internal bottom right of the board, numbered 59. - This binding provides a nexus mapping for 46 pins. + This binding provides a nexus mapping for 49 pins. Pins not described in this map are displayed between curly brackets. ---------------------------------- diff --git a/zephyr/modules/owntech_gpio_api/zephyr/public_api/GpioApi.cpp b/zephyr/modules/owntech_gpio_api/zephyr/public_api/GpioApi.cpp index 05fed1f..b25a84a 100644 --- a/zephyr/modules/owntech_gpio_api/zephyr/public_api/GpioApi.cpp +++ b/zephyr/modules/owntech_gpio_api/zephyr/public_api/GpioApi.cpp @@ -39,7 +39,7 @@ const gpio_flags_t INPUT_PULLUP = GPIO_INPUT | GPIO_PULL_UP; const gpio_flags_t OUTPUT = GPIO_OUTPUT; -void GpioApi::configurePin(pin_t pin, gpio_flags_t flags) +void GpioApi::configurePin(uint8_t pin, gpio_flags_t flags) { gpio_pin_t pin_number = this->getPinNumber(pin); const struct device* port = this->getGpioDevice(pin); @@ -49,7 +49,7 @@ void GpioApi::configurePin(pin_t pin, gpio_flags_t flags) } } -void GpioApi::setPin(pin_t pin) +void GpioApi::setPin(uint8_t pin) { gpio_pin_t pin_number = this->getPinNumber(pin); const struct device* port = this->getGpioDevice(pin); @@ -59,7 +59,7 @@ void GpioApi::setPin(pin_t pin) } } -void GpioApi::resetPin(pin_t pin) +void GpioApi::resetPin(uint8_t pin) { gpio_pin_t pin_number = this->getPinNumber(pin); const struct device* port = this->getGpioDevice(pin); @@ -69,7 +69,7 @@ void GpioApi::resetPin(pin_t pin) } } -void GpioApi::togglePin(pin_t pin) +void GpioApi::togglePin(uint8_t pin) { gpio_pin_t pin_number = this->getPinNumber(pin); const struct device* port = this->getGpioDevice(pin); @@ -79,7 +79,7 @@ void GpioApi::togglePin(pin_t pin) } } -void GpioApi::writePin(pin_t pin, uint8_t value) +void GpioApi::writePin(uint8_t pin, uint8_t value) { gpio_pin_t pin_number = this->getPinNumber(pin); const struct device* port = this->getGpioDevice(pin); @@ -89,7 +89,7 @@ void GpioApi::writePin(pin_t pin, uint8_t value) } } -uint8_t GpioApi::readPin(pin_t pin) +uint8_t GpioApi::readPin(uint8_t pin) { gpio_pin_t pin_number = this->getPinNumber(pin); const struct device* port = this->getGpioDevice(pin); @@ -101,28 +101,142 @@ uint8_t GpioApi::readPin(pin_t pin) return 0; } -gpio_pin_t GpioApi::getPinNumber(pin_t pin) +gpio_pin_t GpioApi::getPinNumber(uint8_t pin) { - return (((uint8_t)pin) & 0x0F) + 1; + if ( (pin & 0x80) != 0) // Nucleo format + { + return (((uint8_t)pin) & 0x0F); + } + else // Pin number + { + if (pin == 1) return 11; + else if (pin == 2) return 12; + else if (pin == 4) return 13; + else if (pin == 5) return 14; + else if (pin == 6) return 15; + else if (pin == 7) return 6; + else if (pin == 9) return 7; + else if (pin == 10) return 8; + else if (pin == 11) return 9; + else if (pin == 12) return 8; + else if (pin == 14) return 9; + else if (pin == 15) return 10; + else if (pin == 16) return 10; + else if (pin == 17) return 11; + else if (pin == 19) return 12; + else if (pin == 20) return 4; + else if (pin == 21) return 9; + else if (pin == 22) return 13; + else if (pin == 24) return 0; + else if (pin == 25) return 1; + else if (pin == 26) return 2; + else if (pin == 27) return 3; + else if (pin == 29) return 0; + else if (pin == 30) return 1; + else if (pin == 31) return 0; + else if (pin == 32) return 5; + else if (pin == 34) return 6; + else if (pin == 35) return 4; + else if (pin == 37) return 1; + else if (pin == 41) return 10; + else if (pin == 42) return 2; + else if (pin == 43) return 5; + else if (pin == 44) return 7; + else if (pin == 45) return 4; + else if (pin == 46) return 13; + else if (pin == 47) return 14; + else if (pin == 48) return 15; + else if (pin == 49) return 2; + else if (pin == 50) return 3; + else if (pin == 51) return 2; + else if (pin == 52) return 3; + else if (pin == 53) return 5; + else if (pin == 55) return 6; + else if (pin == 56) return 7; + else if (pin == 58) return 8; + } + return 0xFF; } -const struct device* GpioApi::getGpioDevice(pin_t pin) +const struct device* GpioApi::getGpioDevice(uint8_t pin) { - uint8_t deviceNumber = ((uint8_t)pin) & 0xF0; - switch (deviceNumber) + if ( (pin & 0x80) != 0) // Nucleo format + { + uint8_t deviceNumber = ((uint8_t)pin) & 0xF0; + switch (deviceNumber) + { + case PA: + return GPIO_A; + break; + case PB: + return GPIO_B; + break; + case PC: + return GPIO_C; + break; + case PD: + return GPIO_D; + break; + } + } + else // Pin number { - case PA: - return GPIO_A; - break; - case PB: - return GPIO_B; - break; - case PC: - return GPIO_C; - break; - case PD: - return GPIO_D; - break; + switch (pin) + { + case 12: + case 14: + case 15: + case 29: + case 30: + case 32: + case 34: + case 44: + case 45: + case 46: + case 47: + case 48: + case 51: + case 52: + return GPIO_A; + break; + case 1: + case 2: + case 4: + case 5: + case 6: + case 20: + case 21: + case 31: + case 37: + case 41: + case 42: + case 50: + case 53: + case 55: + case 56: + case 58: + return GPIO_B; + break; + case 7: + case 9: + case 10: + case 11: + case 16: + case 17: + case 19: + case 22: + case 24: + case 25: + case 26: + case 27: + case 35: + case 43: + return GPIO_C; + break; + case 49: + return GPIO_D; + break; + } } return nullptr; diff --git a/zephyr/modules/owntech_gpio_api/zephyr/public_api/GpioApi.h b/zephyr/modules/owntech_gpio_api/zephyr/public_api/GpioApi.h index 97c169c..e74193e 100644 --- a/zephyr/modules/owntech_gpio_api/zephyr/public_api/GpioApi.h +++ b/zephyr/modules/owntech_gpio_api/zephyr/public_api/GpioApi.h @@ -42,34 +42,36 @@ extern const gpio_flags_t INPUT; extern const gpio_flags_t INPUT_PULLUP; extern const gpio_flags_t OUTPUT; -static const uint8_t PA = 0x00; -static const uint8_t PB = 0x10; -static const uint8_t PC = 0x20; -static const uint8_t PD = 0x30; - -static const uint8_t P1 = 0x0; -static const uint8_t P2 = 0x1; -static const uint8_t P3 = 0x2; -static const uint8_t P4 = 0x3; -static const uint8_t P5 = 0x4; -static const uint8_t P6 = 0x5; -static const uint8_t P7 = 0x6; -static const uint8_t P8 = 0x7; -static const uint8_t P9 = 0x8; -static const uint8_t P10 = 0x9; -static const uint8_t P11 = 0xA; -static const uint8_t P12 = 0xB; -static const uint8_t P13 = 0xC; -static const uint8_t P14 = 0xD; -static const uint8_t P15 = 0xE; -static const uint8_t P16 = 0xF; +// 0x80 is used to indicate nucleo-style pin +static const uint8_t PA = 0x80 | 0x00; +static const uint8_t PB = 0x80 | 0x10; +static const uint8_t PC = 0x80 | 0x20; +static const uint8_t PD = 0x80 | 0x30; + +static const uint8_t P0 = 0x0; +static const uint8_t P1 = 0x1; +static const uint8_t P2 = 0x2; +static const uint8_t P3 = 0x3; +static const uint8_t P4 = 0x4; +static const uint8_t P5 = 0x5; +static const uint8_t P6 = 0x6; +static const uint8_t P7 = 0x7; +static const uint8_t P8 = 0x8; +static const uint8_t P9 = 0x9; +static const uint8_t P10 = 0xA; +static const uint8_t P11 = 0xB; +static const uint8_t P12 = 0xC; +static const uint8_t P13 = 0xD; +static const uint8_t P14 = 0xE; +static const uint8_t P15 = 0xF; ///// // Public types -typedef enum +typedef enum : uint8_t { + PA0 = PA | P0, PA1 = PA | P1, PA2 = PA | P2, PA3 = PA | P3, @@ -85,7 +87,7 @@ typedef enum PA13 = PA | P13, PA14 = PA | P14, PA15 = PA | P15, - PA16 = PA | P16, + PB0 = PB | P0, PB1 = PB | P1, PB2 = PB | P2, PB3 = PB | P3, @@ -101,7 +103,7 @@ typedef enum PB13 = PB | P13, PB14 = PB | P14, PB15 = PB | P15, - PB16 = PB | P16, + PC0 = PC | P0, PC1 = PC | P1, PC2 = PC | P2, PC3 = PC | P3, @@ -117,7 +119,7 @@ typedef enum PC13 = PC | P13, PC14 = PC | P14, PC15 = PC | P15, - PC16 = PC | P16, + PD0 = PD | P0, PD1 = PD | P1, PD2 = PD | P2, PD3 = PD | P3 @@ -136,61 +138,76 @@ public: * prior to accessing any other function * from this API on the pin. * - * @param pin STM32-style name of the pin, - * e.g. PA1, PB10, etc. See pin_t type for - * the full list of existing pins on - * Spin board. + * @param pin Number of the Spin pin OR STM32-style + * name of the pin, e.g. PA1, PB10, etc. + * See pin_t type for the full list of available + * STM32-style pins on Spin board. * @param flags Pin configuration flags. * Authorized values: * - INPUT * - INPUT_PULLUP * - OUTPUT */ - void configurePin(pin_t pin, gpio_flags_t flags); + void configurePin(uint8_t pin, gpio_flags_t flags); /** * @brief Set the value of a pin configured as output to 1. * - * @param pin STM32-style name of the pin. + * @param pin Number of the Spin pin OR STM32-style + * name of the pin, e.g. PA1, PB10, etc. + * See pin_t type for the full list of available + * STM32-style pins on Spin board. */ - void setPin(pin_t pin); + void setPin(uint8_t pin); /** * @brief Reset the value of a pin configured as output to 0. * - * @param pin STM32-style name of the pin. + * @param pin Number of the Spin pin OR STM32-style + * name of the pin, e.g. PA1, PB10, etc. + * See pin_t type for the full list of available + * STM32-style pins on Spin board. */ - void resetPin(pin_t pin); + void resetPin(uint8_t pin); /** * @brief Toggle the value of a pin configured as output: * - if pin value is 1, it will be set to 0 * - if pin value is 0, it will be set to 1. * - * @param pin STM32-style name of the pin. + * @param pin Number of the Spin pin OR STM32-style + * name of the pin, e.g. PA1, PB10, etc. + * See pin_t type for the full list of available + * STM32-style pins on Spin board. */ - void togglePin(pin_t pin); + void togglePin(uint8_t pin); /** * @brief Set the value of a pin configured as output * to a given value. * - * @param pin STM32-style name of the pin. + * @param pin Number of the Spin pin OR STM32-style + * name of the pin, e.g. PA1, PB10, etc. + * See pin_t type for the full list of available + * STM32-style pins on Spin board. * @param value Value (0 or 1) to assign to the pin. */ - void writePin(pin_t pin, uint8_t value); + void writePin(uint8_t pin, uint8_t value); /** * @brief Get the current value of a pin configured as input. * - * @param pin STM32-style name of the pin. + * @param pin Number of the Spin pin OR STM32-style + * name of the pin, e.g. PA1, PB10, etc. + * See pin_t type for the full list of available + * STM32-style pins on Spin board. * @return Current value (0 or 1) of the pin. */ - uint8_t readPin(pin_t pin); + uint8_t readPin(uint8_t pin); private: - gpio_pin_t getPinNumber(pin_t pin); - const struct device* getGpioDevice(pin_t pin); + gpio_pin_t getPinNumber(uint8_t pin); + const struct device* getGpioDevice(uint8_t pin); }; -- GitLab