2 ******************************************************************************
3 * @file stm32f3xx_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 (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
17 configured by software in several modes:
21 (++) Alternate function mode
22 (++) External interrupt/event lines
24 (+) During and just after reset, the alternate functions and external interrupt
25 lines are not active and the I/O ports are configured in input floating mode.
27 (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
30 (+) In Output or Alternate mode, each IO can be configured on open-drain or push-pull
31 type and the IO speed can be selected depending on the VDD value.
33 (+) The microcontroller IO pins are connected to onboard peripherals/modules through a
34 multiplexer that allows only one peripheral alternate function (AF) connected
35 to an IO pin at a time. In this way, there can be no conflict between peripherals
36 sharing the same IO pin.
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.
42 (+) The external interrupt/event controller consists of up to 23 edge detectors
43 (16 lines are connected to GPIO) for generating event/interrupt requests (each
44 input line can be independently configured to select the type (interrupt or event)
45 and the corresponding trigger event (rising or falling or both). Each line can
46 also be masked independently.
48 ##### How to use this driver #####
49 ==============================================================================
51 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
53 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
54 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
55 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
57 (++) In case of Output or alternate function mode selection: the speed is
58 configured through "Speed" member from GPIO_InitTypeDef structure.
59 (++) In alternate mode is selection, the alternate function connected to the IO
60 is configured through "Alternate" member from GPIO_InitTypeDef structure.
61 (++) Analog mode is required when a pin is to be used as ADC channel
63 (++) In case of external interrupt/event selection the "Mode" member from
64 GPIO_InitTypeDef structure select the type (interrupt or event) and
65 the corresponding trigger event (rising or falling or both).
67 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
68 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
71 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
73 (#) To set/reset the level of a pin configured in output mode use
74 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
76 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
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 PC15U, 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 PF0 and PF1, respectively, when the HSE oscillator is off.
88 The HSE has priority over the GPIO function.
91 ******************************************************************************
94 * <h2><center>© COPYRIGHT(c) 2016 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 "stm32f3xx_hal.h"
124 /** @addtogroup STM32F3xx_HAL_Driver
128 /** @defgroup GPIO GPIO
129 * @brief GPIO HAL module driver
133 #ifdef HAL_GPIO_MODULE_ENABLED
135 /* Private typedef -----------------------------------------------------------*/
136 /* Private defines -----------------------------------------------------------*/
137 /** @defgroup GPIO_Private_Defines GPIO Private Defines
140 #define GPIO_MODE (0x00000003U)
141 #define EXTI_MODE (0x10000000U)
142 #define GPIO_MODE_IT (0x00010000U)
143 #define GPIO_MODE_EVT (0x00020000U)
144 #define RISING_EDGE (0x00100000U)
145 #define FALLING_EDGE (0x00200000U)
146 #define GPIO_OUTPUT_TYPE (0x00000010U)
148 #define GPIO_NUMBER (16U)
153 /* Private macros ------------------------------------------------------------*/
154 /* Private macros ------------------------------------------------------------*/
155 /** @defgroup GPIO_Private_Macros GPIO Private Macros
161 /* Private variables ---------------------------------------------------------*/
162 /* Private function prototypes -----------------------------------------------*/
163 /* Exported functions --------------------------------------------------------*/
165 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
169 /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
170 * @brief Initialization and Configuration functions
173 ===============================================================================
174 ##### Initialization and de-initialization functions #####
175 ===============================================================================
182 * @brief Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
183 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family devices
184 * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
185 * the configuration information for the specified GPIO peripheral.
188 void HAL_GPIO_Init(GPIO_TypeDef
*GPIOx
, GPIO_InitTypeDef
*GPIO_Init
)
190 uint32_t position
= 0x00U
;
191 uint32_t iocurrent
= 0x00U
;
192 uint32_t temp
= 0x00U
;
194 /* Check the parameters */
195 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx
));
196 assert_param(IS_GPIO_PIN(GPIO_Init
->Pin
));
197 assert_param(IS_GPIO_MODE(GPIO_Init
->Mode
));
198 assert_param(IS_GPIO_PULL(GPIO_Init
->Pull
));
200 /* Configure the port pins */
201 while (((GPIO_Init
->Pin
) >> position
) != RESET
)
203 /* Get current io position */
204 iocurrent
= (GPIO_Init
->Pin
) & (1U << position
);
208 /*--------------------- GPIO Mode Configuration ------------------------*/
209 /* In case of Alternate function mode selection */
210 if((GPIO_Init
->Mode
== GPIO_MODE_AF_PP
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_OD
))
212 /* Check the Alternate function parameters */
213 assert_param(IS_GPIO_AF_INSTANCE(GPIOx
));
214 assert_param(IS_GPIO_AF(GPIO_Init
->Alternate
));
216 /* Configure Alternate function mapped with the current IO */
217 temp
= GPIOx
->AFR
[position
>> 3];
218 temp
&= ~(0xFU
<< ((uint32_t)(position
& 0x07U
) * 4U)) ;
219 temp
|= ((uint32_t)(GPIO_Init
->Alternate
) << (((uint32_t)position
& 0x07U
) * 4U));
220 GPIOx
->AFR
[position
>> 3] = temp
;
223 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
225 temp
&= ~(GPIO_MODER_MODER0
<< (position
* 2U));
226 temp
|= ((GPIO_Init
->Mode
& GPIO_MODE
) << (position
* 2U));
229 /* In case of Output or Alternate function mode selection */
230 if((GPIO_Init
->Mode
== GPIO_MODE_OUTPUT_PP
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_PP
) ||
231 (GPIO_Init
->Mode
== GPIO_MODE_OUTPUT_OD
) || (GPIO_Init
->Mode
== GPIO_MODE_AF_OD
))
233 /* Check the Speed parameter */
234 assert_param(IS_GPIO_SPEED(GPIO_Init
->Speed
));
235 /* Configure the IO Speed */
236 temp
= GPIOx
->OSPEEDR
;
237 temp
&= ~(GPIO_OSPEEDER_OSPEEDR0
<< (position
* 2U));
238 temp
|= (GPIO_Init
->Speed
<< (position
* 2U));
239 GPIOx
->OSPEEDR
= temp
;
241 /* Configure the IO Output Type */
242 temp
= GPIOx
->OTYPER
;
243 temp
&= ~(GPIO_OTYPER_OT_0
<< position
) ;
244 temp
|= (((GPIO_Init
->Mode
& GPIO_OUTPUT_TYPE
) >> 4U) << position
);
245 GPIOx
->OTYPER
= temp
;
248 /* Activate the Pull-up or Pull down resistor for the current IO */
250 temp
&= ~(GPIO_PUPDR_PUPDR0
<< (position
* 2U));
251 temp
|= ((GPIO_Init
->Pull
) << (position
* 2U));
254 /*--------------------- EXTI Mode Configuration ------------------------*/
255 /* Configure the External Interrupt or event for the current IO */
256 if((GPIO_Init
->Mode
& EXTI_MODE
) == EXTI_MODE
)
258 /* Enable SYSCFG Clock */
259 __HAL_RCC_SYSCFG_CLK_ENABLE();
261 temp
= SYSCFG
->EXTICR
[position
>> 2];
262 temp
&= ~((0x0FU
) << (4U * (position
& 0x03U
)));
263 temp
|= (GPIO_GET_INDEX(GPIOx
) << (4U * (position
& 0x03U
)));
264 SYSCFG
->EXTICR
[position
>> 2] = temp
;
266 /* Clear EXTI line configuration */
268 temp
&= ~((uint32_t)iocurrent
);
269 if((GPIO_Init
->Mode
& GPIO_MODE_IT
) == GPIO_MODE_IT
)
276 temp
&= ~((uint32_t)iocurrent
);
277 if((GPIO_Init
->Mode
& GPIO_MODE_EVT
) == GPIO_MODE_EVT
)
283 /* Clear Rising Falling edge configuration */
285 temp
&= ~((uint32_t)iocurrent
);
286 if((GPIO_Init
->Mode
& RISING_EDGE
) == RISING_EDGE
)
293 temp
&= ~((uint32_t)iocurrent
);
294 if((GPIO_Init
->Mode
& FALLING_EDGE
) == FALLING_EDGE
)
307 * @brief De-initialize the GPIOx peripheral registers to their default reset values.
308 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F30X device or STM32F37X device
309 * @param GPIO_Pin specifies the port bit to be written.
310 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
313 void HAL_GPIO_DeInit(GPIO_TypeDef
*GPIOx
, uint32_t GPIO_Pin
)
315 uint32_t position
= 0x00U
;
316 uint32_t iocurrent
= 0x00U
;
317 uint32_t tmp
= 0x00U
;
319 /* Check the parameters */
320 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx
));
321 assert_param(IS_GPIO_PIN(GPIO_Pin
));
323 /* Configure the port pins */
324 while ((GPIO_Pin
>> position
) != RESET
)
326 /* Get current io position */
327 iocurrent
= GPIO_Pin
& (1U << position
);
331 /*------------------------- GPIO Mode Configuration --------------------*/
332 /* Configure IO Direction in Input Floting Mode */
333 GPIOx
->MODER
&= ~(GPIO_MODER_MODER0
<< (position
* 2U));
335 /* Configure the default Alternate Function in current IO */
336 GPIOx
->AFR
[position
>> 3] &= ~(0xFU
<< ((uint32_t)(position
& 0x07U
) * 4U)) ;
338 /* Configure the default value for IO Speed */
339 GPIOx
->OSPEEDR
&= ~(GPIO_OSPEEDER_OSPEEDR0
<< (position
* 2U));
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
* 2U));
348 /*------------------------- EXTI Mode Configuration --------------------*/
349 /* Clear the External Interrupt or Event for the current IO */
351 tmp
= SYSCFG
->EXTICR
[position
>> 2];
352 tmp
&= ((0x0FU
) << (4U * (position
& 0x03U
)));
353 if(tmp
== (GPIO_GET_INDEX(GPIOx
) << (4U * (position
& 0x03U
))))
355 tmp
= (0x0FU
) << (4U * (position
& 0x03U
));
356 SYSCFG
->EXTICR
[position
>> 2] &= ~tmp
;
358 /* Clear EXTI line configuration */
359 EXTI
->IMR
&= ~((uint32_t)iocurrent
);
360 EXTI
->EMR
&= ~((uint32_t)iocurrent
);
362 /* Clear Rising Falling edge configuration */
363 EXTI
->RTSR
&= ~((uint32_t)iocurrent
);
364 EXTI
->FTSR
&= ~((uint32_t)iocurrent
);
376 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
377 * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
380 ===============================================================================
381 ##### IO operation functions #####
382 ===============================================================================
389 * @brief Read the specified input port pin.
390 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
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 Set or clear the selected data port bit.
416 * @note This function uses GPIOx_BSRR and GPIOx_BRR registers 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..F) to select the GPIO peripheral for STM32F3 family
421 * @param GPIO_Pin specifies the port bit to be written.
422 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
423 * @param PinState specifies the value to be written to the selected bit.
424 * This parameter can be one of the GPIO_PinState enum values:
425 * @arg GPIO_PIN_RESET: to clear the port pin
426 * @arg GPIO_PIN_SET: to set the port pin
429 void HAL_GPIO_WritePin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
, GPIO_PinState PinState
)
431 /* Check the parameters */
432 assert_param(IS_GPIO_PIN(GPIO_Pin
));
433 assert_param(IS_GPIO_PIN_ACTION(PinState
));
435 if(PinState
!= GPIO_PIN_RESET
)
437 GPIOx
->BSRR
= (uint32_t)GPIO_Pin
;
441 GPIOx
->BRR
= (uint32_t)GPIO_Pin
;
446 * @brief Toggle the specified GPIO pin.
447 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
448 * @param GPIO_Pin specifies the pin to be toggled.
451 void HAL_GPIO_TogglePin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
453 /* Check the parameters */
454 assert_param(IS_GPIO_PIN(GPIO_Pin
));
456 GPIOx
->ODR
^= GPIO_Pin
;
460 * @brief Lock GPIO Pins configuration registers.
461 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
462 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
463 * @note The configuration of the locked GPIO pins can no longer be modified
464 * until the next reset.
465 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
466 * @param GPIO_Pin specifies the port bits to be locked.
467 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
470 HAL_StatusTypeDef
HAL_GPIO_LockPin(GPIO_TypeDef
* GPIOx
, uint16_t GPIO_Pin
)
472 __IO
uint32_t tmp
= GPIO_LCKR_LCKK
;
474 /* Check the parameters */
475 assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx
));
476 assert_param(IS_GPIO_PIN(GPIO_Pin
));
478 /* Apply lock key write sequence */
480 /* Set LCKx bit(s): LCKK='1' + LCK[15U-0] */
482 /* Reset LCKx bit(s): LCKK='0' + LCK[15U-0] */
483 GPIOx
->LCKR
= GPIO_Pin
;
484 /* Set LCKx bit(s): LCKK='1' + LCK[15U-0] */
489 if((GPIOx
->LCKR
& GPIO_LCKR_LCKK
) != RESET
)
500 * @brief Handle EXTI interrupt request.
501 * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
504 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin
)
506 /* EXTI line interrupt detected */
507 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin
) != RESET
)
509 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin
);
510 HAL_GPIO_EXTI_Callback(GPIO_Pin
);
515 * @brief EXTI line detection callback.
516 * @param GPIO_Pin Specifies the port pin connected to corresponding EXTI line.
519 __weak
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin
)
521 /* 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****/