stm32f2xx_exti.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_exti.c
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  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. * Copyright (c) 2012 STMicroelectronics.
  50. * All rights reserved.
  51. *
  52. * This software is licensed under terms that can be found in the LICENSE file
  53. * in the root directory of this software component.
  54. * If no LICENSE file comes with this software, it is provided AS-IS.
  55. *
  56. ******************************************************************************
  57. */
  58. /* Includes ------------------------------------------------------------------*/
  59. #include "stm32f2xx_exti.h"
  60. /** @addtogroup STM32F2xx_StdPeriph_Driver
  61. * @{
  62. */
  63. /** @defgroup EXTI
  64. * @brief EXTI driver modules
  65. * @{
  66. */
  67. /* Private typedef -----------------------------------------------------------*/
  68. /* Private define ------------------------------------------------------------*/
  69. #define EXTI_LINENONE ((uint32_t)0x00000) /* No interrupt selected */
  70. /* Private macro -------------------------------------------------------------*/
  71. /* Private variables ---------------------------------------------------------*/
  72. /* Private function prototypes -----------------------------------------------*/
  73. /* Private functions ---------------------------------------------------------*/
  74. /** @defgroup EXTI_Private_Functions
  75. * @{
  76. */
  77. /** @defgroup EXTI_Group1 Initialization and Configuration functions
  78. * @brief Initialization and Configuration functions
  79. *
  80. @verbatim
  81. ===============================================================================
  82. Initialization and Configuration functions
  83. ===============================================================================
  84. @endverbatim
  85. * @{
  86. */
  87. /**
  88. * @brief Deinitializes the EXTI peripheral registers to their default reset values.
  89. * @param None
  90. * @retval None
  91. */
  92. void EXTI_DeInit(void)
  93. {
  94. EXTI->IMR = 0x00000000;
  95. EXTI->EMR = 0x00000000;
  96. EXTI->RTSR = 0x00000000;
  97. EXTI->FTSR = 0x00000000;
  98. EXTI->PR = 0x007FFFFF;
  99. }
  100. /**
  101. * @brief Initializes the EXTI peripheral according to the specified
  102. * parameters in the EXTI_InitStruct.
  103. * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure
  104. * that contains the configuration information for the EXTI peripheral.
  105. * @retval None
  106. */
  107. void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
  108. {
  109. uint32_t tmp = 0;
  110. /* Check the parameters */
  111. assert_param(IS_EXTI_MODE(EXTI_InitStruct->EXTI_Mode));
  112. assert_param(IS_EXTI_TRIGGER(EXTI_InitStruct->EXTI_Trigger));
  113. assert_param(IS_EXTI_LINE(EXTI_InitStruct->EXTI_Line));
  114. assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->EXTI_LineCmd));
  115. tmp = (uint32_t)EXTI_BASE;
  116. if (EXTI_InitStruct->EXTI_LineCmd != DISABLE)
  117. {
  118. /* Clear EXTI line configuration */
  119. EXTI->IMR &= ~EXTI_InitStruct->EXTI_Line;
  120. EXTI->EMR &= ~EXTI_InitStruct->EXTI_Line;
  121. tmp += EXTI_InitStruct->EXTI_Mode;
  122. *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
  123. /* Clear Rising Falling edge configuration */
  124. EXTI->RTSR &= ~EXTI_InitStruct->EXTI_Line;
  125. EXTI->FTSR &= ~EXTI_InitStruct->EXTI_Line;
  126. /* Select the trigger for the selected external interrupts */
  127. if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling)
  128. {
  129. /* Rising Falling edge */
  130. EXTI->RTSR |= EXTI_InitStruct->EXTI_Line;
  131. EXTI->FTSR |= EXTI_InitStruct->EXTI_Line;
  132. }
  133. else
  134. {
  135. tmp = (uint32_t)EXTI_BASE;
  136. tmp += EXTI_InitStruct->EXTI_Trigger;
  137. *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
  138. }
  139. }
  140. else
  141. {
  142. tmp += EXTI_InitStruct->EXTI_Mode;
  143. /* Disable the selected external lines */
  144. *(__IO uint32_t *) tmp &= ~EXTI_InitStruct->EXTI_Line;
  145. }
  146. }
  147. /**
  148. * @brief Fills each EXTI_InitStruct member with its reset value.
  149. * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure which will
  150. * be initialized.
  151. * @retval None
  152. */
  153. void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct)
  154. {
  155. EXTI_InitStruct->EXTI_Line = EXTI_LINENONE;
  156. EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt;
  157. EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling;
  158. EXTI_InitStruct->EXTI_LineCmd = DISABLE;
  159. }
  160. /**
  161. * @brief Generates a Software interrupt on selected EXTI line.
  162. * @param EXTI_Line: specifies the EXTI line on which the software interrupt
  163. * will be generated.
  164. * This parameter can be any combination of EXTI_Linex where x can be (0..22)
  165. * @retval None
  166. */
  167. void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
  168. {
  169. /* Check the parameters */
  170. assert_param(IS_EXTI_LINE(EXTI_Line));
  171. EXTI->SWIER |= EXTI_Line;
  172. }
  173. /**
  174. * @}
  175. */
  176. /** @defgroup EXTI_Group2 Interrupts and flags management functions
  177. * @brief Interrupts and flags management functions
  178. *
  179. @verbatim
  180. ===============================================================================
  181. Interrupts and flags management functions
  182. ===============================================================================
  183. @endverbatim
  184. * @{
  185. */
  186. /**
  187. * @brief Checks whether the specified EXTI line flag is set or not.
  188. * @param EXTI_Line: specifies the EXTI line flag to check.
  189. * This parameter can be EXTI_Linex where x can be(0..22)
  190. * @retval The new state of EXTI_Line (SET or RESET).
  191. */
  192. FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
  193. {
  194. FlagStatus bitstatus = RESET;
  195. /* Check the parameters */
  196. assert_param(IS_GET_EXTI_LINE(EXTI_Line));
  197. if ((EXTI->PR & EXTI_Line) != (uint32_t)RESET)
  198. {
  199. bitstatus = SET;
  200. }
  201. else
  202. {
  203. bitstatus = RESET;
  204. }
  205. return bitstatus;
  206. }
  207. /**
  208. * @brief Clears the EXTI's line pending flags.
  209. * @param EXTI_Line: specifies the EXTI lines flags to clear.
  210. * This parameter can be any combination of EXTI_Linex where x can be (0..22)
  211. * @retval None
  212. */
  213. void EXTI_ClearFlag(uint32_t EXTI_Line)
  214. {
  215. /* Check the parameters */
  216. assert_param(IS_EXTI_LINE(EXTI_Line));
  217. EXTI->PR = EXTI_Line;
  218. }
  219. /**
  220. * @brief Checks whether the specified EXTI line is asserted or not.
  221. * @param EXTI_Line: specifies the EXTI line to check.
  222. * This parameter can be EXTI_Linex where x can be(0..22)
  223. * @retval The new state of EXTI_Line (SET or RESET).
  224. */
  225. ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
  226. {
  227. ITStatus bitstatus = RESET;
  228. uint32_t enablestatus = 0;
  229. /* Check the parameters */
  230. assert_param(IS_GET_EXTI_LINE(EXTI_Line));
  231. enablestatus = EXTI->IMR & EXTI_Line;
  232. if (((EXTI->PR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  233. {
  234. bitstatus = SET;
  235. }
  236. else
  237. {
  238. bitstatus = RESET;
  239. }
  240. return bitstatus;
  241. }
  242. /**
  243. * @brief Clears the EXTI's line pending bits.
  244. * @param EXTI_Line: specifies the EXTI lines to clear.
  245. * This parameter can be any combination of EXTI_Linex where x can be (0..22)
  246. * @retval None
  247. */
  248. void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
  249. {
  250. /* Check the parameters */
  251. assert_param(IS_EXTI_LINE(EXTI_Line));
  252. EXTI->PR = EXTI_Line;
  253. }
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @}
  259. */
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */