Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32F3 / Drivers / STM32F3xx_HAL_Driver / Src / stm32f3xx_hal_gpio.c
blobd205d19c2a43f3d8011372061c36bcb8565c232e
1 /**
2 ******************************************************************************
3 * @file stm32f3xx_hal_gpio.c
4 * @author MCD Application Team
5 * @brief GPIO HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the General Purpose Input/Output (GPIO) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
11 @verbatim
12 ==============================================================================
13 ##### GPIO Peripheral features #####
14 ==============================================================================
15 [..]
16 (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
17 configured by software in several modes:
18 (++) Input mode
19 (++) Analog mode
20 (++) Output mode
21 (++) Alternate function mode
22 (++) External interrupt/event lines
24 (+) During and just after reset, the alternate functions and external interrupt
25 lines are not active and the I/O ports are configured in input floating mode.
27 (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
28 activated or not.
30 (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
31 type and the IO speed can be selected depending on the VDD value.
33 (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
34 multiplexer that allows only one peripheral alternate function (AF) connected
35 to an IO pin at a time. In this way, there can be no conflict between peripherals
36 sharing the same IO pin.
38 (+) All ports have external interrupt/event capability. To use external interrupt
39 lines, the port must be configured in input mode. All available GPIO pins are
40 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
42 (+) The external interrupt/event controller consists of up to 23 edge detectors
43 (16 lines are connected to GPIO) for generating event/interrupt requests (each
44 input line can be independently configured to select the type (interrupt or event)
45 and the corresponding trigger event (rising or falling or both). Each line can
46 also be masked independently.
48 ##### How to use this driver #####
49 ==============================================================================
50 [..]
51 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
53 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
54 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
55 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
56 structure.
57 (++) In case of Output or alternate function mode selection: the speed is
58 configured through "Speed" member from GPIO_InitTypeDef structure.
59 (++) In alternate mode is selection, the alternate function connected to the IO
60 is configured through "Alternate" member from GPIO_InitTypeDef structure.
61 (++) Analog mode is required when a pin is to be used as ADC channel
62 or DAC output.
63 (++) In case of external interrupt/event selection the "Mode" member from
64 GPIO_InitTypeDef structure select the type (interrupt or event) and
65 the corresponding trigger event (rising or falling or both).
67 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
68 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
69 HAL_NVIC_EnableIRQ().
71 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
73 (#) To set/reset the level of a pin configured in output mode use
74 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
76 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
78 (#) During and just after reset, the alternate functions are not
79 active and the GPIO pins are configured in input floating mode (except JTAG
80 pins).
82 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
83 (PC14 and PC15U, respectively) when the LSE oscillator is off. The LSE has
84 priority over the GPIO function.
86 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
87 general purpose PF0 and PF1, respectively, when the HSE oscillator is off.
88 The HSE has priority over the GPIO function.
90 @endverbatim
91 ******************************************************************************
92 * @attention
94 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
96 * Redistribution and use in source and binary forms, with or without modification,
97 * are permitted provided that the following conditions are met:
98 * 1. Redistributions of source code must retain the above copyright notice,
99 * this list of conditions and the following disclaimer.
100 * 2. Redistributions in binary form must reproduce the above copyright notice,
101 * this list of conditions and the following disclaimer in the documentation
102 * and/or other materials provided with the distribution.
103 * 3. Neither the name of STMicroelectronics nor the names of its contributors
104 * may be used to endorse or promote products derived from this software
105 * without specific prior written permission.
107 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
108 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
109 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
110 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
111 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
112 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
113 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
114 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
115 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
116 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
118 ******************************************************************************
121 /* Includes ------------------------------------------------------------------*/
122 #include "stm32f3xx_hal.h"
124 /** @addtogroup STM32F3xx_HAL_Driver
125 * @{
128 /** @defgroup GPIO GPIO
129 * @brief GPIO HAL module driver
130 * @{
133 #ifdef HAL_GPIO_MODULE_ENABLED
135 /* Private typedef -----------------------------------------------------------*/
136 /* Private defines -----------------------------------------------------------*/
137 /** @defgroup GPIO_Private_Defines GPIO Private Defines
138 * @{
140 #define GPIO_MODE (0x00000003U)
141 #define EXTI_MODE (0x10000000U)
142 #define GPIO_MODE_IT (0x00010000U)
143 #define GPIO_MODE_EVT (0x00020000U)
144 #define RISING_EDGE (0x00100000U)
145 #define FALLING_EDGE (0x00200000U)
146 #define GPIO_OUTPUT_TYPE (0x00000010U)
148 #define GPIO_NUMBER (16U)
150 * @}
153 /* Private macros ------------------------------------------------------------*/
154 /* Private macros ------------------------------------------------------------*/
155 /** @defgroup GPIO_Private_Macros GPIO Private Macros
156 * @{
159 * @}
161 /* Private variables ---------------------------------------------------------*/
162 /* Private function prototypes -----------------------------------------------*/
163 /* Exported functions --------------------------------------------------------*/
165 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
166 * @{
169 /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
170 * @brief Initialization and Configuration functions
172 @verbatim
173 ===============================================================================
174 ##### Initialization and de-initialization functions #####
175 ===============================================================================
177 @endverbatim
178 * @{
182 * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
183 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family devices
184 * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
185 * the configuration information for the specified GPIO peripheral.
186 * @retval None
188 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
190 uint32_t position = 0x00U;
191 uint32_t iocurrent = 0x00U;
192 uint32_t temp = 0x00U;
194 /* Check the parameters */
195 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
196 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
197 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
198 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
200 /* Configure the port pins */
201 while (((GPIO_Init->Pin) >> position) != RESET)
203 /* Get current io position */
204 iocurrent = (GPIO_Init->Pin) & (1U << position);
206 if(iocurrent)
208 /*--------------------- GPIO Mode Configuration ------------------------*/
209 /* In case of Alternate function mode selection */
210 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
212 /* Check the Alternate function parameters */
213 assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
214 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
216 /* Configure Alternate function mapped with the current IO */
217 temp = GPIOx->AFR[position >> 3];
218 temp &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
219 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4U));
220 GPIOx->AFR[position >> 3] = temp;
223 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
224 temp = GPIOx->MODER;
225 temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
226 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
227 GPIOx->MODER = temp;
229 /* In case of Output or Alternate function mode selection */
230 if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
231 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
233 /* Check the Speed parameter */
234 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
235 /* Configure the IO Speed */
236 temp = GPIOx->OSPEEDR;
237 temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
238 temp |= (GPIO_Init->Speed << (position * 2U));
239 GPIOx->OSPEEDR = temp;
241 /* Configure the IO Output Type */
242 temp = GPIOx->OTYPER;
243 temp &= ~(GPIO_OTYPER_OT_0 << position) ;
244 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
245 GPIOx->OTYPER = temp;
248 /* Activate the Pull-up or Pull down resistor for the current IO */
249 temp = GPIOx->PUPDR;
250 temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
251 temp |= ((GPIO_Init->Pull) << (position * 2U));
252 GPIOx->PUPDR = temp;
254 /*--------------------- EXTI Mode Configuration ------------------------*/
255 /* Configure the External Interrupt or event for the current IO */
256 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
258 /* Enable SYSCFG Clock */
259 __HAL_RCC_SYSCFG_CLK_ENABLE();
261 temp = SYSCFG->EXTICR[position >> 2];
262 temp &= ~((0x0FU) << (4U * (position & 0x03U)));
263 temp |= (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U)));
264 SYSCFG->EXTICR[position >> 2] = temp;
266 /* Clear EXTI line configuration */
267 temp = EXTI->IMR;
268 temp &= ~((uint32_t)iocurrent);
269 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
271 temp |= iocurrent;
273 EXTI->IMR = temp;
275 temp = EXTI->EMR;
276 temp &= ~((uint32_t)iocurrent);
277 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
279 temp |= iocurrent;
281 EXTI->EMR = temp;
283 /* Clear Rising Falling edge configuration */
284 temp = EXTI->RTSR;
285 temp &= ~((uint32_t)iocurrent);
286 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
288 temp |= iocurrent;
290 EXTI->RTSR = temp;
292 temp = EXTI->FTSR;
293 temp &= ~((uint32_t)iocurrent);
294 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
296 temp |= iocurrent;
298 EXTI->FTSR = temp;
302 position++;
307 * @brief De-initialize the GPIOx peripheral registers to their default reset values.
308 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F30X device or STM32F37X device
309 * @param GPIO_Pin specifies the port bit to be written.
310 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
311 * @retval None
313 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
315 uint32_t position = 0x00U;
316 uint32_t iocurrent = 0x00U;
317 uint32_t tmp = 0x00U;
319 /* Check the parameters */
320 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
321 assert_param(IS_GPIO_PIN(GPIO_Pin));
323 /* Configure the port pins */
324 while ((GPIO_Pin >> position) != RESET)
326 /* Get current io position */
327 iocurrent = GPIO_Pin & (1U << position);
329 if (iocurrent)
331 /*------------------------- GPIO Mode Configuration --------------------*/
332 /* Configure IO Direction in Input Floting Mode */
333 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2U));
335 /* Configure the default Alternate Function in current IO */
336 GPIOx->AFR[position >> 3] &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
338 /* Configure the default value for IO Speed */
339 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
341 /* Configure the default value IO Output Type */
342 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
344 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
345 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
348 /*------------------------- EXTI Mode Configuration --------------------*/
349 /* Clear the External Interrupt or Event for the current IO */
351 tmp = SYSCFG->EXTICR[position >> 2];
352 tmp &= ((0x0FU) << (4U * (position & 0x03U)));
353 if(tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
355 tmp = (0x0FU) << (4U * (position & 0x03U));
356 SYSCFG->EXTICR[position >> 2] &= ~tmp;
358 /* Clear EXTI line configuration */
359 EXTI->IMR &= ~((uint32_t)iocurrent);
360 EXTI->EMR &= ~((uint32_t)iocurrent);
362 /* Clear Rising Falling edge configuration */
363 EXTI->RTSR &= ~((uint32_t)iocurrent);
364 EXTI->FTSR &= ~((uint32_t)iocurrent);
368 position++;
373 * @}
376 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
377 * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
379 @verbatim
380 ===============================================================================
381 ##### IO operation functions #####
382 ===============================================================================
384 @endverbatim
385 * @{
389 * @brief Read the specified input port pin.
390 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
391 * @param GPIO_Pin specifies the port bit to read.
392 * This parameter can be GPIO_PIN_x where x can be (0..15).
393 * @retval The input port pin value.
395 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
397 GPIO_PinState bitstatus;
399 /* Check the parameters */
400 assert_param(IS_GPIO_PIN(GPIO_Pin));
402 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
404 bitstatus = GPIO_PIN_SET;
406 else
408 bitstatus = GPIO_PIN_RESET;
410 return bitstatus;
414 * @brief Set or clear the selected data port bit.
416 * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
417 * accesses. In this way, there is no risk of an IRQ occurring between
418 * the read and the modify access.
420 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
421 * @param GPIO_Pin specifies the port bit to be written.
422 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
423 * @param PinState specifies the value to be written to the selected bit.
424 * This parameter can be one of the GPIO_PinState enum values:
425 * @arg GPIO_PIN_RESET: to clear the port pin
426 * @arg GPIO_PIN_SET: to set the port pin
427 * @retval None
429 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
431 /* Check the parameters */
432 assert_param(IS_GPIO_PIN(GPIO_Pin));
433 assert_param(IS_GPIO_PIN_ACTION(PinState));
435 if(PinState != GPIO_PIN_RESET)
437 GPIOx->BSRR = (uint32_t)GPIO_Pin;
439 else
441 GPIOx->BRR = (uint32_t)GPIO_Pin;
446 * @brief Toggle the specified GPIO pin.
447 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
448 * @param GPIO_Pin specifies the pin to be toggled.
449 * @retval None
451 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
453 /* Check the parameters */
454 assert_param(IS_GPIO_PIN(GPIO_Pin));
456 GPIOx->ODR ^= GPIO_Pin;
460 * @brief Lock GPIO Pins configuration registers.
461 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
462 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
463 * @note The configuration of the locked GPIO pins can no longer be modified
464 * until the next reset.
465 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
466 * @param GPIO_Pin specifies the port bits to be locked.
467 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
468 * @retval None
470 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
472 __IO uint32_t tmp = GPIO_LCKR_LCKK;
474 /* Check the parameters */
475 assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
476 assert_param(IS_GPIO_PIN(GPIO_Pin));
478 /* Apply lock key write sequence */
479 tmp |= GPIO_Pin;
480 /* Set LCKx bit(s): LCKK='1' + LCK[15U-0] */
481 GPIOx->LCKR = tmp;
482 /* Reset LCKx bit(s): LCKK='0' + LCK[15U-0] */
483 GPIOx->LCKR = GPIO_Pin;
484 /* Set LCKx bit(s): LCKK='1' + LCK[15U-0] */
485 GPIOx->LCKR = tmp;
486 /* Read LCKK bit*/
487 tmp = GPIOx->LCKR;
489 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
491 return HAL_OK;
493 else
495 return HAL_ERROR;
500 * @brief Handle EXTI interrupt request.
501 * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
502 * @retval None
504 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
506 /* EXTI line interrupt detected */
507 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
509 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
510 HAL_GPIO_EXTI_Callback(GPIO_Pin);
515 * @brief EXTI line detection callback.
516 * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
517 * @retval None
519 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
521 /* Prevent unused argument(s) compilation warning */
522 UNUSED(GPIO_Pin);
524 /* NOTE: This function should not be modified, when the callback is needed,
525 the HAL_GPIO_EXTI_Callback could be implemented in the user file
530 * @}
535 * @}
538 #endif /* HAL_GPIO_MODULE_ENABLED */
540 * @}
544 * @}
547 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/