1 /**
2   ******************************************************************************
3   * @file    stm32mp1xx_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   Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
28   port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software
29   in several modes:
30   (+) Input mode
31   (+) Analog mode
32   (+) Output mode
33   (+) Alternate function mode
34   (+) External interrupt/event lines
35 
36   [..]
37   During and just after reset, the alternate functions and external interrupt
38         lines are not active and the I/O ports are configured in input floating mode.
39 
40     (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
41         activated or not.
42 
43   [..]
44   In Output or Alternate mode, each IO can be configured on open-drain or push-pull
45         type and the IO speed can be selected depending on the VDD value.
46 
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   [..]
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 "stm32mp1xx_hal.h"
108 
109 /** @addtogroup STM32MP1xx_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 #define GPIO_MODE             ((uint32_t)0x00000003)
126 #define EXTI_MODE             ((uint32_t)0x10000000)
127 #define GPIO_MODE_IT          ((uint32_t)0x00010000)
128 #define GPIO_MODE_EVT         ((uint32_t)0x00020000)
129 #define RISING_EDGE           ((uint32_t)0x00100000)
130 #define FALLING_EDGE          ((uint32_t)0x00200000)
131 #define GPIO_OUTPUT_TYPE      ((uint32_t)0x00000010)
132 
133 #define GPIO_NUMBER           ((uint32_t)16)
134 /**
135   * @}
136   */
137 /* Private macro -------------------------------------------------------------*/
138 /* Private variables ---------------------------------------------------------*/
139 /* Private function prototypes -----------------------------------------------*/
140 /* Private functions ---------------------------------------------------------*/
141 /* Exported functions --------------------------------------------------------*/
142 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
143   * @{
144   */
145 
146 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
147  *  @brief    Initialization and Configuration functions
148  *
149 @verbatim
150  ===============================================================================
151               ##### Initialization and de-initialization functions #####
152  ===============================================================================
153   [..]
154     This section provides functions allowing to initialize and de-initialize the GPIOs
155     to be ready for use.
156 
157 @endverbatim
158   * @{
159   */
160 
161 /**
162   * @brief  Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
163   * @param  GPIOx: where x can be (A..K) to select the GPIO peripheral.
164   * @param  GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
165   *         the configuration information for the specified GPIO peripheral.
166   * @retval None
167   */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)168 void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
169 {
170   uint32_t position;
171   uint32_t ioposition;
172   uint32_t iocurrent;
173   uint32_t temp;
174   EXTI_Core_TypeDef * EXTI_CurrentCPU;
175 
176 #if defined(CORE_CM4)
177   EXTI_CurrentCPU = EXTI_C2; /* EXTI for CM4 CPU */
178 #else
179   EXTI_CurrentCPU = EXTI_C1; /* EXTI for CA7 CPU */
180 #endif
181 
182   /* Check the parameters */
183   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
184   assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
185   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
186   assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
187 
188   /* Configure the port pins */
189   for(position = 0; position < GPIO_NUMBER; position++)
190   {
191     /* Get the IO position */
192     ioposition = ((uint32_t)0x01) << position;
193     /* Get the current IO position */
194     iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
195 
196     if(iocurrent == ioposition)
197     {
198       /*--------------------- GPIO Mode Configuration ------------------------*/
199       /* In case of Alternate function mode selection */
200       if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
201       {
202         /* Check the Alternate function parameter */
203         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
204 
205         /* Configure Alternate function mapped with the current IO */
206         temp = GPIOx->AFR[position >> 3];
207         temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
208         temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));
209         GPIOx->AFR[position >> 3] = temp;
210       }
211 
212       /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
213       temp = GPIOx->MODER;
214       temp &= ~(GPIO_MODER_MODER0 << (position * 2));
215       temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
216       GPIOx->MODER = temp;
217 
218       /* In case of Output or Alternate function mode selection */
219       if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
220          (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
221       {
222         /* Check the Speed parameter */
223         assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
224         /* Configure the IO Speed */
225         temp = GPIOx->OSPEEDR;
226         temp &= ~(GPIO_OSPEEDR_OSPEEDR0 << (position * 2));
227         temp |= (GPIO_Init->Speed << (position * 2));
228         GPIOx->OSPEEDR = temp;
229 
230         /* Configure the IO Output Type */
231         temp = GPIOx->OTYPER;
232         temp &= ~(GPIO_OTYPER_OT0 << position) ;
233         temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
234         GPIOx->OTYPER = temp;
235       }
236 
237       /* Activate the Pull-up or Pull down resistor for the current IO */
238       temp = GPIOx->PUPDR;
239       temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
240       temp |= ((GPIO_Init->Pull) << (position * 2));
241       GPIOx->PUPDR = temp;
242 
243       /*--------------------- EXTI Mode Configuration ------------------------*/
244       /* Configure the External Interrupt or event for the current IO */
245       if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
246       {
247         temp = EXTI->EXTICR[position >> 2U];
248         temp &= ~(0xFFU << (8U * (position & 0x03U)));
249         temp |= (GPIO_GET_INDEX(GPIOx) << (8U * (position & 0x03U)));
250         EXTI->EXTICR[position >> 2U] = temp;
251 
252         /* Clear EXTI line configuration */
253         temp = EXTI_CurrentCPU->IMR1;
254         temp &= ~((uint32_t)iocurrent);
255         if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
256         {
257           temp |= iocurrent;
258         }
259         EXTI_CurrentCPU->IMR1 = temp;
260 
261         temp = EXTI_CurrentCPU->EMR1;
262         temp &= ~((uint32_t)iocurrent);
263         if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
264         {
265           temp |= iocurrent;
266         }
267         EXTI_CurrentCPU->EMR1 = temp;
268 
269         /* Clear Rising Falling edge configuration */
270         temp = EXTI->RTSR1;
271         temp &= ~((uint32_t)iocurrent);
272         if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
273         {
274           temp |= iocurrent;
275         }
276         EXTI->RTSR1 = temp;
277 
278         temp = EXTI->FTSR1;
279         temp &= ~((uint32_t)iocurrent);
280         if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
281         {
282           temp |= iocurrent;
283         }
284         EXTI->FTSR1 = temp;
285       }
286     }
287   }
288 }
289 
290 /**
291   * @brief  De-initializes the GPIOx peripheral registers to their default reset values.
292   * @param  GPIOx: where x can be (A..Z) to select the GPIO peripheral.
293   * @param  GPIO_Pin: specifies the port bit to be written.
294   *          This parameter can be one of GPIO_PIN_x where x can be (0..15).
295   * @retval None
296   */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)297 void HAL_GPIO_DeInit(GPIO_TypeDef  *GPIOx, uint32_t GPIO_Pin)
298 {
299   uint32_t position;
300   uint32_t ioposition;
301   uint32_t iocurrent;
302   uint32_t tmp;
303   EXTI_Core_TypeDef * EXTI_CurrentCPU;
304 
305 #if defined(CORE_CM4)
306   EXTI_CurrentCPU = EXTI_C2; /* EXTI for CM4 CPU */
307 #else
308   EXTI_CurrentCPU = EXTI_C1; /* EXTI for CA7 CPU */
309 #endif
310 
311   /* Check the parameters */
312   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
313 
314   /* Configure the port pins */
315   for(position = 0; position < GPIO_NUMBER; position++)
316   {
317     /* Get the IO position */
318     ioposition = ((uint32_t)0x01) << position;
319     /* Get the current IO position */
320     iocurrent = (GPIO_Pin) & ioposition;
321 
322     if(iocurrent == ioposition)
323     {
324       /*------------------------- EXTI Mode Configuration --------------------*/
325       tmp = EXTI->EXTICR[position >> 2];
326       tmp &= (((uint32_t)0xFF) << (8 * (position & 0x03)));
327       if(tmp == ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (8 * (position & 0x03))))
328       {
329         /* Clear EXTI line configuration for Current CPU */
330         EXTI_CurrentCPU->IMR1 &= ~((uint32_t)iocurrent);
331         EXTI_CurrentCPU->EMR1 &= ~((uint32_t)iocurrent);
332 
333         /* Clear Rising Falling edge configuration */
334         EXTI->RTSR1 &= ~((uint32_t)iocurrent);
335         EXTI->FTSR1 &= ~((uint32_t)iocurrent);
336 
337         /* Configure the External Interrupt or event for the current IO */
338         tmp = ((uint32_t)0xFF) << (8 * (position & 0x03));
339         EXTI->EXTICR[position >> 2] &= ~tmp;
340       }
341 
342       /*------------------------- GPIO Mode Configuration --------------------*/
343       /* Configure IO in Analog Mode */
344       GPIOx->MODER |= (GPIO_MODER_MODER0 << (position * 2));
345 
346       /* Configure the default Alternate Function in current IO */
347       GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
348 
349       /* Configure the default value for IO Speed */
350       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEEDR0 << (position * 2));
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_PUPDR0 << (position * 2));
357     }
358   }
359 }
360 
361 /**
362   * @}
363   */
364 
365 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
366  *  @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
367  *
368 @verbatim
369  ===============================================================================
370                        ##### IO operation functions #####
371  ===============================================================================
372 
373 @endverbatim
374   * @{
375   */
376 
377 /**
378   * @brief  Reads the specified input port pin.
379   * @param  GPIOx: where x can be (A..K) to select the GPIO peripheral.
380   * @param  GPIO_Pin: specifies the port bit to read.
381   *         This parameter can be GPIO_PIN_x where x can be (0..15).
382   * @retval The input port pin value.
383   */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)384 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
385 {
386   GPIO_PinState bitstatus;
387 
388   /* Check the parameters */
389   assert_param(IS_GPIO_PIN(GPIO_Pin));
390 
391   if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
392   {
393     bitstatus = GPIO_PIN_SET;
394   }
395   else
396   {
397     bitstatus = GPIO_PIN_RESET;
398   }
399   return bitstatus;
400 }
401 
402 /**
403   * @brief  Sets or clears the selected data port bit.
404   *
405   * @note   This function uses GPIOx_BSRR register to allow atomic read/modify
406   *         accesses. In this way, there is no risk of an IRQ occurring between
407   *         the read and the modify access.
408   *
409   * @param  GPIOx: where x can be (A..K) to select the GPIO peripheral.
410   * @param  GPIO_Pin: specifies the port bit to be written.
411   *          This parameter can be one 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 = GPIO_Pin;
427   }
428   else
429   {
430     GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
431   }
432 }
433 
434 /**
435   * @brief  Toggles the specified GPIO pins.
436   * @param  GPIOx: Where x can be (A..K) to select the GPIO peripheral.
437   * @param  GPIO_Pin: Specifies the pins to be toggled.
438   * @retval None
439   */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)440 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
441 {
442   /* Check the parameters */
443   assert_param(IS_GPIO_PIN(GPIO_Pin));
444 
445   if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
446   {
447     GPIOx->BRR = (uint32_t)GPIO_Pin;
448   }
449   else
450   {
451     GPIOx->BSRR = (uint32_t)GPIO_Pin;
452   }
453 }
454 
455 /**
456   * @brief  Locks GPIO Pins configuration registers.
457   * @note   The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
458   *         GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
459   * @note   The configuration of the locked GPIO pins can no longer be modified
460   *         until the next reset.
461   * @param  GPIOx: where x can be (A..K) to select the GPIO peripheral
462   * @param  GPIO_Pin: specifies the port bit to be locked.
463   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
464   * @retval None
465   */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)466 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
467 {
468   __IO uint32_t tmp = GPIO_LCKR_LCKK;
469 
470   /* Check the parameters */
471   assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
472   assert_param(IS_GPIO_PIN(GPIO_Pin));
473 
474   /* Apply lock key write sequence */
475   tmp |= GPIO_Pin;
476   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
477   GPIOx->LCKR = tmp;
478   /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
479   GPIOx->LCKR = GPIO_Pin;
480   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
481   GPIOx->LCKR = tmp;
482   /* Read LCKK register. This read is mandatory to complete key lock sequence */
483   tmp = GPIOx->LCKR;
484 
485   /* Read again in order to confirm lock is active */
486   if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
487   {
488     return HAL_OK;
489   }
490   else
491   {
492     return HAL_ERROR;
493   }
494 }
495 
496 /**
497   * @brief  Handle EXTI interrupt request.
498   * @param  GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
499   * @retval None
500   */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)501 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
502 {
503   /* EXTI line interrupt detected */
504   if (__HAL_GPIO_EXTI_GET_RISING_IT(GPIO_Pin) != RESET)
505   {
506     __HAL_GPIO_EXTI_CLEAR_RISING_IT(GPIO_Pin);
507     HAL_GPIO_EXTI_Rising_Callback(GPIO_Pin);
508   }
509 
510   if (__HAL_GPIO_EXTI_GET_FALLING_IT(GPIO_Pin) != RESET)
511   {
512     __HAL_GPIO_EXTI_CLEAR_FALLING_IT(GPIO_Pin);
513     HAL_GPIO_EXTI_Falling_Callback(GPIO_Pin);
514   }
515 }
516 
517 /**
518   * @brief  EXTI line detection callback.
519   * @param  GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
520   * @retval None
521   */
HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)522 __weak void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
523 {
524   /* Prevent unused argument(s) compilation warning */
525   UNUSED(GPIO_Pin);
526 
527   /* NOTE: This function should not be modified, when the callback is needed,
528            the HAL_GPIO_EXTI_Rising_Callback could be implemented in the user file
529    */
530 }
531 
532 /**
533   * @brief  EXTI line detection callback.
534   * @param  GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
535   * @retval None
536   */
HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)537 __weak void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
538 {
539   /* Prevent unused argument(s) compilation warning */
540   UNUSED(GPIO_Pin);
541 
542   /* NOTE: This function should not be modified, when the callback is needed,
543            the HAL_GPIO_EXTI_Falling_Callback could be implemented in the user file
544    */
545 }
546 
547 /**
548   * @}
549   */
550 
551 
552 /**
553   * @}
554   */
555 
556 #endif /* HAL_GPIO_MODULE_ENABLED */
557 /**
558   * @}
559   */
560 
561 /**
562   * @}
563   */
564