1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_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   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   [..]
41   All GPIO pins have weak internal pull-up and pull-down resistors, which can be
42   activated or not.
43 
44   [..]
45   In Output or Alternate mode, each IO can be configured on open-drain or push-pull
46   type and the IO speed can be selected depending on the VDD value.
47 
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   [..]
54   The external interrupt/event controller consists of up to 23 edge detectors
55   (16 lines are connected to GPIO) for generating event/interrupt requests (each
56   input line can be independently configured to select the type (interrupt or event)
57   and the corresponding trigger event (rising or falling or both). Each line can
58   also be masked independently.
59 
60                      ##### How to use this driver #####
61   ==============================================================================
62   [..]
63     (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
64 
65     (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
66         (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
67         (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
68              structure.
69         (++) In case of Output or alternate function mode selection: the speed is
70              configured through "Speed" member from GPIO_InitTypeDef structure.
71         (++) In alternate mode is selection, the alternate function connected to the IO
72              is configured through "Alternate" member from GPIO_InitTypeDef structure.
73         (++) Analog mode is required when a pin is to be used as ADC channel
74              or DAC output.
75         (++) In case of external interrupt/event selection the "Mode" member from
76              GPIO_InitTypeDef structure select the type (interrupt or event) and
77              the corresponding trigger event (rising or falling or both).
78 
79     (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
80         mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
81         HAL_NVIC_EnableIRQ().
82 
83     (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
84 
85     (#) To set/reset the level of a pin configured in output mode use
86         HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
87 
88     (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
89 
90 
91     (#) During and just after reset, the alternate functions are not
92         active and the GPIO pins are configured in input floating mode (except JTAG
93         pins).
94 
95     (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
96         (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
97         priority over the GPIO function.
98 
99     (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
100         general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
101         The HSE has priority over the GPIO function.
102 
103   @endverbatim
104   ******************************************************************************
105   */
106 
107 /* Includes ------------------------------------------------------------------*/
108 #include "stm32f7xx_hal.h"
109 
110 /** @addtogroup STM32F7xx_HAL_Driver
111   * @{
112   */
113 
114 /** @defgroup GPIO GPIO
115   * @brief GPIO HAL module driver
116   * @{
117   */
118 
119 #ifdef HAL_GPIO_MODULE_ENABLED
120 
121 /* Private typedef -----------------------------------------------------------*/
122 /* Private define ------------------------------------------------------------*/
123 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
124   * @{
125   */
126 
127 #define GPIO_NUMBER           ((uint32_t)16U)
128 /**
129   * @}
130   */
131 /* Private macro -------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 /* Private functions ---------------------------------------------------------*/
135 /* Exported functions --------------------------------------------------------*/
136 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
137   * @{
138   */
139 
140 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
141  *  @brief    Initialization and Configuration functions
142  *
143 @verbatim
144  ===============================================================================
145               ##### Initialization and de-initialization functions #####
146  ===============================================================================
147   [..]
148     This section provides functions allowing to initialize and de-initialize the GPIOs
149     to be ready for use.
150 
151 @endverbatim
152   * @{
153   */
154 
155 /**
156   * @brief  Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
157   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral.
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 = 0x00;
165   uint32_t ioposition = 0x00;
166   uint32_t iocurrent = 0x00;
167   uint32_t temp = 0x00;
168 
169   /* Check the parameters */
170   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
171   assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
172   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
173 
174   /* Configure the port pins */
175   for (position = 0; position < GPIO_NUMBER; position++)
176   {
177     /* Get the IO position */
178     ioposition = ((uint32_t)0x01) << position;
179     /* Get the current IO position */
180     iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
181 
182     if (iocurrent == ioposition)
183     {
184       /*--------------------- GPIO Mode Configuration ------------------------*/
185       /* In case of Output or Alternate function mode selection */
186       if (((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
187       {
188         /* Check the Speed parameter */
189         assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
190         /* Configure the IO Speed */
191         temp = GPIOx->OSPEEDR;
192         temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
193         temp |= (GPIO_Init->Speed << (position * 2));
194         GPIOx->OSPEEDR = temp;
195 
196         /* Configure the IO Output Type */
197         temp = GPIOx->OTYPER;
198         temp &= ~(GPIO_OTYPER_OT_0 << 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_PUPDR0 << (position * 2));
211         temp |= ((GPIO_Init->Pull) << (position * 2));
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 parameter */
219         assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
220 
221         /* Configure Alternate function mapped with the current IO */
222         temp = GPIOx->AFR[position >> 3];
223         temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
224         temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));
225         GPIOx->AFR[position >> 3] = temp;
226       }
227 
228       /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
229       temp = GPIOx->MODER;
230       temp &= ~(GPIO_MODER_MODER0 << (position * 2));
231       temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
232       GPIOx->MODER = temp;
233 
234       /*--------------------- EXTI Mode Configuration ------------------------*/
235       /* Configure the External Interrupt or event for the current IO */
236       if ((GPIO_Init->Mode & EXTI_MODE) != 0x00u)
237       {
238         /* Enable SYSCFG Clock */
239         __HAL_RCC_SYSCFG_CLK_ENABLE();
240 
241         temp = SYSCFG->EXTICR[position >> 2];
242         temp &= ~(((uint32_t)0x0F) << (4 * (position & 0x03)));
243         temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03)));
244         SYSCFG->EXTICR[position >> 2] = temp;
245 
246         /* Clear Rising Falling edge configuration */
247         temp = EXTI->RTSR;
248         temp &= ~((uint32_t)iocurrent);
249         if ((GPIO_Init->Mode & TRIGGER_RISING) != 0x00u)
250         {
251           temp |= iocurrent;
252         }
253         EXTI->RTSR = temp;
254 
255         temp = EXTI->FTSR;
256         temp &= ~((uint32_t)iocurrent);
257         if ((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00u)
258         {
259           temp |= iocurrent;
260         }
261         EXTI->FTSR = temp;
262 
263         temp = EXTI->EMR;
264         temp &= ~((uint32_t)iocurrent);
265         if ((GPIO_Init->Mode & EXTI_EVT) != 0x00u)
266         {
267           temp |= iocurrent;
268         }
269         EXTI->EMR = temp;
270 
271         /* Clear EXTI line configuration */
272         temp = EXTI->IMR;
273         temp &= ~((uint32_t)iocurrent);
274         if ((GPIO_Init->Mode & EXTI_IT) != 0x00u)
275         {
276           temp |= iocurrent;
277         }
278         EXTI->IMR = temp;
279       }
280     }
281   }
282 }
283 
284 /**
285   * @brief  De-initializes the GPIOx peripheral registers to their default reset values.
286   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral.
287   * @param  GPIO_Pin specifies the port bit to be written.
288   *          This parameter can be one 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;
294   uint32_t ioposition = 0x00;
295   uint32_t iocurrent = 0x00;
296   uint32_t tmp = 0x00;
297 
298   /* Check the parameters */
299   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
300 
301   /* Configure the port pins */
302   for (position = 0; position < GPIO_NUMBER; position++)
303   {
304     /* Get the IO position */
305     ioposition = ((uint32_t)0x01) << position;
306     /* Get the current IO position */
307     iocurrent = (GPIO_Pin) & ioposition;
308 
309     if (iocurrent == ioposition)
310     {
311       /*------------------------- EXTI Mode Configuration --------------------*/
312       tmp = SYSCFG->EXTICR[position >> 2];
313       tmp &= (((uint32_t)0x0F) << (4 * (position & 0x03)));
314       if (tmp == ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03))))
315       {
316         /* Clear EXTI line configuration */
317         EXTI->IMR &= ~((uint32_t)iocurrent);
318         EXTI->EMR &= ~((uint32_t)iocurrent);
319 
320         /* Clear Rising Falling edge configuration */
321         EXTI->FTSR &= ~((uint32_t)iocurrent);
322         EXTI->RTSR &= ~((uint32_t)iocurrent);
323 
324         /* Configure the External Interrupt or event for the current IO */
325         tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));
326         SYSCFG->EXTICR[position >> 2] &= ~tmp;
327       }
328       /*------------------------- GPIO Mode Configuration --------------------*/
329       /* Configure IO Direction in Input Floating Mode */
330       GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2));
331 
332       /* Configure the default Alternate Function in current IO */
333       GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
334 
335       /* Deactivate the Pull-up and Pull-down resistor for the current IO */
336       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
337 
338       /* Configure the default value IO Output Type */
339       GPIOx->OTYPER  &= ~(GPIO_OTYPER_OT_0 << position) ;
340 
341       /* Configure the default value for IO Speed */
342       GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
343     }
344   }
345 }
346 
347 /**
348   * @}
349   */
350 
351 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
352  *  @brief   GPIO Read and Write
353  *
354 @verbatim
355  ===============================================================================
356                        ##### IO operation functions #####
357  ===============================================================================
358 
359 @endverbatim
360   * @{
361   */
362 
363 /**
364   * @brief  Reads the specified input port pin.
365   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral.
366   * @param  GPIO_Pin specifies the port bit to read.
367   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
368   * @retval The input port pin value.
369   */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)370 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
371 {
372   GPIO_PinState bitstatus;
373 
374   /* Check the parameters */
375   assert_param(IS_GPIO_PIN(GPIO_Pin));
376 
377   if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
378   {
379     bitstatus = GPIO_PIN_SET;
380   }
381   else
382   {
383     bitstatus = GPIO_PIN_RESET;
384   }
385   return bitstatus;
386 }
387 
388 /**
389   * @brief  Sets or clears the selected data port bit.
390   *
391   * @note   This function uses GPIOx_BSRR register to allow atomic read/modify
392   *         accesses. In this way, there is no risk of an IRQ occurring between
393   *         the read and the modify access.
394   *
395   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral.
396   * @param  GPIO_Pin specifies the port bit to be written.
397   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
398   * @param  PinState specifies the value to be written to the selected bit.
399   *          This parameter can be one of the GPIO_PinState enum values:
400   *            @arg GPIO_PIN_RESET: to clear the port pin
401   *            @arg GPIO_PIN_SET: to set the port pin
402   * @retval None
403   */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)404 void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
405 {
406   /* Check the parameters */
407   assert_param(IS_GPIO_PIN(GPIO_Pin));
408   assert_param(IS_GPIO_PIN_ACTION(PinState));
409 
410   if (PinState != GPIO_PIN_RESET)
411   {
412     GPIOx->BSRR = GPIO_Pin;
413   }
414   else
415   {
416     GPIOx->BSRR = (uint32_t)GPIO_Pin << 16;
417   }
418 }
419 
420 /**
421   * @brief  Toggles the specified GPIO pins.
422   * @param  GPIOx Where x can be (A..I) to select the GPIO peripheral.
423   * @param  GPIO_Pin Specifies the pins to be toggled.
424   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
425   * @retval None
426   */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)427 void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
428 {
429   uint32_t odr;
430 
431   /* Check the parameters */
432   assert_param(IS_GPIO_PIN(GPIO_Pin));
433 
434   /* get current Output Data Register value */
435   odr = GPIOx->ODR;
436 
437   /* Set selected pins that were at low level, and reset ones that were high */
438   GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
439 }
440 
441 /**
442   * @brief  Locks GPIO Pins configuration registers.
443   * @note   The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
444   *         GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
445   * @note   The configuration of the locked GPIO pins can no longer be modified
446   *         until the next reset.
447   * @param  GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F7 family
448   * @param  GPIO_Pin specifies the port bit to be locked.
449   *         This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
450   * @retval None
451   */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)452 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
453 {
454   __IO uint32_t tmp = GPIO_LCKR_LCKK;
455 
456   /* Check the parameters */
457   assert_param(IS_GPIO_PIN(GPIO_Pin));
458 
459   /* Apply lock key write sequence */
460   tmp |= GPIO_Pin;
461   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
462   GPIOx->LCKR = tmp;
463   /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
464   GPIOx->LCKR = GPIO_Pin;
465   /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
466   GPIOx->LCKR = tmp;
467   /* Read LCKR register. This read is mandatory to complete key lock sequence */
468   tmp = GPIOx->LCKR;
469 
470   /* Read again in order to confirm lock is active */
471   if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
472   {
473     return HAL_OK;
474   }
475   else
476   {
477     return HAL_ERROR;
478   }
479 }
480 
481 /**
482   * @brief  This function handles EXTI interrupt request.
483   * @param  GPIO_Pin Specifies the pins connected EXTI line
484   * @retval None
485   */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)486 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
487 {
488   /* EXTI line interrupt detected */
489   if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
490   {
491     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
492     HAL_GPIO_EXTI_Callback(GPIO_Pin);
493   }
494 }
495 
496 /**
497   * @brief  EXTI line detection callbacks.
498   * @param  GPIO_Pin Specifies the pins connected EXTI line
499   * @retval None
500   */
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)501 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
502 {
503   /* Prevent unused argument(s) compilation warning */
504   UNUSED(GPIO_Pin);
505 
506   /* NOTE: This function Should not be modified, when the callback is needed,
507            the HAL_GPIO_EXTI_Callback could be implemented in the user file
508    */
509 }
510 
511 /**
512   * @}
513   */
514 
515 
516 /**
517   * @}
518   */
519 
520 #endif /* HAL_GPIO_MODULE_ENABLED */
521 /**
522   * @}
523   */
524 
525 /**
526   * @}
527   */
528 
529