From 2c3b61a248dc74a3802dcac5bfb8b62d72333a24 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 2 Nov 2018 15:00:19 +0100 Subject: [PATCH 01/11] Move pin configured functions as macro Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/PinConfigured.c | 46 ----------------------------- cores/arduino/stm32/PinConfigured.h | 10 ++++--- 2 files changed, 6 insertions(+), 50 deletions(-) delete mode 100644 cores/arduino/stm32/PinConfigured.c diff --git a/cores/arduino/stm32/PinConfigured.c b/cores/arduino/stm32/PinConfigured.c deleted file mode 100644 index 024219c512..0000000000 --- a/cores/arduino/stm32/PinConfigured.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - ******************************************************************************* - * Copyright (c) 2017, STMicroelectronics - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************* - */ -#include "PinConfigured.h" - -bool is_pin_configured(PinName pin, uint32_t* map) { - uint32_t index = PINCONF_INDEX(pin); - return PINCONF_VAL(pin, map[index]); -} - -void set_pin_configured(PinName pin, uint32_t* map) { - uint32_t index = PINCONF_INDEX(pin); - map[index] = map[index] | PINCONF_BIT(pin); -} - -void reset_pin_configured(PinName pin, uint32_t* map) { - uint32_t index = PINCONF_INDEX(pin); - map[index] = map[index] & (~PINCONF_BIT(pin)); -} - diff --git a/cores/arduino/stm32/PinConfigured.h b/cores/arduino/stm32/PinConfigured.h index 7b7d393049..5675a86486 100644 --- a/cores/arduino/stm32/PinConfigured.h +++ b/cores/arduino/stm32/PinConfigured.h @@ -44,10 +44,12 @@ extern "C" { #define PINCONF_VAL(X, Y) ((Y >> PINCONF_SHIFT(X)) & PINCONF_MASK) - -bool is_pin_configured(PinName pin, uint32_t* map); -void set_pin_configured(PinName pin, uint32_t* map); -void reset_pin_configured(PinName pin, uint32_t* map); +#define is_pin_configured(pin, map) \ + (PINCONF_VAL(pin, map[PINCONF_INDEX(pin)])) +#define set_pin_configured(pin, map) \ + (map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] | PINCONF_BIT(pin)) +#define reset_pin_configured(pin, map) \ + (map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] & (~PINCONF_BIT(pin))) #ifdef __cplusplus } From 4fc4add04e8881ebcb5cabd9c1eef03e039d3ad5 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 2 Nov 2018 15:04:31 +0100 Subject: [PATCH 02/11] Remove useless check User have to ensure the pin is configured. Signed-off-by: Frederic.Pillon --- cores/arduino/wiring_digital.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index 6619299d2b..448c9064eb 100644 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -72,9 +72,7 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal ) { PinName p = digitalPinToPinName(ulPin); if(p != NC) { - if(is_pin_configured(p, g_digPinConfigured)) { - digital_io_write(get_GPIO_Port(STM_PORT(p)), STM_GPIO_PIN(p), ulVal); - } + digital_io_write(get_GPIO_Port(STM_PORT(p)), STM_GPIO_PIN(p), ulVal); } } @@ -83,9 +81,7 @@ int digitalRead( uint32_t ulPin ) uint8_t level = 0; PinName p = digitalPinToPinName(ulPin); if(p != NC) { - if(is_pin_configured(p, g_digPinConfigured)) { - level = digital_io_read(get_GPIO_Port(STM_PORT(p)), STM_GPIO_PIN(p)); - } + level = digital_io_read(get_GPIO_Port(STM_PORT(p)), STM_GPIO_PIN(p)); } return (level)? HIGH : LOW; } From b4119c74eebe215893d346974aba22365ef90232 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 2 Nov 2018 17:26:15 +0100 Subject: [PATCH 03/11] Enhance get_GPIO_Port speed Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/PortNames.c | 62 ++++++++------------------------- cores/arduino/stm32/PortNames.h | 6 +++- 2 files changed, 19 insertions(+), 49 deletions(-) diff --git a/cores/arduino/stm32/PortNames.c b/cores/arduino/stm32/PortNames.c index ef193e3c64..87c5378982 100644 --- a/cores/arduino/stm32/PortNames.c +++ b/cores/arduino/stm32/PortNames.c @@ -28,73 +28,40 @@ ******************************************************************************* */ #include "PortNames.h" -#include "stm32_def.h" -// Return GPIO base address -GPIO_TypeDef *get_GPIO_Port(uint32_t port_idx) { - GPIO_TypeDef* gpioPort = 0; - switch (port_idx) { - case PortA: - gpioPort = (GPIO_TypeDef *)GPIOA_BASE; - break; - case PortB: - gpioPort = (GPIO_TypeDef *)GPIOB_BASE; - break; +GPIO_TypeDef * GPIOPort[MAX_NB_PORT] = { + (GPIO_TypeDef *)GPIOA_BASE, + (GPIO_TypeDef *)GPIOB_BASE #if defined GPIOC_BASE - case PortC: - gpioPort = (GPIO_TypeDef *)GPIOC_BASE; - break; + ,(GPIO_TypeDef *)GPIOC_BASE #endif #if defined GPIOD_BASE - case PortD: - gpioPort = (GPIO_TypeDef *)GPIOD_BASE; - break; + ,(GPIO_TypeDef *)GPIOD_BASE #endif #if defined GPIOE_BASE - case PortE: - gpioPort = (GPIO_TypeDef *)GPIOE_BASE; - break; + ,(GPIO_TypeDef *)GPIOE_BASE #endif #if defined GPIOF_BASE - case PortF: - gpioPort = (GPIO_TypeDef *)GPIOF_BASE; - break; + ,(GPIO_TypeDef *)GPIOF_BASE #endif #if defined GPIOG_BASE - case PortG: - gpioPort = (GPIO_TypeDef *)GPIOG_BASE; - break; + ,(GPIO_TypeDef *)GPIOG_BASE #endif #if defined GPIOH_BASE - case PortH: - gpioPort = (GPIO_TypeDef *)GPIOH_BASE; - break; + ,(GPIO_TypeDef *)GPIOH_BASE #endif #if defined GPIOI_BASE - case PortI: - gpioPort = (GPIO_TypeDef *)GPIOI_BASE; - break; + ,(GPIO_TypeDef *)GPIOI_BASE #endif #if defined GPIOJ_BASE - case PortJ: - gpioPort = (GPIO_TypeDef *)GPIOJ_BASE; - break; + ,(GPIO_TypeDef *)GPIOJ_BASE #endif #if defined GPIOK_BASE - case PortK: - gpioPort = (GPIO_TypeDef *)GPIOK_BASE; - break; + ,(GPIO_TypeDef *)GPIOK_BASE #endif - default: - // wrong port number - //TBD: error management - gpioPort = 0; - break; - } - return gpioPort; -} +}; -// Enable GPIO clock and return GPIO base address +/* Enable GPIO clock and return GPIO base address */ GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx) { GPIO_TypeDef* gpioPort = 0; switch (port_idx) { @@ -173,4 +140,3 @@ GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx) { } return gpioPort; } - diff --git a/cores/arduino/stm32/PortNames.h b/cores/arduino/stm32/PortNames.h index a14d5b7233..d970bd76ad 100644 --- a/cores/arduino/stm32/PortNames.h +++ b/cores/arduino/stm32/PortNames.h @@ -36,6 +36,8 @@ extern "C" { #endif +extern GPIO_TypeDef *GPIOPort[]; + typedef enum { FirstPort = 0x00, PortA = FirstPort, @@ -73,7 +75,9 @@ typedef enum { #define MAX_NB_PORT (LastPort-FirstPort+1) -GPIO_TypeDef *get_GPIO_Port(uint32_t port_idx); +/* Return GPIO base address */ +#define get_GPIO_Port(p) ((p < MAX_NB_PORT) ? GPIOPort[p] : (GPIO_TypeDef *)GPIOA_BASE) +/* Enable GPIO clock and return GPIO base address */ GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx); #ifdef __cplusplus From 107ba0baabfcacfd95ca83a6d60902070c62c938 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 2 Nov 2018 17:26:27 +0100 Subject: [PATCH 04/11] Use LL for GPIO access Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/digital_io.c | 32 ++++---------------------------- cores/arduino/stm32/digital_io.h | 31 +++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/cores/arduino/stm32/digital_io.c b/cores/arduino/stm32/digital_io.c index 3824947999..5b5025b4b9 100644 --- a/cores/arduino/stm32/digital_io.c +++ b/cores/arduino/stm32/digital_io.c @@ -57,7 +57,11 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull) GPIO_InitTypeDef GPIO_InitStructure; GPIO_TypeDef *port = set_GPIO_Port_Clock(STM_PORT(pin)); GPIO_InitStructure.Pin = STM_GPIO_PIN(pin); +#ifdef GPIO_SPEED_FREQ_VERY_HIGH + GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; +#else GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH; +#endif GPIO_InitStructure.Mode = mode; GPIO_InitStructure.Pull = pull; #ifdef STM32F1xx @@ -66,34 +70,6 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull) HAL_GPIO_Init(port, &GPIO_InitStructure); } -/** - * @brief This function set a value to an IO - * @param port : one of the gpio port - * @param pin : one of the gpio pin - * @param val : 0 to set to low, any other value to set to high - * @retval None - */ -void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val) -{ - if(val) { - HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET); - } else { - HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET); - } -} - - -/** - * @brief This function set a value to an IO - * @param port : one of the gpio port - * @param pin : one of the gpio pin - * @retval The pin state (LOW or HIGH) - */ -uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin) -{ - return (uint32_t)HAL_GPIO_ReadPin(port, pin); -} - #ifdef __cplusplus } #endif diff --git a/cores/arduino/stm32/digital_io.h b/cores/arduino/stm32/digital_io.h index 941f3c7c73..8165b6baa0 100644 --- a/cores/arduino/stm32/digital_io.h +++ b/cores/arduino/stm32/digital_io.h @@ -42,6 +42,7 @@ /* Includes ------------------------------------------------------------------*/ #include "stm32_def.h" #include "PeripheralPins.h" +#include "stm32yyxx_ll_gpio.h" #ifdef __cplusplus extern "C" { @@ -52,8 +53,34 @@ /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull); -void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val); -uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin); + +/** + * @brief This function set a value to an IO + * @param port : one of the gpio port + * @param pin : one of the gpio pin + * @param val : 0 to set to low, any other value to set to high + * @retval None + */ +static inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val) +{ + if(val) { + LL_GPIO_SetOutputPin(port, pin); + } else { + LL_GPIO_ResetOutputPin(port, pin); + } +} + + +/** + * @brief This function set a value to an IO + * @param port : one of the gpio port + * @param pin : one of the gpio pin + * @retval The pin state (LOW or HIGH) + */ +static inline uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin) +{ + return LL_GPIO_IsInputPinSet(port, pin); +} #ifdef __cplusplus } From 945093bc81ced1393b3649276e4e4a5ef7cadd1f Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 5 Nov 2018 14:41:32 +0100 Subject: [PATCH 05/11] Add digitalRead/WriteFast API Use the PinName (PY_n) instead of the pin number (Pyn) Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/digital_io.c | 2 -- cores/arduino/stm32/digital_io.h | 32 +++++++++++++++++++++++++++++--- cores/arduino/wiring_digital.c | 12 ++---------- keywords.txt | 2 ++ 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/cores/arduino/stm32/digital_io.c b/cores/arduino/stm32/digital_io.c index 5b5025b4b9..6705d59489 100644 --- a/cores/arduino/stm32/digital_io.c +++ b/cores/arduino/stm32/digital_io.c @@ -36,8 +36,6 @@ ****************************************************************************** */ #include "digital_io.h" -#include "stm32_def.h" -#include "hw_config.h" #include "PinAF_STM32F1.h" #ifdef __cplusplus diff --git a/cores/arduino/stm32/digital_io.h b/cores/arduino/stm32/digital_io.h index 8165b6baa0..a4ee8fb792 100644 --- a/cores/arduino/stm32/digital_io.h +++ b/cores/arduino/stm32/digital_io.h @@ -40,8 +40,8 @@ #define __DIGITAL_IO_H /* Includes ------------------------------------------------------------------*/ -#include "stm32_def.h" -#include "PeripheralPins.h" +#include "wiring_constants.h" +#include "PinNames.h" #include "stm32yyxx_ll_gpio.h" #ifdef __cplusplus @@ -70,7 +70,6 @@ static inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t v } } - /** * @brief This function set a value to an IO * @param port : one of the gpio port @@ -82,6 +81,33 @@ static inline uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin) return LL_GPIO_IsInputPinSet(port, pin); } +/** + * @brief This function set a value to an IO + * @param pn : Pin name + * @param val : 0 to set to low, any other value to set to high + * @retval None + */ +static inline void digitalWriteFast( PinName pn, uint32_t ulVal ) +{ + if(pn != NC) { + digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn), ulVal); + } +} + +/** + * @brief This function read the value of an IO + * @param pn : Pin name + * @retval The pin state (LOW or HIGH) + */ +static inline int digitalReadFast( PinName pn ) +{ + uint8_t level = 0; + if(pn != NC) { + level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); + } + return (level)? HIGH : LOW; +} + #ifdef __cplusplus } #endif diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index 448c9064eb..661acdee83 100644 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -70,20 +70,12 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) void digitalWrite( uint32_t ulPin, uint32_t ulVal ) { - PinName p = digitalPinToPinName(ulPin); - if(p != NC) { - digital_io_write(get_GPIO_Port(STM_PORT(p)), STM_GPIO_PIN(p), ulVal); - } + digitalWriteFast(digitalPinToPinName(ulPin), ulVal); } int digitalRead( uint32_t ulPin ) { - uint8_t level = 0; - PinName p = digitalPinToPinName(ulPin); - if(p != NC) { - level = digital_io_read(get_GPIO_Port(STM_PORT(p)), STM_GPIO_PIN(p)); - } - return (level)? HIGH : LOW; + return digitalReadFast(digitalPinToPinName(ulPin)); } #ifdef __cplusplus diff --git a/keywords.txt b/keywords.txt index 13b979ebea..700bdbb123 100644 --- a/keywords.txt +++ b/keywords.txt @@ -350,6 +350,8 @@ portConfigRegister KEYWORD2 digitalPinIsValid KEYWORD2 digitalPinFirstOccurence KEYWORD2 pinIsSerial KEYWORD2 +digitalReadFast KEYWORD2 +digitalWriteFast KEYWORD2 # Pin number PA0 LITERAL1 From b735591f02532fb77516c8dae8f504166bd1d8aa Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 5 Nov 2018 14:41:49 +0100 Subject: [PATCH 06/11] Add missing include Signed-off-by: Frederic.Pillon --- cores/arduino/wiring_constants.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/arduino/wiring_constants.h b/cores/arduino/wiring_constants.h index 312cabde28..ec4cff2b91 100644 --- a/cores/arduino/wiring_constants.h +++ b/cores/arduino/wiring_constants.h @@ -20,12 +20,14 @@ #define _WIRING_CONSTANTS_ #include +#include #ifdef __cplusplus #include using std::min; using std::max; #else // C +#include #ifndef abs #define abs(x) ((x)>0?(x):-(x)) #endif // abs From 62fd2762cc87540445cb88382a6fa4715cb8ccd4 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 5 Nov 2018 14:42:09 +0100 Subject: [PATCH 07/11] Fix analogInPinToBit macro Signed-off-by: Frederic.Pillon --- cores/arduino/pins_arduino.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/pins_arduino.h b/cores/arduino/pins_arduino.h index b13b9c36e0..de0d05965e 100644 --- a/cores/arduino/pins_arduino.h +++ b/cores/arduino/pins_arduino.h @@ -232,7 +232,7 @@ uint32_t pinNametoDigitalPin(PinName p); #define digitalPinToPort(p) (get_GPIO_Port(STM_PORT(digitalPinToPinName(p)))) #define digitalPinToBitMask(p) (STM_GPIO_PIN(digitalPinToPinName(p))) -#define analogInPinToBit(p) (STM_PIN(digitalPinToPinName(p))) +#define analogInPinToBit(p) (STM_GPIO_PIN(digitalPinToPinName(p))) #define portOutputRegister(P) (&(P->ODR)) #define portInputRegister(P) (&(P->IDR)) From 4aff9b756b7f85df9eb94356463e086ba3bfcf8f Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 5 Nov 2018 15:02:08 +0100 Subject: [PATCH 08/11] Add digitalToggle APIs Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/digital_io.h | 24 ++++++++++++++++++++++++ cores/arduino/wiring_digital.c | 5 +++++ cores/arduino/wiring_digital.h | 7 +++++++ keywords.txt | 2 ++ 4 files changed, 38 insertions(+) diff --git a/cores/arduino/stm32/digital_io.h b/cores/arduino/stm32/digital_io.h index a4ee8fb792..c987315fde 100644 --- a/cores/arduino/stm32/digital_io.h +++ b/cores/arduino/stm32/digital_io.h @@ -81,6 +81,17 @@ static inline uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin) return LL_GPIO_IsInputPinSet(port, pin); } +/** + * @brief This function toggle value of an IO + * @param port : one of the gpio port + * @param pin : one of the gpio pin + * @retval None + */ +static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin) +{ + LL_GPIO_TogglePin(port, pin); +} + /** * @brief This function set a value to an IO * @param pn : Pin name @@ -108,6 +119,19 @@ static inline int digitalReadFast( PinName pn ) return (level)? HIGH : LOW; } +/** + * @brief This function toggle value of an IO + * @param port : one of the gpio port + * @param pin : one of the gpio pin + * @retval None + */ +static inline void digitalToggleFast(PinName pn) +{ + if(pn != NC) { + digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); + } +} + #ifdef __cplusplus } #endif diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index 661acdee83..0122c26579 100644 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -78,6 +78,11 @@ int digitalRead( uint32_t ulPin ) return digitalReadFast(digitalPinToPinName(ulPin)); } +void digitalToggle( uint32_t ulPin ) +{ + digitalToggleFast(digitalPinToPinName(ulPin)); +} + #ifdef __cplusplus } #endif diff --git a/cores/arduino/wiring_digital.h b/cores/arduino/wiring_digital.h index 3b69cf834d..fa26f511f3 100644 --- a/cores/arduino/wiring_digital.h +++ b/cores/arduino/wiring_digital.h @@ -62,6 +62,13 @@ extern void digitalWrite( uint32_t dwPin, uint32_t dwVal ) ; */ extern int digitalRead( uint32_t ulPin ) ; +/** + * \brief Toggle the value from a specified digital pin. + * + * \param ulPin The number of the digital pin you want to toggle (int) + */ +extern void digitalToggle( uint32_t ulPin ) ; + #ifdef __cplusplus } #endif diff --git a/keywords.txt b/keywords.txt index 700bdbb123..2a71fbcebe 100644 --- a/keywords.txt +++ b/keywords.txt @@ -352,6 +352,8 @@ digitalPinFirstOccurence KEYWORD2 pinIsSerial KEYWORD2 digitalReadFast KEYWORD2 digitalWriteFast KEYWORD2 +digitalToggle KEYWORD2 +digitalToggleFast KEYWORD2 # Pin number PA0 LITERAL1 From cd7979d2068d497c2c296826449051fefb725472 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 5 Nov 2018 14:47:19 +0100 Subject: [PATCH 09/11] Clean up digital code after update Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/digital_io.c | 5 +---- cores/arduino/stm32/digital_io.h | 24 ++++++++-------------- cores/arduino/wiring_digital.c | 35 ++++++++++++++++---------------- cores/arduino/wiring_digital.h | 29 ++++++++------------------ 4 files changed, 36 insertions(+), 57 deletions(-) diff --git a/cores/arduino/stm32/digital_io.c b/cores/arduino/stm32/digital_io.c index 6705d59489..167e3ec50a 100644 --- a/cores/arduino/stm32/digital_io.c +++ b/cores/arduino/stm32/digital_io.c @@ -1,9 +1,6 @@ /** ****************************************************************************** * @file digital_io.c - * @author WI6LABS - * @version V1.0.0 - * @date 01-August-2016 * @brief Provide an interface to configure hw ios * ****************************************************************************** @@ -39,7 +36,7 @@ #include "PinAF_STM32F1.h" #ifdef __cplusplus - extern "C" { +extern "C" { #endif /** diff --git a/cores/arduino/stm32/digital_io.h b/cores/arduino/stm32/digital_io.h index c987315fde..1e8c259352 100644 --- a/cores/arduino/stm32/digital_io.h +++ b/cores/arduino/stm32/digital_io.h @@ -1,9 +1,6 @@ /** ****************************************************************************** * @file digital_io.h - * @author WI6LABS - * @version V1.0.0 - * @date 01-August-2016 * @brief Header for digital_io module ****************************************************************************** * @attention @@ -45,12 +42,9 @@ #include "stm32yyxx_ll_gpio.h" #ifdef __cplusplus - extern "C" { +extern "C" { #endif -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull); @@ -63,7 +57,7 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull); */ static inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val) { - if(val) { + if (val) { LL_GPIO_SetOutputPin(port, pin); } else { LL_GPIO_ResetOutputPin(port, pin); @@ -71,7 +65,7 @@ static inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t v } /** - * @brief This function set a value to an IO + * @brief This function read the value of an IO * @param port : one of the gpio port * @param pin : one of the gpio pin * @retval The pin state (LOW or HIGH) @@ -98,9 +92,9 @@ static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin) * @param val : 0 to set to low, any other value to set to high * @retval None */ -static inline void digitalWriteFast( PinName pn, uint32_t ulVal ) +static inline void digitalWriteFast(PinName pn, uint32_t ulVal) { - if(pn != NC) { + if (pn != NC) { digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn), ulVal); } } @@ -110,13 +104,13 @@ static inline void digitalWriteFast( PinName pn, uint32_t ulVal ) * @param pn : Pin name * @retval The pin state (LOW or HIGH) */ -static inline int digitalReadFast( PinName pn ) +static inline int digitalReadFast(PinName pn) { uint8_t level = 0; - if(pn != NC) { + if (pn != NC) { level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); } - return (level)? HIGH : LOW; + return (level) ? HIGH : LOW; } /** @@ -127,7 +121,7 @@ static inline int digitalReadFast( PinName pn ) */ static inline void digitalToggleFast(PinName pn) { - if(pn != NC) { + if (pn != NC) { digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); } } diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index 0122c26579..35ff180de1 100644 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -20,7 +20,7 @@ #include "PinConfigured.h" #ifdef __cplusplus - extern "C" { +extern "C" { #endif @@ -29,56 +29,55 @@ uint32_t g_digPinConfigured[MAX_NB_PORT] = {0}; extern uint32_t g_anOutputPinConfigured[MAX_NB_PORT]; -void pinMode( uint32_t ulPin, uint32_t ulMode ) +void pinMode(uint32_t ulPin, uint32_t ulMode) { PinName p = digitalPinToPinName(ulPin); - if(p != NC) { + if (p != NC) { // If the pin that support PWM or DAC output, we need to turn it off - if(is_pin_configured(p, g_anOutputPinConfigured)) { + if (is_pin_configured(p, g_anOutputPinConfigured)) { #ifdef HAL_DAC_MODULE_ENABLED - if(pin_in_pinmap(p, PinMap_DAC)) { + if (pin_in_pinmap(p, PinMap_DAC)) { dac_stop(p); } else #endif //HAL_DAC_MODULE_ENABLED - if(pin_in_pinmap(p, PinMap_PWM)) { - pwm_stop(p); - } + if (pin_in_pinmap(p, PinMap_PWM)) { + pwm_stop(p); + } reset_pin_configured(p, g_anOutputPinConfigured); } - switch ( ulMode ) - { + switch (ulMode) { case INPUT: digital_io_init(p, GPIO_MODE_INPUT, GPIO_NOPULL); - break; + break; case INPUT_PULLUP: digital_io_init(p, GPIO_MODE_INPUT, GPIO_PULLUP); - break; + break; case INPUT_PULLDOWN: digital_io_init(p, GPIO_MODE_INPUT, GPIO_PULLDOWN); - break; + break; case OUTPUT: digital_io_init(p, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL); - break; + break; default: - break; + break; } set_pin_configured(p, g_digPinConfigured); } } -void digitalWrite( uint32_t ulPin, uint32_t ulVal ) +void digitalWrite(uint32_t ulPin, uint32_t ulVal) { digitalWriteFast(digitalPinToPinName(ulPin), ulVal); } -int digitalRead( uint32_t ulPin ) +int digitalRead(uint32_t ulPin) { return digitalReadFast(digitalPinToPinName(ulPin)); } -void digitalToggle( uint32_t ulPin ) +void digitalToggle(uint32_t ulPin) { digitalToggleFast(digitalPinToPinName(ulPin)); } diff --git a/cores/arduino/wiring_digital.h b/cores/arduino/wiring_digital.h index fa26f511f3..a8f4a0a6de 100644 --- a/cores/arduino/wiring_digital.h +++ b/cores/arduino/wiring_digital.h @@ -20,38 +20,27 @@ #define _WIRING_DIGITAL_ #ifdef __cplusplus - extern "C" { +extern "C" { #endif /** - * \brief Configures the specified pin to behave either as an input or an output. See the description of digital pins for details. + * \brief Configures the specified pin to behave either as an input or an output. * - * \param ulPin The number of the pin whose mode you wish to set - * \param ulMode Either INPUT or OUTPUT + * \param dwPin The number of the pin whose mode you wish to set + * \param dwMode Either INPUT, INPUT_PULLUP, INPUT_PULLDOWN or OUTPUT */ -extern void pinMode( uint32_t dwPin, uint32_t dwMode ) ; +extern void pinMode(uint32_t dwPin, uint32_t dwMode) ; /** * \brief Write a HIGH or a LOW value to a digital pin. * * If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the - * corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. - * - * If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal - * 20K pullup resistor (see the tutorial on digital pins). Writing LOW will disable the pullup. The pullup - * resistor is enough to light an LED dimly, so if LEDs appear to work, but very dimly, this is a likely - * cause. The remedy is to set the pin to an output with the pinMode() function. - * - * \note Digital pin PIN_LED is harder to use as a digital input than the other digital pins because it has an LED - * and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up - * resistor, it will hang at around 1.7 V instead of the expected 5V because the onboard LED and series resistor - * pull the voltage level down, meaning it always returns LOW. If you must use pin PIN_LED as a digital input, use an - * external pull down resistor. + * corresponding value: 3.3V for HIGH, 0V (ground) for LOW. * * \param dwPin the pin number * \param dwVal HIGH or LOW */ -extern void digitalWrite( uint32_t dwPin, uint32_t dwVal ) ; +extern void digitalWrite(uint32_t dwPin, uint32_t dwVal) ; /** * \brief Reads the value from a specified digital pin, either HIGH or LOW. @@ -60,14 +49,14 @@ extern void digitalWrite( uint32_t dwPin, uint32_t dwVal ) ; * * \return HIGH or LOW */ -extern int digitalRead( uint32_t ulPin ) ; +extern int digitalRead(uint32_t ulPin) ; /** * \brief Toggle the value from a specified digital pin. * * \param ulPin The number of the digital pin you want to toggle (int) */ -extern void digitalToggle( uint32_t ulPin ) ; +extern void digitalToggle(uint32_t ulPin) ; #ifdef __cplusplus } From 7eeb73141ef35c2e696ab414dabcb5cd573eb450 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Tue, 6 Nov 2018 08:57:52 +0100 Subject: [PATCH 10/11] get_GPIO_Port return NULL if wrong PortName This will cause hard fault if used so check if NC can be removed Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/PortNames.h | 2 +- cores/arduino/stm32/digital_io.h | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/cores/arduino/stm32/PortNames.h b/cores/arduino/stm32/PortNames.h index d970bd76ad..610429018a 100644 --- a/cores/arduino/stm32/PortNames.h +++ b/cores/arduino/stm32/PortNames.h @@ -76,7 +76,7 @@ typedef enum { #define MAX_NB_PORT (LastPort-FirstPort+1) /* Return GPIO base address */ -#define get_GPIO_Port(p) ((p < MAX_NB_PORT) ? GPIOPort[p] : (GPIO_TypeDef *)GPIOA_BASE) +#define get_GPIO_Port(p) ((p < MAX_NB_PORT) ? GPIOPort[p] : (GPIO_TypeDef *)NULL) /* Enable GPIO clock and return GPIO base address */ GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx); diff --git a/cores/arduino/stm32/digital_io.h b/cores/arduino/stm32/digital_io.h index 1e8c259352..c2b05e4487 100644 --- a/cores/arduino/stm32/digital_io.h +++ b/cores/arduino/stm32/digital_io.h @@ -94,9 +94,7 @@ static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin) */ static inline void digitalWriteFast(PinName pn, uint32_t ulVal) { - if (pn != NC) { - digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn), ulVal); - } + digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn), ulVal); } /** @@ -107,9 +105,7 @@ static inline void digitalWriteFast(PinName pn, uint32_t ulVal) static inline int digitalReadFast(PinName pn) { uint8_t level = 0; - if (pn != NC) { - level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); - } + level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); return (level) ? HIGH : LOW; } @@ -121,9 +117,7 @@ static inline int digitalReadFast(PinName pn) */ static inline void digitalToggleFast(PinName pn) { - if (pn != NC) { - digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); - } + digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); } #ifdef __cplusplus From a97e0db24e0b34e9ecfe6d01509741174752334c Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Thu, 8 Nov 2018 10:14:57 +0100 Subject: [PATCH 11/11] Map LL GPIO pin definition to STM_PIN For F1 LL_GPIO_PIN_X is not the same than GPIO_PIN_X Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/digital_io.h | 7 ++++--- cores/arduino/stm32/pinmap.c | 21 +++++++++++++++++++++ cores/arduino/stm32/pinmap.h | 4 ++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/cores/arduino/stm32/digital_io.h b/cores/arduino/stm32/digital_io.h index c2b05e4487..0f8f4d1921 100644 --- a/cores/arduino/stm32/digital_io.h +++ b/cores/arduino/stm32/digital_io.h @@ -39,6 +39,7 @@ /* Includes ------------------------------------------------------------------*/ #include "wiring_constants.h" #include "PinNames.h" +#include "pinmap.h" #include "stm32yyxx_ll_gpio.h" #ifdef __cplusplus @@ -94,7 +95,7 @@ static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin) */ static inline void digitalWriteFast(PinName pn, uint32_t ulVal) { - digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn), ulVal); + digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn), ulVal); } /** @@ -105,7 +106,7 @@ static inline void digitalWriteFast(PinName pn, uint32_t ulVal) static inline int digitalReadFast(PinName pn) { uint8_t level = 0; - level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); + level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn)); return (level) ? HIGH : LOW; } @@ -117,7 +118,7 @@ static inline int digitalReadFast(PinName pn) */ static inline void digitalToggleFast(PinName pn) { - digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_GPIO_PIN(pn)); + digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn)); } #ifdef __cplusplus diff --git a/cores/arduino/stm32/pinmap.c b/cores/arduino/stm32/pinmap.c index abcd381ef9..7436df87f3 100644 --- a/cores/arduino/stm32/pinmap.c +++ b/cores/arduino/stm32/pinmap.c @@ -16,6 +16,27 @@ //Based on mbed-os/hal/mbed_pinmap_common.c #include "pinmap.h" +#include "stm32yyxx_ll_gpio.h" + +/* Map STM_PIN to LL */ +const uint32_t pin_map_ll[16] = { + LL_GPIO_PIN_0, + LL_GPIO_PIN_1, + LL_GPIO_PIN_2, + LL_GPIO_PIN_3, + LL_GPIO_PIN_4, + LL_GPIO_PIN_5, + LL_GPIO_PIN_6, + LL_GPIO_PIN_7, + LL_GPIO_PIN_8, + LL_GPIO_PIN_9, + LL_GPIO_PIN_10, + LL_GPIO_PIN_11, + LL_GPIO_PIN_12, + LL_GPIO_PIN_13, + LL_GPIO_PIN_14, + LL_GPIO_PIN_15 +}; void* pinmap_find_peripheral(PinName pin, const PinMap* map) { while (map->pin != NC) { diff --git a/cores/arduino/stm32/pinmap.h b/cores/arduino/stm32/pinmap.h index 857b9c3461..12e739b937 100644 --- a/cores/arduino/stm32/pinmap.h +++ b/cores/arduino/stm32/pinmap.h @@ -27,6 +27,10 @@ extern "C" { #endif +extern const uint32_t pin_map_ll[16]; + +#define STM_LL_GPIO_PIN(X) (pin_map_ll[STM_PIN(X)]) + // No peripheral #define NP 0U