gd32f3x0_cmp.c 7.6 KB

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