gd32f4xx_exti.c 8.1 KB

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