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