1 /**
2   ******************************************************************************
3   * @file    stm32h7rsxx_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) 2022 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 39 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 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 set the level of several pins and reset level of several other pins in
88         same cycle, use HAL_GPIO_WriteMultipleStatePin().
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 /* Includes ------------------------------------------------------------------*/
108 #include "stm32h7rsxx_hal.h"
109 
110 /** @addtogroup STM32H7RSxx_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
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 and M..P) to select the GPIO peripheral for STM32H7RSxx 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,const GPIO_InitTypeDef * GPIO_Init)164 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, const 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 
175   /* Configure the port pins */
176   while (((GPIO_Init->Pin) >> position) != 0x00u)
177   {
178     /* Get current io position */
179     iocurrent = (GPIO_Init->Pin) & (1uL << position);
180 
181     if (iocurrent != 0x00u)
182     {
183       /*--------------------- GPIO Mode Configuration ------------------------*/
184       /* In case of Output or Alternate function mode selection */
185       if (((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
186       {
187         /* Check the Speed parameter */
188         assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
189 
190         /* Configure the IO Speed */
191         temp = GPIOx->OSPEEDR;
192         temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * GPIO_OSPEEDR_OSPEED1_Pos));
193         temp |= (GPIO_Init->Speed << (position * GPIO_OSPEEDR_OSPEED1_Pos));
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       if ((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
204       {
205         /* Check the Pull parameter */
206         assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
207 
208         /* Activate the Pull-up or Pull down resistor for the current IO */
209         temp = GPIOx->PUPDR;
210         temp &= ~(GPIO_PUPDR_PUPD0 << (position * GPIO_PUPDR_PUPD1_Pos));
211         temp |= ((GPIO_Init->Pull) << (position * GPIO_PUPDR_PUPD1_Pos));
212         GPIOx->PUPDR = temp;
213       }
214 
215       /* In case of Alternate function mode selection */
216       if ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
217       {
218         /* Check the Alternate function parameters */
219         assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
220         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
221 
222         /* Configure Alternate function mapped with the current IO */
223         temp = GPIOx->AFR[position >> 3u];
224         temp &= ~(0xFu << ((position & 0x07u) * GPIO_AFRL_AFSEL1_Pos));
225         temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * GPIO_AFRL_AFSEL1_Pos));
226         GPIOx->AFR[position >> 3u] = temp;
227       }
228 
229       /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
230       temp = GPIOx->MODER;
231       temp &= ~(GPIO_MODER_MODE0 << (position * GPIO_MODER_MODE1_Pos));
232       temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * GPIO_MODER_MODE1_Pos));
233       GPIOx->MODER = temp;
234 
235       /*--------------------- EXTI Mode Configuration ------------------------*/
236       /* Configure the External Interrupt or event for the current IO */
237       if ((GPIO_Init->Mode & EXTI_MODE) != 0x00u)
238       {
239         temp = SBS->EXTICR[position >> 2u];
240         temp &= ~(0x0FuL << ((position & 0x03u) * SBS_EXTICR1_PC_EXTI1_Pos));
241         temp |= (GPIO_GET_INDEX(GPIOx) << ((position & 0x03u) * SBS_EXTICR1_PC_EXTI1_Pos));
242         SBS->EXTICR[position >> 2u] = temp;
243 
244         /* Clear Rising Falling edge configuration */
245         temp = EXTI->RTSR1;
246         temp &= ~(iocurrent);
247         if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00u)
248         {
249           temp |= iocurrent;
250         }
251         EXTI->RTSR1 = temp;
252 
253         temp = EXTI->FTSR1;
254         temp &= ~(iocurrent);
255         if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00u)
256         {
257           temp |= iocurrent;
258         }
259         EXTI->FTSR1 = temp;
260 
261         /* Clear EXTI line configuration */
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         temp = EXTI->IMR1;
271         temp &= ~(iocurrent);
272         if ((GPIO_Init->Mode & EXTI_IT) != 0x00u)
273         {
274           temp |= iocurrent;
275         }
276         EXTI->IMR1 = temp;
277       }
278     }
279 
280     position++;
281   }
282 }
283 
284 /**
285   * @brief  De-initialize the GPIOx peripheral registers to their default reset values.
286   * @param  GPIOx where x can be (A..H and M..P) to select the GPIO peripheral for STM32H7RSxx family
287   * @param  GPIO_Pin specifies the port bit to be written.
288   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
289   * @retval None
290   */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)291 void HAL_GPIO_DeInit(GPIO_TypeDef  *GPIOx, uint32_t GPIO_Pin)
292 {
293   uint32_t position = 0x00u;
294   uint32_t iocurrent;
295   uint32_t tmp;
296 
297   /* Check the parameters */
298   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
299   assert_param(IS_GPIO_PIN(GPIO_Pin));
300 
301   /* Configure the port pins */
302   while ((GPIO_Pin >> position) != 0x00u)
303   {
304     /* Get current io position */
305     iocurrent = (GPIO_Pin) & (1uL << position);
306 
307     if (iocurrent != 0x00u)
308     {
309       /*------------------------- EXTI Mode Configuration --------------------*/
310       /* Clear the External Interrupt or Event for the current IO */
311 
312       tmp = SBS->EXTICR[position >> 2u];
313       tmp &= (0x0FuL << ((position & 0x03u) * SBS_EXTICR1_PC_EXTI1_Pos));
314       if (tmp == (GPIO_GET_INDEX(GPIOx) << ((position & 0x03u) * SBS_EXTICR1_PC_EXTI1_Pos)))
315       {
316         /* Clear EXTI line configuration */
317         EXTI->IMR1 &= ~(iocurrent);
318         EXTI->EMR1 &= ~(iocurrent);
319 
320         /* Clear Rising Falling edge configuration */
321         EXTI->FTSR1 &= ~(iocurrent);
322         EXTI->RTSR1 &= ~(iocurrent);
323 
324         /* Clear EXTICR configuration */
325         tmp = 0x0FuL << ((position & 0x03u) * SBS_EXTICR1_PC_EXTI1_Pos);
326         SBS->EXTICR[position >> 2u] &= ~tmp;
327       }
328 
329       /*------------------------- GPIO Mode Configuration --------------------*/
330       /* Configure IO in Analog Mode */
331       GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * GPIO_MODER_MODE1_Pos));
332 
333       /* Configure the default Alternate Function in current IO */
334       GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * GPIO_AFRL_AFSEL1_Pos)) ;
335 
336       /* Configure the default value for IO Speed */
337       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * GPIO_OSPEEDR_OSPEED1_Pos));
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 * GPIO_PUPDR_PUPD1_Pos));
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..H and M..P) to select the GPIO peripheral for STM32H7RSxx 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(const GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)373 GPIO_PinState HAL_GPIO_ReadPin(const 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   * @note   This function uses GPIOx_BSRR and GPIOx_BRR registers to allow atomic read/modify
394   *         accesses. In this way, there is no risk of an IRQ occurring between
395   *         the read and the modify access.
396   * @param  GPIOx where x can be (A..H and M..P) to select the GPIO peripheral for STM32H7RSxx family
397   * @param  GPIO_Pin specifies the port bit to be written.
398   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
399   * @param  PinState specifies the value to be written to the selected bit.
400   *         This parameter can be one of the GPIO_PinState enum values:
401   *            @arg GPIO_PIN_RESET: to clear the port pin
402   *            @arg GPIO_PIN_SET: to set the port pin
403   * @retval None
404   */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)405 void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
406 {
407   /* Check the parameters */
408   assert_param(IS_GPIO_PIN(GPIO_Pin));
409   assert_param(IS_GPIO_PIN_ACTION(PinState));
410 
411   if (PinState != GPIO_PIN_RESET)
412   {
413     GPIOx->BSRR = (uint32_t)GPIO_Pin;
414   }
415   else
416   {
417     GPIOx->BRR = (uint32_t)GPIO_Pin;
418   }
419 }
420 
421 /**
422   * @brief  Set and clear several pins of a dedicated port in same cycle.
423   * @param  GPIOx where x can be (A..H and M..P) to select the GPIO peripheral for STM32H7RSxx family
424   * @param  PinReset specifies the port bits to be reset
425   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15) or zero.
426   * @param  PinSet specifies the port bits to be set
427   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15) or zero.
428   * @note   Both PinReset and PinSet combinations shall not get any common bit, else
429   *         assert would be triggered.
430   * @note   At least one of the two parameters used to set or reset shall be different from zero.
431   * @retval None
432   */
HAL_GPIO_WriteMultipleStatePin(GPIO_TypeDef * GPIOx,uint16_t PinReset,uint16_t PinSet)433 void HAL_GPIO_WriteMultipleStatePin(GPIO_TypeDef *GPIOx, uint16_t PinReset, uint16_t PinSet)
434 {
435   uint32_t tmp;
436 
437   /* Check the parameters */
438   /* Make sure at least one parameter is different from zero and that there is no common pin */
439   assert_param(IS_GPIO_PIN((uint32_t)PinReset | (uint32_t)PinSet));
440   assert_param(IS_GPIO_COMMON_PIN(PinReset, PinSet));
441 
442   tmp = (((uint32_t)PinReset << 16) | PinSet);
443   GPIOx->BSRR = tmp;
444 }
445 
446 /**
447   * @brief  Toggle the specified GPIO pin.
448   * @param  GPIOx where x can be (A..H and M..P) to select the GPIO peripheral for STM32H7RSxx family
449   * @param  GPIO_Pin specifies the pin to be toggled.
450   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
451   * @retval None
452   */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)453 void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
454 {
455   uint32_t odr;
456 
457   /* Check the parameters */
458   assert_param(IS_GPIO_PIN(GPIO_Pin));
459 
460   /* get current Output Data Register value */
461   odr = GPIOx->ODR;
462 
463   /* Set selected pins that were at low level, and reset ones that were high */
464   GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
465 }
466 
467 /**
468   * @brief  Lock GPIO Pins configuration registers.
469   * @note   The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
470   *         GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
471   * @note   The configuration of the locked GPIO pins can no longer be modified
472   *         until the next reset.
473   * @param  GPIOx where x can be (A..H and M..P) to select the GPIO peripheral for STM32H7RSxx family
474   * @param  GPIO_Pin specifies the port bits to be locked.
475   *         This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
476   * @retval None
477   */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)478 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
479 {
480   __IO uint32_t tmp = GPIO_LCKR_LCKK;
481 
482   /* Check the parameters */
483   assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
484   assert_param(IS_GPIO_PIN(GPIO_Pin));
485 
486   /* Apply lock key write sequence */
487   tmp |= GPIO_Pin;
488   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
489   GPIOx->LCKR = tmp;
490   /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
491   GPIOx->LCKR = GPIO_Pin;
492   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
493   GPIOx->LCKR = tmp;
494   /* Read LCKK register. This read is mandatory to complete key lock sequence */
495   tmp = GPIOx->LCKR;
496 
497   /* read again in order to confirm lock is active */
498   if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
499   {
500     return HAL_OK;
501   }
502   else
503   {
504     return HAL_ERROR;
505   }
506 }
507 
508 /**
509   * @brief  Handle EXTI interrupt request.
510   * @param  GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
511   * @retval None
512   */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)513 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
514 {
515   /* EXTI line interrupt detected */
516   if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0U)
517   {
518     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
519     HAL_GPIO_EXTI_Callback(GPIO_Pin);
520   }
521 }
522 
523 /**
524   * @brief  EXTI line detection callback.
525   * @param  GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
526   * @retval None
527   */
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)528 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
529 {
530   /* Prevent unused argument(s) compilation warning */
531   UNUSED(GPIO_Pin);
532 
533   /* NOTE: This function should not be modified, when the callback is needed,
534            the HAL_GPIO_EXTI_Callback could be implemented in the user file
535    */
536 }
537 
538 /**
539   * @}
540   */
541 
542 
543 /**
544   * @}
545   */
546 
547 #endif /* HAL_GPIO_MODULE_ENABLED */
548 /**
549   * @}
550   */
551 
552 /**
553   * @}
554   */
555