stm32f4xx_exti.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_exti.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the EXTI peripheral:
  9. * - Initialization and Configuration
  10. * - Interrupts and flags management
  11. *
  12. * @verbatim
  13. *
  14. * ===================================================================
  15. * EXTI features
  16. * ===================================================================
  17. *
  18. * External interrupt/event lines are mapped as following:
  19. * 1- All available GPIO pins are connected to the 16 external
  20. * interrupt/event lines from EXTI0 to EXTI15.
  21. * 2- EXTI line 16 is connected to the PVD Output
  22. * 3- EXTI line 17 is connected to the RTC Alarm event
  23. * 4- EXTI line 18 is connected to the USB OTG FS Wakeup from suspend event
  24. * 5- EXTI line 19 is connected to the Ethernet Wakeup event
  25. * 6- EXTI line 20 is connected to the USB OTG HS (configured in FS) Wakeup event
  26. * 7- EXTI line 21 is connected to the RTC Tamper and Time Stamp events
  27. * 8- EXTI line 22 is connected to the RTC Wakeup event
  28. *
  29. * ===================================================================
  30. * How to use this driver
  31. * ===================================================================
  32. *
  33. * In order to use an I/O pin as an external interrupt source, follow
  34. * steps below:
  35. * 1- Configure the I/O in input mode using GPIO_Init()
  36. * 2- Select the input source pin for the EXTI line using SYSCFG_EXTILineConfig()
  37. * 3- Select the mode(interrupt, event) and configure the trigger
  38. * selection (Rising, falling or both) using EXTI_Init()
  39. * 4- Configure NVIC IRQ channel mapped to the EXTI line using NVIC_Init()
  40. *
  41. * @note SYSCFG APB clock must be enabled to get write access to SYSCFG_EXTICRx
  42. * registers using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  43. *
  44. * @endverbatim
  45. *
  46. ******************************************************************************
  47. * @attention
  48. *
  49. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  50. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  51. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  52. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  53. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  54. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  55. *
  56. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  57. ******************************************************************************
  58. */
  59. /* Includes ------------------------------------------------------------------*/
  60. #include "stm32f4xx_exti.h"
  61. /** @addtogroup STM32F4xx_StdPeriph_Driver
  62. * @{
  63. */
  64. /** @defgroup EXTI
  65. * @brief EXTI driver modules
  66. * @{
  67. */
  68. /* Private typedef -----------------------------------------------------------*/
  69. /* Private define ------------------------------------------------------------*/
  70. #define EXTI_LINENONE ((uint32_t)0x00000) /* No interrupt selected */
  71. /* Private macro -------------------------------------------------------------*/
  72. /* Private variables ---------------------------------------------------------*/
  73. /* Private function prototypes -----------------------------------------------*/
  74. /* Private functions ---------------------------------------------------------*/
  75. /** @defgroup EXTI_Private_Functions
  76. * @{
  77. */
  78. /** @defgroup EXTI_Group1 Initialization and Configuration functions
  79. * @brief Initialization and Configuration functions
  80. *
  81. @verbatim
  82. ===============================================================================
  83. Initialization and Configuration functions
  84. ===============================================================================
  85. @endverbatim
  86. * @{
  87. */
  88. /**
  89. * @brief Deinitializes the EXTI peripheral registers to their default reset values.
  90. * @param None
  91. * @retval None
  92. */
  93. void EXTI_DeInit(void)
  94. {
  95. EXTI->IMR = 0x00000000;
  96. EXTI->EMR = 0x00000000;
  97. EXTI->RTSR = 0x00000000;
  98. EXTI->FTSR = 0x00000000;
  99. EXTI->PR = 0x007FFFFF;
  100. }
  101. /**
  102. * @brief Initializes the EXTI peripheral according to the specified
  103. * parameters in the EXTI_InitStruct.
  104. * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure
  105. * that contains the configuration information for the EXTI peripheral.
  106. * @retval None
  107. */
  108. void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
  109. {
  110. uint32_t tmp = 0;
  111. /* Check the parameters */
  112. assert_param(IS_EXTI_MODE(EXTI_InitStruct->EXTI_Mode));
  113. assert_param(IS_EXTI_TRIGGER(EXTI_InitStruct->EXTI_Trigger));
  114. assert_param(IS_EXTI_LINE(EXTI_InitStruct->EXTI_Line));
  115. assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->EXTI_LineCmd));
  116. tmp = (uint32_t)EXTI_BASE;
  117. if (EXTI_InitStruct->EXTI_LineCmd != DISABLE)
  118. {
  119. /* Clear EXTI line configuration */
  120. EXTI->IMR &= ~EXTI_InitStruct->EXTI_Line;
  121. EXTI->EMR &= ~EXTI_InitStruct->EXTI_Line;
  122. tmp += EXTI_InitStruct->EXTI_Mode;
  123. *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
  124. /* Clear Rising Falling edge configuration */
  125. EXTI->RTSR &= ~EXTI_InitStruct->EXTI_Line;
  126. EXTI->FTSR &= ~EXTI_InitStruct->EXTI_Line;
  127. /* Select the trigger for the selected external interrupts */
  128. if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling)
  129. {
  130. /* Rising Falling edge */
  131. EXTI->RTSR |= EXTI_InitStruct->EXTI_Line;
  132. EXTI->FTSR |= EXTI_InitStruct->EXTI_Line;
  133. }
  134. else
  135. {
  136. tmp = (uint32_t)EXTI_BASE;
  137. tmp += EXTI_InitStruct->EXTI_Trigger;
  138. *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
  139. }
  140. }
  141. else
  142. {
  143. tmp += EXTI_InitStruct->EXTI_Mode;
  144. /* Disable the selected external lines */
  145. *(__IO uint32_t *) tmp &= ~EXTI_InitStruct->EXTI_Line;
  146. }
  147. }
  148. /**
  149. * @brief Fills each EXTI_InitStruct member with its reset value.
  150. * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure which will
  151. * be initialized.
  152. * @retval None
  153. */
  154. void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct)
  155. {
  156. EXTI_InitStruct->EXTI_Line = EXTI_LINENONE;
  157. EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt;
  158. EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling;
  159. EXTI_InitStruct->EXTI_LineCmd = DISABLE;
  160. }
  161. /**
  162. * @brief Generates a Software interrupt on selected EXTI line.
  163. * @param EXTI_Line: specifies the EXTI line on which the software interrupt
  164. * will be generated.
  165. * This parameter can be any combination of EXTI_Linex where x can be (0..22)
  166. * @retval None
  167. */
  168. void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
  169. {
  170. /* Check the parameters */
  171. assert_param(IS_EXTI_LINE(EXTI_Line));
  172. EXTI->SWIER |= EXTI_Line;
  173. }
  174. /**
  175. * @}
  176. */
  177. /** @defgroup EXTI_Group2 Interrupts and flags management functions
  178. * @brief Interrupts and flags management functions
  179. *
  180. @verbatim
  181. ===============================================================================
  182. Interrupts and flags management functions
  183. ===============================================================================
  184. @endverbatim
  185. * @{
  186. */
  187. /**
  188. * @brief Checks whether the specified EXTI line flag is set or not.
  189. * @param EXTI_Line: specifies the EXTI line flag to check.
  190. * This parameter can be EXTI_Linex where x can be(0..22)
  191. * @retval The new state of EXTI_Line (SET or RESET).
  192. */
  193. FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
  194. {
  195. FlagStatus bitstatus = RESET;
  196. /* Check the parameters */
  197. assert_param(IS_GET_EXTI_LINE(EXTI_Line));
  198. if ((EXTI->PR & EXTI_Line) != (uint32_t)RESET)
  199. {
  200. bitstatus = SET;
  201. }
  202. else
  203. {
  204. bitstatus = RESET;
  205. }
  206. return bitstatus;
  207. }
  208. /**
  209. * @brief Clears the EXTI's line pending flags.
  210. * @param EXTI_Line: specifies the EXTI lines flags to clear.
  211. * This parameter can be any combination of EXTI_Linex where x can be (0..22)
  212. * @retval None
  213. */
  214. void EXTI_ClearFlag(uint32_t EXTI_Line)
  215. {
  216. /* Check the parameters */
  217. assert_param(IS_EXTI_LINE(EXTI_Line));
  218. EXTI->PR = EXTI_Line;
  219. }
  220. /**
  221. * @brief Checks whether the specified EXTI line is asserted or not.
  222. * @param EXTI_Line: specifies the EXTI line to check.
  223. * This parameter can be EXTI_Linex where x can be(0..22)
  224. * @retval The new state of EXTI_Line (SET or RESET).
  225. */
  226. ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
  227. {
  228. ITStatus bitstatus = RESET;
  229. uint32_t enablestatus = 0;
  230. /* Check the parameters */
  231. assert_param(IS_GET_EXTI_LINE(EXTI_Line));
  232. enablestatus = EXTI->IMR & EXTI_Line;
  233. if (((EXTI->PR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  234. {
  235. bitstatus = SET;
  236. }
  237. else
  238. {
  239. bitstatus = RESET;
  240. }
  241. return bitstatus;
  242. }
  243. /**
  244. * @brief Clears the EXTI's line pending bits.
  245. * @param EXTI_Line: specifies the EXTI lines to clear.
  246. * This parameter can be any combination of EXTI_Linex where x can be (0..22)
  247. * @retval None
  248. */
  249. void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
  250. {
  251. /* Check the parameters */
  252. assert_param(IS_EXTI_LINE(EXTI_Line));
  253. EXTI->PR = EXTI_Line;
  254. }
  255. /**
  256. * @}
  257. */
  258. /**
  259. * @}
  260. */
  261. /**
  262. * @}
  263. */
  264. /**
  265. * @}
  266. */
  267. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/