Martin Johnson / STM32F3-Discovery

Dependents:   Space_Invaders_Demo neopixels gpio_test_stm32f3_discovery gpio_test_systimer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f30x_exti.c Source File

stm32f30x_exti.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f30x_exti.c
00004   * @author  MCD Application Team
00005   * @version V1.2.3
00006   * @date    10-July-2015
00007   * @brief   This file provides firmware functions to manage the following 
00008   *          functionalities of the EXTI peripheral:
00009   *           + Initialization and Configuration 
00010   *           + Interrupts and flags management 
00011   *
00012   @verbatim
00013  ===============================================================================
00014                           ##### EXTI features #####
00015  ===============================================================================
00016     [..] External interrupt/event lines are mapped as following:
00017          (#) All available GPIO pins are connected to the 16 external 
00018              interrupt/event lines from EXTI0 to EXTI15.
00019          (#) EXTI line 16 is connected to the PVD output
00020          (#) EXTI line 17 is connected to the RTC Alarm event
00021          (#) EXTI line 18 is connected to USB Device wakeup event  
00022          (#) EXTI line 19 is connected to the RTC Tamper and TimeStamp events
00023          (#) EXTI line 20 is connected to the RTC wakeup event  
00024          (#) EXTI line 21 is connected to the Comparator 1 wakeup event 
00025          (#) EXTI line 22 is connected to the Comparator 2 wakeup event
00026          (#) EXTI line 23 is connected to the I2C1 wakeup event
00027          (#) EXTI line 24 is connected to the I2C2 wakeup event  
00028          (#) EXTI line 25 is connected to the USART1 wakeup event
00029          (#) EXTI line 26 is connected to the USART2 wakeup event  
00030          (#) EXTI line 27 is reserved
00031          (#) EXTI line 28 is connected to the USART3 wakeup event
00032          (#) EXTI line 29 is connected to the Comparator 3 event
00033          (#) EXTI line 30 is connected to the Comparator 4 event
00034          (#) EXTI line 31 is connected to the Comparator 5 event
00035          (#) EXTI line 32 is connected to the Comparator 6 event
00036          (#) EXTI line 33 is connected to the Comparator 7 event
00037          (#) EXTI line 34 is connected for thr UART4 wakeup event
00038          (#) EXTI line 35 is connected for the UART5 wakeup event               
00039 
00040                        ##### How to use this driver #####
00041  ===============================================================================
00042     [..] In order to use an I/O pin as an external interrupt source, 
00043          follow steps below:
00044          (#) Configure the I/O in input mode using GPIO_Init().
00045          (#) Select the input source pin for the EXTI line using
00046              SYSCFG_EXTILineConfig().
00047          (#) Select the mode(interrupt, event) and configure the trigger 
00048              selection (Rising, falling or both) using EXTI_Init(). For the 
00049              internal interrupt, the trigger selection is not needed 
00050              (the active edge is always the rising one).
00051          (#) Configure NVIC IRQ channel mapped to the EXTI line using NVIC_Init().
00052          (#) Optionally, you can generate a software interrupt using the function 
00053              EXTI_GenerateSWInterrupt().
00054     [..]
00055     (@) SYSCFG APB clock must be enabled to get write access to SYSCFG_EXTICRx
00056       registers using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
00057                 
00058   @endverbatim
00059 
00060   ******************************************************************************
00061   * @attention
00062   *
00063   * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
00064   *
00065   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00066   * You may not use this file except in compliance with the License.
00067   * You may obtain a copy of the License at:
00068   *
00069   *        http://www.st.com/software_license_agreement_liberty_v2
00070   *
00071   * Unless required by applicable law or agreed to in writing, software 
00072   * distributed under the License is distributed on an "AS IS" BASIS, 
00073   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00074   * See the License for the specific language governing permissions and
00075   * limitations under the License.
00076   *
00077   ******************************************************************************
00078   */
00079 
00080 /* Includes ------------------------------------------------------------------*/
00081 #include "stm32f30x_exti.h"
00082 
00083 /** @addtogroup STM32F30x_StdPeriph_Driver
00084   * @{
00085   */
00086 
00087 /** @defgroup EXTI 
00088   * @brief EXTI driver modules
00089   * @{
00090   */
00091 
00092 
00093 /* Private typedef -----------------------------------------------------------*/
00094 /* Private define ------------------------------------------------------------*/
00095 #define EXTI_LINENONE     ((uint32_t)0x00000)        /* No interrupt selected */
00096 
00097 /* Private macro -------------------------------------------------------------*/
00098 /* Private variables ---------------------------------------------------------*/
00099 /* Private function prototypes -----------------------------------------------*/
00100 /* Private functions ---------------------------------------------------------*/
00101 
00102 /** @defgroup EXTI_Private_Functions 
00103   * @{
00104   */
00105 
00106 /** @defgroup EXTI_Group1 Initialization and Configuration functions
00107  *  @brief   Initialization and Configuration functions
00108  *
00109 @verbatim
00110  ===============================================================================
00111               ##### Initialization and Configuration functions #####
00112  ===============================================================================
00113 
00114 @endverbatim
00115   * @{
00116   */
00117     
00118 /**
00119   * @brief  Deinitializes the EXTI peripheral registers to their default reset 
00120   *         values.
00121   * @param  None
00122   * @retval None
00123   */
00124 void EXTI_DeInit(void)
00125 {
00126   EXTI->IMR    = 0x1F800000;
00127   EXTI->EMR    = 0x00000000;
00128   EXTI->RTSR   = 0x00000000;
00129   EXTI->FTSR   = 0x00000000;
00130   EXTI->SWIER  = 0x00000000;
00131   EXTI->PR     = 0xE07FFFFF;
00132   EXTI->IMR2   = 0x0000000C;
00133   EXTI->EMR2   = 0x00000000;
00134   EXTI->RTSR2  = 0x00000000;
00135   EXTI->FTSR2  = 0x00000000;
00136   EXTI->SWIER2 = 0x00000000;
00137   EXTI->PR2    = 0x00000003;
00138 }
00139 
00140 /**
00141   * @brief  Initializes the EXTI peripheral according to the specified
00142   *         parameters in the EXTI_InitStruct.
00143   *    EXTI_Line specifies the EXTI line (EXTI0....EXTI35).
00144   *    EXTI_Mode specifies which EXTI line is used as interrupt or an event.
00145   *    EXTI_Trigger selects the trigger. When the trigger occurs, interrupt
00146   *                 pending bit will be set.
00147   *    EXTI_LineCmd controls (Enable/Disable) the EXTI line.
00148   * @param  EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure that 
00149   *         contains the configuration information for the EXTI peripheral.
00150   * @retval None
00151   */
00152   
00153 
00154 void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
00155 {
00156   uint32_t tmp = 0;
00157 
00158   /* Check the parameters */
00159   assert_param(IS_EXTI_MODE(EXTI_InitStruct->EXTI_Mode));
00160   assert_param(IS_EXTI_TRIGGER(EXTI_InitStruct->EXTI_Trigger));
00161   assert_param(IS_EXTI_LINE_ALL(EXTI_InitStruct->EXTI_Line));
00162   assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->EXTI_LineCmd));
00163 
00164   tmp = (uint32_t)EXTI_BASE;
00165       
00166   if (EXTI_InitStruct->EXTI_LineCmd != DISABLE)
00167   {
00168     /* Clear EXTI line configuration */   
00169     *(__IO uint32_t *) (((uint32_t) &(EXTI->IMR)) + ((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20) &= ~(uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));   
00170     *(__IO uint32_t *) (((uint32_t) &(EXTI->EMR)) + ((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20) &= ~(uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));
00171      
00172     tmp += EXTI_InitStruct->EXTI_Mode + (((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20);
00173 
00174     *(__IO uint32_t *) tmp |= (uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));
00175     
00176     tmp = (uint32_t)EXTI_BASE;
00177 
00178     /* Clear Rising Falling edge configuration */
00179     *(__IO uint32_t *) (((uint32_t) &(EXTI->RTSR)) + ((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20) &= ~(uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));
00180     *(__IO uint32_t *) (((uint32_t) &(EXTI->FTSR)) + ((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20) &= ~(uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));
00181     
00182       /* Select the trigger for the selected interrupts */
00183     if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling)
00184     {
00185       /* Rising Falling edge */
00186     *(__IO uint32_t *) (((uint32_t) &(EXTI->RTSR)) + ((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20) |= (uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));
00187     *(__IO uint32_t *) (((uint32_t) &(EXTI->FTSR)) + ((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20) |= (uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));      
00188     }
00189     else
00190     {
00191       tmp += EXTI_InitStruct->EXTI_Trigger + (((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20);
00192 
00193       *(__IO uint32_t *) tmp |= (uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));
00194     }
00195   }
00196       
00197   else
00198   {
00199     tmp += EXTI_InitStruct->EXTI_Mode + (((EXTI_InitStruct->EXTI_Line) >> 5 ) * 0x20);
00200 
00201     /* Disable the selected external lines */
00202     *(__IO uint32_t *) tmp &= ~(uint32_t)(1 << (EXTI_InitStruct->EXTI_Line & 0x1F));
00203   }
00204          
00205 }
00206 
00207 /**
00208   * @brief  Fills each EXTI_InitStruct member with its reset value.
00209   * @param  EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure which will
00210   *         be initialized.
00211   * @retval None
00212   */
00213 void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct)
00214 {
00215   EXTI_InitStruct->EXTI_Line = EXTI_LINENONE;
00216   EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt;
00217   EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Rising_Falling;
00218   EXTI_InitStruct->EXTI_LineCmd = DISABLE;
00219 }
00220 
00221 /**
00222   * @brief  Generates a Software interrupt on selected EXTI line.
00223   * @param  EXTI_Line: specifies the EXTI line on which the software interrupt
00224   *         will be generated.
00225   *   This parameter can be any combination of EXTI_Linex where x can be (0..20).
00226   * @retval None
00227   */
00228 void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
00229 {
00230   /* Check the parameters */
00231   assert_param(IS_EXTI_LINE_EXT(EXTI_Line));
00232 
00233   *(__IO uint32_t *) (((uint32_t) &(EXTI->SWIER)) + ((EXTI_Line) >> 5 ) * 0x20) |= (uint32_t)(1 << (EXTI_Line & 0x1F));
00234 
00235 }
00236 
00237 /**
00238   * @}
00239   */
00240   
00241 /** @defgroup EXTI_Group2 Interrupts and flags management functions
00242  *  @brief    EXTI Interrupts and flags management functions
00243  *
00244 @verbatim  
00245  ===============================================================================
00246               ##### Interrupts and flags management functions #####
00247  ===============================================================================
00248     [..]
00249     This section provides functions allowing to configure the EXTI Interrupts 
00250     sources and check or clear the flags or pending bits status.
00251     
00252 @endverbatim
00253   * @{
00254   */
00255 
00256 /**
00257   * @brief  Checks whether the specified EXTI line flag is set or not.
00258   * @param  EXTI_Line: specifies the EXTI line flag to check.
00259   *   This parameter can be any combination of EXTI_Linex where x can be (0..20).
00260   * @retval The new state of EXTI_Line (SET or RESET).                  
00261   */
00262 FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
00263 {
00264   FlagStatus bitstatus = RESET;
00265   
00266   /* Check the parameters */
00267   assert_param(IS_GET_EXTI_LINE(EXTI_Line));
00268    
00269   if ((*(__IO uint32_t *) (((uint32_t) &(EXTI->PR)) + ((EXTI_Line) >> 5 ) * 0x20)& (uint32_t)(1 << (EXTI_Line & 0x1F))) != (uint32_t)RESET)
00270   {
00271     bitstatus = SET;
00272   }
00273   else
00274   {
00275     bitstatus = RESET;
00276   }
00277   return bitstatus;
00278 }
00279 
00280 /**
00281   * @brief  Clears the EXTI's line pending flags.
00282   * @param  EXTI_Line: specifies the EXTI lines flags to clear.
00283   *   This parameter can be any combination of EXTI_Linex where x can be (0..20).
00284   * @retval None
00285   */
00286 void EXTI_ClearFlag(uint32_t EXTI_Line)
00287 {
00288   /* Check the parameters */
00289   assert_param(IS_EXTI_LINE_EXT(EXTI_Line));
00290 
00291   *(__IO uint32_t *) (((uint32_t) &(EXTI->PR)) + ((EXTI_Line) >> 5 ) * 0x20) = (1 << (EXTI_Line & 0x1F));  
00292 }
00293 
00294 /**
00295   * @brief  Checks whether the specified EXTI line is asserted or not.
00296   * @param  EXTI_Line: specifies the EXTI line to check.
00297   *   This parameter can be any combination of EXTI_Linex where x can be (0..20).
00298   * @retval The new state of EXTI_Line (SET or RESET).
00299   */
00300 ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
00301 {
00302   ITStatus bitstatus = RESET;
00303   
00304   /* Check the parameters */
00305   assert_param(IS_GET_EXTI_LINE(EXTI_Line));
00306   
00307   if ((*(__IO uint32_t *) (((uint32_t) &(EXTI->PR)) + ((EXTI_Line) >> 5 ) * 0x20)& (uint32_t)(1 << (EXTI_Line & 0x1F))) != (uint32_t)RESET)
00308   {
00309     bitstatus = SET;
00310   }
00311   else
00312   {
00313     bitstatus = RESET;
00314   }
00315   return bitstatus;
00316   
00317 }
00318 
00319 /**
00320   * @brief  Clears the EXTI's line pending bits.
00321   * @param  EXTI_Line: specifies the EXTI lines to clear.
00322   *   This parameter can be any combination of EXTI_Linex where x can be (0..20).
00323   * @retval None
00324   */
00325 void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
00326 {
00327   /* Check the parameters */
00328   assert_param(IS_EXTI_LINE_EXT(EXTI_Line));
00329   
00330   *(__IO uint32_t *) (((uint32_t) &(EXTI->PR)) + ((EXTI_Line) >> 5 ) * 0x20) = (1 << (EXTI_Line & 0x1F));
00331 }
00332 
00333 /**
00334   * @}
00335   */
00336 
00337 /**
00338   * @}
00339   */
00340 
00341 /**
00342   * @}
00343   */
00344 
00345 /**
00346   * @}
00347   */
00348 
00349 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/