stm32f4xx_exti.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_exti.c
  4. * @author MCD Application Team
  5. * @version V1.8.0
  6. * @date 04-November-2016
  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. * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
  45. *
  46. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  47. * You may not use this file except in compliance with the License.
  48. * You may obtain a copy of the License at:
  49. *
  50. * http://www.st.com/software_license_agreement_liberty_v2
  51. *
  52. * Unless required by applicable law or agreed to in writing, software
  53. * distributed under the License is distributed on an "AS IS" BASIS,
  54. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  55. * See the License for the specific language governing permissions and
  56. * limitations under the License.
  57. *
  58. ******************************************************************************
  59. */
  60. /* Includes ------------------------------------------------------------------*/
  61. #include "stm32f4xx_exti.h"
  62. /** @addtogroup STM32F4xx_StdPeriph_Driver
  63. * @{
  64. */
  65. /** @defgroup EXTI
  66. * @brief EXTI driver modules
  67. * @{
  68. */
  69. /* Private typedef -----------------------------------------------------------*/
  70. /* Private define ------------------------------------------------------------*/
  71. #define EXTI_LINENONE ((uint32_t)0x00000) /* No interrupt selected */
  72. /* Private macro -------------------------------------------------------------*/
  73. /* Private variables ---------------------------------------------------------*/
  74. /* Private function prototypes -----------------------------------------------*/
  75. /* Private functions ---------------------------------------------------------*/
  76. /** @defgroup EXTI_Private_Functions
  77. * @{
  78. */
  79. /** @defgroup EXTI_Group1 Initialization and Configuration functions
  80. * @brief Initialization and Configuration functions
  81. *
  82. @verbatim
  83. ===============================================================================
  84. ##### Initialization and Configuration functions #####
  85. ===============================================================================
  86. @endverbatim
  87. * @{
  88. */
  89. /**
  90. * @brief Deinitializes the EXTI peripheral registers to their default reset values.
  91. * @param None
  92. * @retval None
  93. */
  94. void EXTI_DeInit(void)
  95. {
  96. EXTI->IMR = 0x00000000;
  97. EXTI->EMR = 0x00000000;
  98. EXTI->RTSR = 0x00000000;
  99. EXTI->FTSR = 0x00000000;
  100. EXTI->PR = 0x007FFFFF;
  101. }
  102. /**
  103. * @brief Initializes the EXTI peripheral according to the specified
  104. * parameters in the EXTI_InitStruct.
  105. * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure
  106. * that contains the configuration information for the EXTI peripheral.
  107. * @retval None
  108. */
  109. void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
  110. {
  111. uint32_t tmp = 0;
  112. /* Check the parameters */
  113. assert_param(IS_EXTI_MODE(EXTI_InitStruct->EXTI_Mode));
  114. assert_param(IS_EXTI_TRIGGER(EXTI_InitStruct->EXTI_Trigger));
  115. assert_param(IS_EXTI_LINE(EXTI_InitStruct->EXTI_Line));
  116. assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->EXTI_LineCmd));
  117. tmp = (uint32_t)EXTI_BASE;
  118. if (EXTI_InitStruct->EXTI_LineCmd != DISABLE)
  119. {
  120. /* Clear EXTI line configuration */
  121. EXTI->IMR &= ~EXTI_InitStruct->EXTI_Line;
  122. EXTI->EMR &= ~EXTI_InitStruct->EXTI_Line;
  123. tmp += EXTI_InitStruct->EXTI_Mode;
  124. *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
  125. /* Clear Rising Falling edge configuration */
  126. EXTI->RTSR &= ~EXTI_InitStruct->EXTI_Line;
  127. EXTI->FTSR &= ~EXTI_InitStruct->EXTI_Line;
  128. /* Select the trigger for the selected external interrupts */
  129. if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling)
  130. {
  131. /* Rising Falling edge */
  132. EXTI->RTSR |= EXTI_InitStruct->EXTI_Line;
  133. EXTI->FTSR |= EXTI_InitStruct->EXTI_Line;
  134. }
  135. else
  136. {
  137. tmp = (uint32_t)EXTI_BASE;
  138. tmp += EXTI_InitStruct->EXTI_Trigger;
  139. *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
  140. }
  141. }
  142. else
  143. {
  144. tmp += EXTI_InitStruct->EXTI_Mode;
  145. /* Disable the selected external lines */
  146. *(__IO uint32_t *) tmp &= ~EXTI_InitStruct->EXTI_Line;
  147. }
  148. }
  149. /**
  150. * @brief Fills each EXTI_InitStruct member with its reset value.
  151. * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure which will
  152. * be initialized.
  153. * @retval None
  154. */
  155. void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct)
  156. {
  157. EXTI_InitStruct->EXTI_Line = EXTI_LINENONE;
  158. EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt;
  159. EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling;
  160. EXTI_InitStruct->EXTI_LineCmd = DISABLE;
  161. }
  162. /**
  163. * @brief Generates a Software interrupt on selected EXTI line.
  164. * @param EXTI_Line: specifies the EXTI line on which the software interrupt
  165. * will be generated.
  166. * This parameter can be any combination of EXTI_Linex where x can be (0..22)
  167. * @retval None
  168. */
  169. void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
  170. {
  171. /* Check the parameters */
  172. assert_param(IS_EXTI_LINE(EXTI_Line));
  173. EXTI->SWIER |= EXTI_Line;
  174. }
  175. /**
  176. * @}
  177. */
  178. /** @defgroup EXTI_Group2 Interrupts and flags management functions
  179. * @brief Interrupts and flags management functions
  180. *
  181. @verbatim
  182. ===============================================================================
  183. ##### Interrupts and flags management functions #####
  184. ===============================================================================
  185. @endverbatim
  186. * @{
  187. */
  188. /**
  189. * @brief Checks whether the specified EXTI line flag is set or not.
  190. * @param EXTI_Line: specifies the EXTI line flag to check.
  191. * This parameter can be EXTI_Linex where x can be(0..22)
  192. * @retval The new state of EXTI_Line (SET or RESET).
  193. */
  194. FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
  195. {
  196. FlagStatus bitstatus = RESET;
  197. /* Check the parameters */
  198. assert_param(IS_GET_EXTI_LINE(EXTI_Line));
  199. if ((EXTI->PR & EXTI_Line) != (uint32_t)RESET)
  200. {
  201. bitstatus = SET;
  202. }
  203. else
  204. {
  205. bitstatus = RESET;
  206. }
  207. return bitstatus;
  208. }
  209. /**
  210. * @brief Clears the EXTI's line pending flags.
  211. * @param EXTI_Line: specifies the EXTI lines flags to clear.
  212. * This parameter can be any combination of EXTI_Linex where x can be (0..22)
  213. * @retval None
  214. */
  215. void EXTI_ClearFlag(uint32_t EXTI_Line)
  216. {
  217. /* Check the parameters */
  218. assert_param(IS_EXTI_LINE(EXTI_Line));
  219. EXTI->PR = EXTI_Line;
  220. }
  221. /**
  222. * @brief Checks whether the specified EXTI line is asserted or not.
  223. * @param EXTI_Line: specifies the EXTI line to check.
  224. * This parameter can be EXTI_Linex where x can be(0..22)
  225. * @retval The new state of EXTI_Line (SET or RESET).
  226. */
  227. ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
  228. {
  229. FlagStatus bitstatus = RESET;
  230. /* Check the parameters */
  231. assert_param(IS_GET_EXTI_LINE(EXTI_Line));
  232. if ((EXTI->PR & EXTI_Line) != (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. */
  266. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/