2 ******************************************************************************
3 * @file stm32l0xx_hal_gpio.c
4 * @author MCD Application Team
6 * @date 06-February-2015
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 28 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 IOPORT clock using the following function: __HAL_RCC_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 (#) HAL_GPIO_DeInit allows to set register values to their reset value. This function
74 is also to be used when unconfiguring pin which was used as an external interrupt
75 or in event mode. That is the only way to reset the corresponding bit in
76 EXTI & SYSCFG registers.
78 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
80 (#) To set/reset the level of a pin configured in output mode use
81 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
83 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
85 (#) During and just after reset, the alternate functions are not
86 active and the GPIO pins are configured in input floating mode (except JTAG
89 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
90 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
91 priority over the GPIO function.
93 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
94 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
95 The HSE has priority over the GPIO function.
98 ******************************************************************************
101 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
103 * Redistribution and use in source and binary forms, with or without modification,
104 * are permitted provided that the following conditions are met:
105 * 1. Redistributions of source code must retain the above copyright notice,
106 * this list of conditions and the following disclaimer.
107 * 2. Redistributions in binary form must reproduce the above copyright notice,
108 * this list of conditions and the following disclaimer in the documentation
109 * and/or other materials provided with the distribution.
110 * 3. Neither the name of STMicroelectronics nor the names of its contributors
111 * may be used to endorse or promote products derived from this software
112 * without specific prior written permission.
114 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
115 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
116 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
117 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
118 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
119 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
120 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
121 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
122 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
123 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125 ******************************************************************************
128 /* Includes ------------------------------------------------------------------*/
129 #include "stm32l0xx_hal.h"
131 /** @addtogroup STM32L0xx_HAL_Driver
136 * @brief GPIO HAL module driver
140 #ifdef HAL_GPIO_MODULE_ENABLED
142 /* Private typedef -----------------------------------------------------------*/
143 /* Private define ------------------------------------------------------------*/
144 /* Private macro -------------------------------------------------------------*/
147 #define GPIO_MODE ((uint32_t)0x00000003)
148 #define EXTI_MODE ((uint32_t)0x10000000)
149 #define GPIO_MODE_IT ((uint32_t)0x00010000)
150 #define GPIO_MODE_EVT ((uint32_t)0x00020000)
151 #define RISING_EDGE ((uint32_t)0x00100000)
152 #define FALLING_EDGE ((uint32_t)0x00200000)
153 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010)
155 #define GPIO_NUMBER ((uint32_t)16)
156 /* Private variables ---------------------------------------------------------*/
157 /* Private function prototypes -----------------------------------------------*/
158 /* Private functions ---------------------------------------------------------*/
160 /** @addtogroup GPIO_Exported_Functions
164 /** @addtogroup GPIO_Exported_Functions_Group1
165 * @brief Initialization and de-initialization functions
168 ===============================================================================
169 ##### Initialization and de-initialization functions #####
170 ===============================================================================
177 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
178 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0XX family devices.
179 * Note that GPIOE is not available on all devices.
180 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
181 * the configuration information for the specified GPIO peripheral.
184 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
186 uint32_t position = 0x00;
187 uint32_t ioposition = 0x00;
188 uint32_t iocurrent = 0x00;
189 uint32_t temp = 0x00;
191 /* Check the parameters */
192 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
193 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
194 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,(GPIO_Init->Pin)));
196 /* Configure the port pins */
197 for(position = 0; position < GPIO_NUMBER; position++)
199 /* Get the IO position */
200 ioposition = ((uint32_t)0x01) << position;
201 /* Get the current IO position */
202 iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
204 if(iocurrent == ioposition)
206 /*--------------------- GPIO Mode Configuration ------------------------*/
207 /* In case of Alternate function mode selection */
208 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
210 /* Check the Alternate function parameter */
211 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
212 /* Check if the Alternate function is compliant with the GPIO in use */
213 assert_param(IS_GPIO_AF_AVAILABLE(GPIOx,(GPIO_Init->Alternate)));
214 /* Configure Alternate function mapped with the current IO */
215 temp = GPIOx->AFR[position >> 3];
216 temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
217 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4)) ;
218 GPIOx->AFR[position >> 3] = temp;
221 /* In case of Output or Alternate function mode selection */
222 if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
223 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
225 /* Check the Speed parameter */
226 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
227 /* Configure the IO Speed */
228 temp = GPIOx->OSPEEDR;
229 temp &= ~(GPIO_OSPEEDER_OSPEED0 << (position * 2));
230 temp |= (GPIO_Init->Speed << (position * 2));
231 GPIOx->OSPEEDR = temp;
233 /* Configure the IO Output Type */
235 temp &= ~(GPIO_OTYPER_OT_0 << position) ;
236 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
237 GPIOx->OTYPER = temp;
240 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
242 temp &= ~(GPIO_MODER_MODE0 << (position * 2));
243 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
246 /* Activate the Pull-up or Pull down resistor for the current IO */
248 temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2));
249 temp |= ((GPIO_Init->Pull) << (position * 2));
252 /*--------------------- EXTI Mode Configuration ------------------------*/
253 /* Configure the External Interrupt or event for the current IO */
254 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
256 /* Enable SYSCFG Clock */
257 __HAL_RCC_SYSCFG_CLK_ENABLE();
259 temp = SYSCFG->EXTICR[position >> 2];
260 temp &= ~(((uint32_t)0x0F) << (4 * (position & 0x03)));
261 temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03)));
262 SYSCFG->EXTICR[position >> 2] = temp;
264 /* Clear EXTI line configuration */
266 temp &= ~((uint32_t)iocurrent);
267 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
274 temp &= ~((uint32_t)iocurrent);
275 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
281 /* Clear Rising Falling edge configuration */
283 temp &= ~((uint32_t)iocurrent);
284 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
291 temp &= ~((uint32_t)iocurrent);
292 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
303 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
304 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0XX family devices.
305 * Note that GPIOE is not available on all devices.
306 * @param GPIO_Pin: specifies the port bit to be written.
307 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
308 * All port bits are not necessarily available on all GPIOs.
311 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
313 uint32_t position = 0x00;
314 uint32_t ioposition = 0x00;
315 uint32_t iocurrent = 0x00;
318 /* Check the parameters */
319 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
321 /* Configure the port pins */
322 for(position = 0; position < GPIO_NUMBER; position++)
324 /* Get the IO position */
325 ioposition = ((uint32_t)0x01) << position;
326 /* Get the current IO position */
327 iocurrent = (GPIO_Pin) & ioposition;
329 if(iocurrent == ioposition)
331 /*------------------------- GPIO Mode Configuration --------------------*/
332 /* Configure IO Direction in Input Floting Mode */
333 GPIOx->MODER &= ~(GPIO_MODER_MODE0 << (position * 2));
335 /* Configure the default Alternate Function in current IO */
336 GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
338 /* Configure the default value for IO Speed */
339 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEED0 << (position * 2));
341 /* Configure the default value IO Output Type */
342 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
344 /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
345 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2));
348 /*------------------------- EXTI Mode Configuration --------------------*/
349 /* Configure the External Interrupt or event for the current IO */
350 tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));
351 SYSCFG->EXTICR[position >> 2] &= ~tmp;
353 /* Clear EXTI line configuration */
354 EXTI->IMR &= ~((uint32_t)iocurrent);
355 EXTI->EMR &= ~((uint32_t)iocurrent);
357 /* Clear Rising Falling edge configuration */
358 EXTI->RTSR &= ~((uint32_t)iocurrent);
359 EXTI->FTSR &= ~((uint32_t)iocurrent);
368 /** @addtogroup GPIO_Exported_Functions_Group2
369 * @brief GPIO Read and Write
372 ===============================================================================
373 ##### IO operation functions #####
374 ===============================================================================
381 * @brief Reads the specified input port pin.
382 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
383 * Note that GPIOE is not available on all devices.
384 * @param GPIO_Pin: specifies the port bit to read.
385 * This parameter can be GPIO_PIN_x where x can be (0..15).
386 * All port bits are not necessarily available on all GPIOs.
387 * @retval The input port pin value.
389 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
391 GPIO_PinState bitstatus;
393 /* Check the parameters */
394 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
396 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
398 bitstatus = GPIO_PIN_SET;
402 bitstatus = GPIO_PIN_RESET;
408 * @brief Sets or clears the selected data port bit.
410 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
411 * accesses. In this way, there is no risk of an IRQ occurring between
412 * the read and the modify access.
414 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
415 * Note that GPIOE is not available on all devices.
416 * @param GPIO_Pin: specifies the port bit to be written.
417 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
418 * All port bits are not necessarily available on all GPIOs.
419 * @param PinState: specifies the value to be written to the selected bit.
420 * This parameter can be one of the GPIO_PinState enum values:
421 * GPIO_PIN_RESET: to clear the port pin
422 * GPIO_PIN_SET: to set the port pin
425 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
427 /* Check the parameters */
428 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
429 assert_param(IS_GPIO_PIN_ACTION(PinState));
431 if(PinState != GPIO_PIN_RESET)
433 GPIOx->BSRR = GPIO_Pin;
437 GPIOx->BRR = GPIO_Pin ;
442 * @brief Toggles the specified GPIO pins.
443 * @param GPIOx: Where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
444 * Note that GPIOE is not available on all devices.
445 * All port bits are not necessarily available on all GPIOs.
446 * @param GPIO_Pin: Specifies the pins to be toggled.
449 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
451 /* Check the parameters */
452 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
454 GPIOx->ODR ^= GPIO_Pin;
458 * @brief Locks GPIO Pins configuration registers.
459 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
460 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
461 * @note The configuration of the locked GPIO pins can no longer be modified
462 * until the next reset.
463 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family.
464 * Note that GPIOE is not available on all devices.
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).
467 * All port bits are not necessarily available on all GPIOs.
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_PIN_AVAILABLE(GPIOx,GPIO_Pin));
477 /* Apply lock key write sequence */
479 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
481 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
482 GPIOx->LCKR = GPIO_Pin;
483 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
488 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
498 * @brief This function handles EXTI interrupt request.
499 * @param GPIO_Pin: Specifies the pins connected to the 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 to the 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****/