2 ******************************************************************************
3 * @file stm32f0xx_hal_gpio.c
4 * @author MCD Application Team
6 * @date 11-December-2014
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
27 During and just after reset, the alternate functions and external interrupt
28 lines are not active and the I/O ports are configured in input floating mode.
31 All GPIO pins have weak internal pull-up and pull-down resistors, which can be
35 In Output or Alternate mode, each IO can be configured on open-drain or push-pull
36 type and the IO speed can be selected depending on the VDD value.
39 The microcontroller IO pins are connected to onboard peripherals/modules through a
40 multiplexer that allows only one peripheral s alternate function (AF) connected
41 to an IO pin at a time. In this way, there can be no conflict between peripherals
42 sharing the same IO pin.
45 All ports have external interrupt/event capability. To use external interrupt
46 lines, the port must be configured in input mode. All available GPIO pins are
47 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
50 The external interrupt/event controller consists of up to 28 edge detectors
51 (depending on products 16 lines are connected to GPIO) for generating event/interrupt
52 requests (each input line can be independently configured to select the type
53 (interrupt or event) and the corresponding trigger event (rising or falling or both).
54 Each line can also be masked independently.
56 ##### How to use this driver #####
57 ==============================================================================
59 (#) Enable the GPIO AHB clock using the following function : __GPIOx_CLK_ENABLE().
61 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
62 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
63 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
65 (++) In case of Output or alternate function mode selection: the speed is
66 configured through "Speed" member from GPIO_InitTypeDef structure,
67 the speed is configurable: Low, Medium and High.
68 (++) If alternate mode is selected, the alternate function connected to the IO
69 is configured through "Alternate" member from GPIO_InitTypeDef structure
70 (++) Analog mode is required when a pin is to be used as ADC channel
72 (++) In case of external interrupt/event selection the "Mode" member from
73 GPIO_InitTypeDef structure select the type (interrupt or event) and
74 the corresponding trigger event (rising or falling or both).
76 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
77 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
80 (#) HAL_GPIO_DeInit allows to set register values to their reset value. It's also
81 recommended to use it to unconfigure pin which was used as an external interrupt
82 or in event mode. That's the only way to reset corresponding bit in EXTI & SYSCFG
85 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
87 (#) To set/reset the level of a pin configured in output mode use
88 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
90 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
92 (#) During and just after reset, the alternate functions are not
93 active and the GPIO pins are configured in input floating mode (except JTAG
96 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
97 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
98 priority over the GPIO function.
100 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
101 general purpose PD0 and PD1, respectively, when the HSE oscillator is off.
102 The HSE has priority over the GPIO function.
105 ******************************************************************************
108 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
110 * Redistribution and use in source and binary forms, with or without modification,
111 * are permitted provided that the following conditions are met:
112 * 1. Redistributions of source code must retain the above copyright notice,
113 * this list of conditions and the following disclaimer.
114 * 2. Redistributions in binary form must reproduce the above copyright notice,
115 * this list of conditions and the following disclaimer in the documentation
116 * and/or other materials provided with the distribution.
117 * 3. Neither the name of STMicroelectronics nor the names of its contributors
118 * may be used to endorse or promote products derived from this software
119 * without specific prior written permission.
121 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
122 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
123 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
124 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
125 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
126 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
127 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
128 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
129 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
130 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
132 ******************************************************************************
135 /* Includes ------------------------------------------------------------------*/
136 #include "stm32f0xx_hal.h"
138 /** @addtogroup STM32F0xx_HAL_Driver
142 /** @defgroup GPIO GPIO HAL module driver
143 * @brief GPIO HAL module driver
147 #ifdef HAL_GPIO_MODULE_ENABLED
149 /* Private typedef -----------------------------------------------------------*/
150 /* Private define ------------------------------------------------------------*/
151 /** @defgroup GPIO_Private_Define GPIO Private Define
154 #define GPIO_MODE ((uint32_t)0x00000003)
155 #define EXTI_MODE ((uint32_t)0x10000000)
156 #define GPIO_MODE_IT ((uint32_t)0x00010000)
157 #define GPIO_MODE_EVT ((uint32_t)0x00020000)
158 #define RISING_EDGE ((uint32_t)0x00100000)
159 #define FALLING_EDGE ((uint32_t)0x00200000)
160 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010)
162 #define GPIO_NUMBER ((uint32_t)16)
168 /* Private macro -------------------------------------------------------------*/
169 /* Private variables ---------------------------------------------------------*/
170 /* Private function prototypes -----------------------------------------------*/
171 /* Exporte functions ---------------------------------------------------------*/
173 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
177 /** @defgroup GPIO_Exported_Functions_Group1 Initialization/de-initialization functions
178 * @brief Initialization and Configuration functions
181 ===============================================================================
182 ##### Initialization and de-initialization functions #####
183 ===============================================================================
190 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
191 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F0 family
192 * @note GPIOD is only available on STM32F05xx, STM32F07xx and STM32F09xx
193 * @note GPIOE is only available on STM32F07xx and STM32F09xx
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)
200 uint32_t position = 0x00;
201 uint32_t iocurrent = 0x00;
202 uint32_t temp = 0x00;
204 /* Check the parameters */
205 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
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 while ((GPIO_Init->Pin) >> position)
213 /* Get current io position */
214 iocurrent = (GPIO_Init->Pin) & (1 << position);
218 /*--------------------- GPIO Mode Configuration ------------------------*/
219 /* In case of Alternate function mode selection */
220 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
222 /* Check the Alternate function parameters */
223 assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
224 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 |= (GPIO_GET_INDEX(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)
317 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
318 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F0 family
319 * @note GPIOD is only available on STM32F05xx, STM32F07xx and STM32F09xx
320 * @note GPIOE is only available on STM32F07xx and STM32F09xx
321 * @param GPIO_Pin: specifies the port bit to be written.
322 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
325 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
327 uint32_t position = 0x00;
328 uint32_t iocurrent = 0x00;
331 /* Check the parameters */
332 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
333 assert_param(IS_GPIO_PIN(GPIO_Pin));
335 /* Configure the port pins */
336 while (GPIO_Pin >> position)
338 /* Get current io position */
339 iocurrent = (GPIO_Pin) & (1 << position);
343 /*------------------------- GPIO Mode Configuration --------------------*/
344 /* Configure IO Direction in Input Floting Mode */
345 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2));
347 /* Configure the default Alternate Function in current IO */
348 GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
350 /* Configure the default value for IO Speed */
351 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
353 /* Configure the default value IO Output Type */
354 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
356 /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
357 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
359 /*------------------------- EXTI Mode Configuration --------------------*/
360 /* Clear the External Interrupt or Event for the current IO */
362 tmp = SYSCFG->EXTICR[position >> 2];
363 tmp &= (((uint32_t)0x0F) << (4 * (position & 0x03)));
364 if(tmp == (GPIO_GET_INDEX(GPIOx) << (4 * (position & 0x03))))
366 tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));
367 SYSCFG->EXTICR[position >> 2] &= ~tmp;
369 /* Clear EXTI line configuration */
370 EXTI->IMR &= ~((uint32_t)iocurrent);
371 EXTI->EMR &= ~((uint32_t)iocurrent);
373 /* Clear Rising Falling edge configuration */
374 EXTI->RTSR &= ~((uint32_t)iocurrent);
375 EXTI->FTSR &= ~((uint32_t)iocurrent);
387 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
388 * @brief GPIO Read, Write, Toggle, Lock and EXTI management functions.
391 ===============================================================================
392 ##### IO operation functions #####
393 ===============================================================================
400 * @brief Reads the specified input port pin.
401 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F0 family
402 * @note GPIOD is only available on STM32F05xx, STM32F07xx and STM32F09xx
403 * @note GPIOE is only available on STM32F07xx and STM32F09xx
404 * @param GPIO_Pin: specifies the port bit to read.
405 * This parameter can be GPIO_PIN_x where x can be (0..15).
406 * @retval The input port pin value.
408 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
410 GPIO_PinState bitstatus;
412 /* Check the parameters */
413 assert_param(IS_GPIO_PIN(GPIO_Pin));
415 if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
417 bitstatus = GPIO_PIN_SET;
421 bitstatus = GPIO_PIN_RESET;
427 * @brief Sets or clears the selected data port bit.
428 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
429 * accesses. In this way, there is no risk of an IRQ occurring between
430 * the read and the modify access.
431 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F0 family
432 * @note GPIOD is only available on STM32F05xx, STM32F07xx and STM32F09xx
433 * @note GPIOE is only available on STM32F07xx and STM32F09xx
434 * @param GPIO_Pin: specifies the port bit to be written.
435 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
436 * @param PinState: specifies the value to be written to the selected bit.
437 * This parameter can be one of the GPIO_PinState enum values:
438 * @arg GPIO_PIN_RESET: to clear the port pin
439 * @arg GPIO_PIN_SET: to set the port pin
442 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
444 /* Check the parameters */
445 assert_param(IS_GPIO_PIN(GPIO_Pin));
446 assert_param(IS_GPIO_PIN_ACTION(PinState));
448 if (PinState != GPIO_PIN_RESET)
450 GPIOx->BSRR = (uint32_t)GPIO_Pin;
454 GPIOx->BSRR = (uint32_t)GPIO_Pin << 16 ;
459 * @brief Toggles the specified GPIO pin
460 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F0 family
461 * @note GPIOD is only available on STM32F05xx, STM32F07xx and STM32F09xx
462 * @note GPIOE is only available on STM32F07xx and STM32F09xx
463 * @param GPIO_Pin: specifies the pins to be toggled.
466 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
468 /* Check the parameters */
469 assert_param(IS_GPIO_PIN(GPIO_Pin));
471 GPIOx->ODR ^= GPIO_Pin;
475 * @brief Locks GPIO Pins configuration registers.
476 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
477 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
478 * @note The configuration of the locked GPIO pins can no longer be modified
479 * until the next reset.
480 * @param GPIOx: where x can be (A..F) to select the GPIO peripheral for STM32F0 family
481 * @note GPIOD is only available on STM32F05xx, STM32F07xx and STM32F09xx
482 * @note GPIOE is only available on STM32F07xx and STM32F09xx
483 * @param GPIO_Pin: specifies the port bit to be locked.
484 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
487 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
489 __IO uint32_t tmp = GPIO_LCKR_LCKK;
491 /* Check the parameters */
492 assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
493 assert_param(IS_GPIO_PIN(GPIO_Pin));
495 /* Apply lock key write sequence */
497 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
499 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
500 GPIOx->LCKR = GPIO_Pin;
501 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
506 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
517 * @brief This function handles EXTI interrupt request.
518 * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
521 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
523 /* EXTI line interrupt detected */
524 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
526 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
527 HAL_GPIO_EXTI_Callback(GPIO_Pin);
532 * @brief EXTI line detection callbacks.
533 * @param GPIO_Pin: Specifies the port pin connected to corresponding EXTI line.
536 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
538 /* NOTE : This function Should not be modified, when the callback is needed,
539 the HAL_GPIO_EXTI_Callback could be implemented in the user file
552 #endif /* HAL_GPIO_MODULE_ENABLED */
561 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/