xref: /btstack/port/stm32-wb55xx-nucleo-freertos/Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_gpio.c (revision 0561b2d8d5dba972c7daa57d5e677f7a1327edfd)
1 /**
2   ******************************************************************************
3   * @file    stm32wbxx_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 AHB 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     (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
72 
73     (#) To set/reset the level of a pin configured in output mode use
74         HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
75 
76    (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
77 
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).
81 
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.
85 
86     (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
87         general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
88         The HSE has priority over the GPIO function.
89 
90   @endverbatim
91   ******************************************************************************
92   * @attention
93   *
94   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
95   * All rights reserved.</center></h2>
96   *
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
101   *
102   ******************************************************************************
103   */
104 
105 /* Includes ------------------------------------------------------------------*/
106 #include "stm32wbxx_hal.h"
107 
108 /** @addtogroup STM32WBxx_HAL_Driver
109   * @{
110   */
111 
112 /** @addtogroup GPIO
113   * @{
114   */
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
120   */
121 
122 #ifdef HAL_GPIO_MODULE_ENABLED
123 
124 /* Private typedef -----------------------------------------------------------*/
125 /* Private defines ------------------------------------------------------------*/
126 /** @defgroup GPIO_Private_Constants GPIO Private Constants
127   * @{
128   */
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)
136 
137 #define GPIO_NUMBER           (16u)
138 /**
139   * @}
140   */
141 
142 /* Private macros ------------------------------------------------------------*/
143 /* Private variables ---------------------------------------------------------*/
144 /* Private function prototypes -----------------------------------------------*/
145 /* Exported functions --------------------------------------------------------*/
146 
147 /** @addtogroup GPIO_Exported_Functions
148   * @{
149   */
150 
151 /** @addtogroup GPIO_Exported_Functions_Group1
152  *  @brief    Initialization and Configuration functions
153  *
154 @verbatim
155  ===============================================================================
156               ##### Initialization and de-initialization functions #####
157  ===============================================================================
158 
159 @endverbatim
160   * @{
161   */
162 
163 /**
164   * @brief  Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
165   * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WBxx 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
169   */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)170 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
171 {
172   uint32_t position = 0x00u;
173   uint32_t iocurrent;
174   uint32_t temp;
175 
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));
181 
182   /* Configure the port pins */
183   while (((GPIO_Init->Pin) >> position) != 0x00u)
184   {
185     /* Get current io position */
186     iocurrent = (GPIO_Init->Pin) & (1uL << position);
187 
188     if (iocurrent != 0x00u)
189     {
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))
193       {
194         /* Check the Alternate function parameters */
195         assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
196         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
197 
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;
203       }
204 
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;
210 
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))
214       {
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;
222 
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;
228       }
229 
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;
235 
236       /*--------------------- EXTI Mode Configuration ------------------------*/
237       /* Configure the External Interrupt or event for the current IO */
238       if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
239       {
240         temp = SYSCFG->EXTICR[position >> 2u];
241         temp &= ~(0x0FuL << (4u * (position & 0x03u)));
242         temp |= (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u)));
243         SYSCFG->EXTICR[position >> 2u] = temp;
244 
245         /* Clear EXTI line configuration */
246         temp = EXTI->IMR1;
247         temp &= ~(iocurrent);
248         if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
249         {
250           temp |= iocurrent;
251         }
252         EXTI->IMR1 = temp;
253 
254         temp = EXTI->EMR1;
255         temp &= ~(iocurrent);
256         if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
257         {
258           temp |= iocurrent;
259         }
260         EXTI->EMR1 = temp;
261 
262         /* Clear Rising Falling edge configuration */
263         temp = EXTI->RTSR1;
264         temp &= ~(iocurrent);
265         if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
266         {
267           temp |= iocurrent;
268         }
269         EXTI->RTSR1 = temp;
270 
271         temp = EXTI->FTSR1;
272         temp &= ~(iocurrent);
273         if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
274         {
275           temp |= iocurrent;
276         }
277         EXTI->FTSR1 = temp;
278       }
279     }
280 
281     position++;
282   }
283 }
284 
285 /**
286   * @brief  De-initialize the GPIOx peripheral registers to their default reset values.
287   * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WBxx family
288   * @param GPIO_Pin specifies the port bit to be written.
289   *         This parameter can be one of GPIO_PIN_x where x can be (0..15).
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;
296   uint32_t tmp;
297 
298   /* Check the parameters */
299   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
300   assert_param(IS_GPIO_PIN(GPIO_Pin));
301 
302   /* Configure the port pins */
303   while ((GPIO_Pin >> position) != 0x00u)
304   {
305     /* Get current io position */
306     iocurrent = (GPIO_Pin) & (1uL << position);
307 
308     if (iocurrent != 0x00u)
309     {
310       /*------------------------- EXTI Mode Configuration --------------------*/
311       /* Clear the External Interrupt or Event for the current IO */
312 
313       tmp = SYSCFG->EXTICR[position >> 2u];
314       tmp &= (0x0FUL << (4u * (position & 0x03u)));
315       if (tmp == (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u))))
316       {
317         tmp = 0x0FuL << (4u * (position & 0x03u));
318         SYSCFG->EXTICR[position >> 2u] &= ~tmp;
319 
320         /* Clear EXTI line configuration */
321         EXTI->IMR1 &= ~(iocurrent);
322         EXTI->EMR1 &= ~(iocurrent);
323 
324         /* Clear Rising Falling edge configuration */
325         EXTI->RTSR1 &= ~(iocurrent);
326         EXTI->FTSR1 &= ~(iocurrent);
327       }
328 
329       /*------------------------- GPIO Mode Configuration --------------------*/
330       /* Configure IO in Analog Mode */
331       GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
332 
333       /* Configure the default Alternate Function in current IO */
334       GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
335 
336       /* Configure the default value for IO Speed */
337       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
338 
339       /* Configure the default value IO Output Type */
340       GPIOx->OTYPER  &= ~(GPIO_OTYPER_OT0 << position) ;
341 
342       /* Deactivate the Pull-up and Pull-down resistor for the current IO */
343       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
344     }
345 
346     position++;
347   }
348 }
349 
350 /**
351   * @}
352   */
353 
354 /** @addtogroup GPIO_Exported_Functions_Group2
355  *  @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
356  *
357 @verbatim
358  ===============================================================================
359                        ##### IO operation functions #####
360  ===============================================================================
361 
362 @endverbatim
363   * @{
364   */
365 
366 /**
367   * @brief  Read the specified input port pin.
368   * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32WBxx family
369   * @param GPIO_Pin specifies the port bit to read.
370   *         This parameter can be GPIO_PIN_x where x can be (0..15).
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(GPIO_Pin));
379 
380   if ((GPIOx->IDR & GPIO_Pin) != 0x00u)
381   {
382     bitstatus = GPIO_PIN_SET;
383   }
384   else
385   {
386     bitstatus = GPIO_PIN_RESET;
387   }
388   return bitstatus;
389 }
390 
391 /**
392   * @brief  Set or clear the selected data port bit.
393   *
394   * @note   This function uses GPIOx_BSRR and GPIOx_BRR registers 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..F) to select the GPIO peripheral for STM32WBxx family
399   * @param GPIO_Pin specifies the port bit to be written.
400   *         This parameter can be one of GPIO_PIN_x where x can be (0..15).
401   * @param PinState specifies the value to be written to the selected bit.
402   *         This parameter can be one of the GPIO_PinState enum values:
403   *            @arg GPIO_PIN_RESET: to clear the port pin
404   *            @arg GPIO_PIN_SET: to set the port pin
405   * @retval None
406   */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)407 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
408 {
409   /* Check the parameters */
410   assert_param(IS_GPIO_PIN(GPIO_Pin));
411   assert_param(IS_GPIO_PIN_ACTION(PinState));
412 
413   if(PinState != GPIO_PIN_RESET)
414   {
415     GPIOx->BSRR = (uint32_t)GPIO_Pin;
416   }
417   else
418   {
419     GPIOx->BRR = (uint32_t)GPIO_Pin;
420   }
421 }
422 
423 /**
424   * @brief  Toggle the specified GPIO pin.
425   * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WBxx family
426   * @param GPIO_Pin specifies the pin to be toggled.
427   * @retval None
428   */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)429 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
430 {
431   /* Check the parameters */
432   assert_param(IS_GPIO_PIN(GPIO_Pin));
433 
434   if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
435   {
436     GPIOx->BRR = (uint32_t)GPIO_Pin;
437   }
438   else
439   {
440     GPIOx->BSRR = (uint32_t)GPIO_Pin;
441   }
442 }
443 
444 /**
445 * @brief  Lock GPIO Pins configuration registers.
446   * @note   The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
447   *         GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
448   * @note   The configuration of the locked GPIO pins can no longer be modified
449   *         until the next reset.
450   * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WBxx family
451   * @param GPIO_Pin specifies the port bits to be locked.
452   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
453   * @retval None
454   */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)455 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
456 {
457   __IO uint32_t tmp = GPIO_LCKR_LCKK;
458 
459   /* Check the parameters */
460   assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
461   assert_param(IS_GPIO_PIN(GPIO_Pin));
462 
463   /* Apply lock key write sequence */
464   tmp |= GPIO_Pin;
465   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
466   GPIOx->LCKR = tmp;
467   /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
468   GPIOx->LCKR = GPIO_Pin;
469   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
470   GPIOx->LCKR = tmp;
471   /* Read LCKK register. This read is mandatory to complete key lock sequence */
472   tmp = GPIOx->LCKR;
473 
474   /* read again in order to confirm lock is active */
475   if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
476   {
477     return HAL_OK;
478   }
479   else
480   {
481     return HAL_ERROR;
482   }
483 }
484 
485 /**
486   * @brief  Handle EXTI interrupt request.
487   * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
488   * @retval None
489   */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)490 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
491 {
492   /* EXTI line interrupt detected */
493   if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
494   {
495     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
496     HAL_GPIO_EXTI_Callback(GPIO_Pin);
497   }
498 }
499 
500 /**
501   * @brief  EXTI line detection callback.
502   * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
503   * @retval None
504   */
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)505 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
506 {
507   /* Prevent unused argument(s) compilation warning */
508   UNUSED(GPIO_Pin);
509 
510   /* NOTE: This function should not be modified, when the callback is needed,
511            the HAL_GPIO_EXTI_Callback could be implemented in the user file
512    */
513 }
514 
515 /**
516   * @}
517   */
518 
519 
520 /**
521   * @}
522   */
523 
524 #endif /* HAL_GPIO_MODULE_ENABLED */
525 /**
526   * @}
527   */
528 
529 /**
530   * @}
531   */
532 
533 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
534