stm32f4xx_exti.c 9.8 KB

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