stm32f4xx_exti.c 8.9 KB

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