gd32f3x0_exti.c 8.2 KB

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