2 ******************************************************************************
3 * @file stm32f4xx_hal_gpio.c
4 * @author MCD Application Team
7 * @brief GPIO HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the General Purpose Input/Output (GPIO) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
14 ==============================================================================
15 ##### GPIO Peripheral features #####
16 ==============================================================================
18 Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
19 port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software
24 (+) Alternate function mode
25 (+) External interrupt/event lines
28 During and just after reset, the alternate functions and external interrupt
29 lines are not active and the I/O ports are configured in input floating mode.
32 All GPIO pins have weak internal pull-up and pull-down resistors, which can be
36 In Output or Alternate mode, each IO can be configured on open-drain or push-pull
37 type and the IO speed can be selected depending on the VDD value.
40 All ports have external interrupt/event capability. To use external interrupt
41 lines, the port must be configured in input mode. All available GPIO pins are
42 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
45 The external interrupt/event controller consists of up to 23 edge detectors
46 (16 lines are connected to GPIO) for generating event/interrupt requests (each
47 input line can be independently configured to select the type (interrupt or event)
48 and the corresponding trigger event (rising or falling or both). Each line can
49 also be masked independently.
51 ##### How to use this driver #####
52 ==============================================================================
54 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
56 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
57 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
58 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
60 (++) In case of Output or alternate function mode selection: the speed is
61 configured through "Speed" member from GPIO_InitTypeDef structure.
62 (++) In alternate mode is selection, the alternate function connected to the IO
63 is configured through "Alternate" member from GPIO_InitTypeDef structure.
64 (++) Analog mode is required when a pin is to be used as ADC channel
66 (++) In case of external interrupt/event selection the "Mode" member from
67 GPIO_InitTypeDef structure select the type (interrupt or event) and
68 the corresponding trigger event (rising or falling or both).
70 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
71 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
74 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
76 (#) To set/reset the level of a pin configured in output mode use
77 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
79 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
82 (#) During and just after reset, the alternate functions are not
83 active and the GPIO pins are configured in input floating mode (except JTAG
86 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
87 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
88 priority over the GPIO function.
90 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
91 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
92 The HSE has priority over the GPIO function.
95 ******************************************************************************
98 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
100 * Redistribution and use in source and binary forms, with or without modification,
101 * are permitted provided that the following conditions are met:
102 * 1. Redistributions of source code must retain the above copyright notice,
103 * this list of conditions and the following disclaimer.
104 * 2. Redistributions in binary form must reproduce the above copyright notice,
105 * this list of conditions and the following disclaimer in the documentation
106 * and/or other materials provided with the distribution.
107 * 3. Neither the name of STMicroelectronics nor the names of its contributors
108 * may be used to endorse or promote products derived from this software
109 * without specific prior written permission.
111 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
112 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
113 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
114 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
115 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
116 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
117 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
118 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
119 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
120 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
122 ******************************************************************************
125 /* Includes ------------------------------------------------------------------*/
126 #include "stm32f4xx_hal.h"
128 /** @addtogroup STM32F4xx_HAL_Driver
132 /** @defgroup GPIO GPIO
133 * @brief GPIO HAL module driver
137 #ifdef HAL_GPIO_MODULE_ENABLED
139 /* Private typedef -----------------------------------------------------------*/
140 /* Private define ------------------------------------------------------------*/
141 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
144 #define GPIO_MODE 0x00000003U
145 #define EXTI_MODE 0x10000000U
146 #define GPIO_MODE_IT 0x00010000U
147 #define GPIO_MODE_EVT 0x00020000U
148 #define RISING_EDGE 0x00100000U
149 #define FALLING_EDGE 0x00200000U
150 #define GPIO_OUTPUT_TYPE 0x00000010U
152 #define GPIO_NUMBER 16U
156 /* Private macro -------------------------------------------------------------*/
157 /* Private variables ---------------------------------------------------------*/
158 /* Private function prototypes -----------------------------------------------*/
159 /* Private functions ---------------------------------------------------------*/
160 /* Exported functions --------------------------------------------------------*/
161 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
165 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
166 * @brief Initialization and Configuration functions
169 ===============================================================================
170 ##### Initialization and de-initialization functions #####
171 ===============================================================================
173 This section provides functions allowing to initialize and de-initialize the GPIOs
182 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
183 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
184 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
185 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
186 * the configuration information for the specified GPIO peripheral.
189 void HAL_GPIO_Init(GPIO_TypeDef
*GPIOx
, GPIO_InitTypeDef
*GPIO_Init
)
192 uint32_t ioposition
= 0x00U
;
193 uint32_t iocurrent
= 0x00U
;
194 uint32_t temp
= 0x00U
;
196 /* Check the parameters */
197 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx
));
198 assert_param(IS_GPIO_PIN(GPIO_Init
->Pin
));
199 assert_param(IS_GPIO_MODE(GPIO_Init
->Mode
));
200 assert_param(IS_GPIO_PULL(GPIO_Init
->Pull
));
202 /* Configure the port pins */
203 for(position
= 0U; position
< GPIO_NUMBER
; position
++)
205 /* Get the IO position */
206 ioposition
= 0x01U
<< position
;
207 /* Get the current IO position */
208 iocurrent
= (uint32_t)(GPIO_Init
->Pin
) & ioposition
;
210 if(iocurrent
== ioposition
)
212 /*--------------------- GPIO Mode Configuration ------------------------*/
213 /* In case of Alternate function mode selection */
214 if((GPIO_Init
->Mode
== GPIO_MODE_AF_PP
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_OD
))
216 /* Check the Alternate function parameter */
217 assert_param(IS_GPIO_AF(GPIO_Init
->Alternate
));
218 /* Configure Alternate function mapped with the current IO */
219 temp
= GPIOx
->AFR
[position
>> 3U];
220 temp
&= ~(0xFU
<< ((uint32_t)(position
& 0x07U
) * 4U)) ;
221 temp
|= ((uint32_t)(GPIO_Init
->Alternate
) << (((uint32_t)position
& 0x07U
) * 4U));
222 GPIOx
->AFR
[position
>> 3U] = temp
;
225 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
227 temp
&= ~(GPIO_MODER_MODER0
<< (position
* 2U));
228 temp
|= ((GPIO_Init
->Mode
& GPIO_MODE
) << (position
* 2U));
231 /* In case of Output or Alternate function mode selection */
232 if((GPIO_Init
->Mode
== GPIO_MODE_OUTPUT_PP
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_PP
) ||
233 (GPIO_Init
->Mode
== GPIO_MODE_OUTPUT_OD
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_OD
))
235 /* Check the Speed parameter */
236 assert_param(IS_GPIO_SPEED(GPIO_Init
->Speed
));
237 /* Configure the IO Speed */
238 temp
= GPIOx
->OSPEEDR
;
239 temp
&= ~(GPIO_OSPEEDER_OSPEEDR0
<< (position
* 2U));
240 temp
|= (GPIO_Init
->Speed
<< (position
* 2U));
241 GPIOx
->OSPEEDR
= temp
;
243 /* Configure the IO Output Type */
244 temp
= GPIOx
->OTYPER
;
245 temp
&= ~(GPIO_OTYPER_OT_0
<< position
) ;
246 temp
|= (((GPIO_Init
->Mode
& GPIO_OUTPUT_TYPE
) >> 4U) << position
);
247 GPIOx
->OTYPER
= temp
;
250 /* Activate the Pull-up or Pull down resistor for the current IO */
252 temp
&= ~(GPIO_PUPDR_PUPDR0
<< (position
* 2U));
253 temp
|= ((GPIO_Init
->Pull
) << (position
* 2U));
256 /*--------------------- EXTI Mode Configuration ------------------------*/
257 /* Configure the External Interrupt or event for the current IO */
258 if((GPIO_Init
->Mode
& EXTI_MODE
) == EXTI_MODE
)
260 /* Enable SYSCFG Clock */
261 __HAL_RCC_SYSCFG_CLK_ENABLE();
263 temp
= SYSCFG
->EXTICR
[position
>> 2U];
264 temp
&= ~(0x0FU
<< (4U * (position
& 0x03U
)));
265 temp
|= ((uint32_t)(GPIO_GET_INDEX(GPIOx
)) << (4U * (position
& 0x03U
)));
266 SYSCFG
->EXTICR
[position
>> 2U] = temp
;
268 /* Clear EXTI line configuration */
270 temp
&= ~((uint32_t)iocurrent
);
271 if((GPIO_Init
->Mode
& GPIO_MODE_IT
) == GPIO_MODE_IT
)
278 temp
&= ~((uint32_t)iocurrent
);
279 if((GPIO_Init
->Mode
& GPIO_MODE_EVT
) == GPIO_MODE_EVT
)
285 /* Clear Rising Falling edge configuration */
287 temp
&= ~((uint32_t)iocurrent
);
288 if((GPIO_Init
->Mode
& RISING_EDGE
) == RISING_EDGE
)
295 temp
&= ~((uint32_t)iocurrent
);
296 if((GPIO_Init
->Mode
& FALLING_EDGE
) == FALLING_EDGE
)
307 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
308 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
309 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
310 * @param GPIO_Pin: specifies the port bit to be written.
311 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
314 void HAL_GPIO_DeInit(GPIO_TypeDef
*GPIOx
, uint32_t GPIO_Pin
)
317 uint32_t ioposition
= 0x00U
;
318 uint32_t iocurrent
= 0x00U
;
319 uint32_t tmp
= 0x00U
;
321 /* Check the parameters */
322 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx
));
324 /* Configure the port pins */
325 for(position
= 0U; position
< GPIO_NUMBER
; position
++)
327 /* Get the IO position */
328 ioposition
= 0x01U
<< position
;
329 /* Get the current IO position */
330 iocurrent
= (GPIO_Pin
) & ioposition
;
332 if(iocurrent
== ioposition
)
334 /*------------------------- GPIO Mode Configuration --------------------*/
335 /* Configure IO Direction in Input Floating Mode */
336 GPIOx
->MODER
&= ~(GPIO_MODER_MODER0
<< (position
* 2U));
338 /* Configure the default Alternate Function in current IO */
339 GPIOx
->AFR
[position
>> 3U] &= ~(0xFU
<< ((uint32_t)(position
& 0x07U
) * 4U)) ;
341 /* Configure the default value for IO Speed */
342 GPIOx
->OSPEEDR
&= ~(GPIO_OSPEEDER_OSPEEDR0
<< (position
* 2U));
344 /* Configure the default value IO Output Type */
345 GPIOx
->OTYPER
&= ~(GPIO_OTYPER_OT_0
<< position
) ;
347 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
348 GPIOx
->PUPDR
&= ~(GPIO_PUPDR_PUPDR0
<< (position
* 2U));
350 /*------------------------- EXTI Mode Configuration --------------------*/
351 tmp
= SYSCFG
->EXTICR
[position
>> 2U];
352 tmp
&= (0x0FU
<< (4U * (position
& 0x03U
)));
353 if(tmp
== ((uint32_t)(GPIO_GET_INDEX(GPIOx
)) << (4U * (position
& 0x03U
))))
355 /* Configure the External Interrupt or event for the current IO */
356 tmp
= 0x0FU
<< (4U * (position
& 0x03U
));
357 SYSCFG
->EXTICR
[position
>> 2U] &= ~tmp
;
359 /* Clear EXTI line configuration */
360 EXTI
->IMR
&= ~((uint32_t)iocurrent
);
361 EXTI
->EMR
&= ~((uint32_t)iocurrent
);
363 /* Clear Rising Falling edge configuration */
364 EXTI
->RTSR
&= ~((uint32_t)iocurrent
);
365 EXTI
->FTSR
&= ~((uint32_t)iocurrent
);
375 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
376 * @brief GPIO Read and Write
379 ===============================================================================
380 ##### IO operation functions #####
381 ===============================================================================
388 * @brief Reads the specified input port pin.
389 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
390 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
391 * @param GPIO_Pin: specifies the port bit to read.
392 * This parameter can be GPIO_PIN_x where x can be (0..15).
393 * @retval The input port pin value.
395 GPIO_PinState
HAL_GPIO_ReadPin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
397 GPIO_PinState bitstatus
;
399 /* Check the parameters */
400 assert_param(IS_GPIO_PIN(GPIO_Pin
));
402 if((GPIOx
->IDR
& GPIO_Pin
) != (uint32_t)GPIO_PIN_RESET
)
404 bitstatus
= GPIO_PIN_SET
;
408 bitstatus
= GPIO_PIN_RESET
;
414 * @brief Sets or clears the selected data port bit.
416 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
417 * accesses. In this way, there is no risk of an IRQ occurring between
418 * the read and the modify access.
420 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
421 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
422 * @param GPIO_Pin: specifies the port bit to be written.
423 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
424 * @param PinState: specifies the value to be written to the selected bit.
425 * This parameter can be one of the GPIO_PinState enum values:
426 * @arg GPIO_PIN_RESET: to clear the port pin
427 * @arg GPIO_PIN_SET: to set the port pin
430 void HAL_GPIO_WritePin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
, GPIO_PinState PinState
)
432 /* Check the parameters */
433 assert_param(IS_GPIO_PIN(GPIO_Pin
));
434 assert_param(IS_GPIO_PIN_ACTION(PinState
));
436 if(PinState
!= GPIO_PIN_RESET
)
438 GPIOx
->BSRR
= GPIO_Pin
;
442 GPIOx
->BSRR
= (uint32_t)GPIO_Pin
<< 16U;
447 * @brief Toggles the specified GPIO pins.
448 * @param GPIOx: Where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
449 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
450 * @param GPIO_Pin: Specifies the pins to be toggled.
453 void HAL_GPIO_TogglePin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
455 /* Check the parameters */
456 assert_param(IS_GPIO_PIN(GPIO_Pin
));
458 GPIOx
->ODR
^= GPIO_Pin
;
462 * @brief Locks GPIO Pins configuration registers.
463 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
464 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
465 * @note The configuration of the locked GPIO pins can no longer be modified
466 * until the next reset.
467 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F4 family
468 * @param GPIO_Pin: specifies the port bit to be locked.
469 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
472 HAL_StatusTypeDef
HAL_GPIO_LockPin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
474 __IO
uint32_t tmp
= GPIO_LCKR_LCKK
;
476 /* Check the parameters */
477 assert_param(IS_GPIO_PIN(GPIO_Pin
));
479 /* Apply lock key write sequence */
481 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
483 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
484 GPIOx
->LCKR
= GPIO_Pin
;
485 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
490 if((GPIOx
->LCKR
& GPIO_LCKR_LCKK
) != RESET
)
501 * @brief This function handles EXTI interrupt request.
502 * @param GPIO_Pin: Specifies the pins connected EXTI line
505 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin
)
507 /* EXTI line interrupt detected */
508 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin
) != RESET
)
510 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin
);
511 HAL_GPIO_EXTI_Callback(GPIO_Pin
);
516 * @brief EXTI line detection callbacks.
517 * @param GPIO_Pin: Specifies the pins connected EXTI line
520 __weak
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin
)
522 /* Prevent unused argument(s) compilation warning */
524 /* NOTE: This function Should not be modified, when the callback is needed,
525 the HAL_GPIO_EXTI_Callback could be implemented in the user file
538 #endif /* HAL_GPIO_MODULE_ENABLED */
547 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/