Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_hal_gpio.c
blob0317f70e68af7fa53237c5cbe59d2da83805c2d0
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_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 44 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 PC15, 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) 2017 STMicroelectronics.
95 * All rights reserved.</center></h2>
97 * This software component is licensed by ST under BSD 3-Clause license,
98 * the "License"; You may not use this file except in compliance with the
99 * License. You may obtain a copy of the License at:
100 * opensource.org/licenses/BSD-3-Clause
102 ******************************************************************************
105 /* Includes ------------------------------------------------------------------*/
106 #include "stm32g4xx_hal.h"
108 /** @addtogroup STM32G4xx_HAL_Driver
109 * @{
112 /** @addtogroup GPIO
113 * @{
115 /** MISRA C:2012 deviation rule has been granted for following rules:
116 * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
117 * range of the shift operator in following API :
118 * HAL_GPIO_Init
119 * HAL_GPIO_DeInit
122 #ifdef HAL_GPIO_MODULE_ENABLED
124 /* Private typedef -----------------------------------------------------------*/
125 /* Private defines ------------------------------------------------------------*/
126 /** @defgroup GPIO_Private_Constants GPIO Private Constants
127 * @{
129 #define GPIO_MODE (0x00000003U)
130 #define EXTI_MODE (0x10000000U)
131 #define GPIO_MODE_IT (0x00010000U)
132 #define GPIO_MODE_EVT (0x00020000U)
133 #define RISING_EDGE (0x00100000U)
134 #define FALLING_EDGE (0x00200000U)
135 #define GPIO_OUTPUT_TYPE (0x00000010U)
137 #define GPIO_NUMBER (16U)
139 * @}
142 /* Private macros ------------------------------------------------------------*/
143 /* Private variables ---------------------------------------------------------*/
144 /* Private function prototypes -----------------------------------------------*/
145 /* Exported functions --------------------------------------------------------*/
147 /** @addtogroup GPIO_Exported_Functions
148 * @{
151 /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
152 * @brief Initialization and Configuration functions
154 @verbatim
155 ===============================================================================
156 ##### Initialization and de-initialization functions #####
157 ===============================================================================
159 @endverbatim
160 * @{
164 * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
165 * @param GPIOx where x can be (A..G) to select the GPIO peripheral for STM32G4xx family
166 * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
167 * the configuration information for the specified GPIO peripheral.
168 * @retval None
170 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
172 uint32_t position = 0x00U;
173 uint32_t iocurrent;
174 uint32_t temp;
176 /* Check the parameters */
177 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
178 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
179 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
180 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
182 /* Configure the port pins */
183 while (((GPIO_Init->Pin) >> position) != 0U)
185 /* Get current io position */
186 iocurrent = (GPIO_Init->Pin) & (1UL << position);
188 if (iocurrent != 0x00u)
190 /*--------------------- GPIO Mode Configuration ------------------------*/
191 /* In case of Alternate function mode selection */
192 if ((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
194 /* Check the Alternate function parameters */
195 assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
196 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
198 /* Configure Alternate function mapped with the current IO */
199 temp = GPIOx->AFR[position >> 3U];
200 temp &= ~(0xFU << ((position & 0x07U) * 4U));
201 temp |= ((GPIO_Init->Alternate) << ((position & 0x07U) * 4U));
202 GPIOx->AFR[position >> 3U] = temp;
205 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
206 temp = GPIOx->MODER;
207 temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
208 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
209 GPIOx->MODER = temp;
211 /* In case of Output or Alternate function mode selection */
212 if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
213 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
215 /* Check the Speed parameter */
216 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
217 /* Configure the IO Speed */
218 temp = GPIOx->OSPEEDR;
219 temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
220 temp |= (GPIO_Init->Speed << (position * 2U));
221 GPIOx->OSPEEDR = temp;
223 /* Configure the IO Output Type */
224 temp = GPIOx->OTYPER;
225 temp &= ~(GPIO_OTYPER_OT0 << position) ;
226 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
227 GPIOx->OTYPER = temp;
230 /* Activate the Pull-up or Pull down resistor for the current IO */
231 temp = GPIOx->PUPDR;
232 temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
233 temp |= ((GPIO_Init->Pull) << (position * 2U));
234 GPIOx->PUPDR = temp;
236 /*--------------------- EXTI Mode Configuration ------------------------*/
237 /* Configure the External Interrupt or event for the current IO */
238 if ((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
240 /* Enable SYSCFG Clock */
241 __HAL_RCC_SYSCFG_CLK_ENABLE();
243 temp = SYSCFG->EXTICR[position >> 2U];
244 temp &= ~(0x0FUL << (4U * (position & 0x03U)));
245 temp |= (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U)));
246 SYSCFG->EXTICR[position >> 2U] = temp;
248 /* Clear EXTI line configuration */
249 temp = EXTI->IMR1;
250 temp &= ~(iocurrent);
251 if ((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
253 temp |= iocurrent;
255 EXTI->IMR1 = temp;
257 temp = EXTI->EMR1;
258 temp &= ~(iocurrent);
259 if ((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
261 temp |= iocurrent;
263 EXTI->EMR1 = temp;
265 /* Clear Rising Falling edge configuration */
266 temp = EXTI->RTSR1;
267 temp &= ~(iocurrent);
268 if ((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
270 temp |= iocurrent;
272 EXTI->RTSR1 = temp;
274 temp = EXTI->FTSR1;
275 temp &= ~(iocurrent);
276 if ((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
278 temp |= iocurrent;
280 EXTI->FTSR1 = temp;
284 position++;
289 * @brief De-initialize the GPIOx peripheral registers to their default reset values.
290 * @param GPIOx where x can be (A..G) to select the GPIO peripheral for STM32G4xx family
291 * @param GPIO_Pin specifies the port bit to be written.
292 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
293 * @retval None
295 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
297 uint32_t position = 0x00U;
298 uint32_t iocurrent;
299 uint32_t tmp;
301 /* Check the parameters */
302 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
303 assert_param(IS_GPIO_PIN(GPIO_Pin));
305 /* Configure the port pins */
306 while ((GPIO_Pin >> position) != 0U)
308 /* Get current io position */
309 iocurrent = (GPIO_Pin) & (1UL << position);
311 if (iocurrent != 0x00u)
313 /*------------------------- EXTI Mode Configuration --------------------*/
314 /* Clear the External Interrupt or Event for the current IO */
316 tmp = SYSCFG->EXTICR[position >> 2U];
317 tmp &= (0x0FUL << (4U * (position & 0x03U)));
318 if (tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
320 tmp = 0x0FUL << (4U * (position & 0x03U));
321 SYSCFG->EXTICR[position >> 2U] &= ~tmp;
323 /* Clear EXTI line configuration */
324 EXTI->IMR1 &= ~(iocurrent);
325 EXTI->EMR1 &= ~(iocurrent);
327 /* Clear Rising Falling edge configuration */
328 EXTI->RTSR1 &= ~(iocurrent);
329 EXTI->FTSR1 &= ~(iocurrent);
332 /*------------------------- GPIO Mode Configuration --------------------*/
333 /* Configure IO in Analog Mode */
334 GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
336 /* Configure the default Alternate Function in current IO */
337 GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
339 /* Configure the default value for IO Speed */
340 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
342 /* Configure the default value IO Output Type */
343 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
345 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
346 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
349 position++;
354 * @}
357 /** @addtogroup GPIO_Exported_Functions_Group2
358 * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
360 @verbatim
361 ===============================================================================
362 ##### IO operation functions #####
363 ===============================================================================
365 @endverbatim
366 * @{
370 * @brief Read the specified input port pin.
371 * @param GPIOx where x can be (A..G) to select the GPIO peripheral for STM32G4xx family
372 * @param GPIO_Pin specifies the port bit to read.
373 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
374 * @retval The input port pin value.
376 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
378 GPIO_PinState bitstatus;
380 /* Check the parameters */
381 assert_param(IS_GPIO_PIN(GPIO_Pin));
383 if ((GPIOx->IDR & GPIO_Pin) != 0x00U)
385 bitstatus = GPIO_PIN_SET;
387 else
389 bitstatus = GPIO_PIN_RESET;
391 return bitstatus;
395 * @brief Set or clear the selected data port bit.
397 * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
398 * accesses. In this way, there is no risk of an IRQ occurring between
399 * the read and the modify access.
401 * @param GPIOx where x can be (A..G) to select the GPIO peripheral for STM32G4xx family
402 * @param GPIO_Pin specifies the port bit to be written.
403 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
404 * @param PinState specifies the value to be written to the selected bit.
405 * This parameter can be one of the GPIO_PinState enum values:
406 * @arg GPIO_PIN_RESET: to clear the port pin
407 * @arg GPIO_PIN_SET: to set the port pin
408 * @retval None
410 void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
412 /* Check the parameters */
413 assert_param(IS_GPIO_PIN(GPIO_Pin));
414 assert_param(IS_GPIO_PIN_ACTION(PinState));
416 if (PinState != GPIO_PIN_RESET)
418 GPIOx->BSRR = (uint32_t)GPIO_Pin;
420 else
422 GPIOx->BRR = (uint32_t)GPIO_Pin;
427 * @brief Toggle the specified GPIO pin.
428 * @param GPIOx where x can be (A..G) to select the GPIO peripheral for STM32G4xx family
429 * @param GPIO_Pin specifies the pin to be toggled.
430 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
431 * @retval None
433 void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
435 /* Check the parameters */
436 assert_param(IS_GPIO_PIN(GPIO_Pin));
438 if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
440 GPIOx->BRR = (uint32_t)GPIO_Pin;
442 else
444 GPIOx->BSRR = (uint32_t)GPIO_Pin;
449 * @brief Lock GPIO Pins configuration registers.
450 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
451 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
452 * @note The configuration of the locked GPIO pins can no longer be modified
453 * until the next reset.
454 * @param GPIOx where x can be (A..G) to select the GPIO peripheral for STM32G4xx family
455 * @param GPIO_Pin specifies the port bits to be locked.
456 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
457 * @retval None
459 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
461 __IO uint32_t tmp = GPIO_LCKR_LCKK;
463 /* Check the parameters */
464 assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
465 assert_param(IS_GPIO_PIN(GPIO_Pin));
467 /* Apply lock key write sequence */
468 tmp |= GPIO_Pin;
469 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
470 GPIOx->LCKR = tmp;
471 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
472 GPIOx->LCKR = GPIO_Pin;
473 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
474 GPIOx->LCKR = tmp;
475 /* Read LCKK register. This read is mandatory to complete key lock sequence */
476 tmp = GPIOx->LCKR;
478 /* read again in order to confirm lock is active */
479 if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
481 return HAL_OK;
483 else
485 return HAL_ERROR;
490 * @brief Handle EXTI interrupt request.
491 * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
492 * @retval None
494 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
496 /* EXTI line interrupt detected */
497 if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
499 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
500 HAL_GPIO_EXTI_Callback(GPIO_Pin);
505 * @brief EXTI line detection callback.
506 * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
507 * @retval None
509 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
511 /* Prevent unused argument(s) compilation warning */
512 UNUSED(GPIO_Pin);
514 /* NOTE: This function should not be modified, when the callback is needed,
515 the HAL_GPIO_EXTI_Callback could be implemented in the user file
520 * @}
525 * @}
528 #endif /* HAL_GPIO_MODULE_ENABLED */
530 * @}
534 * @}
537 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/