gd32f10x_exti.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*!
  2. \file gd32f10x_exti.c
  3. \brief EXTI driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f10x_exti.h"
  33. #define EXTI_REG_RESET_VALUE ((uint32_t)0x00000000U)
  34. /*!
  35. \brief deinitialize the EXTI
  36. \param[in] none
  37. \param[out] none
  38. \retval none
  39. */
  40. void exti_deinit(void)
  41. {
  42. /* reset the value of all the EXTI registers */
  43. EXTI_INTEN = EXTI_REG_RESET_VALUE;
  44. EXTI_EVEN = EXTI_REG_RESET_VALUE;
  45. EXTI_RTEN = EXTI_REG_RESET_VALUE;
  46. EXTI_FTEN = EXTI_REG_RESET_VALUE;
  47. EXTI_SWIEV = EXTI_REG_RESET_VALUE;
  48. }
  49. /*!
  50. \brief initialize the EXTI
  51. \param[in] linex: EXTI line number, refer to exti_line_enum
  52. only one parameter can be selected which is shown as below:
  53. \arg EXTI_x (x=0..19): EXTI line x
  54. \param[in] mode: interrupt or event mode, refer to exti_mode_enum
  55. only one parameter can be selected which is shown as below:
  56. \arg EXTI_INTERRUPT: interrupt mode
  57. \arg EXTI_EVENT: event mode
  58. \param[in] trig_type: trigger type, refer to exti_trig_type_enum
  59. only one parameter can be selected which is shown as below:
  60. \arg EXTI_TRIG_RISING: rising edge trigger
  61. \arg EXTI_TRIG_FALLING: falling edge trigger
  62. \arg EXTI_TRIG_BOTH: rising edge and falling edge trigger
  63. \param[out] none
  64. \retval none
  65. */
  66. void exti_init(exti_line_enum linex, exti_mode_enum mode, 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. 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..19): 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 enable the events 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..19): EXTI line x
  119. \param[out] none
  120. \retval none
  121. */
  122. void exti_event_enable(exti_line_enum linex)
  123. {
  124. EXTI_EVEN |= (uint32_t)linex;
  125. }
  126. /*!
  127. \brief disable the interrupt 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..19): EXTI line x
  131. \param[out] none
  132. \retval none
  133. */
  134. void exti_interrupt_disable(exti_line_enum linex)
  135. {
  136. EXTI_INTEN &= ~(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..19): 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 get EXTI lines flag
  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..19): EXTI line x
  155. \param[out] none
  156. \retval FlagStatus: status of flag (RESET or SET)
  157. */
  158. FlagStatus exti_flag_get(exti_line_enum linex)
  159. {
  160. if(RESET != (EXTI_PD & (uint32_t)linex)){
  161. return SET;
  162. }else{
  163. return RESET;
  164. }
  165. }
  166. /*!
  167. \brief clear EXTI lines pending flag
  168. \param[in] linex: EXTI line number, refer to exti_line_enum
  169. only one parameter can be selected which is shown as below:
  170. \arg EXTI_x (x=0..19): EXTI line x
  171. \param[out] none
  172. \retval none
  173. */
  174. void exti_flag_clear(exti_line_enum linex)
  175. {
  176. EXTI_PD = (uint32_t)linex;
  177. }
  178. /*!
  179. \brief get EXTI lines flag when the interrupt flag is set
  180. \param[in] linex: EXTI line number, refer to exti_line_enum
  181. only one parameter can be selected which is shown as below:
  182. \arg EXTI_x (x=0..19): EXTI line x
  183. \param[out] none
  184. \retval FlagStatus: status of flag (RESET or SET)
  185. */
  186. FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
  187. {
  188. uint32_t flag_left, flag_right;
  189. flag_left = EXTI_PD & (uint32_t)linex;
  190. flag_right = EXTI_INTEN & (uint32_t)linex;
  191. if((RESET != flag_left) && (RESET != flag_right)){
  192. return SET;
  193. }else{
  194. return RESET;
  195. }
  196. }
  197. /*!
  198. \brief clear EXTI lines pending flag
  199. \param[in] linex: EXTI line number, refer to exti_line_enum
  200. only one parameter can be selected which is shown as below:
  201. \arg EXTI_x (x=0..19): EXTI line x
  202. \param[out] none
  203. \retval none
  204. */
  205. void exti_interrupt_flag_clear(exti_line_enum linex)
  206. {
  207. EXTI_PD = (uint32_t)linex;
  208. }
  209. /*!
  210. \brief enable EXTI software interrupt event
  211. \param[in] linex: EXTI line number, refer to exti_line_enum
  212. only one parameter can be selected which is shown as below:
  213. \arg EXTI_x (x=0..19): EXTI line x
  214. \param[out] none
  215. \retval none
  216. */
  217. void exti_software_interrupt_enable(exti_line_enum linex)
  218. {
  219. EXTI_SWIEV |= (uint32_t)linex;
  220. }
  221. /*!
  222. \brief disable EXTI software interrupt event
  223. \param[in] linex: EXTI line number, refer to exti_line_enum
  224. only one parameter can be selected which is shown as below:
  225. \arg EXTI_x (x=0..19): EXTI line x
  226. \param[out] none
  227. \retval none
  228. */
  229. void exti_software_interrupt_disable(exti_line_enum linex)
  230. {
  231. EXTI_SWIEV &= ~(uint32_t)linex;
  232. }