1 /**
2 ******************************************************************************
3 * @file stm32l0xx_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
10 *
11 @verbatim
12 ==============================================================================
13 ##### GPIO Peripheral features #####
14 ==============================================================================
15 [..]
16 (+) Each port bit of the general-purpose I/O (GPIO) ports can be individually
17 configured by software in several modes:
18 (++) Input mode
19 (++) Analog mode
20 (++) Output mode
21 (++) Alternate function mode
22 (++) External interrupt/event lines
23
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.
26
27 (+) All GPIO pins have weak internal pull-up and pull-down resistors, which can be
28 activated or not.
29
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.
32
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.
37
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.
41
42 (+) The external interrupt/event controller consists of up to 28 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.
47
48 ##### How to use this driver #####
49 ==============================================================================
50 [..]
51 (#) Enable the GPIO IOPORT clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
52
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
56 structure.
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
62 or DAC output.
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).
66
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
69 HAL_NVIC_EnableIRQ().
70
71 (#) HAL_GPIO_DeInit allows to set register values to their reset value. This function
72 is also to be used when unconfiguring pin which was used as an external interrupt
73 or in event mode. That is the only way to reset the corresponding bit in
74 EXTI & SYSCFG registers.
75
76 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
77
78 (#) To set/reset the level of a pin configured in output mode use
79 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
80
81 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
82
83 (#) During and just after reset, the alternate functions are not
84 active and the GPIO pins are configured in input floating mode (except JTAG
85 pins).
86
87 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
88 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
89 priority over the GPIO function.
90
91 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
92 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
93 The HSE has priority over the GPIO function.
94
95 @endverbatim
96 ******************************************************************************
97 * @attention
98 *
99 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
100 *
101 * Redistribution and use in source and binary forms, with or without modification,
102 * are permitted provided that the following conditions are met:
103 * 1. Redistributions of source code must retain the above copyright notice,
104 * this list of conditions and the following disclaimer.
105 * 2. Redistributions in binary form must reproduce the above copyright notice,
106 * this list of conditions and the following disclaimer in the documentation
107 * and/or other materials provided with the distribution.
108 * 3. Neither the name of STMicroelectronics nor the names of its contributors
109 * may be used to endorse or promote products derived from this software
110 * without specific prior written permission.
111 *
112 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
113 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
114 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
115 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
116 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
117 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
118 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
119 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
120 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
121 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
122 *
123 ******************************************************************************
124 */
125
126 /* Includes ------------------------------------------------------------------*/
127 #include "stm32l0xx_hal.h"
128
129 /** @addtogroup STM32L0xx_HAL_Driver
130 * @{
131 */
132
133 #ifdef HAL_GPIO_MODULE_ENABLED
134
135 /** @addtogroup GPIO
136 * @brief GPIO HAL module driver
137 * @{
138 */
139
140 /** @addtogroup GPIO_Private
141 * @{
142 */
143 /* Private define ------------------------------------------------------------*/
144
145
146 #define GPIO_MODE ((uint32_t)0x00000003U)
147 #define EXTI_MODE ((uint32_t)0x10000000U)
148 #define GPIO_MODE_IT ((uint32_t)0x00010000U)
149 #define GPIO_MODE_EVT ((uint32_t)0x00020000U)
150 #define RISING_EDGE ((uint32_t)0x00100000U)
151 #define FALLING_EDGE ((uint32_t)0x00200000U)
152 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010U)
153
154 #define GPIO_NUMBER ((uint32_t)16U)
155
156 /**
157 * @}
158 */
159 /** @addtogroup GPIO_Exported_Functions
160 * @{
161 */
162
163 /** @addtogroup GPIO_Exported_Functions_Group1
164 * @brief Initialization and de-initialization functions
165 *
166 @verbatim
167 ===============================================================================
168 ##### Initialization and de-initialization functions #####
169 ===============================================================================
170
171 @endverbatim
172 * @{
173 */
174
175 /**
176 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
177 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0XX family devices.
178 * Note that GPIOE is not available on all devices.
179 * @param GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
180 * the configuration information for the specified GPIO peripheral.
181 * @retval None
182 */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)183 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
184 {
185 uint32_t position = 0x00U;
186 uint32_t iocurrent = 0x00U;
187 uint32_t temp = 0x00U;
188
189 /* Check the parameters */
190 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
191 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
192 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,(GPIO_Init->Pin)));
193
194 /* Configure the port pins */
195 while (((GPIO_Init->Pin) >> position) != 0)
196 {
197 /* Get the IO position */
198 iocurrent = (GPIO_Init->Pin) & (1U << position);
199
200 if(iocurrent)
201 {
202 /*--------------------- GPIO Mode Configuration ------------------------*/
203 /* In case of Alternate function mode selection */
204 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
205 {
206 /* Check if the Alternate function is compliant with the GPIO in use */
207 assert_param(IS_GPIO_AF_AVAILABLE(GPIOx,(GPIO_Init->Alternate)));
208 /* Configure Alternate function mapped with the current IO */
209 temp = GPIOx->AFR[position >> 3U];
210 temp &= ~((uint32_t)0xFU << ((uint32_t)(position & (uint32_t)0x07U) * 4U)) ;
211 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07U) * 4U)) ;
212 GPIOx->AFR[position >> 3U] = temp;
213 }
214
215 /* In case of Output or Alternate function mode selection */
216 if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
217 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
218 {
219 /* Check the Speed parameter */
220 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
221 /* Configure the IO Speed */
222 temp = GPIOx->OSPEEDR;
223 temp &= ~(GPIO_OSPEEDER_OSPEED0 << (position * 2U));
224 temp |= (GPIO_Init->Speed << (position * 2U));
225 GPIOx->OSPEEDR = temp;
226
227 /* Configure the IO Output Type */
228 temp= GPIOx->OTYPER;
229 temp &= ~(GPIO_OTYPER_OT_0 << position) ;
230 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
231 GPIOx->OTYPER = temp;
232 }
233
234 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
235 temp = GPIOx->MODER;
236 temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
237 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
238 GPIOx->MODER = temp;
239
240 /* Activate the Pull-up or Pull down resistor for the current IO */
241 temp = GPIOx->PUPDR;
242 temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
243 temp |= ((GPIO_Init->Pull) << (position * 2U));
244 GPIOx->PUPDR = temp;
245
246 /*--------------------- EXTI Mode Configuration ------------------------*/
247 /* Configure the External Interrupt or event for the current IO */
248 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
249 {
250 /* Enable SYSCFG Clock */
251 __HAL_RCC_SYSCFG_CLK_ENABLE();
252
253 temp = SYSCFG->EXTICR[position >> 2U];
254 CLEAR_BIT(temp, ((uint32_t)0x0FU) << (4U * (position & 0x03U)));
255 SET_BIT(temp, (GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03U)));
256 SYSCFG->EXTICR[position >> 2U] = temp;
257
258 /* Clear EXTI line configuration */
259 temp = EXTI->IMR;
260 temp &= ~((uint32_t)iocurrent);
261 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
262 {
263 temp |= iocurrent;
264 }
265 EXTI->IMR = temp;
266
267 temp = EXTI->EMR;
268 temp &= ~((uint32_t)iocurrent);
269 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
270 {
271 temp |= iocurrent;
272 }
273 EXTI->EMR = temp;
274
275 /* Clear Rising Falling edge configuration */
276 temp = EXTI->RTSR;
277 temp &= ~((uint32_t)iocurrent);
278 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
279 {
280 temp |= iocurrent;
281 }
282 EXTI->RTSR = temp;
283
284 temp = EXTI->FTSR;
285 temp &= ~((uint32_t)iocurrent);
286 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
287 {
288 temp |= iocurrent;
289 }
290 EXTI->FTSR = temp;
291 }
292 }
293 position++;
294 }
295 }
296
297 /**
298 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
299 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0XX family devices.
300 * Note that GPIOE is not available on all devices.
301 * @param GPIO_Pin: specifies the port bit to be written.
302 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
303 * All port bits are not necessarily available on all GPIOs.
304 * @retval None
305 */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)306 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
307 {
308 uint32_t position = 0x00U;
309 uint32_t iocurrent = 0x00U;
310 uint32_t tmp = 0x00U;
311
312 /* Check the parameters */
313 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
314
315 /* Configure the port pins */
316 while ((GPIO_Pin >> position) != 0)
317 {
318 /* Get the IO position */
319 iocurrent = (GPIO_Pin) & (1U << position);
320
321 if(iocurrent)
322 {
323 /*------------------------- GPIO Mode Configuration --------------------*/
324 /* Configure IO Direction in Input Floting Mode */
325 GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2U));
326
327 /* Configure the default Alternate Function in current IO */
328 GPIOx->AFR[position >> 3U] &= ~((uint32_t)0xFU << ((uint32_t)(position & (uint32_t)0x07U) * 4U)) ;
329
330 /* Configure the default value for IO Speed */
331 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEED0 << (position * 2U));
332
333 /* Configure the default value IO Output Type */
334 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
335
336 /* Deactivate the Pull-up oand Pull-down resistor for the current IO */
337 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
338
339 /*------------------------- EXTI Mode Configuration --------------------*/
340 /* Clear the External Interrupt or Event for the current IO */
341
342 tmp = SYSCFG->EXTICR[position >> 2U];
343 tmp &= (((uint32_t)0x0FU) << (4U * (position & 0x03U)));
344 if(tmp == (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U))))
345 {
346 tmp = ((uint32_t)0x0FU) << (4U * (position & 0x03U));
347 SYSCFG->EXTICR[position >> 2U] &= ~tmp;
348
349 /* Clear EXTI line configuration */
350 EXTI->IMR &= ~((uint32_t)iocurrent);
351 EXTI->EMR &= ~((uint32_t)iocurrent);
352
353 /* Clear Rising Falling edge configuration */
354 EXTI->RTSR &= ~((uint32_t)iocurrent);
355 EXTI->FTSR &= ~((uint32_t)iocurrent);
356 }
357 }
358 position++;
359 }
360 }
361
362 /**
363 * @}
364 */
365
366 /** @addtogroup GPIO_Exported_Functions_Group2
367 * @brief GPIO Read and Write
368 *
369 @verbatim
370 ===============================================================================
371 ##### IO operation functions #####
372 ===============================================================================
373
374 @endverbatim
375 * @{
376 */
377
378 /**
379 * @brief Reads the specified input port pin.
380 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
381 * Note that GPIOE is not available on all devices.
382 * @param GPIO_Pin: specifies the port bit to read.
383 * This parameter can be GPIO_PIN_x where x can be (0..15).
384 * All port bits are not necessarily available on all GPIOs.
385 * @retval The input port pin value.
386 */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)387 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
388 {
389 GPIO_PinState bitstatus;
390
391 /* Check the parameters */
392 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
393
394 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
395 {
396 bitstatus = GPIO_PIN_SET;
397 }
398 else
399 {
400 bitstatus = GPIO_PIN_RESET;
401 }
402 return bitstatus;
403 }
404
405 /**
406 * @brief Sets or clears the selected data port bit.
407 *
408 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
409 * accesses. In this way, there is no risk of an IRQ occurring between
410 * the read and the modify access.
411 *
412 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
413 * Note that GPIOE is not available on all devices.
414 * @param GPIO_Pin: specifies the port bit to be written.
415 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
416 * All port bits are not necessarily available on all GPIOs.
417 * @param PinState: specifies the value to be written to the selected bit.
418 * This parameter can be one of the GPIO_PinState enum values:
419 * GPIO_PIN_RESET: to clear the port pin
420 * GPIO_PIN_SET: to set the port pin
421 * @retval None
422 */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)423 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
424 {
425 /* Check the parameters */
426 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
427 assert_param(IS_GPIO_PIN_ACTION(PinState));
428
429 if(PinState != GPIO_PIN_RESET)
430 {
431 GPIOx->BSRR = GPIO_Pin;
432 }
433 else
434 {
435 GPIOx->BRR = GPIO_Pin ;
436 }
437 }
438
439 /**
440 * @brief Toggles the specified GPIO pins.
441 * @param GPIOx: Where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.
442 * Note that GPIOE is not available on all devices.
443 * All port bits are not necessarily available on all GPIOs.
444 * @param GPIO_Pin: Specifies the pins to be toggled.
445 * @retval None
446 */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)447 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
448 {
449 /* Check the parameters */
450 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
451
452 GPIOx->ODR ^= GPIO_Pin;
453 }
454
455 /**
456 * @brief Locks GPIO Pins configuration registers.
457 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
458 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
459 * @note The configuration of the locked GPIO pins can no longer be modified
460 * until the next reset.
461 * @param GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family.
462 * Note that GPIOE is not available on all devices.
463 * @param GPIO_Pin: specifies the port bit to be locked.
464 * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
465 * All port bits are not necessarily available on all GPIOs.
466 * @retval None
467 */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)468 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
469 {
470 __IO uint32_t tmp = GPIO_LCKR_LCKK;
471
472 /* Check the parameters */
473 assert_param(IS_GPIO_PIN_AVAILABLE(GPIOx,GPIO_Pin));
474
475 /* Apply lock key write sequence */
476 tmp |= GPIO_Pin;
477 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
478 GPIOx->LCKR = tmp;
479 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
480 GPIOx->LCKR = GPIO_Pin;
481 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
482 GPIOx->LCKR = tmp;
483 /* Read LCKK bit*/
484 tmp = GPIOx->LCKR;
485
486 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
487 {
488 return HAL_OK;
489 }
490 else
491 {
492 return HAL_ERROR;
493 }
494 }
495 /**
496 * @brief This function handles EXTI interrupt request.
497 * @param GPIO_Pin: Specifies the pins connected to the EXTI line.
498 * @retval None
499 */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)500 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
501 {
502 /* EXTI line interrupt detected */
503 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
504 {
505 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
506 HAL_GPIO_EXTI_Callback(GPIO_Pin);
507 }
508 }
509
510 /**
511 * @brief EXTI line detection callbacks.
512 * @param GPIO_Pin: Specifies the pins connected to the EXTI line.
513 * @retval None
514 */
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)515 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
516 {
517 /* Prevent unused argument(s) compilation warning */
518 UNUSED(GPIO_Pin);
519
520 /* NOTE: This function Should not be modified, when the callback is needed,
521 the HAL_GPIO_EXTI_Callback could be implemented in the user file
522 */
523 }
524
525 /**
526 * @}
527 */
528
529
530 /**
531 * @}
532 */
533
534 /**
535 * @}
536 */
537
538 #endif /* HAL_GPIO_MODULE_ENABLED */
539
540 /**
541 * @}
542 */
543
544 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
545
546