ch32v20x_exti.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v20x_exti.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2021/06/06
  6. * Description : This file provides all the EXTI firmware functions.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #include "ch32v20x_exti.h"
  13. /* No interrupt selected */
  14. #define EXTI_LINENONE ((uint32_t)0x00000)
  15. /*********************************************************************
  16. * @fn EXTI_DeInit
  17. *
  18. * @brief Deinitializes the EXTI peripheral registers to their default
  19. * reset values.
  20. *
  21. * @return none.
  22. */
  23. void EXTI_DeInit(void)
  24. {
  25. EXTI->INTENR = 0x00000000;
  26. EXTI->EVENR = 0x00000000;
  27. EXTI->RTENR = 0x00000000;
  28. EXTI->FTENR = 0x00000000;
  29. EXTI->INTFR = 0x000FFFFF;
  30. }
  31. /*********************************************************************
  32. * @fn EXTI_Init
  33. *
  34. * @brief Initializes the EXTI peripheral according to the specified
  35. * parameters in the EXTI_InitStruct.
  36. *
  37. * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure
  38. *
  39. * @return none.
  40. */
  41. void EXTI_Init(EXTI_InitTypeDef *EXTI_InitStruct)
  42. {
  43. uint32_t tmp = 0;
  44. tmp = (uint32_t)EXTI_BASE;
  45. if(EXTI_InitStruct->EXTI_LineCmd != DISABLE)
  46. {
  47. EXTI->INTENR &= ~EXTI_InitStruct->EXTI_Line;
  48. EXTI->EVENR &= ~EXTI_InitStruct->EXTI_Line;
  49. tmp += EXTI_InitStruct->EXTI_Mode;
  50. *(__IO uint32_t *)tmp |= EXTI_InitStruct->EXTI_Line;
  51. EXTI->RTENR &= ~EXTI_InitStruct->EXTI_Line;
  52. EXTI->FTENR &= ~EXTI_InitStruct->EXTI_Line;
  53. if(EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling)
  54. {
  55. EXTI->RTENR |= EXTI_InitStruct->EXTI_Line;
  56. EXTI->FTENR |= EXTI_InitStruct->EXTI_Line;
  57. }
  58. else
  59. {
  60. tmp = (uint32_t)EXTI_BASE;
  61. tmp += EXTI_InitStruct->EXTI_Trigger;
  62. *(__IO uint32_t *)tmp |= EXTI_InitStruct->EXTI_Line;
  63. }
  64. }
  65. else
  66. {
  67. tmp += EXTI_InitStruct->EXTI_Mode;
  68. *(__IO uint32_t *)tmp &= ~EXTI_InitStruct->EXTI_Line;
  69. }
  70. }
  71. /*********************************************************************
  72. * @fn EXTI_StructInit
  73. *
  74. * @brief Fills each EXTI_InitStruct member with its reset value.
  75. *
  76. * @param EXTI_InitStruct - pointer to a EXTI_InitTypeDef structure
  77. *
  78. * @return none.
  79. */
  80. void EXTI_StructInit(EXTI_InitTypeDef *EXTI_InitStruct)
  81. {
  82. EXTI_InitStruct->EXTI_Line = EXTI_LINENONE;
  83. EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt;
  84. EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling;
  85. EXTI_InitStruct->EXTI_LineCmd = DISABLE;
  86. }
  87. /*********************************************************************
  88. * @fn EXTI_GenerateSWInterrupt
  89. *
  90. * @brief Generates a Software interrupt.
  91. *
  92. * @param EXTI_Line - specifies the EXTI lines to be enabled or disabled.
  93. *
  94. * @return none.
  95. */
  96. void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
  97. {
  98. EXTI->SWIEVR |= EXTI_Line;
  99. }
  100. /*********************************************************************
  101. * @fn EXTI_GetFlagStatus
  102. *
  103. * @brief Checks whether the specified EXTI line flag is set or not.
  104. *
  105. * @param EXTI_Line - specifies the EXTI lines to be enabled or disabled.
  106. *
  107. * @return The new state of EXTI_Line (SET or RESET).
  108. */
  109. FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
  110. {
  111. FlagStatus bitstatus = RESET;
  112. if((EXTI->INTFR & EXTI_Line) != (uint32_t)RESET)
  113. {
  114. bitstatus = SET;
  115. }
  116. else
  117. {
  118. bitstatus = RESET;
  119. }
  120. return bitstatus;
  121. }
  122. /*********************************************************************
  123. * @fn EXTI_ClearFlag
  124. *
  125. * @brief Clears the EXTI's line pending flags.
  126. *
  127. * @param EXTI_Line - specifies the EXTI lines to be enabled or disabled.
  128. *
  129. * @return None
  130. */
  131. void EXTI_ClearFlag(uint32_t EXTI_Line)
  132. {
  133. EXTI->INTFR = EXTI_Line;
  134. }
  135. /*********************************************************************
  136. * @fn EXTI_GetITStatus
  137. *
  138. * @brief Checks whether the specified EXTI line is asserted or not.
  139. *
  140. * @param EXTI_Line - specifies the EXTI lines to be enabled or disabled.
  141. *
  142. * @return The new state of EXTI_Line (SET or RESET).
  143. */
  144. ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
  145. {
  146. ITStatus bitstatus = RESET;
  147. uint32_t enablestatus = 0;
  148. enablestatus = EXTI->INTENR & EXTI_Line;
  149. if(((EXTI->INTFR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  150. {
  151. bitstatus = SET;
  152. }
  153. else
  154. {
  155. bitstatus = RESET;
  156. }
  157. return bitstatus;
  158. }
  159. /*********************************************************************
  160. * @fn EXTI_ClearITPendingBit
  161. *
  162. * @brief Clears the EXTI's line pending bits.
  163. *
  164. * @param EXTI_Line - specifies the EXTI lines to be enabled or disabled.
  165. *
  166. * @return none
  167. */
  168. void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
  169. {
  170. EXTI->INTFR = EXTI_Line;
  171. }