Library 53- GPIO for STM32F4
GPIO is main thing when connecting your device with external things. As you know, ST started with HAL drivers and they want to remove STD periph drivers on which I’m working on this site.
So this is a next step, how to be independent of things you use. GPIO is used everywhere on my libs, and most problems will be with porting GPIO stuff to new system (when it will be need for that). I will start to migrate all my libs to my GPIO system (which will then be easily to port to HAL layer) so I will update my libs and you will be notified about that.
For start, I’ve update my USART library and it worked just great.
New system is more flexible to select which pins you want to use in your peripheral. I will update all my libs to this version.
Library
Features
- Works with standard peripheral drivers
- Works with HAL drivers
- All GPIO functions included
- Easy GPIO initialization
- Version 1.1 – March 09, 2015
- Added option to deinit GPIO pin
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx GPIO
- TM
- defines.h
- defines.h
Functions and enumerations
Below are listed all functions which can be used with GPIO.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
/** * @brief GPIO Mode enumeration */ typedef enum { TM_GPIO_Mode_IN = 0x00, /*!< GPIO Pin as General Purpose Input */ TM_GPIO_Mode_OUT = 0x01, /*!< GPIO Pin as General Purpose Output */ TM_GPIO_Mode_AF = 0x02, /*!< GPIO Pin as Alternate Function */ TM_GPIO_Mode_AN = 0x03, /*!< GPIO Pin as Analog */ } TM_GPIO_Mode_t; /** * @brief GPIO Output type */ typedef enum { TM_GPIO_OType_PP = 0x00, /*!< GPIO Output Type Push-Pull */ TM_GPIO_OType_OD = 0x01 /*!< GPIO Output Type Open-Drain */ } TM_GPIO_OType_t; /** * @brief GPIO Speed */ typedef enum { TM_GPIO_Speed_Low = 0x00, /*!< GPIO Speed Low */ TM_GPIO_Speed_Medium = 0x01, /*!< GPIO Speed Medium */ TM_GPIO_Speed_Fast = 0x02, /*!< GPIO Speed Fast */ TM_GPIO_Speed_High = 0x03 /*!< GPIO Speed High */ } TM_GPIO_Speed_t; /** * @brief GPIO pull resistors enumeration */ typedef enum { TM_GPIO_PuPd_NOPULL = 0x00, /*!< No pull resistor */ TM_GPIO_PuPd_UP = 0x01, /*!< Pull up resistor enabled */ TM_GPIO_PuPd_DOWN = 0x02 /*!< Pull down resistor enabled */ } TM_GPIO_PuPd_t; /** * @} TM_GPIO_Typedefs */ /** * @defgroup TM_GPIO_Functions * @brief GPIO Functions * @{ */ /** * @brief Initializes GPIO pins(s) * @note This function also enables clock for GPIO port * @param GPIOx: Pointer to GPIOx port you will use for initialization * @param GPIO_Pin: GPIO pin(s) you will use for initialization * @param GPIO_Mode: Select GPIO mode. This parameter can be a value of @ref TM_GPIO_Mode_t enumeration * @param GPIO_OType: Select GPIO Output type. This parameter can be a value of @ref TM_GPIO_OType_t enumeration * @param GPIO_PuPd: Select GPIO pull resistor. This parameter can be a value of @ref TM_GPIO_PuPd_t enumeration * @param GPIO_Speed: Select GPIO speed. This parameter can be a value of @ref TM_GPIO_Speed_t enumeration * @retval None */ void TM_GPIO_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, TM_GPIO_Mode_t GPIO_Mode, TM_GPIO_OType_t GPIO_OType, TM_GPIO_PuPd_t GPIO_PuPd, TM_GPIO_Speed_t GPIO_Speed); /** * @brief Initializes GPIO pins(s) as alternate function * @note This function also enables clock for GPIO port * @param GPIOx: Pointer to GPIOx port you will use for initialization * @param GPIO_Pin: GPIO pin(s) you will use for initialization * @param GPIO_OType: Select GPIO Output type. This parameter can be a value of @ref TM_GPIO_OType_t enumeration * @param GPIO_PuPd: Select GPIO pull resistor. This parameter can be a value of @ref TM_GPIO_PuPd_t enumeration * @param GPIO_Speed: Select GPIO speed. This parameter can be a value of @ref TM_GPIO_Speed_t enumeration * @param Alternate: Alternate function you will use * @retval None */ void TM_GPIO_InitAlternate(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, TM_GPIO_OType_t GPIO_OType, TM_GPIO_PuPd_t GPIO_PuPd, TM_GPIO_Speed_t GPIO_Speed, uint8_t Alternate); /** * @brief Deinitializes pin(s) * @note Pins(s) will be set as analog mode to get low power consumption * @param GPIOx: GPIOx PORT where you want to set pin as input * @param GPIO_Pin: Select GPIO pin(s). You can select more pins with | (OR) operator to set them as input * @retval None */ void TM_GPIO_DeInit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); /** * @brief Sets pin(s) as input * @note Pins HAVE to be initialized first using @ref TM_GPIO_Init() or @ref TM_GPIO_InitAlternate() function * @note This is just an option for fast input mode * @param GPIOx: GPIOx PORT where you want to set pin as input * @param GPIO_Pin: Select GPIO pin(s). You can select more pins with | (OR) operator to set them as input * @retval None */ void TM_GPIO_SetPinAsInput(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); /** * @brief Sets pin(s) as output * @note Pins HAVE to be initialized first using @ref TM_GPIO_Init() or @ref TM_GPIO_InitAlternate() function * @note This is just an option for fast output mode * @param GPIOx: GPIOx PORT where you want to set pin as output * @param GPIO_Pin: Select GPIO pin(s). You can select more pins with | (OR) operator to set them as output * @retval None */ void TM_GPIO_SetPinAsOutput(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); /** * @brief Sets pin(s) as analog * @note Pins HAVE to be initialized first using @ref TM_GPIO_Init() or @ref TM_GPIO_InitAlternate() function * @note This is just an option for fast analog mode * @param GPIOx: GPIOx PORT where you want to set pin as output * @param GPIO_Pin: Select GPIO pin(s). You can select more pins with | (OR) operator to set them as output * @retval None */ void TM_GPIO_SetPinAsAnalog(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); /** * @brief Sets pin(s) low * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to set pin low * @param GPIO_Pin: Select GPIO pin(s). You can select more pins with | (OR) operator to set them low * @retval None */ #define TM_GPIO_SetPinLow(GPIOx, GPIO_Pin) ((GPIOx)->BSRRH = (GPIO_Pin)) /** * @brief Sets pin(s) high * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to set pin high * @param GPIO_Pin: Select GPIO pin(s). You can select more pins with | (OR) operator to set them high * @retval None */ #define TM_GPIO_SetPinHigh(GPIOx, GPIO_Pin) ((GPIOx)->BSRRL = (GPIO_Pin)) /** * @brief Sets pin(s) value * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to set pin value * @param GPIO_Pin: Select GPIO pin(s). You can select more pins with | (OR) operator to set them value * @param val: If parameter is 0 then pin will be low, otherwise high * @retval None */ #define TM_GPIO_SetPinValue(GPIOx, GPIO_Pin, val) ((val) ? TM_GPIO_SetPinHigh(GPIOx, GPIO_Pin) : TM_GPIO_SetPinLow(GPIOx, GPIO_Pin)) /** * @brief Toggles pin(s) * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to toggle pin value * @param GPIO_Pin: Select GPIO pin(s). You can select more pins with | (OR) operator to toggle them all at a time * @retval None */ #define TM_GPIO_TogglePinValue(GPIOx, GPIO_Pin) ((GPIOx)->ODR ^= (GPIO_Pin)) /** * @brief Sets value to entire GPIO PORT * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to set value * @param value: Value for GPIO OUTPUT data * @retval None */ #define TM_GPIO_SetPortValue(GPIOx, value) ((GPIOx)->ODR = (value)) /** * @brief Gets input data bit * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to read input bit value * @param GPIO_Pin: GPIO pin where you want to read value * @retval 1 in case pin is high, or 0 if low */ #define TM_GPIO_GetInputPinValue(GPIOx, GPIO_Pin) (((GPIOx)->IDR & (GPIO_Pin)) == 0 ? 0 : 1) /** * @brief Gets output data bit * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to read output bit value * @param GPIO_Pin: GPIO pin where you want to read value * @retval 1 in case pin is high, or 0 if low */ #define TM_GPIO_GetOutputPinValue(GPIOx, GPIO_Pin) (((GPIOx)->ODR & (GPIO_Pin)) == 0 ? 0 : 1) /** * @brief Gets input value from entire GPIO PORT * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to read input data value * @retval Entire PORT INPUT register */ #define TM_GPIO_GetPortInputValue(GPIOx) ((GPIOx)->IDR) /** * @brief Gets output value from entire GPIO PORT * @note Defined as macro to get maximum speed using register access * @param GPIOx: GPIOx PORT where you want to read output data value * @retval Entire PORT OUTPUT register */ #define TM_GPIO_GetPortOutputValue(GPIOx) ((GPIOx)->ODR) /** * @brief Gets port source from desired GPIOx PORT * @note Meant for private use, unless you know what are you doing * @param GPIOx: GPIO PORT for calculating port source * @retval Calculated port source for GPIO */ uint16_t TM_GPIO_GetPortSource(GPIO_TypeDef* GPIOx); /** * @brief Gets pin source from desired GPIO pin * @note Meant for private use, unless you know what are you doing * @param GPIO_Pin: GPIO pin for calculating port source * @retval Calculated pin source for GPIO pin */ uint16_t TM_GPIO_GetPinSource(uint16_t GPIO_Pin); /** * @brief Locks GPIOx register for future changes * @note You are not able to config GPIO registers until new MCU reset occurs * @param *GPIOx: GPIOx PORT where you want to lock config registers * @param GPIO_Pin: GPIO pin(s) where you want to lock config registers * @retval None */ void TM_GPIO_Lock(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); /** * @} */ |
Example
- Bottom example just toggles leds, depending on which board you have selected on my Keil project template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
/** * Keil project for GPIO pins * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_gpio.h" int main(void) { /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize pins for LEDS on discovery boards */ #if defined(TM_DISCO_STM32F4_DISCOVERY) /* Init pins PD12, 13, 14, 15 */ TM_GPIO_Init(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_High); #elif defined(TM_DISCO_STM32F429_DISCOVERY) /* Init pins PG13, 14 */ TM_GPIO_Init(GPIOG, GPIO_Pin_13 | GPIO_Pin_14, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_High); #elif defined(TM_DISCO_NUCLEO_F401) || defined(TM_DISCO_NUCLEO_F411) /* Init pin PA5 */ TM_GPIO_Init(GPIOA, GPIO_Pin_5, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_High); #endif while (1) { /* Toggle leds */ #if defined(TM_DISCO_STM32F4_DISCOVERY) /* Init pins PD12, 13, 14, 15 */ TM_GPIO_TogglePinValue(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15); #elif defined(TM_DISCO_STM32F429_DISCOVERY) /* Init pins PG13, 14 */ TM_GPIO_TogglePinValue(GPIOG, GPIO_Pin_13 | GPIO_Pin_14); #elif defined(TM_DISCO_NUCLEO_F401) || defined(TM_DISCO_NUCLEO_F411) /* Init pin PA5 */ TM_GPIO_TogglePinValue(GPIOA, GPIO_Pin_5); #endif /* Delay */ Delayms(500); } } |
Recent comments