STM32F103xB HAL User Manual
stm32f1xx_hal_exti.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_hal_exti.c
00004   * @author  MCD Application Team
00005   * @brief   EXTI HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Extended Interrupts and events controller (EXTI) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + IO operation functions
00010   *
00011   @verbatim
00012   ==============================================================================
00013                     ##### EXTI Peripheral features #####
00014   ==============================================================================
00015   [..]
00016     (+) Each Exti line can be configured within this driver.
00017 
00018     (+) Exti line can be configured in 3 different modes
00019         (++) Interrupt
00020         (++) Event
00021         (++) Both of them
00022 
00023     (+) Configurable Exti lines can be configured with 3 different triggers
00024         (++) Rising
00025         (++) Falling
00026         (++) Both of them
00027 
00028     (+) When set in interrupt mode, configurable Exti lines have two different
00029         interrupts pending registers which allow to distinguish which transition
00030         occurs:
00031         (++) Rising edge pending interrupt
00032         (++) Falling
00033 
00034     (+) Exti lines 0 to 15 are linked to gpio pin number 0 to 15. Gpio port can
00035         be selected through multiplexer.
00036 
00037                      ##### How to use this driver #####
00038   ==============================================================================
00039   [..]
00040 
00041     (#) Configure the EXTI line using HAL_EXTI_SetConfigLine().
00042         (++) Choose the interrupt line number by setting "Line" member from
00043              EXTI_ConfigTypeDef structure.
00044         (++) Configure the interrupt and/or event mode using "Mode" member from
00045              EXTI_ConfigTypeDef structure.
00046         (++) For configurable lines, configure rising and/or falling trigger
00047              "Trigger" member from EXTI_ConfigTypeDef structure.
00048         (++) For Exti lines linked to gpio, choose gpio port using "GPIOSel"
00049              member from GPIO_InitTypeDef structure.
00050 
00051     (#) Get current Exti configuration of a dedicated line using
00052         HAL_EXTI_GetConfigLine().
00053         (++) Provide exiting handle as parameter.
00054         (++) Provide pointer on EXTI_ConfigTypeDef structure as second parameter.
00055 
00056     (#) Clear Exti configuration of a dedicated line using HAL_EXTI_GetConfigLine().
00057         (++) Provide exiting handle as parameter.
00058 
00059     (#) Register callback to treat Exti interrupts using HAL_EXTI_RegisterCallback().
00060         (++) Provide exiting handle as first parameter.
00061         (++) Provide which callback will be registered using one value from
00062              EXTI_CallbackIDTypeDef.
00063         (++) Provide callback function pointer.
00064 
00065     (#) Get interrupt pending bit using HAL_EXTI_GetPending().
00066 
00067     (#) Clear interrupt pending bit using HAL_EXTI_GetPending().
00068 
00069     (#) Generate software interrupt using HAL_EXTI_GenerateSWI().
00070 
00071   @endverbatim
00072   ******************************************************************************
00073   * @attention
00074   *
00075   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
00076   * All rights reserved.</center></h2>
00077   *
00078   * This software component is licensed by ST under BSD 3-Clause license,
00079   * the "License"; You may not use this file except in compliance with the
00080   * License. You may obtain a copy of the License at:
00081   *                        opensource.org/licenses/BSD-3-Clause
00082   *
00083   ******************************************************************************
00084   */
00085 
00086 /* Includes ------------------------------------------------------------------*/
00087 #include "stm32f1xx_hal.h"
00088 
00089 /** @addtogroup STM32F1xx_HAL_Driver
00090   * @{
00091   */
00092 
00093 /** @addtogroup EXTI
00094   * @{
00095   */
00096 /** MISRA C:2012 deviation rule has been granted for following rule:
00097   * Rule-18.1_b - Medium: Array `EXTICR' 1st subscript interval [0,7] may be out
00098   * of bounds [0,3] in following API :
00099   * HAL_EXTI_SetConfigLine
00100   * HAL_EXTI_GetConfigLine
00101   * HAL_EXTI_ClearConfigLine
00102   */
00103 
00104 #ifdef HAL_EXTI_MODULE_ENABLED
00105 
00106 /* Private typedef -----------------------------------------------------------*/
00107 /* Private defines -----------------------------------------------------------*/
00108 /** @defgroup EXTI_Private_Constants EXTI Private Constants
00109   * @{
00110   */
00111 
00112 /**
00113   * @}
00114   */
00115 
00116 /* Private macros ------------------------------------------------------------*/
00117 /* Private variables ---------------------------------------------------------*/
00118 /* Private function prototypes -----------------------------------------------*/
00119 /* Exported functions --------------------------------------------------------*/
00120 
00121 /** @addtogroup EXTI_Exported_Functions
00122   * @{
00123   */
00124 
00125 /** @addtogroup EXTI_Exported_Functions_Group1
00126   *  @brief    Configuration functions
00127   *
00128 @verbatim
00129  ===============================================================================
00130               ##### Configuration functions #####
00131  ===============================================================================
00132 
00133 @endverbatim
00134   * @{
00135   */
00136 
00137 /**
00138   * @brief  Set configuration of a dedicated Exti line.
00139   * @param  hexti Exti handle.
00140   * @param  pExtiConfig Pointer on EXTI configuration to be set.
00141   * @retval HAL Status.
00142   */
00143 HAL_StatusTypeDef HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
00144 {
00145   uint32_t regval;
00146   uint32_t linepos;
00147   uint32_t maskline;
00148 
00149   /* Check null pointer */
00150   if ((hexti == NULL) || (pExtiConfig == NULL))
00151   {
00152     return HAL_ERROR;
00153   }
00154 
00155   /* Check parameters */
00156   assert_param(IS_EXTI_LINE(pExtiConfig->Line));
00157   assert_param(IS_EXTI_MODE(pExtiConfig->Mode));
00158 
00159   /* Assign line number to handle */
00160   hexti->Line = pExtiConfig->Line;
00161 
00162   /* Compute line mask */
00163   linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
00164   maskline = (1uL << linepos);
00165 
00166   /* Configure triggers for configurable lines */
00167   if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
00168   {
00169     assert_param(IS_EXTI_TRIGGER(pExtiConfig->Trigger));
00170 
00171     /* Configure rising trigger */
00172     /* Mask or set line */
00173     if ((pExtiConfig->Trigger & EXTI_TRIGGER_RISING) != 0x00u)
00174     {
00175       EXTI->RTSR |= maskline;
00176     }
00177     else
00178     {
00179       EXTI->RTSR &= ~maskline;
00180     }
00181 
00182     /* Configure falling trigger */
00183     /* Mask or set line */
00184     if ((pExtiConfig->Trigger & EXTI_TRIGGER_FALLING) != 0x00u)
00185     {
00186       EXTI->FTSR |= maskline;
00187     }
00188     else
00189     {
00190       EXTI->FTSR &= ~maskline;
00191     }
00192 
00193 
00194     /* Configure gpio port selection in case of gpio exti line */
00195     if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
00196     {
00197       assert_param(IS_EXTI_GPIO_PORT(pExtiConfig->GPIOSel));
00198       assert_param(IS_EXTI_GPIO_PIN(linepos));
00199       
00200       regval = AFIO->EXTICR[linepos >> 2u];
00201       regval &= ~(AFIO_EXTICR1_EXTI0 << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
00202       regval |= (pExtiConfig->GPIOSel << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
00203       AFIO->EXTICR[linepos >> 2u] = regval;
00204     }
00205   }
00206 
00207   /* Configure interrupt mode : read current mode */
00208   /* Mask or set line */
00209   if ((pExtiConfig->Mode & EXTI_MODE_INTERRUPT) != 0x00u)
00210   {
00211     EXTI->IMR |= maskline;
00212   }
00213   else
00214   {
00215     EXTI->IMR &= ~maskline;
00216   }
00217 
00218   /* Configure event mode : read current mode */
00219   /* Mask or set line */
00220   if ((pExtiConfig->Mode & EXTI_MODE_EVENT) != 0x00u)
00221   {
00222     EXTI->EMR |= maskline;
00223   }
00224   else
00225   {
00226     EXTI->EMR &= ~maskline;
00227   }
00228 
00229   return HAL_OK;
00230 }
00231 
00232 /**
00233   * @brief  Get configuration of a dedicated Exti line.
00234   * @param  hexti Exti handle.
00235   * @param  pExtiConfig Pointer on structure to store Exti configuration.
00236   * @retval HAL Status.
00237   */
00238 HAL_StatusTypeDef HAL_EXTI_GetConfigLine(EXTI_HandleTypeDef *hexti, EXTI_ConfigTypeDef *pExtiConfig)
00239 {
00240   uint32_t regval;
00241   uint32_t linepos;
00242   uint32_t maskline;
00243 
00244   /* Check null pointer */
00245   if ((hexti == NULL) || (pExtiConfig == NULL))
00246   {
00247     return HAL_ERROR;
00248   }
00249 
00250   /* Check the parameter */
00251   assert_param(IS_EXTI_LINE(hexti->Line));
00252 
00253   /* Store handle line number to configuration structure */
00254   pExtiConfig->Line = hexti->Line;
00255 
00256   /* Compute line mask */
00257   linepos = (pExtiConfig->Line & EXTI_PIN_MASK);
00258   maskline = (1uL << linepos);
00259 
00260   /* 1] Get core mode : interrupt */
00261 
00262   /* Check if selected line is enable */
00263   if ((EXTI->IMR & maskline) != 0x00u)
00264   {
00265     pExtiConfig->Mode = EXTI_MODE_INTERRUPT;
00266   }
00267   else
00268   {
00269     pExtiConfig->Mode = EXTI_MODE_NONE;
00270   }
00271 
00272   /* Get event mode */
00273   /* Check if selected line is enable */
00274   if ((EXTI->EMR & maskline) != 0x00u)
00275   {
00276     pExtiConfig->Mode |= EXTI_MODE_EVENT;
00277   }
00278 
00279   /* Get default Trigger and GPIOSel configuration */
00280   pExtiConfig->Trigger = EXTI_TRIGGER_NONE;
00281   pExtiConfig->GPIOSel = 0x00u;
00282 
00283   /* 2] Get trigger for configurable lines : rising */
00284   if ((pExtiConfig->Line & EXTI_CONFIG) != 0x00u)
00285   {
00286     /* Check if configuration of selected line is enable */
00287     if ((EXTI->RTSR & maskline) != 0x00u)
00288     {
00289       pExtiConfig->Trigger = EXTI_TRIGGER_RISING;
00290     }
00291 
00292     /* Get falling configuration */
00293     /* Check if configuration of selected line is enable */
00294     if ((EXTI->FTSR & maskline) != 0x00u)
00295     {
00296       pExtiConfig->Trigger |= EXTI_TRIGGER_FALLING;
00297     }
00298 
00299     /* Get Gpio port selection for gpio lines */
00300     if ((pExtiConfig->Line & EXTI_GPIO) == EXTI_GPIO)
00301     {
00302       assert_param(IS_EXTI_GPIO_PIN(linepos));
00303 
00304       regval = AFIO->EXTICR[linepos >> 2u];
00305       pExtiConfig->GPIOSel = ((regval << (AFIO_EXTICR1_EXTI1_Pos * (3uL - (linepos & 0x03u)))) >> 24);
00306     }
00307   }
00308 
00309   return HAL_OK;
00310 }
00311 
00312 /**
00313   * @brief  Clear whole configuration of a dedicated Exti line.
00314   * @param  hexti Exti handle.
00315   * @retval HAL Status.
00316   */
00317 HAL_StatusTypeDef HAL_EXTI_ClearConfigLine(EXTI_HandleTypeDef *hexti)
00318 {
00319   uint32_t regval;
00320   uint32_t linepos;
00321   uint32_t maskline;
00322 
00323   /* Check null pointer */
00324   if (hexti == NULL)
00325   {
00326     return HAL_ERROR;
00327   }
00328 
00329   /* Check the parameter */
00330   assert_param(IS_EXTI_LINE(hexti->Line));
00331 
00332   /* compute line mask */
00333   linepos = (hexti->Line & EXTI_PIN_MASK);
00334   maskline = (1uL << linepos);
00335 
00336   /* 1] Clear interrupt mode */
00337   EXTI->IMR = (EXTI->IMR & ~maskline);
00338 
00339   /* 2] Clear event mode */
00340   EXTI->EMR = (EXTI->EMR & ~maskline);
00341 
00342   /* 3] Clear triggers in case of configurable lines */
00343   if ((hexti->Line & EXTI_CONFIG) != 0x00u)
00344   {
00345     EXTI->RTSR = (EXTI->RTSR & ~maskline);
00346     EXTI->FTSR = (EXTI->FTSR & ~maskline);
00347 
00348     /* Get Gpio port selection for gpio lines */
00349     if ((hexti->Line & EXTI_GPIO) == EXTI_GPIO)
00350     {
00351       assert_param(IS_EXTI_GPIO_PIN(linepos));
00352 
00353       regval = AFIO->EXTICR[linepos >> 2u];
00354       regval &= ~(AFIO_EXTICR1_EXTI0 << (AFIO_EXTICR1_EXTI1_Pos * (linepos & 0x03u)));
00355       AFIO->EXTICR[linepos >> 2u] = regval;
00356     }
00357   }
00358 
00359   return HAL_OK;
00360 }
00361 
00362 /**
00363   * @brief  Register callback for a dedicated Exti line.
00364   * @param  hexti Exti handle.
00365   * @param  CallbackID User callback identifier.
00366   *         This parameter can be one of @arg @ref EXTI_CallbackIDTypeDef values.
00367   * @param  pPendingCbfn function pointer to be stored as callback.
00368   * @retval HAL Status.
00369   */
00370 HAL_StatusTypeDef HAL_EXTI_RegisterCallback(EXTI_HandleTypeDef *hexti, EXTI_CallbackIDTypeDef CallbackID, void (*pPendingCbfn)(void))
00371 {
00372   HAL_StatusTypeDef status = HAL_OK;
00373 
00374   switch (CallbackID)
00375   {
00376     case  HAL_EXTI_COMMON_CB_ID:
00377       hexti->PendingCallback = pPendingCbfn;
00378       break;
00379 
00380     default:
00381       status = HAL_ERROR;
00382       break;
00383   }
00384 
00385   return status;
00386 }
00387 
00388 /**
00389   * @brief  Store line number as handle private field.
00390   * @param  hexti Exti handle.
00391   * @param  ExtiLine Exti line number.
00392   *         This parameter can be from 0 to @ref EXTI_LINE_NB.
00393   * @retval HAL Status.
00394   */
00395 HAL_StatusTypeDef HAL_EXTI_GetHandle(EXTI_HandleTypeDef *hexti, uint32_t ExtiLine)
00396 {
00397   /* Check the parameters */
00398   assert_param(IS_EXTI_LINE(ExtiLine));
00399 
00400   /* Check null pointer */
00401   if (hexti == NULL)
00402   {
00403     return HAL_ERROR;
00404   }
00405   else
00406   {
00407     /* Store line number as handle private field */
00408     hexti->Line = ExtiLine;
00409 
00410     return HAL_OK;
00411   }
00412 }
00413 
00414 /**
00415   * @}
00416   */
00417 
00418 /** @addtogroup EXTI_Exported_Functions_Group2
00419   *  @brief EXTI IO functions.
00420   *
00421 @verbatim
00422  ===============================================================================
00423                        ##### IO operation functions #####
00424  ===============================================================================
00425 
00426 @endverbatim
00427   * @{
00428   */
00429 
00430 /**
00431   * @brief  Handle EXTI interrupt request.
00432   * @param  hexti Exti handle.
00433   * @retval none.
00434   */
00435 void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
00436 {
00437   uint32_t regval;
00438   uint32_t maskline;
00439 
00440   /* Compute line mask */
00441   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
00442 
00443   /* Get pending bit  */
00444   regval = (EXTI->PR & maskline);
00445   if (regval != 0x00u)
00446   {
00447     /* Clear pending bit */
00448     EXTI->PR = maskline;
00449 
00450     /* Call callback */
00451     if (hexti->PendingCallback != NULL)
00452     {
00453       hexti->PendingCallback();
00454     }
00455   }
00456 }
00457 
00458 /**
00459   * @brief  Get interrupt pending bit of a dedicated line.
00460   * @param  hexti Exti handle.
00461   * @param  Edge Specify which pending edge as to be checked.
00462   *         This parameter can be one of the following values:
00463   *           @arg @ref EXTI_TRIGGER_RISING_FALLING
00464   *         This parameter is kept for compatibility with other series.
00465   * @retval 1 if interrupt is pending else 0.
00466   */
00467 uint32_t HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
00468 {
00469   uint32_t regval;
00470   uint32_t maskline;
00471   uint32_t linepos;
00472 
00473   /* Check parameters */
00474   assert_param(IS_EXTI_LINE(hexti->Line));
00475   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
00476   assert_param(IS_EXTI_PENDING_EDGE(Edge));
00477 
00478   /* Prevent unused argument compilation warning */
00479   UNUSED(Edge);
00480 
00481   /* Compute line mask */
00482   linepos = (hexti->Line & EXTI_PIN_MASK);
00483   maskline = (1uL << linepos);
00484 
00485   /* return 1 if bit is set else 0 */
00486   regval = ((EXTI->PR & maskline) >> linepos);
00487   return regval;
00488 }
00489 
00490 /**
00491   * @brief  Clear interrupt pending bit of a dedicated line.
00492   * @param  hexti Exti handle.
00493   * @param  Edge Specify which pending edge as to be clear.
00494   *         This parameter can be one of the following values:
00495   *           @arg @ref EXTI_TRIGGER_RISING_FALLING
00496   *         This parameter is kept for compatibility with other series.
00497   * @retval None.
00498   */
00499 void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti, uint32_t Edge)
00500 {
00501   uint32_t maskline;
00502 
00503   /* Check parameters */
00504   assert_param(IS_EXTI_LINE(hexti->Line));
00505   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
00506   assert_param(IS_EXTI_PENDING_EDGE(Edge));
00507 
00508   /* Prevent unused argument compilation warning */
00509   UNUSED(Edge);
00510 
00511   /* Compute line mask */
00512   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
00513 
00514   /* Clear Pending bit */
00515   EXTI->PR =  maskline;
00516 }
00517 
00518 /**
00519   * @brief  Generate a software interrupt for a dedicated line.
00520   * @param  hexti Exti handle.
00521   * @retval None.
00522   */
00523 void HAL_EXTI_GenerateSWI(EXTI_HandleTypeDef *hexti)
00524 {
00525   uint32_t maskline;
00526 
00527   /* Check parameters */
00528   assert_param(IS_EXTI_LINE(hexti->Line));
00529   assert_param(IS_EXTI_CONFIG_LINE(hexti->Line));
00530 
00531   /* Compute line mask */
00532   maskline = (1uL << (hexti->Line & EXTI_PIN_MASK));
00533 
00534   /* Generate Software interrupt */
00535   EXTI->SWIER = maskline;
00536 }
00537 
00538 /**
00539   * @}
00540   */
00541 
00542 /**
00543   * @}
00544   */
00545 
00546 #endif /* HAL_EXTI_MODULE_ENABLED */
00547 /**
00548   * @}
00549   */
00550 
00551 /**
00552   * @}
00553   */
00554 
00555 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/