Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_gpio.c
blob25a19dea3659868895d35c8636e0b985ac4114bc
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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().
79 (#) During and just after reset, the alternate functions are not
80 active and the GPIO pins are configured in input floating mode (except JTAG
81 pins).
83 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
84 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
85 priority over the GPIO function.
87 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
88 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
89 The HSE has priority over the GPIO function.
91 @endverbatim
92 ******************************************************************************
93 * @attention
95 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics.
96 * All rights reserved.</center></h2>
98 * This software component is licensed by ST under BSD 3-Clause license,
99 * the "License"; You may not use this file except in compliance with the
100 * License. You may obtain a copy of the License at:
101 * opensource.org/licenses/BSD-3-Clause
103 ******************************************************************************
106 /* Includes ------------------------------------------------------------------*/
107 #include "stm32h7xx_hal.h"
109 /** @addtogroup STM32H7xx_HAL_Driver
110 * @{
113 /** @defgroup GPIO GPIO
114 * @brief GPIO HAL module driver
115 * @{
118 #ifdef HAL_GPIO_MODULE_ENABLED
120 /* Private typedef -----------------------------------------------------------*/
121 /* Private defines ------------------------------------------------------------*/
122 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
123 * @{
125 #define GPIO_MODE (0x00000003U)
126 #define ANALOG_MODE (0x00000008U)
127 #define EXTI_MODE (0x10000000U)
128 #define GPIO_MODE_IT (0x00010000U)
129 #define GPIO_MODE_EVT (0x00020000U)
130 #define RISING_EDGE (0x00100000U)
131 #define FALLING_EDGE (0x00200000U)
132 #define GPIO_OUTPUT_TYPE (0x00000010U)
134 #if defined(DUAL_CORE)
135 #define EXTI_CPU1 (0x01000000U)
136 #define EXTI_CPU2 (0x02000000U)
137 #endif /*DUAL_CORE*/
138 #define GPIO_NUMBER (16U)
140 * @}
142 /* Private macro -------------------------------------------------------------*/
143 /* Private variables ---------------------------------------------------------*/
144 /* Private function prototypes -----------------------------------------------*/
145 /* Private functions ---------------------------------------------------------*/
146 /* Exported functions --------------------------------------------------------*/
147 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
148 * @{
151 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
152 * @brief Initialization and Configuration functions
154 @verbatim
155 ===============================================================================
156 ##### Initialization and de-initialization functions #####
157 ===============================================================================
158 [..]
159 This section provides functions allowing to initialize and de-initialize the GPIOs
160 to be ready for use.
162 @endverbatim
163 * @{
167 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
168 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
169 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
170 * the configuration information for the specified GPIO peripheral.
171 * @retval None
173 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
175 uint32_t position = 0x00U;
176 uint32_t iocurrent;
177 uint32_t temp;
178 EXTI_Core_TypeDef *EXTI_CurrentCPU;
180 #if defined(DUAL_CORE) && defined(CORE_CM4)
181 EXTI_CurrentCPU = EXTI_D2; /* EXTI for CM4 CPU */
182 #else
183 EXTI_CurrentCPU = EXTI_D1; /* EXTI for CM7 CPU */
184 #endif
186 /* Check the parameters */
187 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
188 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
189 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
190 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
192 /* Configure the port pins */
193 while (((GPIO_Init->Pin) >> position) != 0x00U)
195 /* Get current io position */
196 iocurrent = (GPIO_Init->Pin) & (1UL << position);
198 if (iocurrent != 0x00U)
200 /*--------------------- GPIO Mode Configuration ------------------------*/
201 /* In case of Output or Alternate function mode selection */
202 if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
203 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
205 /* Check the Speed parameter */
206 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
207 /* Configure the IO Speed */
208 temp = GPIOx->OSPEEDR;
209 temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
210 temp |= (GPIO_Init->Speed << (position * 2U));
211 GPIOx->OSPEEDR = temp;
213 /* Configure the IO Output Type */
214 temp = GPIOx->OTYPER;
215 temp &= ~(GPIO_OTYPER_OT0 << position) ;
216 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
217 GPIOx->OTYPER = temp;
220 /* Activate the Pull-up or Pull down resistor for the current IO */
221 temp = GPIOx->PUPDR;
222 temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
223 temp |= ((GPIO_Init->Pull) << (position * 2U));
224 GPIOx->PUPDR = temp;
226 /* In case of Alternate function mode selection */
227 if ((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
229 /* Check the Alternate function parameters */
230 assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
231 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
233 /* Configure Alternate function mapped with the current IO */
234 temp = GPIOx->AFR[position >> 3U];
235 temp &= ~(0xFU << ((position & 0x07U) * 4U));
236 temp |= ((GPIO_Init->Alternate) << ((position & 0x07U) * 4U));
237 GPIOx->AFR[position >> 3U] = temp;
240 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
241 temp = GPIOx->MODER;
242 temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
243 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
244 GPIOx->MODER = temp;
246 /*--------------------- EXTI Mode Configuration ------------------------*/
247 /* Configure the External Interrupt or event for the current IO */
248 if ((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
250 /* Enable SYSCFG Clock */
251 __HAL_RCC_SYSCFG_CLK_ENABLE();
253 temp = SYSCFG->EXTICR[position >> 2U];
254 temp &= ~(0x0FUL << (4U * (position & 0x03U)));
255 temp |= (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U)));
256 SYSCFG->EXTICR[position >> 2U] = temp;
258 /* Clear EXTI line configuration */
259 temp = EXTI_CurrentCPU->IMR1;
260 temp &= ~(iocurrent);
261 if ((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
263 temp |= iocurrent;
265 EXTI_CurrentCPU->IMR1 = temp;
267 temp = EXTI_CurrentCPU->EMR1;
268 temp &= ~(iocurrent);
269 if ((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
271 temp |= iocurrent;
273 EXTI_CurrentCPU->EMR1 = temp;
275 /* Clear Rising Falling edge configuration */
276 temp = EXTI->RTSR1;
277 temp &= ~(iocurrent);
278 if ((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
280 temp |= iocurrent;
282 EXTI->RTSR1 = temp;
284 temp = EXTI->FTSR1;
285 temp &= ~(iocurrent);
286 if ((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
288 temp |= iocurrent;
290 EXTI->FTSR1 = temp;
294 position++;
299 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
300 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
301 * @param GPIO_Pin: specifies the port bit to be written.
302 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
303 * @retval None
305 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
307 uint32_t position = 0x00U;
308 uint32_t iocurrent;
309 uint32_t tmp;
310 EXTI_Core_TypeDef *EXTI_CurrentCPU;
312 #if defined(DUAL_CORE) && defined(CORE_CM4)
313 EXTI_CurrentCPU = EXTI_D2; /* EXTI for CM4 CPU */
314 #else
315 EXTI_CurrentCPU = EXTI_D1; /* EXTI for CM7 CPU */
316 #endif
318 /* Check the parameters */
319 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
320 assert_param(IS_GPIO_PIN(GPIO_Pin));
322 /* Configure the port pins */
323 while ((GPIO_Pin >> position) != 0x00U)
325 /* Get current io position */
326 iocurrent = GPIO_Pin & (1UL << position) ;
328 if (iocurrent != 0x00U)
330 /*------------------------- EXTI Mode Configuration --------------------*/
331 /* Clear the External Interrupt or Event for the current IO */
332 tmp = SYSCFG->EXTICR[position >> 2U];
333 tmp &= (0x0FUL << (4U * (position & 0x03U)));
334 if (tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
336 /* Clear EXTI line configuration for Current CPU */
337 EXTI_CurrentCPU->IMR1 &= ~(iocurrent);
338 EXTI_CurrentCPU->EMR1 &= ~(iocurrent);
340 /* Clear Rising Falling edge configuration */
341 EXTI->RTSR1 &= ~(iocurrent);
342 EXTI->FTSR1 &= ~(iocurrent);
344 tmp = 0x0FUL << (4U * (position & 0x03U));
345 SYSCFG->EXTICR[position >> 2U] &= ~tmp;
348 /*------------------------- GPIO Mode Configuration --------------------*/
349 /* Configure IO in Analog Mode */
350 GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2U));
352 /* Configure the default Alternate Function in current IO */
353 GPIOx->AFR[position >> 3U] &= ~(0xFU << ((position & 0x07U) * 4U)) ;
355 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
356 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
358 /* Configure the default value IO Output Type */
359 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
361 /* Configure the default value for IO Speed */
362 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
365 position++;
370 * @}
373 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
374 * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
376 @verbatim
377 ===============================================================================
378 ##### IO operation functions #####
379 ===============================================================================
381 @endverbatim
382 * @{
386 * @brief Reads the specified input port pin.
387 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
388 * @param GPIO_Pin: specifies the port bit to read.
389 * This parameter can be GPIO_PIN_x where x can be (0..15).
390 * @retval The input port pin value.
392 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
394 GPIO_PinState bitstatus;
396 /* Check the parameters */
397 assert_param(IS_GPIO_PIN(GPIO_Pin));
399 if ((GPIOx->IDR & GPIO_Pin) != 0x00U)
401 bitstatus = GPIO_PIN_SET;
403 else
405 bitstatus = GPIO_PIN_RESET;
407 return bitstatus;
411 * @brief Sets or clears the selected data port bit.
413 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
414 * accesses. In this way, there is no risk of an IRQ occurring between
415 * the read and the modify access.
417 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
418 * @param GPIO_Pin: specifies the port bit to be written.
419 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
420 * @param PinState: specifies the value to be written to the selected bit.
421 * This parameter can be one of the GPIO_PinState enum values:
422 * @arg GPIO_PIN_RESET: to clear the port pin
423 * @arg GPIO_PIN_SET: to set the port pin
424 * @retval None
426 void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
428 /* Check the parameters */
429 assert_param(IS_GPIO_PIN(GPIO_Pin));
430 assert_param(IS_GPIO_PIN_ACTION(PinState));
432 if (PinState != GPIO_PIN_RESET)
434 GPIOx->BSRR = GPIO_Pin;
436 else
438 GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
443 * @brief Toggles the specified GPIO pins.
444 * @param GPIOx: Where x can be (A..K) to select the GPIO peripheral.
445 * @param GPIO_Pin: Specifies the pins to be toggled.
446 * @retval None
448 void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
450 /* Check the parameters */
451 assert_param(IS_GPIO_PIN(GPIO_Pin));
453 if ((GPIOx->ODR & GPIO_Pin) == GPIO_Pin)
455 GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
457 else
459 GPIOx->BSRR = GPIO_Pin;
464 * @brief Locks GPIO Pins configuration registers.
465 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
466 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
467 * @note The configuration of the locked GPIO pins can no longer be modified
468 * until the next reset.
469 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32H7 family
470 * @param GPIO_Pin: specifies the port bit to be locked.
471 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
472 * @retval None
474 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
476 __IO uint32_t tmp = GPIO_LCKR_LCKK;
478 /* Check the parameters */
479 assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
480 assert_param(IS_GPIO_PIN(GPIO_Pin));
482 /* Apply lock key write sequence */
483 tmp |= GPIO_Pin;
484 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
485 GPIOx->LCKR = tmp;
486 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
487 GPIOx->LCKR = GPIO_Pin;
488 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
489 GPIOx->LCKR = tmp;
490 /* Read LCKK register. This read is mandatory to complete key lock sequence*/
491 tmp = GPIOx->LCKR;
493 /* read again in order to confirm lock is active */
494 if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00U)
496 return HAL_OK;
498 else
500 return HAL_ERROR;
505 * @brief Handle EXTI interrupt request.
506 * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
507 * @retval None
509 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
511 #if defined(DUAL_CORE) && defined(CORE_CM4)
512 if (__HAL_GPIO_EXTID2_GET_IT(GPIO_Pin) != 0x00U)
514 __HAL_GPIO_EXTID2_CLEAR_IT(GPIO_Pin);
515 HAL_GPIO_EXTI_Callback(GPIO_Pin);
517 #else
518 /* EXTI line interrupt detected */
519 if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00U)
521 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
522 HAL_GPIO_EXTI_Callback(GPIO_Pin);
524 #endif
528 * @brief EXTI line detection callback.
529 * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
530 * @retval None
532 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
534 /* Prevent unused argument(s) compilation warning */
535 UNUSED(GPIO_Pin);
537 /* NOTE: This function Should not be modified, when the callback is needed,
538 the HAL_GPIO_EXTI_Callback could be implemented in the user file
543 * @}
548 * @}
551 #endif /* HAL_GPIO_MODULE_ENABLED */
553 * @}
557 * @}
560 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/