1 /**
2   ******************************************************************************
3   * @file    stm32g4xx_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) 2019 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 44 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 PF0 and PF1, 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 "stm32g4xx_hal.h"
107 
108 /** @addtogroup STM32G4xx_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 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
127   * @{
128   */
129 #define GPIO_NUMBER           (16U)
130 /**
131   * @}
132   */
133 
134 /* Private macros ------------------------------------------------------------*/
135 /* Private variables ---------------------------------------------------------*/
136 /* Private function prototypes -----------------------------------------------*/
137 /* Exported functions --------------------------------------------------------*/
138 
139 /** @addtogroup GPIO_Exported_Functions
140   * @{
141   */
142 
143 /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
144   *  @brief    Initialization and Configuration functions
145   *
146 @verbatim
147  ===============================================================================
148               ##### Initialization and de-initialization functions #####
149  ===============================================================================
150 
151 @endverbatim
152   * @{
153   */
154 
155 /**
156   * @brief  Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
157   * @param  GPIOx where x can be (A..G) to select the GPIO peripheral for STM32G4xx family
158   * @param  GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
159   *         the configuration information for the specified GPIO peripheral.
160   * @retval None
161   */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)162 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
163 {
164   uint32_t position = 0x00U;
165   uint32_t iocurrent;
166   uint32_t temp;
167 
168   /* Check the parameters */
169   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
170   assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
171   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
172 
173   /* Configure the port pins */
174   while (((GPIO_Init->Pin) >> position) != 0U)
175   {
176     /* Get current io position */
177     iocurrent = (GPIO_Init->Pin) & (1UL << position);
178 
179     if (iocurrent != 0x00u)
180     {
181       /*--------------------- GPIO Mode Configuration ------------------------*/
182       /* In case of Output or Alternate function mode selection */
183       if(((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) ||
184          ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
185       {
186         /* Check the Speed parameter */
187         assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
188         /* Configure the IO Speed */
189         temp = GPIOx->OSPEEDR;
190         temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
191         temp |= (GPIO_Init->Speed << (position * 2U));
192         GPIOx->OSPEEDR = temp;
193 
194         /* Configure the IO Output Type */
195         temp = GPIOx->OTYPER;
196         temp &= ~(GPIO_OTYPER_OT0 << position) ;
197         temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
198         GPIOx->OTYPER = temp;
199       }
200 
201       if ((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
202       {
203         /* Check the Pull parameter */
204         assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
205 
206         /* Activate the Pull-up or Pull down resistor for the current IO */
207         temp = GPIOx->PUPDR;
208         temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
209         temp |= ((GPIO_Init->Pull) << (position * 2U));
210         GPIOx->PUPDR = temp;
211       }
212 
213       /* In case of Alternate function mode selection */
214       if ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
215       {
216         /* Check the Alternate function parameters */
217         assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
218         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
219 
220         /* Configure Alternate function mapped with the current IO */
221         temp = GPIOx->AFR[position >> 3U];
222         temp &= ~(0xFU << ((position & 0x07U) * 4U));
223         temp |= ((GPIO_Init->Alternate) << ((position & 0x07U) * 4U));
224         GPIOx->AFR[position >> 3U] = temp;
225       }
226 
227       /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
228       temp = GPIOx->MODER;
229       temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
230       temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
231       GPIOx->MODER = temp;
232 
233       /*--------------------- EXTI Mode Configuration ------------------------*/
234       /* Configure the External Interrupt or event for the current IO */
235       if ((GPIO_Init->Mode & EXTI_MODE) != 0x00u)
236       {
237         /* Enable SYSCFG Clock */
238         __HAL_RCC_SYSCFG_CLK_ENABLE();
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 Rising Falling edge configuration */
246         temp = EXTI->RTSR1;
247         temp &= ~(iocurrent);
248         if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00U)
249         {
250           temp |= iocurrent;
251         }
252         EXTI->RTSR1 = temp;
253 
254         temp = EXTI->FTSR1;
255         temp &= ~(iocurrent);
256         if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00U)
257         {
258           temp |= iocurrent;
259         }
260         EXTI->FTSR1 = temp;
261 
262         temp = EXTI->EMR1;
263         temp &= ~(iocurrent);
264         if ((GPIO_Init->Mode & EXTI_EVT) != 0x00U)
265         {
266           temp |= iocurrent;
267         }
268         EXTI->EMR1 = temp;
269 
270         /* Clear EXTI line configuration */
271         temp = EXTI->IMR1;
272         temp &= ~(iocurrent);
273         if ((GPIO_Init->Mode & EXTI_IT) != 0x00U)
274         {
275           temp |= iocurrent;
276         }
277         EXTI->IMR1 = 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..G) to select the GPIO peripheral for STM32G4xx family
288   * @param  GPIO_Pin specifies the port bit to be written.
289   *         This parameter can be any combination 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) != 0U)
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         /* Clear EXTI line configuration */
318         EXTI->IMR1 &= ~(iocurrent);
319         EXTI->EMR1 &= ~(iocurrent);
320 
321         /* Clear Rising Falling edge configuration */
322         EXTI->FTSR1 &= ~(iocurrent);
323         EXTI->RTSR1 &= ~(iocurrent);
324 
325         tmp = 0x0FUL << (4U * (position & 0x03U));
326         SYSCFG->EXTICR[position >> 2U] &= ~tmp;
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       /* Deactivate the Pull-up and Pull-down resistor for the current IO */
337       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
338 
339       /* Configure the default value IO Output Type */
340       GPIOx->OTYPER  &= ~(GPIO_OTYPER_OT0 << position);
341 
342       /* Configure the default value for IO Speed */
343       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (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..G) to select the GPIO peripheral for STM32G4xx family
369   * @param  GPIO_Pin specifies the port bit to read.
370   *         This parameter can be any combination of 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..G) to select the GPIO peripheral for STM32G4xx family
399   * @param  GPIO_Pin specifies the port bit to be written.
400   *         This parameter can be any combination 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..G) to select the GPIO peripheral for STM32G4xx family
426   * @param  GPIO_Pin specifies the pin to be toggled.
427   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
428   * @retval None
429   */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)430 void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
431 {
432   uint32_t odr;
433 
434   /* Check the parameters */
435   assert_param(IS_GPIO_PIN(GPIO_Pin));
436 
437   /* get current Output Data Register value */
438   odr = GPIOx->ODR;
439 
440   /* Set selected pins that were at low level, and reset ones that were high */
441   GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
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..G) to select the GPIO peripheral for STM32G4xx 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