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
12 ==============================================================================
13 ##### GPIO Peripheral features #####
14 ==============================================================================
16 Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
17 port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software
22 (+) Alternate function mode
23 (+) External interrupt/event lines
26 During and just after reset, the alternate functions and external interrupt
27 lines are not active and the I/O ports are configured in input floating mode.
30 All GPIO pins have weak internal pull-up and pull-down resistors, which can be
34 In Output or Alternate mode, each IO can be configured on open-drain or push-pull
35 type and the IO speed can be selected depending on the VDD value.
38 All ports have external interrupt/event capability. To use external interrupt
39 lines, the port must be configured in input mode. All available GPIO pins are
40 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
43 The external interrupt/event controller consists of up to 23 edge detectors
44 (16 lines are connected to GPIO) for generating event/interrupt requests (each
45 input line can be independently configured to select the type (interrupt or event)
46 and the corresponding trigger event (rising or falling or both). Each line can
47 also be masked independently.
49 ##### How to use this driver #####
50 ==============================================================================
52 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
54 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
55 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
56 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
58 (++) In case of Output or alternate function mode selection: the speed is
59 configured through "Speed" member from GPIO_InitTypeDef structure.
60 (++) In alternate mode is selection, the alternate function connected to the IO
61 is configured through "Alternate" member from GPIO_InitTypeDef structure.
62 (++) Analog mode is required when a pin is to be used as ADC channel
64 (++) In case of external interrupt/event selection the "Mode" member from
65 GPIO_InitTypeDef structure select the type (interrupt or event) and
66 the corresponding trigger event (rising or falling or both).
68 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
69 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
72 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
74 (#) To set/reset the level of a pin configured in output mode use
75 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
77 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
80 (#) During and just after reset, the alternate functions are not
81 active and the GPIO pins are configured in input floating mode (except JTAG
84 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
85 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
86 priority over the GPIO function.
88 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
89 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
90 The HSE has priority over the GPIO function.
93 ******************************************************************************
96 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
97 * All rights reserved.</center></h2>
99 * This software component is licensed by ST under BSD 3-Clause license,
100 * the "License"; You may not use this file except in compliance with the
101 * License. You may obtain a copy of the License at:
102 * opensource.org/licenses/BSD-3-Clause
104 ******************************************************************************
107 /* Includes ------------------------------------------------------------------*/
108 #include "stm32f7xx_hal.h"
110 /** @addtogroup STM32F7xx_HAL_Driver
114 /** @defgroup GPIO GPIO
115 * @brief GPIO HAL module driver
119 #ifdef HAL_GPIO_MODULE_ENABLED
121 /* Private typedef -----------------------------------------------------------*/
122 /* Private define ------------------------------------------------------------*/
123 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
126 #define GPIO_MODE ((uint32_t)0x00000003U)
127 #define EXTI_MODE ((uint32_t)0x10000000U)
128 #define GPIO_MODE_IT ((uint32_t)0x00010000U)
129 #define GPIO_MODE_EVT ((uint32_t)0x00020000U)
130 #define RISING_EDGE ((uint32_t)0x00100000U)
131 #define FALLING_EDGE ((uint32_t)0x00200000U)
132 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010U)
134 #define GPIO_NUMBER ((uint32_t)16U)
138 /* Private macro -------------------------------------------------------------*/
139 /* Private variables ---------------------------------------------------------*/
140 /* Private function prototypes -----------------------------------------------*/
141 /* Private functions ---------------------------------------------------------*/
142 /* Exported functions --------------------------------------------------------*/
143 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
147 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
148 * @brief Initialization and Configuration functions
151 ===============================================================================
152 ##### Initialization and de-initialization functions #####
153 ===============================================================================
155 This section provides functions allowing to initialize and de-initialize the GPIOs
163 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
164 * @param GPIOx where x can be (A..K) to select the GPIO peripheral.
165 * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
166 * the configuration information for the specified GPIO peripheral.
169 void HAL_GPIO_Init(GPIO_TypeDef
*GPIOx
, GPIO_InitTypeDef
*GPIO_Init
)
171 uint32_t position
= 0x00;
172 uint32_t ioposition
= 0x00;
173 uint32_t iocurrent
= 0x00;
174 uint32_t temp
= 0x00;
176 /* Check the parameters */
177 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx
));
178 assert_param(IS_GPIO_PIN(GPIO_Init
->Pin
));
179 assert_param(IS_GPIO_MODE(GPIO_Init
->Mode
));
180 assert_param(IS_GPIO_PULL(GPIO_Init
->Pull
));
182 /* Configure the port pins */
183 for(position
= 0; position
< GPIO_NUMBER
; position
++)
185 /* Get the IO position */
186 ioposition
= ((uint32_t)0x01) << position
;
187 /* Get the current IO position */
188 iocurrent
= (uint32_t)(GPIO_Init
->Pin
) & ioposition
;
190 if(iocurrent
== ioposition
)
192 /*--------------------- GPIO Mode Configuration ------------------------*/
193 /* In case of Alternate function mode selection */
194 if((GPIO_Init
->Mode
== GPIO_MODE_AF_PP
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_OD
))
196 /* Check the Alternate function parameter */
197 assert_param(IS_GPIO_AF(GPIO_Init
->Alternate
));
199 /* Configure Alternate function mapped with the current IO */
200 temp
= GPIOx
->AFR
[position
>> 3];
201 temp
&= ~((uint32_t)0xF << ((uint32_t)(position
& (uint32_t)0x07) * 4)) ;
202 temp
|= ((uint32_t)(GPIO_Init
->Alternate
) << (((uint32_t)position
& (uint32_t)0x07) * 4));
203 GPIOx
->AFR
[position
>> 3] = temp
;
206 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
208 temp
&= ~(GPIO_MODER_MODER0
<< (position
* 2));
209 temp
|= ((GPIO_Init
->Mode
& GPIO_MODE
) << (position
* 2));
212 /* In case of Output or Alternate function mode selection */
213 if((GPIO_Init
->Mode
== GPIO_MODE_OUTPUT_PP
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_PP
) ||
214 (GPIO_Init
->Mode
== GPIO_MODE_OUTPUT_OD
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_OD
))
216 /* Check the Speed parameter */
217 assert_param(IS_GPIO_SPEED(GPIO_Init
->Speed
));
218 /* Configure the IO Speed */
219 temp
= GPIOx
->OSPEEDR
;
220 temp
&= ~(GPIO_OSPEEDER_OSPEEDR0
<< (position
* 2));
221 temp
|= (GPIO_Init
->Speed
<< (position
* 2));
222 GPIOx
->OSPEEDR
= temp
;
224 /* Configure the IO Output Type */
225 temp
= GPIOx
->OTYPER
;
226 temp
&= ~(GPIO_OTYPER_OT_0
<< position
) ;
227 temp
|= (((GPIO_Init
->Mode
& GPIO_OUTPUT_TYPE
) >> 4) << position
);
228 GPIOx
->OTYPER
= temp
;
231 /* Activate the Pull-up or Pull down resistor for the current IO */
233 temp
&= ~(GPIO_PUPDR_PUPDR0
<< (position
* 2));
234 temp
|= ((GPIO_Init
->Pull
) << (position
* 2));
237 /*--------------------- EXTI Mode Configuration ------------------------*/
238 /* Configure the External Interrupt or event for the current IO */
239 if((GPIO_Init
->Mode
& EXTI_MODE
) == EXTI_MODE
)
241 /* Enable SYSCFG Clock */
242 __HAL_RCC_SYSCFG_CLK_ENABLE();
244 temp
= SYSCFG
->EXTICR
[position
>> 2];
245 temp
&= ~(((uint32_t)0x0F) << (4 * (position
& 0x03)));
246 temp
|= ((uint32_t)(GPIO_GET_INDEX(GPIOx
)) << (4 * (position
& 0x03)));
247 SYSCFG
->EXTICR
[position
>> 2] = temp
;
249 /* Clear EXTI line configuration */
251 temp
&= ~((uint32_t)iocurrent
);
252 if((GPIO_Init
->Mode
& GPIO_MODE_IT
) == GPIO_MODE_IT
)
259 temp
&= ~((uint32_t)iocurrent
);
260 if((GPIO_Init
->Mode
& GPIO_MODE_EVT
) == GPIO_MODE_EVT
)
266 /* Clear Rising Falling edge configuration */
268 temp
&= ~((uint32_t)iocurrent
);
269 if((GPIO_Init
->Mode
& RISING_EDGE
) == RISING_EDGE
)
276 temp
&= ~((uint32_t)iocurrent
);
277 if((GPIO_Init
->Mode
& FALLING_EDGE
) == FALLING_EDGE
)
288 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
289 * @param GPIOx where x can be (A..K) to select the GPIO peripheral.
290 * @param GPIO_Pin specifies the port bit to be written.
291 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
294 void HAL_GPIO_DeInit(GPIO_TypeDef
*GPIOx
, uint32_t GPIO_Pin
)
297 uint32_t ioposition
= 0x00;
298 uint32_t iocurrent
= 0x00;
301 /* Check the parameters */
302 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx
));
304 /* Configure the port pins */
305 for(position
= 0; position
< GPIO_NUMBER
; position
++)
307 /* Get the IO position */
308 ioposition
= ((uint32_t)0x01) << position
;
309 /* Get the current IO position */
310 iocurrent
= (GPIO_Pin
) & ioposition
;
312 if(iocurrent
== ioposition
)
314 /*------------------------- EXTI Mode Configuration --------------------*/
315 tmp
= SYSCFG
->EXTICR
[position
>> 2];
316 tmp
&= (((uint32_t)0x0F) << (4 * (position
& 0x03)));
317 if(tmp
== ((uint32_t)(GPIO_GET_INDEX(GPIOx
)) << (4 * (position
& 0x03))))
319 /* Configure the External Interrupt or event for the current IO */
320 tmp
= ((uint32_t)0x0F) << (4 * (position
& 0x03));
321 SYSCFG
->EXTICR
[position
>> 2] &= ~tmp
;
323 /* Clear EXTI line configuration */
324 EXTI
->IMR
&= ~((uint32_t)iocurrent
);
325 EXTI
->EMR
&= ~((uint32_t)iocurrent
);
327 /* Clear Rising Falling edge configuration */
328 EXTI
->RTSR
&= ~((uint32_t)iocurrent
);
329 EXTI
->FTSR
&= ~((uint32_t)iocurrent
);
331 /*------------------------- GPIO Mode Configuration --------------------*/
332 /* Configure IO Direction in Input Floating Mode */
333 GPIOx
->MODER
&= ~(GPIO_MODER_MODER0
<< (position
* 2));
335 /* Configure the default Alternate Function in current IO */
336 GPIOx
->AFR
[position
>> 3] &= ~((uint32_t)0xF << ((uint32_t)(position
& (uint32_t)0x07) * 4)) ;
338 /* Configure the default value for IO Speed */
339 GPIOx
->OSPEEDR
&= ~(GPIO_OSPEEDER_OSPEEDR0
<< (position
* 2));
341 /* Configure the default value IO Output Type */
342 GPIOx
->OTYPER
&= ~(GPIO_OTYPER_OT_0
<< position
) ;
344 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
345 GPIOx
->PUPDR
&= ~(GPIO_PUPDR_PUPDR0
<< (position
* 2));
354 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
355 * @brief GPIO Read and Write
358 ===============================================================================
359 ##### IO operation functions #####
360 ===============================================================================
367 * @brief Reads the specified input port pin.
368 * @param GPIOx where x can be (A..K) to select the GPIO peripheral.
369 * @param GPIO_Pin specifies the port bit to read.
370 * This parameter can be GPIO_PIN_x where x can be (0..15).
371 * @retval The input port pin value.
373 GPIO_PinState
HAL_GPIO_ReadPin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
375 GPIO_PinState bitstatus
;
377 /* Check the parameters */
378 assert_param(IS_GPIO_PIN(GPIO_Pin
));
380 if((GPIOx
->IDR
& GPIO_Pin
) != (uint32_t)GPIO_PIN_RESET
)
382 bitstatus
= GPIO_PIN_SET
;
386 bitstatus
= GPIO_PIN_RESET
;
392 * @brief Sets or clears the selected data port bit.
394 * @note This function uses GPIOx_BSRR register 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.
398 * @param GPIOx where x can be (A..K) to select the GPIO peripheral.
399 * @param GPIO_Pin specifies the port bit to be written.
400 * This parameter can be one 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
407 void HAL_GPIO_WritePin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
, GPIO_PinState PinState
)
409 /* Check the parameters */
410 assert_param(IS_GPIO_PIN(GPIO_Pin
));
411 assert_param(IS_GPIO_PIN_ACTION(PinState
));
413 if(PinState
!= GPIO_PIN_RESET
)
415 GPIOx
->BSRR
= GPIO_Pin
;
419 GPIOx
->BSRR
= (uint32_t)GPIO_Pin
<< 16;
424 * @brief Toggles the specified GPIO pins.
425 * @param GPIOx Where x can be (A..I) to select the GPIO peripheral.
426 * @param GPIO_Pin Specifies the pins to be toggled.
429 void HAL_GPIO_TogglePin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
431 /* Check the parameters */
432 assert_param(IS_GPIO_PIN(GPIO_Pin
));
434 if ((GPIOx
->ODR
& GPIO_Pin
) == GPIO_Pin
)
436 GPIOx
->BSRR
= (uint32_t)GPIO_Pin
<< GPIO_NUMBER
;
440 GPIOx
->BSRR
= GPIO_Pin
;
445 * @brief Locks 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..F) to select the GPIO peripheral for STM32F7 family
451 * @param GPIO_Pin specifies the port bit to be locked.
452 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
455 HAL_StatusTypeDef
HAL_GPIO_LockPin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
457 __IO
uint32_t tmp
= GPIO_LCKR_LCKK
;
459 /* Check the parameters */
460 assert_param(IS_GPIO_PIN(GPIO_Pin
));
462 /* Apply lock key write sequence */
464 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
466 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
467 GPIOx
->LCKR
= GPIO_Pin
;
468 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
473 if((GPIOx
->LCKR
& GPIO_LCKR_LCKK
) != RESET
)
484 * @brief This function handles EXTI interrupt request.
485 * @param GPIO_Pin Specifies the pins connected EXTI line
488 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin
)
490 /* EXTI line interrupt detected */
491 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin
) != RESET
)
493 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin
);
494 HAL_GPIO_EXTI_Callback(GPIO_Pin
);
499 * @brief EXTI line detection callbacks.
500 * @param GPIO_Pin Specifies the pins connected EXTI line
503 __weak
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin
)
505 /* Prevent unused argument(s) compilation warning */
508 /* NOTE: This function Should not be modified, when the callback is needed,
509 the HAL_GPIO_EXTI_Callback could be implemented in the user file
522 #endif /* HAL_GPIO_MODULE_ENABLED */
531 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/