STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_gpio.c
Go to the documentation of this file.
1 
105 /* Includes ------------------------------------------------------------------*/
106 #include "stm32l4xx_hal.h"
107 
123 #ifdef HAL_GPIO_MODULE_ENABLED
124 
125 /* Private typedef -----------------------------------------------------------*/
126 /* Private defines -----------------------------------------------------------*/
130 #define GPIO_MODE (0x00000003u)
131 #define ANALOG_MODE (0x00000008u)
132 #define EXTI_MODE (0x10000000u)
133 #define GPIO_MODE_IT (0x00010000u)
134 #define GPIO_MODE_EVT (0x00020000u)
135 #define RISING_EDGE (0x00100000u)
136 #define FALLING_EDGE (0x00200000u)
137 #define GPIO_OUTPUT_TYPE (0x00000010u)
138 
139 #define GPIO_NUMBER (16u)
140 
144 /* Private macros ------------------------------------------------------------*/
145 /* Private variables ---------------------------------------------------------*/
146 /* Private function prototypes -----------------------------------------------*/
147 /* Exported functions --------------------------------------------------------*/
148 
172 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
173 {
174  uint32_t position = 0x00u;
175  uint32_t iocurrent;
176  uint32_t temp;
177 
178  /* Check the parameters */
179  assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
180  assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
181  assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
182  assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
183 
184  /* Configure the port pins */
185  while (((GPIO_Init->Pin) >> position) != 0x00u)
186  {
187  /* Get current io position */
188  iocurrent = (GPIO_Init->Pin) & (1uL << position);
189 
190  if (iocurrent != 0x00u)
191  {
192  /*--------------------- GPIO Mode Configuration ------------------------*/
193  /* In case of Alternate function mode selection */
194  if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
195  {
196  /* Check the Alternate function parameters */
197  assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
198  assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
199 
200  /* Configure Alternate function mapped with the current IO */
201  temp = GPIOx->AFR[position >> 3u];
202  temp &= ~(0xFu << ((position & 0x07u) * 4u));
203  temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * 4u));
204  GPIOx->AFR[position >> 3u] = temp;
205  }
206 
207  /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
208  temp = GPIOx->MODER;
209  temp &= ~(GPIO_MODER_MODE0 << (position * 2u));
210  temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u));
211  GPIOx->MODER = temp;
212 
213  /* In case of Output or Alternate function mode selection */
214  if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
215  (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
216  {
217  /* Check the Speed parameter */
218  assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
219  /* Configure the IO Speed */
220  temp = GPIOx->OSPEEDR;
221  temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
222  temp |= (GPIO_Init->Speed << (position * 2u));
223  GPIOx->OSPEEDR = temp;
224 
225  /* Configure the IO Output Type */
226  temp = GPIOx->OTYPER;
227  temp &= ~(GPIO_OTYPER_OT0 << position) ;
228  temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4u) << position);
229  GPIOx->OTYPER = temp;
230  }
231 
232 #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
233 
234  /* In case of Analog mode, check if ADC control mode is selected */
235  if((GPIO_Init->Mode & GPIO_MODE_ANALOG) == GPIO_MODE_ANALOG)
236  {
237  /* Configure the IO Output Type */
238  temp = GPIOx->ASCR;
239  temp &= ~(GPIO_ASCR_ASC0 << position) ;
240  temp |= (((GPIO_Init->Mode & ANALOG_MODE) >> 3) << position);
241  GPIOx->ASCR = temp;
242  }
243 
244 #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
245 
246  /* Activate the Pull-up or Pull down resistor for the current IO */
247  temp = GPIOx->PUPDR;
248  temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
249  temp |= ((GPIO_Init->Pull) << (position * 2u));
250  GPIOx->PUPDR = temp;
251 
252  /*--------------------- EXTI Mode Configuration ------------------------*/
253  /* Configure the External Interrupt or event for the current IO */
254  if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
255  {
256  /* Enable SYSCFG Clock */
257  __HAL_RCC_SYSCFG_CLK_ENABLE();
258 
259  temp = SYSCFG->EXTICR[position >> 2u];
260  temp &= ~(0x0FuL << (4u * (position & 0x03u)));
261  temp |= (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u)));
262  SYSCFG->EXTICR[position >> 2u] = temp;
263 
264  /* Clear EXTI line configuration */
265  temp = EXTI->IMR1;
266  temp &= ~(iocurrent);
267  if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
268  {
269  temp |= iocurrent;
270  }
271  EXTI->IMR1 = temp;
272 
273  temp = EXTI->EMR1;
274  temp &= ~(iocurrent);
275  if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
276  {
277  temp |= iocurrent;
278  }
279  EXTI->EMR1 = temp;
280 
281  /* Clear Rising Falling edge configuration */
282  temp = EXTI->RTSR1;
283  temp &= ~(iocurrent);
284  if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
285  {
286  temp |= iocurrent;
287  }
288  EXTI->RTSR1 = temp;
289 
290  temp = EXTI->FTSR1;
291  temp &= ~(iocurrent);
292  if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
293  {
294  temp |= iocurrent;
295  }
296  EXTI->FTSR1 = temp;
297  }
298  }
299 
300  position++;
301  }
302 }
303 
311 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
312 {
313  uint32_t position = 0x00u;
314  uint32_t iocurrent;
315  uint32_t tmp;
316 
317  /* Check the parameters */
318  assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
319  assert_param(IS_GPIO_PIN(GPIO_Pin));
320 
321  /* Configure the port pins */
322  while ((GPIO_Pin >> position) != 0x00u)
323  {
324  /* Get current io position */
325  iocurrent = (GPIO_Pin) & (1uL << position);
326 
327  if (iocurrent != 0x00u)
328  {
329  /*------------------------- EXTI Mode Configuration --------------------*/
330  /* Clear the External Interrupt or Event for the current IO */
331 
332  tmp = SYSCFG->EXTICR[position >> 2u];
333  tmp &= (0x0FuL << (4u * (position & 0x03u)));
334  if (tmp == (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u))))
335  {
336  /* Clear EXTI line configuration */
337  EXTI->IMR1 &= ~(iocurrent);
338  EXTI->EMR1 &= ~(iocurrent);
339 
340  /* Clear Rising Falling edge configuration */
341  EXTI->RTSR1 &= ~(iocurrent);
342  EXTI->FTSR1 &= ~(iocurrent);
343 
344  tmp = 0x0FuL << (4u * (position & 0x03u));
345  SYSCFG->EXTICR[position >> 2u] &= ~tmp;
346  }
347 
348  /*------------------------- GPIO Mode Configuration --------------------*/
349  /* Configure IO in Analog Mode */
350  GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2u));
351 
352  /* Configure the default Alternate Function in current IO */
353  GPIOx->AFR[position >> 3u] &= ~(0xFu << ((position & 0x07u) * 4u)) ;
354 
355  /* Configure the default value for IO Speed */
356  GPIOx->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
357 
358  /* Configure the default value IO Output Type */
359  GPIOx->OTYPER &= ~(GPIO_OTYPER_OT0 << position) ;
360 
361  /* Deactivate the Pull-up and Pull-down resistor for the current IO */
362  GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
363 
364 #if defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
365  /* Deactivate the Control bit of Analog mode for the current IO */
366  GPIOx->ASCR &= ~(GPIO_ASCR_ASC0<< position);
367 #endif /* STM32L471xx || STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
368  }
369 
370  position++;
371  }
372 }
373 
397 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
398 {
399  GPIO_PinState bitstatus;
400 
401  /* Check the parameters */
402  assert_param(IS_GPIO_PIN(GPIO_Pin));
403 
404  if ((GPIOx->IDR & GPIO_Pin) != 0x00u)
405  {
406  bitstatus = GPIO_PIN_SET;
407  }
408  else
409  {
410  bitstatus = GPIO_PIN_RESET;
411  }
412  return bitstatus;
413 }
414 
431 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
432 {
433  /* Check the parameters */
434  assert_param(IS_GPIO_PIN(GPIO_Pin));
435  assert_param(IS_GPIO_PIN_ACTION(PinState));
436 
437  if(PinState != GPIO_PIN_RESET)
438  {
439  GPIOx->BSRR = (uint32_t)GPIO_Pin;
440  }
441  else
442  {
443  GPIOx->BRR = (uint32_t)GPIO_Pin;
444  }
445 }
446 
453 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
454 {
455  /* Check the parameters */
456  assert_param(IS_GPIO_PIN(GPIO_Pin));
457 
458  if ((GPIOx->ODR & GPIO_Pin) != 0x00u)
459  {
460  GPIOx->BRR = (uint32_t)GPIO_Pin;
461  }
462  else
463  {
464  GPIOx->BSRR = (uint32_t)GPIO_Pin;
465  }
466 }
467 
479 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
480 {
481  __IO uint32_t tmp = GPIO_LCKR_LCKK;
482 
483  /* Check the parameters */
484  assert_param(IS_GPIO_LOCK_INSTANCE(GPIOx));
485  assert_param(IS_GPIO_PIN(GPIO_Pin));
486 
487  /* Apply lock key write sequence */
488  tmp |= GPIO_Pin;
489  /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
490  GPIOx->LCKR = tmp;
491  /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
492  GPIOx->LCKR = GPIO_Pin;
493  /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
494  GPIOx->LCKR = tmp;
495  /* Read LCKK register. This read is mandatory to complete key lock sequence */
496  tmp = GPIOx->LCKR;
497 
498  /* Read again in order to confirm lock is active */
499  if ((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
500  {
501  return HAL_OK;
502  }
503  else
504  {
505  return HAL_ERROR;
506  }
507 }
508 
514 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
515 {
516  /* EXTI line interrupt detected */
517  if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
518  {
519  __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
520  HAL_GPIO_EXTI_Callback(GPIO_Pin);
521  }
522 }
523 
529 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
530 {
531  /* Prevent unused argument(s) compilation warning */
532  UNUSED(GPIO_Pin);
533 
534  /* NOTE: This function should not be modified, when the callback is needed,
535  the HAL_GPIO_EXTI_Callback could be implemented in the user file
536  */
537 }
538 
548 #endif /* HAL_GPIO_MODULE_ENABLED */
549 
557 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Read the specified input port pin.
void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
De-initialize the GPIOx peripheral registers to their default reset values.
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
Handle EXTI interrupt request.
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
EXTI line detection callback.
This file contains all the functions prototypes for the HAL module driver.
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
Set or clear the selected data port bit.
return HAL_OK
GPIO_PinState
GPIO Bit SET and Bit RESET enumeration.
HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Lock GPIO Pins configuration registers.
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
Toggle the specified GPIO pin.