stm32f0xx_exti.c 10 KB

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