1 /**
2 ******************************************************************************
3 * @file stm32l4xx_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
10 *
11 ******************************************************************************
12 * @attention
13 *
14 * Copyright (c) 2017 STMicroelectronics.
15 * All rights reserved.
16 *
17 * This software is licensed under terms that can be found in the LICENSE file
18 * in the root directory of this software component.
19 * If no LICENSE file comes with this software, it is provided AS-IS.
20 *
21 ******************************************************************************
22 @verbatim
23 ==============================================================================
24 ##### GPIO Peripheral features #####
25 ==============================================================================
26 [..]
27 (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
28 configured by software in several modes:
29 (++) Input mode
30 (++) Analog mode
31 (++) Output mode
32 (++) Alternate function mode
33 (++) External interrupt/event lines
34
35 (+) During and just after reset, the alternate functions and external interrupt
36 lines are not active and the I/O ports are configured in input floating mode.
37
38 (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
39 activated or not.
40
41 (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
42 type and the IO speed can be selected depending on the VDD value.
43
44 (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
45 multiplexer that allows only one peripheral alternate function (AF) connected
46 to an IO pin at a time. In this way, there can be no conflict between peripherals
47 sharing the same IO pin.
48
49 (+) All ports have external interrupt/event capability. To use external interrupt
50 lines, the port must be configured in input mode. All available GPIO pins are
51 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
52
53 (+) The external interrupt/event controller consists of up to 39 edge detectors
54 (16 lines are connected to GPIO) for generating event/interrupt requests (each
55 input line can be independently configured to select the type (interrupt or event)
56 and the corresponding trigger event (rising or falling or both). Each line can
57 also be masked independently.
58
59 ##### How to use this driver #####
60 ==============================================================================
61 [..]
62 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
63
64 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
65 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
66 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
67 structure.
68 (++) In case of Output or alternate function mode selection: the speed is
69 configured through "Speed" member from GPIO_InitTypeDef structure.
70 (++) In alternate mode is selection, the alternate function connected to the IO
71 is configured through "Alternate" member from GPIO_InitTypeDef structure.
72 (++) Analog mode is required when a pin is to be used as ADC channel
73 or DAC output.
74 (++) In case of external interrupt/event selection the "Mode" member from
75 GPIO_InitTypeDef structure select the type (interrupt or event) and
76 the corresponding trigger event (rising or falling or both).
77
78 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
79 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
80 HAL_NVIC_EnableIRQ().
81
82 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
83
84 (#) To set/reset the level of a pin configured in output mode use
85 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
86
87 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
88
89 (#) During and just after reset, the alternate functions are not
90 active and the GPIO pins are configured in input floating mode (except JTAG
91 pins).
92
93 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
94 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
95 priority over the GPIO function.
96
97 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
98 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
99 The HSE has priority over the GPIO function.
100
101 @endverbatim
102 ******************************************************************************
103 */
104
105 /* Includes ------------------------------------------------------------------*/
106 #include "stm32l4xx_hal.h"
107
108 /** @addtogroup STM32L4xx_HAL_Driver
109 * @{
110 */
111
112 /** @defgroup GPIO GPIO
113 * @brief GPIO HAL module driver
114 * @{
115 */
116 /** MISRA C:2012 deviation rule has been granted for following rules:
117 * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
118 * range of the shift operator in following API :
119 * HAL_GPIO_Init
120 * HAL_GPIO_DeInit
121 */
122
123 #ifdef HAL_GPIO_MODULE_ENABLED
124
125 /* Private typedef -----------------------------------------------------------*/
126 /* Private defines -----------------------------------------------------------*/
127 /** @addtogroup GPIO_Private_Defines GPIO Private Defines
128 * @{
129 */
130 #define GPIO_NUMBER (16u)
131 /**
132 * @}
133 */
134
135 /* Private macros ------------------------------------------------------------*/
136 /* Private variables ---------------------------------------------------------*/
137 /* Private function prototypes -----------------------------------------------*/
138 /* Exported functions --------------------------------------------------------*/
139
140 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
141 * @{
142 */
143
144 /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
145 * @brief Initialization and Configuration functions
146 *
147 @verbatim
148 ===============================================================================
149 ##### Initialization and de-initialization functions #####
150 ===============================================================================
151
152 @endverbatim
153 * @{
154 */
155
156 /**
157 * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
158 * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
159 * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
160 * the configuration information for the specified GPIO peripheral.
161 * @retval None
162 */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)163 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
164 {
165 uint32_t position = 0x00u;
166 uint32_t iocurrent;
167 uint32_t temp;
168
169 /* Check the parameters */
170 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
171 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
172 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
173
174 /* Configure the port pins */
175 while (((GPIO_Init->Pin) >> position) != 0x00u)
176 {
177 /* Get current io position */
178 iocurrent = (GPIO_Init->Pin) & (1uL << position);
179
180 if (iocurrent != 0x00u)
181 {
182 /*--------------------- GPIO Mode Configuration ------------------------*/
183 /* In case of Output or Alternate function mode selection */
184 if (((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
185 {
186 /* Check the Speed parameter */
187 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
188
189 /* Configure the IO Speed */
190 temp = GPIOx->OSPEEDR;
191 temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
192 temp |= (GPIO_Init->Speed << (position * 2u));
193 GPIOx->OSPEEDR = temp;
194
195 /* Configure the IO Output Type */
196 temp = GPIOx->OTYPER;
197 temp &= ~(GPIO_OTYPER_OT0 << position) ;
198 temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
199 GPIOx->OTYPER = temp;
200 }
201
202 #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
203
204 /* In case of Analog mode, check if ADC control mode is selected */
205 if((GPIO_Init->Mode & GPIO_MODE_ANALOG) == GPIO_MODE_ANALOG)
206 {
207 /* Configure the IO Output Type */
208 temp = GPIOx->ASCR;
209 temp &= ~(GPIO_ASCR_ASC0 << position) ;
210 temp |= (((GPIO_Init->Mode & GPIO_MODE_ANALOG_ADC_CONTROL) >> 3) << position);
211 GPIOx->ASCR = temp;
212 }
213
214 #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
215
216 /* Activate the Pull-up or Pull down resistor for the current IO */
217 if ((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
218 {
219 /* Check the Pull parameter */
220 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
221
222 temp = GPIOx->PUPDR;
223 temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
224 temp |= ((GPIO_Init->Pull) << (position * 2U));
225 GPIOx->PUPDR = temp;
226 }
227
228 /* In case of Alternate function mode selection */
229 if ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
230 {
231 /* Check the Alternate function parameters */
232 assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
233 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
234
235 /* Configure Alternate function mapped with the current IO */
236 temp = GPIOx->AFR[position >> 3u];
237 temp &= ~(0xFu << ((position & 0x07u) * 4u));
238 temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * 4u));
239 GPIOx->AFR[position >> 3u] = temp;
240 }
241
242 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
243 temp = GPIOx->MODER;
244 temp &= ~(GPIO_MODER_MODE0 << (position * 2u));
245 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u));
246 GPIOx->MODER = temp;
247
248 /*--------------------- EXTI Mode Configuration ------------------------*/
249 /* Configure the External Interrupt or event for the current IO */
250 if ((GPIO_Init->Mode & EXTI_MODE) != 0x00u)
251 {
252 /* Enable SYSCFG Clock */
253 __HAL_RCC_SYSCFG_CLK_ENABLE();
254
255 temp = SYSCFG->EXTICR[position >> 2u];
256 temp &= ~(0x0FuL << (4u * (position & 0x03u)));
257 temp |= (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u)));
258 SYSCFG->EXTICR[position >> 2u] = temp;
259
260 /* Clear Rising Falling edge configuration */
261 temp = EXTI->RTSR1;
262 temp &= ~(iocurrent);
263 if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00u)
264 {
265 temp |= iocurrent;
266 }
267 EXTI->RTSR1 = temp;
268
269 temp = EXTI->FTSR1;
270 temp &= ~(iocurrent);
271 if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00u)
272 {
273 temp |= iocurrent;
274 }
275 EXTI->FTSR1 = temp;
276
277 /* Clear EXTI line configuration */
278 temp = EXTI->EMR1;
279 temp &= ~(iocurrent);
280 if ((GPIO_Init->Mode & EXTI_EVT) != 0x00u)
281 {
282 temp |= iocurrent;
283 }
284 EXTI->EMR1 = temp;
285
286 temp = EXTI->IMR1;
287 temp &= ~(iocurrent);
288 if ((GPIO_Init->Mode & EXTI_IT) != 0x00u)
289 {
290 temp |= iocurrent;
291 }
292 EXTI->IMR1 = temp;
293 }
294 }
295
296 position++;
297 }
298 }
299
300 /**
301 * @brief De-initialize the GPIOx peripheral registers to their default reset values.
302 * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
303 * @param GPIO_Pin specifies the port bit to be written.
304 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
305 * @retval None
306 */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)307 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
308 {
309 uint32_t position = 0x00u;
310 uint32_t iocurrent;
311 uint32_t tmp;
312
313 /* Check the parameters */
314 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
315 assert_param(IS_GPIO_PIN(GPIO_Pin));
316
317 /* Configure the port pins */
318 while ((GPIO_Pin >> position) != 0x00u)
319 {
320 /* Get current io position */
321 iocurrent = (GPIO_Pin) & (1uL << position);
322
323 if (iocurrent != 0x00u)
324 {
325 /*------------------------- EXTI Mode Configuration --------------------*/
326 /* Clear the External Interrupt or Event for the current IO */
327
328 tmp = SYSCFG->EXTICR[position >> 2u];
329 tmp &= (0x0FuL << (4u * (position & 0x03u)));
330 if (tmp == (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u))))
331 {
332 /* Clear EXTI line configuration */
333 EXTI->IMR1 &= ~(iocurrent);
334 EXTI->EMR1 &= ~(iocurrent);
335
336 /* Clear Rising Falling edge configuration */
337 EXTI->FTSR1 &= ~(iocurrent);
338 EXTI->RTSR1 &= ~(iocurrent);
339
340 tmp = 0x0FuL << (4u * (position & 0x03u));
341 SYSCFG->EXTICR[position >> 2u] &= ~tmp;
342 }
343
344 /*------------------------- GPIO Mode Configuration --------------------*/
345 /* Configure IO in Analog Mode */
346 GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
347
348 /* Configure the default Alternate Function in current IO */
349 GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
350
351 /* Configure the default value for IO Speed */
352 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
353
354 /* Configure the default value IO Output Type */
355 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
356
357 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
358 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
359
360 #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
361 /* Deactivate the Control bit of Analog mode for the current IO */
362 GPIOx->ASCR &= ~(GPIO_ASCR_ASC0<< position);
363 #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
364 }
365
366 position++;
367 }
368 }
369
370 /**
371 * @}
372 */
373
374 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
375 * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
376 *
377 @verbatim
378 ===============================================================================
379 ##### IO operation functions #####
380 ===============================================================================
381
382 @endverbatim
383 * @{
384 */
385
386 /**
387 * @brief Read the specified input port pin.
388 * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
389 * @param GPIO_Pin specifies the port bit to read.
390 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
391 * @retval The input port pin value.
392 */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)393 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
394 {
395 GPIO_PinState bitstatus;
396
397 /* Check the parameters */
398 assert_param(IS_GPIO_PIN(GPIO_Pin));
399
400 if ((GPIOx->IDR & GPIO_Pin) != 0x00u)
401 {
402 bitstatus = GPIO_PIN_SET;
403 }
404 else
405 {
406 bitstatus = GPIO_PIN_RESET;
407 }
408 return bitstatus;
409 }
410
411 /**
412 * @brief Set or clear the selected data port bit.
413 *
414 * @note This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
415 * accesses. In this way, there is no risk of an IRQ occurring between
416 * the read and the modify access.
417 *
418 * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
419 * @param GPIO_Pin specifies the port bit to be written.
420 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
421 * @param PinState specifies the value to be written to the selected bit.
422 * This parameter can be one of the GPIO_PinState enum values:
423 * @arg GPIO_PIN_RESET: to clear the port pin
424 * @arg GPIO_PIN_SET: to set the port pin
425 * @retval None
426 */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)427 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
428 {
429 /* Check the parameters */
430 assert_param(IS_GPIO_PIN(GPIO_Pin));
431 assert_param(IS_GPIO_PIN_ACTION(PinState));
432
433 if(PinState != GPIO_PIN_RESET)
434 {
435 GPIOx->BSRR = (uint32_t)GPIO_Pin;
436 }
437 else
438 {
439 GPIOx->BRR = (uint32_t)GPIO_Pin;
440 }
441 }
442
443 /**
444 * @brief Toggle the specified GPIO pin.
445 * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32L4 family
446 * @param GPIO_Pin specifies the pin to be toggled.
447 * @retval None
448 */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)449 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
450 {
451 uint32_t odr;
452
453 /* Check the parameters */
454 assert_param(IS_GPIO_PIN(GPIO_Pin));
455
456 /* get current Output Data Register value */
457 odr = GPIOx->ODR;
458
459 /* Set selected pins that were at low level, and reset ones that were high */
460 GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
461 }
462
463 /**
464 * @brief Lock 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..H) to select the GPIO peripheral for STM32L4 family
470 * @param GPIO_Pin specifies the port bits to be locked.
471 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
472 * @retval None
473 */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)474 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
475 {
476 __IO uint32_t tmp = GPIO_LCKR_LCKK;
477
478 /* Check the parameters */
479 assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
480 assert_param(IS_GPIO_PIN(GPIO_Pin));
481
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;
492
493 /* Read again in order to confirm lock is active */
494 if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
495 {
496 return HAL_OK;
497 }
498 else
499 {
500 return HAL_ERROR;
501 }
502 }
503
504 /**
505 * @brief Handle EXTI interrupt request.
506 * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
507 * @retval None
508 */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)509 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
510 {
511 /* EXTI line interrupt detected */
512 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
513 {
514 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
515 HAL_GPIO_EXTI_Callback(GPIO_Pin);
516 }
517 }
518
519 /**
520 * @brief EXTI line detection callback.
521 * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
522 * @retval None
523 */
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)524 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
525 {
526 /* Prevent unused argument(s) compilation warning */
527 UNUSED(GPIO_Pin);
528
529 /* NOTE: This function should not be modified, when the callback is needed,
530 the HAL_GPIO_EXTI_Callback could be implemented in the user file
531 */
532 }
533
534 /**
535 * @}
536 */
537
538
539 /**
540 * @}
541 */
542
543 #endif /* HAL_GPIO_MODULE_ENABLED */
544 /**
545 * @}
546 */
547
548 /**
549 * @}
550 */
551
552