1 /**
2   ******************************************************************************
3   * @file    stm32wlxx_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) 2020 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 
90    (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
91 
92     (#) During and just after reset, the alternate functions are not
93         active and the GPIO pins are configured in input floating mode (except JTAG
94         pins).
95 
96     (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
97         (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
98         priority over the GPIO function.
99 
100     (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
101         general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
102         The HSE has priority over the GPIO function.
103 
104   @endverbatim
105   ******************************************************************************
106   */
107 
108 /* Includes ------------------------------------------------------------------*/
109 #include "stm32wlxx_hal.h"
110 
111 /** @addtogroup STM32WLxx_HAL_Driver
112   * @{
113   */
114 
115 /** @addtogroup GPIO
116   * @{
117   */
118 
119 #ifdef HAL_GPIO_MODULE_ENABLED
120 
121 /* Private typedef -----------------------------------------------------------*/
122 /* Private defines ------------------------------------------------------------*/
123 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
124   * @{
125   */
126 #define GPIO_NUMBER           (16U)
127 /**
128   * @}
129   */
130 
131 /* Private macros ------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 /* Exported functions --------------------------------------------------------*/
135 
136 /** @addtogroup GPIO_Exported_Functions
137   * @{
138   */
139 
140 /** @addtogroup GPIO_Exported_Functions_Group1
141   *  @brief    Initialization and Configuration functions
142   *
143 @verbatim
144  ===============================================================================
145               ##### Initialization and de-initialization functions #####
146  ===============================================================================
147 
148 @endverbatim
149   * @{
150   */
151 
152 /**
153   * @brief  Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
154   * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WLxx family
155   * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
156   *         the configuration information for the specified GPIO peripheral.
157   * @retval None
158   */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)159 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
160 {
161   uint32_t position = 0x00u;
162   uint32_t iocurrent;
163   uint32_t temp;
164 
165   /* Check the parameters */
166   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
167   assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
168   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
169   assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
170 
171   /* Configure the port pins */
172   while (((GPIO_Init->Pin) >> position) != 0x00u)
173   {
174     /* Get current io position */
175     iocurrent = (GPIO_Init->Pin) & (1uL << position);
176 
177     if (iocurrent != 0x00u)
178     {
179       /*--------------------- GPIO Mode Configuration ------------------------*/
180       /* In case of Output or Alternate function mode selection */
181       if (((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
182       {
183         /* Check the Speed parameter */
184         assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
185         /* Configure the IO Speed */
186         temp = GPIOx->OSPEEDR;
187         temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
188         temp |= (GPIO_Init->Speed << (position * 2U));
189         GPIOx->OSPEEDR = temp;
190 
191         /* Configure the IO Output Type */
192         temp = GPIOx->OTYPER;
193         temp &= ~(GPIO_OTYPER_OT0 << position) ;
194         temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
195         GPIOx->OTYPER = temp;
196       }
197 
198       /* Activate the Pull-up or Pull down resistor for the current IO */
199       if ((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
200       {
201         temp = GPIOx->PUPDR;
202         temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
203         temp |= ((GPIO_Init->Pull) << (position * 2U));
204         GPIOx->PUPDR = temp;
205       }
206 
207       /* In case of Alternate function mode selection */
208       if ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
209       {
210         /* Check the Alternate function parameters */
211         assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
212         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
213 
214         /* Configure Alternate function mapped with the current IO */
215         temp = GPIOx->AFR[position >> 3U];
216         temp &= ~(0xFU << ((position & 0x07U) * 4U));
217         temp |= ((GPIO_Init->Alternate) << ((position & 0x07U) * 4U));
218         GPIOx->AFR[position >> 3u] = temp;
219       }
220 
221       /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
222       temp = GPIOx->MODER;
223       temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
224       temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
225       GPIOx->MODER = temp;
226 
227       /*--------------------- EXTI Mode Configuration ------------------------*/
228       /* Configure the External Interrupt or event for the current IO */
229       if ((GPIO_Init->Mode & EXTI_MODE) != 0x00u)
230       {
231         temp = SYSCFG->EXTICR[position >> 2u];
232         temp &= ~(0x07uL << (4U * (position & 0x03U)));
233         temp |= (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U)));
234         SYSCFG->EXTICR[position >> 2u] = temp;
235 
236         /* Clear Rising Falling edge configuration */
237         temp = EXTI->RTSR1;
238         temp &= ~(iocurrent);
239         if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00u)
240         {
241           temp |= iocurrent;
242         }
243         EXTI->RTSR1 = temp;
244 
245         temp = EXTI->FTSR1;
246         temp &= ~(iocurrent);
247         if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00u)
248         {
249           temp |= iocurrent;
250         }
251         EXTI->FTSR1 = temp;
252 
253         /* Clear EXTI line configuration */
254 #ifdef CORE_CM0PLUS
255         temp = EXTI->C2IMR1;
256 #else
257         temp = EXTI->IMR1;
258 #endif /* CORE_CM0PLUS */
259         temp &= ~(iocurrent);
260         if ((GPIO_Init->Mode & EXTI_IT) != 0x00u)
261         {
262           temp |= iocurrent;
263         }
264 #ifdef CORE_CM0PLUS
265         EXTI->C2IMR1 = temp;
266 #else
267         EXTI->IMR1 = temp;
268 #endif /* CORE_CM0PLUS */
269 
270 #ifdef CORE_CM0PLUS
271         temp = EXTI->C2EMR1;
272 #else
273         temp = EXTI->EMR1;
274 #endif /* CORE_CM0PLUS */
275         temp &= ~(iocurrent);
276         if ((GPIO_Init->Mode & EXTI_EVT) != 0x00u)
277         {
278           temp |= iocurrent;
279         }
280 #ifdef CORE_CM0PLUS
281         EXTI->C2EMR1 = temp;
282 #else
283         EXTI->EMR1 = temp;
284 #endif /* CORE_CM0PLUS */
285       }
286     }
287 
288     position++;
289   }
290 }
291 
292 /**
293   * @brief  De-initialize the GPIOx peripheral registers to their default reset values.
294   * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WLxx family
295   * @param GPIO_Pin specifies the port bit to be written.
296   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
297   * @retval None
298   */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)299 void HAL_GPIO_DeInit(GPIO_TypeDef  *GPIOx, uint32_t GPIO_Pin)
300 {
301   uint32_t position = 0x00u;
302   uint32_t iocurrent;
303   uint32_t tmp;
304 
305   /* Check the parameters */
306   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
307   assert_param(IS_GPIO_PIN(GPIO_Pin));
308 
309   /* Configure the port pins */
310   while ((GPIO_Pin >> position) != 0x00u)
311   {
312     /* Get current io position */
313     iocurrent = (GPIO_Pin) & (1uL << position);
314 
315     if (iocurrent != 0x00u)
316     {
317       /*------------------------- EXTI Mode Configuration --------------------*/
318       /* Clear the External Interrupt or Event for the current IO */
319 
320       tmp = SYSCFG->EXTICR[position >> 2u];
321       tmp &= (0x07uL << (4U * (position & 0x03U)));
322       if (tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
323       {
324         /* Clear EXTI line configuration */
325 #ifdef CORE_CM0PLUS
326         EXTI->C2IMR1 &= ~(iocurrent);
327         EXTI->C2EMR1 &= ~(iocurrent);
328 #else
329         EXTI->IMR1 &= ~(iocurrent);
330         EXTI->EMR1 &= ~(iocurrent);
331 #endif /* CORE_CM0PLUS */
332 
333         /* Clear Rising Falling edge configuration */
334         EXTI->RTSR1 &= ~(iocurrent);
335         EXTI->FTSR1 &= ~(iocurrent);
336 
337         /* Clear EXTICR configuration */
338         tmp = 0x07uL << (4u * (position & 0x03U));
339         SYSCFG->EXTICR[position >> 2u] &= ~tmp;
340       }
341 
342       /*------------------------- GPIO Mode Configuration --------------------*/
343       /* Configure IO in Analog Mode */
344       GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2U));
345 
346       /* Configure the default Alternate Function in current IO */
347       GPIOx->AFR[position >> 3U] &= ~(0xFU << ((position & 0x07U) * 4U)) ;
348 
349       /* Configure the default value for IO Speed */
350       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
351 
352       /* Configure the default value IO Output Type */
353       GPIOx->OTYPER  &= ~(GPIO_OTYPER_OT0 << position) ;
354 
355       /* Deactivate the Pull-up and Pull-down resistor for the current IO */
356       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
357     }
358 
359     position++;
360   }
361 }
362 
363 /**
364   * @}
365   */
366 
367 /** @addtogroup GPIO_Exported_Functions_Group2
368   *  @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
369   *
370 @verbatim
371  ===============================================================================
372                        ##### IO operation functions #####
373  ===============================================================================
374 
375 @endverbatim
376   * @{
377   */
378 
379 /**
380   * @brief  Read the specified input port pin.
381   * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32WLxx family
382   * @param GPIO_Pin specifies the port bit to read.
383   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
384   * @retval The input port pin value.
385   */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)386 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
387 {
388   GPIO_PinState bitstatus;
389 
390   /* Check the parameters */
391   assert_param(IS_GPIO_PIN(GPIO_Pin));
392 
393   if ((GPIOx->IDR & GPIO_Pin) != 0x00u)
394   {
395     bitstatus = GPIO_PIN_SET;
396   }
397   else
398   {
399     bitstatus = GPIO_PIN_RESET;
400   }
401   return bitstatus;
402 }
403 
404 /**
405   * @brief  Set or clear the selected data port bit.
406   * @note   This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
407   *         accesses. In this way, there is no risk of an IRQ occurring between
408   *         the read and the modify access.
409   * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32WLxx family
410   * @param GPIO_Pin specifies the port bit to be written.
411   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
412   * @param PinState specifies the value to be written to the selected bit.
413   *         This parameter can be one of the GPIO_PinState enum values:
414   *            @arg GPIO_PIN_RESET: to clear the port pin
415   *            @arg GPIO_PIN_SET: to set the port pin
416   * @retval None
417   */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)418 void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
419 {
420   /* Check the parameters */
421   assert_param(IS_GPIO_PIN(GPIO_Pin));
422   assert_param(IS_GPIO_PIN_ACTION(PinState));
423 
424   if (PinState != GPIO_PIN_RESET)
425   {
426     GPIOx->BSRR = (uint32_t)GPIO_Pin;
427   }
428   else
429   {
430     GPIOx->BRR = (uint32_t)GPIO_Pin;
431   }
432 }
433 
434 /**
435   * @brief  Set and clear several pins of a dedicated port in same cycle.
436   * @note   This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
437   *         accesses.
438   * @param  GPIOx where x can be (A..F) to select the GPIO peripheral for STM32WLxx family
439   * @param  PinReset specifies the port bits to be reset
440   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15) or zero.
441   * @param  PinSet specifies the port bits to be set
442   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15) or zero.
443   * @note   Both PinReset and PinSet combinations shall not get any common bit, else
444   *         assert would be triggered.
445   * @note   At least one of the two parameters used to set or reset shall be different from zero.
446   * @retval None
447   */
HAL_GPIO_WriteMultipleStatePin(GPIO_TypeDef * GPIOx,uint16_t PinReset,uint16_t PinSet)448 void HAL_GPIO_WriteMultipleStatePin(GPIO_TypeDef *GPIOx, uint16_t PinReset, uint16_t PinSet)
449 {
450   uint32_t tmp;
451 
452   /* Check the parameters */
453   /* Make sure at least one parameter is different from zero and that there is no common pin */
454   assert_param(IS_GPIO_PIN((uint32_t)PinReset | (uint32_t)PinSet));
455   assert_param(IS_GPIO_COMMON_PIN(PinReset, PinSet));
456 
457   tmp = (((uint32_t)PinReset << 16) | PinSet);
458   GPIOx->BSRR = tmp;
459 }
460 
461 /**
462   * @brief  Toggle the specified GPIO pin.
463   * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WLxx family
464   * @param GPIO_Pin specifies the pin to be toggled.
465   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
466   * @retval None
467   */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)468 void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
469 {
470   uint32_t odr;
471 
472   /* Check the parameters */
473   assert_param(IS_GPIO_PIN(GPIO_Pin));
474 
475   /* get current Output Data Register value */
476   odr = GPIOx->ODR;
477 
478   /* Set selected pins that were at low level, and reset ones that were high */
479   GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
480 }
481 
482 /**
483   * @brief  Lock GPIO Pins configuration registers.
484   * @note   The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
485   *         GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
486   * @note   The configuration of the locked GPIO pins can no longer be modified
487   *         until the next reset.
488   * @param GPIOx where x can be (A..H) to select the GPIO peripheral for STM32WLxx family
489   * @param GPIO_Pin specifies the port bits to be locked.
490   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
491   * @retval None
492   */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)493 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
494 {
495   __IO uint32_t tmp = GPIO_LCKR_LCKK;
496 
497   /* Check the parameters */
498   assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
499   assert_param(IS_GPIO_PIN(GPIO_Pin));
500 
501   /* Apply lock key write sequence */
502   tmp |= GPIO_Pin;
503   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
504   GPIOx->LCKR = tmp;
505   /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
506   GPIOx->LCKR = GPIO_Pin;
507   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
508   GPIOx->LCKR = tmp;
509   /* Read LCKK register. This read is mandatory to complete key lock sequence */
510   tmp = GPIOx->LCKR;
511 
512   /* read again in order to confirm lock is active */
513   if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
514   {
515     return HAL_OK;
516   }
517   else
518   {
519     return HAL_ERROR;
520   }
521 }
522 
523 /**
524   * @brief  Handle EXTI interrupt request.
525   * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
526   * @retval None
527   */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)528 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
529 {
530   /* EXTI line interrupt detected */
531   if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
532   {
533     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
534     HAL_GPIO_EXTI_Callback(GPIO_Pin);
535   }
536 }
537 
538 /**
539   * @brief  EXTI line detection callback.
540   * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
541   * @retval None
542   */
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)543 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
544 {
545   /* Prevent unused argument(s) compilation warning */
546   UNUSED(GPIO_Pin);
547 
548   /* NOTE: This function should not be modified, when the callback is needed,
549            the HAL_GPIO_EXTI_Callback could be implemented in the user file
550    */
551 }
552 
553 /**
554   * @}
555   */
556 
557 
558 /**
559   * @}
560   */
561 
562 #endif /* HAL_GPIO_MODULE_ENABLED */
563 /**
564   * @}
565   */
566 
567 /**
568   * @}
569   */
570