gd32e23x_exti.c 8.3 KB

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