gd32f30x_exti.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*!
  2. \file gd32f30x_exti.c
  3. \brief EXTI driver
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, 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 "gd32f30x_exti.h"
  30. #define EXTI_REG_RESET_VALUE ((uint32_t)0x00000000U)
  31. /*!
  32. \brief deinitialize the EXTI
  33. \param[in] none
  34. \param[out] none
  35. \retval none
  36. */
  37. void exti_deinit(void)
  38. {
  39. /* reset the value of all the EXTI registers */
  40. EXTI_INTEN = EXTI_REG_RESET_VALUE;
  41. EXTI_EVEN = EXTI_REG_RESET_VALUE;
  42. EXTI_RTEN = EXTI_REG_RESET_VALUE;
  43. EXTI_FTEN = EXTI_REG_RESET_VALUE;
  44. EXTI_SWIEV = EXTI_REG_RESET_VALUE;
  45. }
  46. /*!
  47. \brief initialize the EXTI line x
  48. \param[in] linex: EXTI line number, refer to exti_line_enum
  49. only one parameter can be selected which is shown as below:
  50. \arg EXTI_x (x=0..19): EXTI line x
  51. \param[in] mode: interrupt or event mode, refer to exti_mode_enum
  52. only one parameter can be selected which is shown as below:
  53. \arg EXTI_INTERRUPT: interrupt mode
  54. \arg EXTI_EVENT: event mode
  55. \param[in] trig_type: interrupt trigger type, refer to exti_trig_type_enum
  56. only one parameter can be selected which is shown as below:
  57. \arg EXTI_TRIG_RISING: rising edge trigger
  58. \arg EXTI_TRIG_FALLING: falling trigger
  59. \arg EXTI_TRIG_BOTH: rising and falling trigger
  60. \arg EXTI_TRIG_NONE: without rising edge or falling edge trigger
  61. \param[out] none
  62. \retval none
  63. */
  64. void exti_init(exti_line_enum linex, exti_mode_enum mode, exti_trig_type_enum trig_type)
  65. {
  66. /* reset the EXTI line x */
  67. EXTI_INTEN &= ~(uint32_t)linex;
  68. EXTI_EVEN &= ~(uint32_t)linex;
  69. EXTI_RTEN &= ~(uint32_t)linex;
  70. EXTI_FTEN &= ~(uint32_t)linex;
  71. /* set the EXTI mode and enable the interrupts or events from EXTI line x */
  72. switch(mode){
  73. case EXTI_INTERRUPT:
  74. EXTI_INTEN |= (uint32_t)linex;
  75. break;
  76. case EXTI_EVENT:
  77. EXTI_EVEN |= (uint32_t)linex;
  78. break;
  79. default:
  80. break;
  81. }
  82. /* set the EXTI trigger type */
  83. switch(trig_type){
  84. case EXTI_TRIG_RISING:
  85. EXTI_RTEN |= (uint32_t)linex;
  86. EXTI_FTEN &= ~(uint32_t)linex;
  87. break;
  88. case EXTI_TRIG_FALLING:
  89. EXTI_RTEN &= ~(uint32_t)linex;
  90. EXTI_FTEN |= (uint32_t)linex;
  91. break;
  92. case EXTI_TRIG_BOTH:
  93. EXTI_RTEN |= (uint32_t)linex;
  94. EXTI_FTEN |= (uint32_t)linex;
  95. break;
  96. case EXTI_TRIG_NONE:
  97. default:
  98. break;
  99. }
  100. }
  101. /*!
  102. \brief enable the interrupts from EXTI line x
  103. \param[in] linex: EXTI line number, refer to exti_line_enum
  104. only one parameter can be selected which is shown as below:
  105. \arg EXTI_x (x=0..19): EXTI line x
  106. \param[out] none
  107. \retval none
  108. */
  109. void exti_interrupt_enable(exti_line_enum linex)
  110. {
  111. EXTI_INTEN |= (uint32_t)linex;
  112. }
  113. /*!
  114. \brief disable the interrupt from EXTI line x
  115. \param[in] linex: EXTI line number, refer to exti_line_enum
  116. only one parameter can be selected which is shown as below:
  117. \arg EXTI_x (x=0..19): EXTI line x
  118. \param[out] none
  119. \retval none
  120. */
  121. void exti_interrupt_disable(exti_line_enum linex)
  122. {
  123. EXTI_INTEN &= ~(uint32_t)linex;
  124. }
  125. /*!
  126. \brief enable the events from EXTI line x
  127. \param[in] linex: EXTI line number, refer to exti_line_enum
  128. only one parameter can be selected which is shown as below:
  129. \arg EXTI_x (x=0..19): EXTI line x
  130. \param[out] none
  131. \retval none
  132. */
  133. void exti_event_enable(exti_line_enum linex)
  134. {
  135. EXTI_EVEN |= (uint32_t)linex;
  136. }
  137. /*!
  138. \brief disable the events from EXTI line x
  139. \param[in] linex: EXTI line number, refer to exti_line_enum
  140. only one parameter can be selected which is shown as below:
  141. \arg EXTI_x (x=0..19): EXTI line x
  142. \param[out] none
  143. \retval none
  144. */
  145. void exti_event_disable(exti_line_enum linex)
  146. {
  147. EXTI_EVEN &= ~(uint32_t)linex;
  148. }
  149. /*!
  150. \brief enable the software interrupt event from EXTI line x
  151. \param[in] linex: EXTI line number, refer to exti_line_enum
  152. only one parameter can be selected which is shown as below:
  153. \arg EXTI_x (x=0..19): EXTI line x
  154. \param[out] none
  155. \retval none
  156. */
  157. void exti_software_interrupt_enable(exti_line_enum linex)
  158. {
  159. EXTI_SWIEV |= (uint32_t)linex;
  160. }
  161. /*!
  162. \brief disable the software interrupt event from EXTI line x
  163. \param[in] linex: EXTI line number, refer to exti_line_enum
  164. only one parameter can be selected which is shown as below:
  165. \arg EXTI_x (x=0..19): EXTI line x
  166. \param[out] none
  167. \retval none
  168. */
  169. void exti_software_interrupt_disable(exti_line_enum linex)
  170. {
  171. EXTI_SWIEV &= ~(uint32_t)linex;
  172. }
  173. /*!
  174. \brief get EXTI line x interrupt pending flag
  175. \param[in] linex: EXTI line number, refer to exti_line_enum
  176. only one parameter can be selected which is shown as below:
  177. \arg EXTI_x (x=0..19): EXTI line x
  178. \param[out] none
  179. \retval FlagStatus: status of flag (RESET or SET)
  180. */
  181. FlagStatus exti_flag_get(exti_line_enum linex)
  182. {
  183. if(RESET != (EXTI_PD & (uint32_t)linex)){
  184. return SET;
  185. }else{
  186. return RESET;
  187. }
  188. }
  189. /*!
  190. \brief clear EXTI line x interrupt pending flag
  191. \param[in] linex: EXTI line number, refer to exti_line_enum
  192. only one parameter can be selected which is shown as below:
  193. \arg EXTI_x (x=0..19): EXTI line x
  194. \param[out] none
  195. \retval none
  196. */
  197. void exti_flag_clear(exti_line_enum linex)
  198. {
  199. EXTI_PD = (uint32_t)linex;
  200. }
  201. /*!
  202. \brief get EXTI line x interrupt pending flag
  203. \param[in] linex: EXTI line number, refer to exti_line_enum
  204. only one parameter can be selected which is shown as below:
  205. \arg EXTI_x (x=0..19): EXTI line x
  206. \param[out] none
  207. \retval FlagStatus: status of flag (RESET or SET)
  208. */
  209. FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
  210. {
  211. if(RESET != (EXTI_PD & (uint32_t)linex)) {
  212. return SET;
  213. } else {
  214. return RESET;
  215. }
  216. }
  217. /*!
  218. \brief clear EXTI line x interrupt pending flag
  219. \param[in] linex: EXTI line number, refer to exti_line_enum
  220. only one parameter can be selected which is shown as below:
  221. \arg EXTI_x (x=0..19): EXTI line x
  222. \param[out] none
  223. \retval none
  224. */
  225. void exti_interrupt_flag_clear(exti_line_enum linex)
  226. {
  227. EXTI_PD = (uint32_t)linex;
  228. }