2 ******************************************************************************
3 * @file stm32f3xx_hal_gpio.c
4 * @author MCD Application Team
7 * @brief GPIO HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the General Purpose Input/Output (GPIO) peripheral:
11 * + Initialization/de-initialization functions
12 * + IO operation functions
15 ==============================================================================
16 ##### GPIO specific features #####
17 ==============================================================================
19 Each port bit of the general-purpose I/O (GPIO) ports can be individually
20 configured by software in several modes:
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 The microcontroller IO pins are connected to onboard peripherals/modules through a
41 multiplexer that allows only one peripheral
\92s alternate function (AF) connected
42 to an IO pin at a time. In this way, there can be no conflict between peripherals
43 sharing the same IO pin.
46 All ports have external interrupt/event capability. To use external interrupt
47 lines, the port must be configured in input mode. All available GPIO pins are
48 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
51 The external interrupt/event controller consists of up to 23 edge detectors
52 (16 lines are connected to GPIO) for generating event/interrupt requests (each
53 input line can be independently configured to select the type (interrupt or event)
54 and the corresponding trigger event (rising or falling or both). Each line can
55 also be masked independently.
57 ##### How to use this driver #####
58 ==============================================================================
60 (#) Enable the GPIO AHB clock using the following function : __GPIOx_CLK_ENABLE().
62 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
63 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
64 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
66 (++) In case of Output or alternate function mode selection: the speed is
67 configured through "Speed" member from GPIO_InitTypeDef structure,
68 the speed is configurable: 2 MHz, 10 MHz and 50 MHz.
69 (++) If alternate mode is selected, the alternate function connected to the IO
70 is configured through "Alternate" member from GPIO_InitTypeDef structure
71 (++) Analog mode is required when a pin is to be used as ADC channel
73 (++) In case of external interrupt/event selection the "Mode" member from
74 GPIO_InitTypeDef structure select the type (interrupt or event) and
75 the corresponding trigger event (rising or falling or both).
77 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
78 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
81 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
83 (#) To set/reset the level of a pin configured in output mode use
84 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
86 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
88 (#) During and just after reset, the alternate functions are not
89 active and the GPIO pins are configured in input floating mode (except JTAG
92 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
93 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
94 priority over the GPIO function.
96 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
97 general purpose Px0 and Px1, respectively, when the HSE oscillator is off.
98 The HSE has priority over the GPIO function.
101 ******************************************************************************
104 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
106 * Redistribution and use in source and binary forms, with or without modification,
107 * are permitted provided that the following conditions are met:
108 * 1. Redistributions of source code must retain the above copyright notice,
109 * this list of conditions and the following disclaimer.
110 * 2. Redistributions in binary form must reproduce the above copyright notice,
111 * this list of conditions and the following disclaimer in the documentation
112 * and/or other materials provided with the distribution.
113 * 3. Neither the name of STMicroelectronics nor the names of its contributors
114 * may be used to endorse or promote products derived from this software
115 * without specific prior written permission.
117 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
118 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
119 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
120 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
121 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
122 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
123 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
124 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
125 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
126 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
128 ******************************************************************************
131 /* Includes ------------------------------------------------------------------*/
132 #include "stm32f3xx_hal.h"
134 /** @addtogroup STM32F3xx_HAL_Driver
138 /** @defgroup GPIO GPIO HAL module driver
139 * @brief GPIO HAL module driver
143 #ifdef HAL_GPIO_MODULE_ENABLED
145 /* Private typedef -----------------------------------------------------------*/
146 /* Private define ------------------------------------------------------------*/
147 /* Private macro -------------------------------------------------------------*/
148 /** @defgroup GPIO_Private_Macros GPIO Private Macros
151 #define GET_GPIO_SOURCE(__GPIOx__) \
152 (((uint32_t)(__GPIOx__) == ((uint32_t)GPIOA_BASE))? 0U :\
153 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0400)))? 1U :\
154 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0800)))? 2U :\
155 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x0C00)))? 3U :\
156 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x1000)))? 4U :\
157 ((uint32_t)(__GPIOx__) == ((uint32_t)(GPIOA_BASE + 0x1400)))? 5U : 6U)
159 #define GPIO_MODE ((uint32_t)0x00000003)
160 #define EXTI_MODE ((uint32_t)0x10000000)
161 #define GPIO_MODE_IT ((uint32_t)0x00010000)
162 #define GPIO_MODE_EVT ((uint32_t)0x00020000)
163 #define RISING_EDGE ((uint32_t)0x00100000)
164 #define FALLING_EDGE ((uint32_t)0x00200000)
165 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010)
167 #define GPIO_NUMBER ((uint32_t)16)
172 /* Private variables ---------------------------------------------------------*/
173 /* Private function prototypes -----------------------------------------------*/
174 /* Exported functions ---------------------------------------------------------*/
175 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
179 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
180 * @brief Initialization and Configuration functions
183 ===============================================================================
184 ##### Initialization and de-initialization functions #####
185 ===============================================================================
192 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
193 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family devices
194 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
195 * the configuration information for the specified GPIO peripheral.
198 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
201 uint32_t ioposition = 0x00;
202 uint32_t iocurrent = 0x00;
203 uint32_t temp = 0x00;
205 /* Check the parameters */
206 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
207 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
208 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
210 /* Configure the port pins */
211 for (position = 0; position < GPIO_NUMBER; position++)
213 /* Get the IO position */
214 ioposition = ((uint32_t)0x01) << position;
215 /* Get the current IO position */
216 iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
218 if (iocurrent == ioposition)
220 /*--------------------- GPIO Mode Configuration ------------------------*/
221 /* In case of Alternate function mode selection */
222 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
224 /* Check the Alternate function parameter */
225 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
226 /* Configure Alternate function mapped with the current IO */
227 temp = GPIOx->AFR[position >> 3];
228 temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
229 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));
230 GPIOx->AFR[position >> 3] = temp;
233 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
235 temp &= ~(GPIO_MODER_MODER0 << (position * 2));
236 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
239 /* In case of Output or Alternate function mode selection */
240 if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
241 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
243 /* Check the Speed parameter */
244 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
245 /* Configure the IO Speed */
246 temp = GPIOx->OSPEEDR;
247 temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
248 temp |= (GPIO_Init->Speed << (position * 2));
249 GPIOx->OSPEEDR = temp;
251 /* Configure the IO Output Type */
252 temp = GPIOx->OTYPER;
253 temp &= ~(GPIO_OTYPER_OT_0 << position) ;
254 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
255 GPIOx->OTYPER = temp;
258 /* Activate the Pull-up or Pull down resistor for the current IO */
260 temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
261 temp |= ((GPIO_Init->Pull) << (position * 2));
264 /*--------------------- EXTI Mode Configuration ------------------------*/
265 /* Configure the External Interrupt or event for the current IO */
266 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
268 /* Enable SYSCFG Clock */
269 __SYSCFG_CLK_ENABLE();
271 temp = SYSCFG->EXTICR[position >> 2];
272 temp &= ~(((uint32_t)0x0F) << (4 * (position & 0x03)));
273 temp |= ((uint32_t)(GET_GPIO_SOURCE(GPIOx)) << (4 * (position & 0x03)));
274 SYSCFG->EXTICR[position >> 2] = temp;
276 /* Clear EXTI line configuration */
278 temp &= ~((uint32_t)iocurrent);
279 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
286 temp &= ~((uint32_t)iocurrent);
287 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
293 /* Clear Rising Falling edge configuration */
295 temp &= ~((uint32_t)iocurrent);
296 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
303 temp &= ~((uint32_t)iocurrent);
304 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
315 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
316 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F30X device or STM32F37X device
317 * @param GPIO_Pin: specifies the port bit to be written.
318 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
321 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
324 uint32_t ioposition = 0x00;
325 uint32_t iocurrent = 0x00;
328 /* Configure the port pins */
329 for (position = 0; position < GPIO_NUMBER; position++)
331 /* Get the IO position */
332 ioposition = ((uint32_t)0x01) << position;
333 /* Get the current IO position */
334 iocurrent = (GPIO_Pin) & ioposition;
336 if (iocurrent == ioposition)
338 /*------------------------- GPIO Mode Configuration --------------------*/
339 /* Configure IO Direction in Input Floting Mode */
340 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2));
342 /* Configure the default Alternate Function in current IO */
343 GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
345 /* Configure the default value for IO Speed */
346 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
348 /* Configure the default value IO Output Type */
349 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
351 /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
352 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
355 /*------------------------- EXTI Mode Configuration --------------------*/
356 /* Configure the External Interrupt or event for the current IO */
357 tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));
358 SYSCFG->EXTICR[position >> 2] &= ~tmp;
360 /* Clear EXTI line configuration */
361 EXTI->IMR &= ~((uint32_t)iocurrent);
362 EXTI->EMR &= ~((uint32_t)iocurrent);
364 /* Clear Rising Falling edge configuration */
365 EXTI->RTSR &= ~((uint32_t)iocurrent);
366 EXTI->FTSR &= ~((uint32_t)iocurrent);
375 /** @defgroup GPIO_Exported_Functions_Group2 Input and Output 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..F) to select the GPIO peripheral for STM32F3 family
390 * @param GPIO_Pin: specifies the port bit to read.
391 * This parameter can be GPIO_PIN_x where x can be (0..15).
392 * @retval The input port pin value.
394 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
396 GPIO_PinState bitstatus;
398 /* Check the parameters */
399 assert_param(IS_GPIO_PIN(GPIO_Pin));
401 if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
403 bitstatus = GPIO_PIN_SET;
407 bitstatus = GPIO_PIN_RESET;
413 * @brief Sets or clears the selected data port bit.
415 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
416 * accesses. In this way, there is no risk of an IRQ occurring between
417 * the read and the modify access.
419 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family
420 * @param GPIO_Pin: specifies the port bit to be written.
421 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
422 * @param PinState: specifies the value to be written to the selected bit.
423 * This parameter can be one of the GPIO_PinState enum values:
424 * @arg GPIO_PIN_RESET: to clear the port pin
425 * @arg GPIO_PIN_SET: to set the port pin
428 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
430 /* Check the parameters */
431 assert_param(IS_GPIO_PIN(GPIO_Pin));
432 assert_param(IS_GPIO_PIN_ACTION(PinState));
434 if (PinState != GPIO_PIN_RESET)
436 GPIOx->BSRRL = GPIO_Pin;
440 GPIOx->BSRRH = GPIO_Pin ;
445 * @brief Toggles the specified GPIO pin
446 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family
447 * @param GPIO_Pin: specifies the pins to be toggled.
450 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
452 /* Check the parameters */
453 assert_param(IS_GPIO_PIN(GPIO_Pin));
455 GPIOx->ODR ^= GPIO_Pin;
459 * @brief Locks GPIO Pins configuration registers.
460 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
461 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
462 * @note The configuration of the locked GPIO pins can no longer be modified
463 * until the next reset.
464 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F3 family
465 * @param GPIO_Pin: specifies the port bit to be locked.
466 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
469 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
471 __IO uint32_t tmp = GPIO_LCKR_LCKK;
473 /* Check the parameters */
474 assert_param(IS_GPIO_PIN(GPIO_Pin));
476 /* Apply lock key write sequence */
478 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
480 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
481 GPIOx->LCKR = GPIO_Pin;
482 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
487 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
498 * @brief This function handles EXTI interrupt request.
499 * @param GPIO_Pin: Specifies the pins connected EXTI line
502 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
504 /* EXTI line interrupt detected */
505 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
507 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
508 HAL_GPIO_EXTI_Callback(GPIO_Pin);
513 * @brief EXTI line detection callbacks.
514 * @param GPIO_Pin: Specifies the pins connected EXTI line
517 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
519 /* NOTE : This function Should not be modified, when the callback is needed,
520 the HAL_GPIO_EXTI_Callback could be implemented in the user file
533 #endif /* HAL_GPIO_MODULE_ENABLED */
542 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/