gd32e23x_exti.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*!
  2. \file gd32e23x_exti.c
  3. \brief EXTI driver
  4. \version 2024-02-22, V2.1.0, firmware for GD32E23x
  5. */
  6. /*
  7. Copyright (c) 2024, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32e23x_exti.h"
  30. #define EXTI_INTEN_RESET_VAL ((uint32_t)0x0F900000U)
  31. #define EXTI_REG_RESET_VAL ((uint32_t)0x00000000U)
  32. /*!
  33. \brief reset the value of all EXTI registers with initial values
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void exti_deinit(void)
  39. {
  40. /* reset the value of the EXTI registers */
  41. EXTI_INTEN = EXTI_INTEN_RESET_VAL;
  42. EXTI_EVEN = EXTI_REG_RESET_VAL;
  43. EXTI_RTEN = EXTI_REG_RESET_VAL;
  44. EXTI_FTEN = EXTI_REG_RESET_VAL;
  45. EXTI_SWIEV = EXTI_REG_RESET_VAL;
  46. }
  47. /*!
  48. \brief initialize the EXTI line x
  49. \param[in] linex: EXTI line number, refer to exti_line_enum
  50. only one parameter can be selected which is shown as below:
  51. \arg EXTI_x (x=0..17,19,21): EXTI line x
  52. \param[in] mode: interrupt or event mode, refer to exti_mode_enum
  53. only one parameter can be selected which is shown as below:
  54. \arg EXTI_INTERRUPT: interrupt mode
  55. \arg EXTI_EVENT: event mode
  56. \param[in] trig_type: interrupt trigger type, refer to exti_trig_type_enum
  57. only one parameter can be selected which is shown as below:
  58. \arg EXTI_TRIG_RISING: rising edge trigger
  59. \arg EXTI_TRIG_FALLING: falling trigger
  60. \arg EXTI_TRIG_BOTH: rising and falling trigger
  61. \arg EXTI_TRIG_NONE: without rising edge or falling edge trigger
  62. \param[out] none
  63. \retval none
  64. */
  65. void exti_init(exti_line_enum linex, exti_mode_enum mode, exti_trig_type_enum trig_type)
  66. {
  67. /* reset the EXTI line x */
  68. EXTI_INTEN &= ~(uint32_t)linex;
  69. EXTI_EVEN &= ~(uint32_t)linex;
  70. EXTI_RTEN &= ~(uint32_t)linex;
  71. EXTI_FTEN &= ~(uint32_t)linex;
  72. /* set the EXTI mode and enable the interrupts or events from EXTI line x */
  73. switch(mode) {
  74. case EXTI_INTERRUPT:
  75. EXTI_INTEN |= (uint32_t)linex;
  76. break;
  77. case EXTI_EVENT:
  78. EXTI_EVEN |= (uint32_t)linex;
  79. break;
  80. default:
  81. break;
  82. }
  83. /* set the EXTI trigger type */
  84. switch(trig_type) {
  85. case EXTI_TRIG_RISING:
  86. EXTI_RTEN |= (uint32_t)linex;
  87. EXTI_FTEN &= ~(uint32_t)linex;
  88. break;
  89. case EXTI_TRIG_FALLING:
  90. EXTI_RTEN &= ~(uint32_t)linex;
  91. EXTI_FTEN |= (uint32_t)linex;
  92. break;
  93. case EXTI_TRIG_BOTH:
  94. EXTI_RTEN |= (uint32_t)linex;
  95. EXTI_FTEN |= (uint32_t)linex;
  96. break;
  97. case EXTI_TRIG_NONE:
  98. default:
  99. break;
  100. }
  101. }
  102. /*!
  103. \brief enable the interrupts from EXTI line x
  104. \param[in] linex: EXTI line number, refer to exti_line_enum
  105. only one parameter can be selected which is shown as below:
  106. \arg EXTI_x (x=0..27): EXTI line x
  107. \param[out] none
  108. \retval none
  109. */
  110. void exti_interrupt_enable(exti_line_enum linex)
  111. {
  112. EXTI_INTEN |= (uint32_t)linex;
  113. }
  114. /*!
  115. \brief disable the interrupts from EXTI line x
  116. \param[in] linex: EXTI line number, refer to exti_line_enum
  117. only one parameter can be selected which is shown as below:
  118. \arg EXTI_x (x=0..27): EXTI line x
  119. \param[out] none
  120. \retval none
  121. */
  122. void exti_interrupt_disable(exti_line_enum linex)
  123. {
  124. EXTI_INTEN &= ~(uint32_t)linex;
  125. }
  126. /*!
  127. \brief enable the events from EXTI line x
  128. \param[in] linex: EXTI line number, refer to exti_line_enum
  129. only one parameter can be selected which is shown as below:
  130. \arg EXTI_x (x=0..27): EXTI line x
  131. \param[out] none
  132. \retval none
  133. */
  134. void exti_event_enable(exti_line_enum linex)
  135. {
  136. EXTI_EVEN |= (uint32_t)linex;
  137. }
  138. /*!
  139. \brief disable the events from EXTI line x
  140. \param[in] linex: EXTI line number, refer to exti_line_enum
  141. only one parameter can be selected which is shown as below:
  142. \arg EXTI_x (x=0..27): EXTI line x
  143. \param[out] none
  144. \retval none
  145. */
  146. void exti_event_disable(exti_line_enum linex)
  147. {
  148. EXTI_EVEN &= ~(uint32_t)linex;
  149. }
  150. /*!
  151. \brief enable the software interrupt event from EXTI line x
  152. \param[in] linex: EXTI line number, refer to exti_line_enum
  153. only one parameter can be selected which is shown as below:
  154. \arg EXTI_x (x=0..17,19,21): EXTI line x
  155. \param[out] none
  156. \retval none
  157. */
  158. void exti_software_interrupt_enable(exti_line_enum linex)
  159. {
  160. EXTI_SWIEV |= (uint32_t)linex;
  161. }
  162. /*!
  163. \brief disable the software interrupt event from EXTI line x
  164. \param[in] linex: EXTI line number, refer to exti_line_enum
  165. only one parameter can be selected which is shown as below:
  166. \arg EXTI_x (x=0..17,19,21): EXTI line x
  167. \param[out] none
  168. \retval none
  169. */
  170. void exti_software_interrupt_disable(exti_line_enum linex)
  171. {
  172. EXTI_SWIEV &= ~(uint32_t)linex;
  173. }
  174. /*!
  175. \brief get EXTI line x interrupt pending flag
  176. \param[in] linex: EXTI line number, refer to exti_line_enum
  177. only one parameter can be selected which is shown as below:
  178. \arg EXTI_x (x=0..17,19,21): EXTI line x
  179. \param[out] none
  180. \retval FlagStatus: status of flag (RESET or SET)
  181. */
  182. FlagStatus exti_flag_get(exti_line_enum linex)
  183. {
  184. if(RESET != (EXTI_PD & (uint32_t)linex)) {
  185. return SET;
  186. } else {
  187. return RESET;
  188. }
  189. }
  190. /*!
  191. \brief clear EXTI line x interrupt pending flag
  192. \param[in] linex: EXTI line number, refer to exti_line_enum
  193. only one parameter can be selected which is shown as below:
  194. \arg EXTI_x (x=0..17,19,21): EXTI line x
  195. \param[out] none
  196. \retval none
  197. */
  198. void exti_flag_clear(exti_line_enum linex)
  199. {
  200. EXTI_PD = (uint32_t)linex;
  201. }
  202. /*!
  203. \brief get EXTI line x interrupt pending flag
  204. \param[in] linex: EXTI line number, refer to exti_line_enum
  205. only one parameter can be selected which is shown as below:
  206. \arg EXTI_x (x=0..17,19,21): EXTI line x
  207. \param[out] none
  208. \retval FlagStatus: status of flag (RESET or SET)
  209. */
  210. FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
  211. {
  212. if(RESET != (EXTI_PD & (uint32_t)linex)) {
  213. return SET;
  214. } else {
  215. return RESET;
  216. }
  217. }
  218. /*!
  219. \brief clear EXTI line x interrupt pending flag
  220. \param[in] linex: EXTI line number, refer to exti_line_enum
  221. only one parameter can be selected which is shown as below:
  222. \arg EXTI_x (x=0..17,19,21): EXTI line x
  223. \param[out] none
  224. \retval none
  225. */
  226. void exti_interrupt_flag_clear(exti_line_enum linex)
  227. {
  228. EXTI_PD = (uint32_t)linex;
  229. }