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