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 (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
19 configured by software in several modes:
23 (++) Alternate function mode
24 (++) 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.
29 (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
32 (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
33 type and the IO speed can be selected depending on the VDD value.
35 (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
36 multiplexer that allows only one peripheral alternate function (AF) connected
37 to an IO pin at a time. In this way, there can be no conflict between peripherals
38 sharing the same IO pin.
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.
44 (+) The external interrupt/event controller consists of up to 23 edge detectors
45 (16 lines are connected to GPIO) for generating event/interrupt requests (each
46 input line can be independently configured to select the type (interrupt or event)
47 and the corresponding trigger event (rising or falling or both). Each line can
48 also be masked independently.
50 ##### How to use this driver #####
51 ==============================================================================
53 (#) Enable the GPIO AHB clock using the following function: __GPIOx_CLK_ENABLE().
55 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
56 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
57 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
59 (++) In case of Output or alternate function mode selection: the speed is
60 configured through "Speed" member from GPIO_InitTypeDef structure.
61 (++) In alternate mode is selection, the alternate function connected to the IO
62 is configured through "Alternate" member from GPIO_InitTypeDef structure.
63 (++) Analog mode is required when a pin is to be used as ADC channel
65 (++) In case of external interrupt/event selection the "Mode" member from
66 GPIO_InitTypeDef structure select the type (interrupt or event) and
67 the corresponding trigger event (rising or falling or both).
69 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
70 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
73 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
75 (#) To set/reset the level of a pin configured in output mode use
76 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
78 (#) During and just after reset, the alternate functions are not
79 active and the GPIO pins are configured in input floating mode (except JTAG
82 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
83 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
84 priority over the GPIO function.
86 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
87 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
88 The HSE has priority over the GPIO function.
91 ******************************************************************************
94 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
96 * Redistribution and use in source and binary forms, with or without modification,
97 * are permitted provided that the following conditions are met:
98 * 1. Redistributions of source code must retain the above copyright notice,
99 * this list of conditions and the following disclaimer.
100 * 2. Redistributions in binary form must reproduce the above copyright notice,
101 * this list of conditions and the following disclaimer in the documentation
102 * and/or other materials provided with the distribution.
103 * 3. Neither the name of STMicroelectronics nor the names of its contributors
104 * may be used to endorse or promote products derived from this software
105 * without specific prior written permission.
107 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
108 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
109 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
110 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
111 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
112 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
113 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
114 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
115 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
116 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
118 ******************************************************************************
121 /* Includes ------------------------------------------------------------------*/
122 #include "stm32f4xx_hal.h"
124 /** @addtogroup STM32F4xx_HAL_Driver
129 * @brief GPIO HAL module driver
133 #ifdef HAL_GPIO_MODULE_ENABLED
135 /* Private typedef -----------------------------------------------------------*/
136 /* Private define ------------------------------------------------------------*/
137 /* Private macro -------------------------------------------------------------*/
138 #define GET_GPIO_SOURCE(__GPIOx__) \
139 (((uint32_t)(__GPIOx__) == ((uint32_t)GPIOA_BASE))? (uint32_t)0 :\
140 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0400)))? (uint32_t)1 :\
141 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0800)))? (uint32_t)2 :\
142 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0C00)))? (uint32_t)3 :\
143 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x1000)))? (uint32_t)4 :\
144 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x1400)))? (uint32_t)5 :\
145 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x1800)))? (uint32_t)6 :\
146 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x1C00)))? (uint32_t)7 :\
147 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x2000)))? (uint32_t)8 :\
148 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x2400)))? (uint32_t)9 : (uint32_t)10)
150 #define GPIO_MODE ((uint32_t)0x00000003)
151 #define EXTI_MODE ((uint32_t)0x10000000)
152 #define GPIO_MODE_IT ((uint32_t)0x00010000)
153 #define GPIO_MODE_EVT ((uint32_t)0x00020000)
154 #define RISING_EDGE ((uint32_t)0x00100000)
155 #define FALLING_EDGE ((uint32_t)0x00200000)
156 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010)
158 #define GPIO_NUMBER ((uint32_t)16)
159 /* Private variables ---------------------------------------------------------*/
160 /* Private function prototypes -----------------------------------------------*/
161 /* Private functions ---------------------------------------------------------*/
163 /** @defgroup GPIO_Private_Functions
167 /** @defgroup GPIO_Group1 Initialization and de-initialization functions
168 * @brief Initialization and Configuration functions
171 ===============================================================================
172 ##### Initialization and de-initialization functions #####
173 ===============================================================================
180 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
181 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
182 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
183 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
184 * the configuration information for the specified GPIO peripheral.
187 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
190 uint32_t ioposition = 0x00;
191 uint32_t iocurrent = 0x00;
192 uint32_t temp = 0x00;
194 /* Check the parameters */
195 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
196 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
197 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
199 /* Configure the port pins */
200 for(position = 0; position < GPIO_NUMBER; position++)
202 /* Get the IO position */
203 ioposition = ((uint32_t)0x01) << position;
204 /* Get the current IO position */
205 iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
207 if(iocurrent == ioposition)
209 /*--------------------- GPIO Mode Configuration ------------------------*/
210 /* In case of Alternate function mode selection */
211 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
213 /* Check the Alternate function parameter */
214 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
215 /* Configure Alternate function mapped with the current IO */
216 temp = GPIOx->AFR[position >> 3];
217 temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
218 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));
219 GPIOx->AFR[position >> 3] = temp;
222 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
224 temp &= ~(GPIO_MODER_MODER0 << (position * 2));
225 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
228 /* In case of Output or Alternate function mode selection */
229 if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
230 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
232 /* Check the Speed parameter */
233 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
234 /* Configure the IO Speed */
235 temp = GPIOx->OSPEEDR;
236 temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
237 temp |= (GPIO_Init->Speed << (position * 2));
238 GPIOx->OSPEEDR = temp;
240 /* Configure the IO Output Type */
241 temp = GPIOx->OTYPER;
242 temp &= ~(GPIO_OTYPER_OT_0 << position) ;
243 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
244 GPIOx->OTYPER = temp;
247 /* Activate the Pull-up or Pull down resistor for the current IO */
249 temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
250 temp |= ((GPIO_Init->Pull) << (position * 2));
253 /*--------------------- EXTI Mode Configuration ------------------------*/
254 /* Configure the External Interrupt or event for the current IO */
255 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
257 /* Enable SYSCFG Clock */
258 __SYSCFG_CLK_ENABLE();
260 temp = SYSCFG->EXTICR[position >> 2];
261 temp &= ~(((uint32_t)0x0F) << (4 * (position & 0x03)));
262 temp |= ((uint32_t)(GET_GPIO_SOURCE(GPIOx)) << (4 * (position & 0x03)));
263 SYSCFG->EXTICR[position >> 2] = temp;
265 /* Clear EXTI line configuration */
267 temp &= ~((uint32_t)iocurrent);
268 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
275 temp &= ~((uint32_t)iocurrent);
276 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
282 /* Clear Rising Falling edge configuration */
284 temp &= ~((uint32_t)iocurrent);
285 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
292 temp &= ~((uint32_t)iocurrent);
293 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
304 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
305 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
306 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
307 * @param GPIO_Pin: specifies the port bit to be written.
308 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
311 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
314 uint32_t ioposition = 0x00;
315 uint32_t iocurrent = 0x00;
318 /* Configure the port pins */
319 for(position = 0; position < GPIO_NUMBER; position++)
321 /* Get the IO position */
322 ioposition = ((uint32_t)0x01) << position;
323 /* Get the current IO position */
324 iocurrent = (GPIO_Pin) & ioposition;
326 if(iocurrent == ioposition)
328 /*------------------------- GPIO Mode Configuration --------------------*/
329 /* Configure IO Direction in Input Floting Mode */
330 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2));
332 /* Configure the default Alternate Function in current IO */
333 GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
335 /* Configure the default value for IO Speed */
336 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
338 /* Configure the default value IO Output Type */
339 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
341 /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
342 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
344 /*------------------------- EXTI Mode Configuration --------------------*/
345 /* Configure the External Interrupt or event for the current IO */
346 tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));
347 SYSCFG->EXTICR[position >> 2] &= ~tmp;
349 /* Clear EXTI line configuration */
350 EXTI->IMR &= ~((uint32_t)iocurrent);
351 EXTI->EMR &= ~((uint32_t)iocurrent);
353 /* Clear Rising Falling edge configuration */
354 EXTI->RTSR &= ~((uint32_t)iocurrent);
355 EXTI->FTSR &= ~((uint32_t)iocurrent);
364 /** @defgroup GPIO_Group2 IO operation functions
365 * @brief GPIO Read and Write
368 ===============================================================================
369 ##### IO operation functions #####
370 ===============================================================================
377 * @brief Reads the specified input port pin.
378 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
379 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
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.
384 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
386 GPIO_PinState bitstatus;
388 /* Check the parameters */
389 assert_param(IS_GPIO_PIN(GPIO_Pin));
391 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
393 bitstatus = GPIO_PIN_SET;
397 bitstatus = GPIO_PIN_RESET;
403 * @brief Sets or clears the selected data port bit.
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.
409 * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
410 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
411 * @param GPIO_Pin: specifies the port bit to be written.
412 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
413 * @param PinState: specifies the value to be written to the selected bit.
414 * This parameter can be one of the GPIO_PinState enum values:
415 * @arg GPIO_PIN_RESET: to clear the port pin
416 * @arg GPIO_PIN_SET: to set the port pin
419 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
421 /* Check the parameters */
422 assert_param(IS_GPIO_PIN(GPIO_Pin));
423 assert_param(IS_GPIO_PIN_ACTION(PinState));
425 if(PinState != GPIO_PIN_RESET)
427 GPIOx->BSRRL = GPIO_Pin;
431 GPIOx->BSRRH = GPIO_Pin ;
436 * @brief Toggles the specified GPIO pins.
437 * @param GPIOx: Where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
438 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
439 * @param GPIO_Pin: Specifies the pins to be toggled.
442 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
444 /* Check the parameters */
445 assert_param(IS_GPIO_PIN(GPIO_Pin));
447 GPIOx->ODR ^= GPIO_Pin;
451 * @brief Locks GPIO Pins configuration registers.
452 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
453 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
454 * @note The configuration of the locked GPIO pins can no longer be modified
455 * until the next reset.
456 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F4 family
457 * @param GPIO_Pin: specifies the port bit to be locked.
458 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
461 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
463 __IO uint32_t tmp = GPIO_LCKR_LCKK;
465 /* Check the parameters */
466 assert_param(IS_GPIO_PIN(GPIO_Pin));
468 /* Apply lock key write sequence */
470 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
472 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
473 GPIOx->LCKR = GPIO_Pin;
474 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
479 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
490 * @brief This function handles EXTI interrupt request.
491 * @param GPIO_Pin: Specifies the pins connected EXTI line
494 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
496 /* EXTI line interrupt detected */
497 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
499 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
500 HAL_GPIO_EXTI_Callback(GPIO_Pin);
505 * @brief EXTI line detection callbacks.
506 * @param GPIO_Pin: Specifies the pins connected EXTI line
509 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
511 /* NOTE: This function Should not be modified, when the callback is needed,
512 the HAL_GPIO_EXTI_Callback could be implemented in the user file
525 #endif /* HAL_GPIO_MODULE_ENABLED */
534 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/