gd32f3x0_cmp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*!
  2. \file gd32f3x0_cmp.c
  3. \brief CMP driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0_cmp.h"
  10. /*!
  11. \brief deinitialize comparator
  12. \param[in] none
  13. \param[out] none
  14. \retval none
  15. */
  16. void cmp_deinit(void)
  17. {
  18. CMP_CS = ((uint32_t)0x00000000U);
  19. }
  20. /*!
  21. \brief initialize comparator mode
  22. \param[in] cmp_periph
  23. \arg CMP0: comparator 0
  24. \arg CMP1: comparator 1
  25. \param[in] operating_mode
  26. \arg CMP_HIGHSPEED: high speed mode
  27. \arg CMP_MIDDLESPEED: medium speed mode
  28. \arg CMP_LOWSPEED: low speed mode
  29. \arg CMP_VERYLOWSPEED: very-low speed mode
  30. \param[in] inverting_input
  31. \arg CMP_1_4VREFINT: VREFINT *1/4 input
  32. \arg CMP_1_2VREFINT: VREFINT *1/2 input
  33. \arg CMP_3_4VREFINT: VREFINT *3/4 input
  34. \arg CMP_VREFINT: VREFINT input
  35. \arg CMP_DAC: PA4 (DAC) input
  36. \arg CMP_PA5: PA5 input
  37. \arg CMP_PA_0_2: PA0 or PA2 input
  38. \param[in] hysteresis
  39. \arg CMP_HYSTERESIS_NO: output no hysteresis
  40. \arg CMP_HYSTERESIS_LOW: output low hysteresis
  41. \arg CMP_HYSTERESIS_MIDDLE: output middle hysteresis
  42. \arg CMP_HYSTERESIS_HIGH: output high hysteresis
  43. \param[out] none
  44. \retval none
  45. */
  46. void cmp_mode_init(uint32_t cmp_periph, operating_mode_enum operating_mode, inverting_input_enum inverting_input, cmp_hysteresis_enum output_hysteresis)
  47. {
  48. if(CMP0 == cmp_periph){
  49. /* initialize comparator 0 mode */
  50. CMP_CS |= CS_CMP0M(operating_mode) | CS_CMP0MSEL(inverting_input) | CS_CMP0HST(output_hysteresis);
  51. }else{
  52. /* initialize comparator 1 mode */
  53. CMP_CS |= CS_CMP1M(operating_mode) | CS_CMP1MSEL(inverting_input) | CS_CMP1HST(output_hysteresis);
  54. }
  55. }
  56. /*!
  57. \brief initialize comparator output
  58. \param[in] cmp_periph
  59. \arg CMP0: comparator 0
  60. \arg CMP1: comparator 1
  61. \param[in] output_slection
  62. \arg CMP_OUTPUT_NONE: output no selection
  63. \arg CMP_OUTPUT_TIMER0BKIN: TIMER 0 break input
  64. \arg CMP_OUTPUT_TIMER0IC0: TIMER 0 channel0 input capture
  65. \arg CMP_OUTPUT_TIMER0OCPRECLR: TIMER 0 OCPRE_CLR input
  66. \arg CMP_OUTPUT_TIMER1IC3: TIMER 1 channel3 input capture
  67. \arg CMP_OUTPUT_TIMER1OCPRECLR: TIMER 1 OCPRE_CLR input
  68. \arg CMP_OUTPUT_TIMER2IC0: TIMER 2 channel0 input capture
  69. \arg CMP_OUTPUT_TIMER2OCPRECLR: TIMER 2 OCPRE_CLR input
  70. \param[in] output_polarity
  71. \arg CMP_OUTPUT_POLARITY_INVERTED: output is inverted
  72. \arg CMP_OUTPUT_POLARITY_NOINVERTED: output is not inverted
  73. \param[out] none
  74. \retval none
  75. */
  76. void cmp_output_init(uint32_t cmp_periph, cmp_output_enum output_slection, uint32_t output_polarity)
  77. {
  78. /* initialize comparator 0 output */
  79. if(CMP0 == cmp_periph){
  80. CMP_CS |= CS_CMP0OSEL(output_slection);
  81. /* output polarity */
  82. if(CMP_OUTPUT_POLARITY_INVERTED == output_polarity){
  83. CMP_CS |= CMP_CS_CMP0PL;
  84. }else{
  85. CMP_CS &= ~CMP_CS_CMP0PL;
  86. }
  87. }else{
  88. CMP_CS |= CS_CMP1OSEL(output_slection);
  89. if(CMP_OUTPUT_POLARITY_INVERTED == output_polarity){
  90. CMP_CS |= CMP_CS_CMP1PL;
  91. }else{
  92. CMP_CS &= ~CMP_CS_CMP1PL;
  93. }
  94. }
  95. }
  96. /*!
  97. \brief enable comparator
  98. \param[in] cmp_periph
  99. \arg CMP0: comparator 0
  100. \arg CMP1: comparator 1
  101. \param[out] none
  102. \retval none
  103. */
  104. void cmp_enable(uint32_t cmp_periph)
  105. {
  106. if(CMP0 == cmp_periph){
  107. CMP_CS |= CMP_CS_CMP0EN;
  108. }else{
  109. CMP_CS |= CMP_CS_CMP1EN;
  110. }
  111. }
  112. /*!
  113. \brief disable comparator
  114. \param[in] cmp_periph
  115. \arg CMP0: comparator 0
  116. \arg CMP1: comparator 1
  117. \param[out] none
  118. \retval none
  119. */
  120. void cmp_disable(uint32_t cmp_periph)
  121. {
  122. if(CMP0 == cmp_periph){
  123. CMP_CS &= ~CMP_CS_CMP0EN;
  124. }else{
  125. CMP_CS &= ~CMP_CS_CMP1EN;
  126. }
  127. }
  128. /*!
  129. \brief enable comparator switch
  130. \param[in] none
  131. \param[out] none
  132. \retval none
  133. */
  134. void cmp_switch_enable(void)
  135. {
  136. CMP_CS |= CMP_CS_CMP0SW;
  137. }
  138. /*!
  139. \brief disable comparator switch
  140. \param[in] none
  141. \param[out] none
  142. \retval none
  143. */
  144. void cmp_switch_disable(void)
  145. {
  146. CMP_CS &= ~CMP_CS_CMP0SW;
  147. }
  148. /*!
  149. \brief get output level
  150. \param[in] cmp_periph
  151. \arg CMP0: comparator 0
  152. \arg CMP1: comparator 1
  153. \param[out] none
  154. \retval the output level
  155. */
  156. uint32_t cmp_output_level_get(uint32_t cmp_periph)
  157. {
  158. if(CMP0 == cmp_periph){
  159. if(CMP_CS & CMP_CS_CMP0O){
  160. return CMP_OUTPUTLEVEL_HIGH;
  161. }else{
  162. return CMP_OUTPUTLEVEL_LOW;
  163. }
  164. }else{
  165. if(CMP_CS & CMP_CS_CMP1O){
  166. return CMP_OUTPUTLEVEL_HIGH;
  167. }else{
  168. return CMP_OUTPUTLEVEL_LOW;
  169. }
  170. }
  171. }
  172. /*!
  173. \brief enable the window mode
  174. \param[in] none
  175. \param[out] none
  176. \retval none
  177. */
  178. void cmp_window_enable(void)
  179. {
  180. CMP_CS |= CMP_CS_WNDEN;
  181. }
  182. /*!
  183. \brief disable the window mode
  184. \param[in] none
  185. \param[out] none
  186. \retval none
  187. */
  188. void cmp_window_disable(void)
  189. {
  190. CMP_CS &= ~CMP_CS_WNDEN;
  191. }
  192. /*!
  193. \brief lock the comparator
  194. \param[in] cmp_periph
  195. \arg CMP0: comparator 0
  196. \arg CMP1: comparator 1
  197. \param[out] none
  198. \retval none
  199. */
  200. void cmp_lock_enable(uint32_t cmp_periph)
  201. {
  202. if(CMP0 == cmp_periph){
  203. CMP_CS |= CMP_CS_CMP0LK;
  204. }else{
  205. CMP_CS |= CMP_CS_CMP1LK;
  206. }
  207. }
  208. /*!
  209. \brief unlock the comparator
  210. \param[in] cmp_periph
  211. \arg CMP0: comparator 0
  212. \arg CMP1: comparator 1
  213. \param[out] none
  214. \retval none
  215. */
  216. void cmp_lock_disable(uint32_t cmp_periph)
  217. {
  218. if(CMP0 == cmp_periph){
  219. CMP_CS &= ~CMP_CS_CMP0LK;
  220. }else{
  221. CMP_CS &= ~CMP_CS_CMP1LK;
  222. }
  223. }