2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_gpio.c
\r
4 * @author MCD Application Team
\r
5 * @brief GPIO HAL module driver.
\r
6 * This file provides firmware functions to manage the following
\r
7 * functionalities of the General Purpose Input/Output (GPIO) peripheral:
\r
8 * + Initialization and de-initialization functions
\r
9 * + IO operation functions
\r
12 ==============================================================================
\r
13 ##### GPIO Peripheral features #####
\r
14 ==============================================================================
\r
16 (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
\r
17 configured by software in several modes:
\r
21 (++) Alternate function mode
\r
22 (++) External interrupt/event lines
\r
24 (+) During and just after reset, the alternate functions and external interrupt
\r
25 lines are not active and the I/O ports are configured in input floating mode.
\r
27 (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
\r
30 (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
\r
31 type and the IO speed can be selected depending on the VDD value.
\r
33 (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
\r
34 multiplexer that allows only one peripheral alternate function (AF) connected
\r
35 to an IO pin at a time. In this way, there can be no conflict between peripherals
\r
36 sharing the same IO pin.
\r
38 (+) All ports have external interrupt/event capability. To use external interrupt
\r
39 lines, the port must be configured in input mode. All available GPIO pins are
\r
40 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
\r
42 (+) The external interrupt/event controller consists of up to 39 edge detectors
\r
43 (16 lines are connected to GPIO) for generating event/interrupt requests (each
\r
44 input line can be independently configured to select the type (interrupt or event)
\r
45 and the corresponding trigger event (rising or falling or both). Each line can
\r
46 also be masked independently.
\r
48 ##### How to use this driver #####
\r
49 ==============================================================================
\r
51 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
\r
53 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
\r
54 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
\r
55 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
\r
57 (++) In case of Output or alternate function mode selection: the speed is
\r
58 configured through "Speed" member from GPIO_InitTypeDef structure.
\r
59 (++) In alternate mode is selection, the alternate function connected to the IO
\r
60 is configured through "Alternate" member from GPIO_InitTypeDef structure.
\r
61 (++) Analog mode is required when a pin is to be used as ADC channel
\r
63 (++) In case of external interrupt/event selection the "Mode" member from
\r
64 GPIO_InitTypeDef structure select the type (interrupt or event) and
\r
65 the corresponding trigger event (rising or falling or both).
\r
67 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
\r
68 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
\r
69 HAL_NVIC_EnableIRQ().
\r
71 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
\r
73 (#) To set/reset the level of a pin configured in output mode use
\r
74 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
\r
76 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
\r
78 (#) During and just after reset, the alternate functions are not
\r
79 active and the GPIO pins are configured in input floating mode (except JTAG
\r
82 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
\r
83 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
\r
84 priority over the GPIO function.
\r
86 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
\r
87 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
\r
88 The HSE has priority over the GPIO function.
\r
91 ******************************************************************************
\r
94 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
95 * All rights reserved.</center></h2>
\r
97 * This software component is licensed by ST under BSD 3-Clause license,
\r
98 * the "License"; You may not use this file except in compliance with the
\r
99 * License. You may obtain a copy of the License at:
\r
100 * opensource.org/licenses/BSD-3-Clause
\r
102 ******************************************************************************
\r
105 /* Includes ------------------------------------------------------------------*/
\r
106 #include "stm32l4xx_hal.h"
\r
108 /** @addtogroup STM32L4xx_HAL_Driver
\r
112 /** @defgroup GPIO GPIO
\r
113 * @brief GPIO HAL module driver
\r
116 /** MISRA C:2012 deviation rule has been granted for following rules:
\r
117 * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
\r
118 * range of the shift operator in following API :
\r
123 #ifdef HAL_GPIO_MODULE_ENABLED
\r
125 /* Private typedef -----------------------------------------------------------*/
\r
126 /* Private defines -----------------------------------------------------------*/
\r
127 /** @defgroup GPIO_Private_Defines GPIO Private Defines
\r
130 #define GPIO_MODE (0x00000003u)
\r
131 #define ANALOG_MODE (0x00000008u)
\r
132 #define EXTI_MODE (0x10000000u)
\r
133 #define GPIO_MODE_IT (0x00010000u)
\r
134 #define GPIO_MODE_EVT (0x00020000u)
\r
135 #define RISING_EDGE (0x00100000u)
\r
136 #define FALLING_EDGE (0x00200000u)
\r
137 #define GPIO_OUTPUT_TYPE (0x00000010u)
\r
139 #define GPIO_NUMBER (16u)
\r
144 /* Private macros ------------------------------------------------------------*/
\r
145 /* Private variables ---------------------------------------------------------*/
\r
146 /* Private function prototypes -----------------------------------------------*/
\r
147 /* Exported functions --------------------------------------------------------*/
\r
149 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
\r
153 /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
\r
154 * @brief Initialization and Configuration functions
\r
157 ===============================================================================
\r
158 ##### Initialization and de-initialization functions #####
\r
159 ===============================================================================
\r
166 * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
\r
167 * @param GPIOx: where x can be (A..H) to select the GPIO peripheral for STM32L4 family
\r
168 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
\r
169 * the configuration information for the specified GPIO peripheral.
\r
172 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
\r
174 uint32_t position = 0x00u;
\r
175 uint32_t iocurrent;
\r
178 /* Check the parameters */
\r
179 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
\r
180 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
\r
181 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
\r
182 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
\r
184 /* Configure the port pins */
\r
185 while (((GPIO_Init->Pin) >> position) != 0x00u)
\r
187 /* Get current io position */
\r
188 iocurrent = (GPIO_Init->Pin) & (1uL << position);
\r
190 if (iocurrent != 0x00u)
\r
192 /*--------------------- GPIO Mode Configuration ------------------------*/
\r
193 /* In case of Alternate function mode selection */
\r
194 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
\r
196 /* Check the Alternate function parameters */
\r
197 assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
\r
198 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
\r
200 /* Configure Alternate function mapped with the current IO */
\r
201 temp = GPIOx->AFR[position >> 3u];
\r
202 temp &= ~(0xFu << ((position & 0x07u) * 4u));
\r
203 temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * 4u));
\r
204 GPIOx->AFR[position >> 3u] = temp;
\r
207 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
\r
208 temp = GPIOx->MODER;
\r
209 temp &= ~(GPIO_MODER_MODE0 << (position * 2u));
\r
210 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u));
\r
211 GPIOx->MODER = temp;
\r
213 /* In case of Output or Alternate function mode selection */
\r
214 if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
\r
215 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
\r
217 /* Check the Speed parameter */
\r
218 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
\r
219 /* Configure the IO Speed */
\r
220 temp = GPIOx->OSPEEDR;
\r
221 temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
\r
222 temp |= (GPIO_Init->Speed << (position * 2u));
\r
223 GPIOx->OSPEEDR = temp;
\r
225 /* Configure the IO Output Type */
\r
226 temp = GPIOx->OTYPER;
\r
227 temp &= ~(GPIO_OTYPER_OT0 << position) ;
\r
228 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4u) << position);
\r
229 GPIOx->OTYPER = temp;
\r
232 #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
\r
234 /* In case of Analog mode, check if ADC control mode is selected */
\r
235 if((GPIO_Init->Mode & GPIO_MODE_ANALOG) == GPIO_MODE_ANALOG)
\r
237 /* Configure the IO Output Type */
\r
238 temp = GPIOx->ASCR;
\r
239 temp &= ~(GPIO_ASCR_ASC0 << position) ;
\r
240 temp |= (((GPIO_Init->Mode & ANALOG_MODE) >> 3) << position);
\r
241 GPIOx->ASCR = temp;
\r
244 #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
\r
246 /* Activate the Pull-up or Pull down resistor for the current IO */
\r
247 temp = GPIOx->PUPDR;
\r
248 temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
\r
249 temp |= ((GPIO_Init->Pull) << (position * 2u));
\r
250 GPIOx->PUPDR = temp;
\r
252 /*--------------------- EXTI Mode Configuration ------------------------*/
\r
253 /* Configure the External Interrupt or event for the current IO */
\r
254 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
\r
256 /* Enable SYSCFG Clock */
\r
257 __HAL_RCC_SYSCFG_CLK_ENABLE();
\r
259 temp = SYSCFG->EXTICR[position >> 2u];
\r
260 temp &= ~(0x0FuL << (4u * (position & 0x03u)));
\r
261 temp |= (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u)));
\r
262 SYSCFG->EXTICR[position >> 2u] = temp;
\r
264 /* Clear EXTI line configuration */
\r
266 temp &= ~(iocurrent);
\r
267 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
\r
274 temp &= ~(iocurrent);
\r
275 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
\r
281 /* Clear Rising Falling edge configuration */
\r
282 temp = EXTI->RTSR1;
\r
283 temp &= ~(iocurrent);
\r
284 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
\r
288 EXTI->RTSR1 = temp;
\r
290 temp = EXTI->FTSR1;
\r
291 temp &= ~(iocurrent);
\r
292 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
\r
296 EXTI->FTSR1 = temp;
\r
305 * @brief De-initialize the GPIOx peripheral registers to their default reset values.
\r
306 * @param GPIOx: where x can be (A..H) to select the GPIO peripheral for STM32L4 family
\r
307 * @param GPIO_Pin: specifies the port bit to be written.
\r
308 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
311 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
\r
313 uint32_t position = 0x00u;
\r
314 uint32_t iocurrent;
\r
317 /* Check the parameters */
\r
318 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
\r
319 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
321 /* Configure the port pins */
\r
322 while ((GPIO_Pin >> position) != 0x00u)
\r
324 /* Get current io position */
\r
325 iocurrent = (GPIO_Pin) & (1uL << position);
\r
327 if (iocurrent != 0x00u)
\r
329 /*------------------------- EXTI Mode Configuration --------------------*/
\r
330 /* Clear the External Interrupt or Event for the current IO */
\r
332 tmp = SYSCFG->EXTICR[position >> 2u];
\r
333 tmp &= (0x0FuL << (4u * (position & 0x03u)));
\r
334 if (tmp == (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u))))
\r
336 /* Clear EXTI line configuration */
\r
337 EXTI->IMR1 &= ~(iocurrent);
\r
338 EXTI->EMR1 &= ~(iocurrent);
\r
340 /* Clear Rising Falling edge configuration */
\r
341 EXTI->RTSR1 &= ~(iocurrent);
\r
342 EXTI->FTSR1 &= ~(iocurrent);
\r
344 tmp = 0x0FuL << (4u * (position & 0x03u));
\r
345 SYSCFG->EXTICR[position >> 2u] &= ~tmp;
\r
348 /*------------------------- GPIO Mode Configuration --------------------*/
\r
349 /* Configure IO in Analog Mode */
\r
350 GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
\r
352 /* Configure the default Alternate Function in current IO */
\r
353 GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
\r
355 /* Configure the default value for IO Speed */
\r
356 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
\r
358 /* Configure the default value IO Output Type */
\r
359 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
\r
361 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
\r
362 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
\r
364 #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
\r
365 /* Deactivate the Control bit of Analog mode for the current IO */
\r
366 GPIOx->ASCR &= ~(GPIO_ASCR_ASC0<< position);
\r
367 #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
\r
378 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
\r
379 * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
\r
382 ===============================================================================
\r
383 ##### IO operation functions #####
\r
384 ===============================================================================
\r
391 * @brief Read the specified input port pin.
\r
392 * @param GPIOx: where x can be (A..H) to select the GPIO peripheral for STM32L4 family
\r
393 * @param GPIO_Pin: specifies the port bit to read.
\r
394 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
395 * @retval The input port pin value.
\r
397 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
399 GPIO_PinState bitstatus;
\r
401 /* Check the parameters */
\r
402 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
404 if ((GPIOx->IDR & GPIO_Pin) != 0x00u)
\r
406 bitstatus = GPIO_PIN_SET;
\r
410 bitstatus = GPIO_PIN_RESET;
\r
416 * @brief Set or clear the selected data port bit.
\r
418 * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
\r
419 * accesses. In this way, there is no risk of an IRQ occurring between
\r
420 * the read and the modify access.
\r
422 * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
\r
423 * @param GPIO_Pin specifies the port bit to be written.
\r
424 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
425 * @param PinState specifies the value to be written to the selected bit.
\r
426 * This parameter can be one of the GPIO_PinState enum values:
\r
427 * @arg GPIO_PIN_RESET: to clear the port pin
\r
428 * @arg GPIO_PIN_SET: to set the port pin
\r
431 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
\r
433 /* Check the parameters */
\r
434 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
435 assert_param(IS_GPIO_PIN_ACTION(PinState));
\r
437 if(PinState != GPIO_PIN_RESET)
\r
439 GPIOx->BSRR = (uint32_t)GPIO_Pin;
\r
443 GPIOx->BRR = (uint32_t)GPIO_Pin;
\r
448 * @brief Toggle the specified GPIO pin.
\r
449 * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
\r
450 * @param GPIO_Pin specifies the pin to be toggled.
\r
453 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
455 /* Check the parameters */
\r
456 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
458 if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
\r
460 GPIOx->BRR = (uint32_t)GPIO_Pin;
\r
464 GPIOx->BSRR = (uint32_t)GPIO_Pin;
\r
469 * @brief Lock GPIO Pins configuration registers.
\r
470 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
\r
471 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
\r
472 * @note The configuration of the locked GPIO pins can no longer be modified
\r
473 * until the next reset.
\r
474 * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
\r
475 * @param GPIO_Pin specifies the port bits to be locked.
\r
476 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
\r
479 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
\r
481 __IO uint32_t tmp = GPIO_LCKR_LCKK;
\r
483 /* Check the parameters */
\r
484 assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
\r
485 assert_param(IS_GPIO_PIN(GPIO_Pin));
\r
487 /* Apply lock key write sequence */
\r
489 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
\r
491 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
\r
492 GPIOx->LCKR = GPIO_Pin;
\r
493 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
\r
495 /* Read LCKK register. This read is mandatory to complete key lock sequence */
\r
498 /* Read again in order to confirm lock is active */
\r
499 if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
\r
510 * @brief Handle EXTI interrupt request.
\r
511 * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
\r
514 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
\r
516 /* EXTI line interrupt detected */
\r
517 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
\r
519 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
\r
520 HAL_GPIO_EXTI_Callback(GPIO_Pin);
\r
525 * @brief EXTI line detection callback.
\r
526 * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
\r
529 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
\r
531 /* Prevent unused argument(s) compilation warning */
\r
534 /* NOTE: This function should not be modified, when the callback is needed,
\r
535 the HAL_GPIO_EXTI_Callback could be implemented in the user file
\r
548 #endif /* HAL_GPIO_MODULE_ENABLED */
\r
557 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r