xref: /btstack/port/stm32-l073rz-nucleo-em9304/Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_gpio.c (revision e838079242074edcbcbb400962776e15fe6ca6cb)
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_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   @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
23 
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.
26 
27     (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
28   activated or not.
29 
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.
32 
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.
37 
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.
41 
42     (+) The external interrupt/event controller consists of up to 28 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.
47 
48                      ##### How to use this driver #####
49   ==============================================================================
50   [..]
51     (#) Enable the GPIO IOPORT clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
52 
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).
66 
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().
70 
71    (#) HAL_GPIO_DeInit allows to set register values to their reset value. This function
72        is also to be used when unconfiguring pin which was used as an external interrupt
73        or in event mode. That is the only way to reset the corresponding bit in
74        EXTI & SYSCFG registers.
75 
76     (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
77 
78     (#) To set/reset the level of a pin configured in output mode use
79         HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
80 
81     (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
82 
83     (#) During and just after reset, the alternate functions are not
84         active and the GPIO pins are configured in input floating mode (except JTAG
85         pins).
86 
87     (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
88         (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
89         priority over the GPIO function.
90 
91     (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
92         general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
93         The HSE has priority over the GPIO function.
94 
95   @endverbatim
96   ******************************************************************************
97   * @attention
98   *
99   * <h2><center>&copy; Copyright(c) 2016 STMicroelectronics.
100   * All rights reserved.</center></h2>
101   *
102   * This software component is licensed by ST under BSD 3-Clause license,
103   * the "License"; You may not use this file except in compliance with the
104   * License. You may obtain a copy of the License at:
105   *                        opensource.org/licenses/BSD-3-Clause
106   *
107   ******************************************************************************
108   */
109 
110 /* Includes ------------------------------------------------------------------*/
111 #include "stm32l0xx_hal.h"
112 
113 /** @addtogroup STM32L0xx_HAL_Driver
114   * @{
115   */
116 
117 #ifdef HAL_GPIO_MODULE_ENABLED
118 
119 /** @addtogroup GPIO
120   * @brief GPIO HAL module driver
121   * @{
122   */
123 
124 /** @addtogroup GPIO_Private
125   * @{
126   */
127 /* Private define ------------------------------------------------------------*/
128 
129 
130 #define GPIO_MODE             ((uint32_t)0x00000003U)
131 #define EXTI_MODE             ((uint32_t)0x10000000U)
132 #define GPIO_MODE_IT          ((uint32_t)0x00010000U)
133 #define GPIO_MODE_EVT         ((uint32_t)0x00020000U)
134 #define RISING_EDGE           ((uint32_t)0x00100000U)
135 #define FALLING_EDGE          ((uint32_t)0x00200000U)
136 #define GPIO_OUTPUT_TYPE      ((uint32_t)0x00000010U)
137 
138 #define GPIO_NUMBER           ((uint32_t)16U)
139 
140 /**
141   * @}
142   */
143 /** @addtogroup GPIO_Exported_Functions
144   * @{
145   */
146 
147 /** @addtogroup GPIO_Exported_Functions_Group1
148  *  @brief    Initialization and de-initialization functions
149  *
150 @verbatim
151  ===============================================================================
152               ##### Initialization and de-initialization functions #####
153  ===============================================================================
154 
155 @endverbatim
156   * @{
157   */
158 
159 /**
160   * @brief  Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
161   * @param  GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0XX family devices.
162   *                Note that GPIOE is not available on all devices.
163   * @param  GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
164   *                    the configuration information for the specified GPIO peripheral.
165   * @retval None
166   */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)167 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
168 {
169   uint32_t position = 0x00U;
170   uint32_t iocurrent = 0x00U;
171   uint32_t temp = 0x00U;
172 
173   /* Check the parameters */
174   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
175   assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
176   assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,(GPIO_Init->Pin)));
177 
178   /* Configure the port pins */
179   while (((GPIO_Init->Pin) >> position) != 0)
180   {
181     /* Get the IO position */
182     iocurrent = (GPIO_Init->Pin) & (1U << position);
183 
184     if(iocurrent)
185     {
186       /*--------------------- GPIO Mode Configuration ------------------------*/
187       /* In case of Alternate function mode selection */
188       if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
189       {
190         /* Check the Alternate function parameters */
191         assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
192         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
193 
194         /* Configure Alternate function mapped with the current IO */
195         temp = GPIOx->AFR[position >> 3U];
196         temp &= ~((uint32_t)0xFU << ((uint32_t)(position & (uint32_t)0x07U) * 4U)) ;
197         temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07U) * 4U)) ;
198         GPIOx->AFR[position >> 3U] = temp;
199       }
200 
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))
204       {
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_OSPEEDER_OSPEED0 << (position * 2U));
210         temp |= (GPIO_Init->Speed << (position * 2U));
211         GPIOx->OSPEEDR = temp;
212 
213         /* Configure the IO Output Type */
214         temp= GPIOx->OTYPER;
215         temp &= ~(GPIO_OTYPER_OT_0 << position) ;
216         temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
217         GPIOx->OTYPER = temp;
218       }
219 
220       /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
221       temp = GPIOx->MODER;
222       temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
223       temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
224       GPIOx->MODER = temp;
225 
226       /* Activate the Pull-up or Pull down resistor for the current IO */
227       temp = GPIOx->PUPDR;
228       temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
229       temp |= ((GPIO_Init->Pull) << (position * 2U));
230       GPIOx->PUPDR = temp;
231 
232       /*--------------------- EXTI Mode Configuration ------------------------*/
233       /* Configure the External Interrupt or event for the current IO */
234       if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
235       {
236         /* Enable SYSCFG Clock */
237         __HAL_RCC_SYSCFG_CLK_ENABLE();
238 
239         temp = SYSCFG->EXTICR[position >> 2U];
240         CLEAR_BIT(temp, ((uint32_t)0x0FU) << (4U * (position & 0x03U)));
241         SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03U)));
242         SYSCFG->EXTICR[position >> 2U] = temp;
243 
244         /* Clear EXTI line configuration */
245         temp = EXTI->IMR;
246         temp &= ~((uint32_t)iocurrent);
247         if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
248         {
249           temp |= iocurrent;
250         }
251         EXTI->IMR = temp;
252 
253         temp = EXTI->EMR;
254         temp &= ~((uint32_t)iocurrent);
255         if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
256         {
257           temp |= iocurrent;
258         }
259         EXTI->EMR = temp;
260 
261         /* Clear Rising Falling edge configuration */
262         temp = EXTI->RTSR;
263         temp &= ~((uint32_t)iocurrent);
264         if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
265         {
266           temp |= iocurrent;
267         }
268         EXTI->RTSR = temp;
269 
270         temp = EXTI->FTSR;
271         temp &= ~((uint32_t)iocurrent);
272         if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
273         {
274           temp |= iocurrent;
275         }
276         EXTI->FTSR = temp;
277       }
278     }
279     position++;
280   }
281 }
282 
283 /**
284   * @brief  De-initializes the GPIOx peripheral registers to their default reset values.
285   * @param  GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0XX family devices.
286   *                Note that GPIOE is not available on all devices.
287   * @param  GPIO_Pin specifies the port bit to be written.
288   *                   This parameter can be one of GPIO_PIN_x where x can be (0..15).
289   *                   All port bits are not necessarily available on all GPIOs.
290   * @retval None
291   */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)292 void HAL_GPIO_DeInit(GPIO_TypeDef  *GPIOx, uint32_t GPIO_Pin)
293 {
294   uint32_t position = 0x00U;
295   uint32_t iocurrent = 0x00U;
296   uint32_t tmp = 0x00U;
297 
298   /* Check the parameters */
299   assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
300 
301   /* Configure the port pins */
302   while ((GPIO_Pin >> position) != 0)
303   {
304     /* Get the IO position */
305     iocurrent = (GPIO_Pin) & (1U << position);
306 
307     if(iocurrent)
308     {
309       /*------------------------- GPIO Mode Configuration --------------------*/
310       /* Configure IO Direction in Input Floting Mode */
311       GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2U));
312 
313       /* Configure the default Alternate Function in current IO */
314       GPIOx->AFR[position >> 3U] &= ~((uint32_t)0xFU << ((uint32_t)(position & (uint32_t)0x07U) * 4U)) ;
315 
316       /* Configure the default value for IO Speed */
317       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEED0 << (position * 2U));
318 
319       /* Configure the default value IO Output Type */
320       GPIOx->OTYPER  &= ~(GPIO_OTYPER_OT_0 << position) ;
321 
322       /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
323       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
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 &= (((uint32_t)0x0FU) << (4U * (position & 0x03U)));
330       if(tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
331       {
332         tmp = ((uint32_t)0x0FU) << (4U * (position & 0x03U));
333         SYSCFG->EXTICR[position >> 2U] &= ~tmp;
334 
335         /* Clear EXTI line configuration */
336         EXTI->IMR &= ~((uint32_t)iocurrent);
337         EXTI->EMR &= ~((uint32_t)iocurrent);
338 
339         /* Clear Rising Falling edge configuration */
340         EXTI->RTSR &= ~((uint32_t)iocurrent);
341         EXTI->FTSR &= ~((uint32_t)iocurrent);
342       }
343     }
344      position++;
345   }
346 }
347 
348 /**
349   * @}
350   */
351 
352 /** @addtogroup GPIO_Exported_Functions_Group2
353  *  @brief   GPIO Read and Write
354  *
355 @verbatim
356  ===============================================================================
357                        ##### IO operation functions #####
358  ===============================================================================
359 
360 @endverbatim
361   * @{
362   */
363 
364 /**
365   * @brief  Reads the specified input port pin.
366   * @param  GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
367   *                Note that GPIOE is not available on all devices.
368   * @param  GPIO_Pin specifies the port bit to read.
369   *                   This parameter can be GPIO_PIN_x where x can be (0..15).
370   *                   All port bits are not necessarily available on all GPIOs.
371   * @retval The input port pin value.
372   */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)373 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
374 {
375   GPIO_PinState bitstatus;
376 
377   /* Check the parameters */
378   assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
379 
380   if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
381   {
382     bitstatus = GPIO_PIN_SET;
383   }
384   else
385   {
386     bitstatus = GPIO_PIN_RESET;
387   }
388   return bitstatus;
389 }
390 
391 /**
392   * @brief  Sets or clears the selected data port bit.
393   *
394   * @note   This function uses GPIOx_BSRR register to allow atomic read/modify
395   *         accesses. In this way, there is no risk of an IRQ occurring between
396   *         the read and the modify access.
397   *
398   * @param  GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
399   *                Note that GPIOE is not available on all devices.
400   * @param  GPIO_Pin specifies the port bit to be written.
401   *                   This parameter can be one of GPIO_PIN_x where x can be (0..15).
402   *                   All port bits are not necessarily available on all GPIOs.
403   * @param  PinState specifies the value to be written to the selected bit.
404   *                   This parameter can be one of the GPIO_PinState enum values:
405   *                        GPIO_PIN_RESET: to clear the port pin
406   *                        GPIO_PIN_SET: to set the port pin
407   * @retval None
408   */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)409 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
410 {
411   /* Check the parameters */
412   assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
413   assert_param(IS_GPIO_PIN_ACTION(PinState));
414 
415   if(PinState != GPIO_PIN_RESET)
416   {
417     GPIOx->BSRR = GPIO_Pin;
418   }
419   else
420   {
421     GPIOx->BRR = GPIO_Pin ;
422   }
423 }
424 
425 /**
426   * @brief  Toggles the specified GPIO pins.
427   * @param  GPIOx Where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
428   *                Note that GPIOE is not available on all devices.
429   *                All port bits are not necessarily available on all GPIOs.
430   * @param  GPIO_Pin Specifies the pins to be toggled.
431   * @retval None
432   */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)433 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
434 {
435   /* Check the parameters */
436   assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
437 
438   GPIOx->ODR ^= GPIO_Pin;
439 }
440 
441 /**
442 * @brief  Locks GPIO Pins configuration registers.
443 * @note   The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
444 *         GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
445 * @note   The configuration of the locked GPIO pins can no longer be modified
446 *         until the next reset.
447 * @param  GPIOx where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family.
448 *                Note that GPIOE is not available on all devices.
449 * @param  GPIO_Pin specifies the port bit to be locked.
450 *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
451 *         All port bits are not necessarily available on all GPIOs.
452 * @retval None
453 */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)454 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
455 {
456   __IO uint32_t tmp = GPIO_LCKR_LCKK;
457 
458   /* Check the parameters */
459   assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
460 
461   /* Apply lock key write sequence */
462   tmp |= GPIO_Pin;
463   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
464   GPIOx->LCKR = tmp;
465   /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
466   GPIOx->LCKR = GPIO_Pin;
467   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
468   GPIOx->LCKR = tmp;
469   /* Read LCKK bit*/
470   tmp = GPIOx->LCKR;
471 
472   if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
473   {
474     return HAL_OK;
475   }
476   else
477   {
478     return HAL_ERROR;
479   }
480 }
481 /**
482   * @brief  This function handles EXTI interrupt request.
483   * @param  GPIO_Pin Specifies the pins connected to the EXTI line.
484   * @retval None
485   */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)486 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
487 {
488   /* EXTI line interrupt detected */
489   if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
490   {
491     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
492     HAL_GPIO_EXTI_Callback(GPIO_Pin);
493   }
494 }
495 
496 /**
497   * @brief  EXTI line detection callbacks.
498   * @param  GPIO_Pin Specifies the pins connected to the EXTI line.
499   * @retval None
500   */
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)501 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
502 {
503   /* Prevent unused argument(s) compilation warning */
504   UNUSED(GPIO_Pin);
505 
506   /* NOTE: This function Should not be modified, when the callback is needed,
507            the HAL_GPIO_EXTI_Callback could be implemented in the user file
508    */
509 }
510 
511 /**
512   * @}
513   */
514 
515 
516 /**
517   * @}
518   */
519 
520 /**
521   * @}
522   */
523 
524 #endif /* HAL_GPIO_MODULE_ENABLED */
525 
526 /**
527   * @}
528   */
529 
530 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
531 
532