gd32e23x_cmp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*!
  2. \file gd32e23x_cmp.c
  3. \brief CMP driver
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. \version 2021-05-19, V1.1.1, firmware for GD32E23x
  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 "gd32e23x_cmp.h"
  32. /*!
  33. \brief deinitialize comparator
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void cmp_deinit(void)
  39. {
  40. CMP_CS = ((uint32_t)0x00000000U);
  41. }
  42. /*!
  43. \brief initialize comparator mode
  44. \param[in] operating_mode
  45. \arg CMP_HIGHSPEED: high speed mode
  46. \arg CMP_MIDDLESPEED: medium speed mode
  47. \arg CMP_LOWSPEED: low speed mode
  48. \arg CMP_VERYLOWSPEED: very-low speed mode
  49. \param[in] inverting_input
  50. \arg CMP_1_4VREFINT: VREFINT *1/4 input
  51. \arg CMP_1_2VREFINT: VREFINT *1/2 input
  52. \arg CMP_3_4VREFINT: VREFINT *3/4 input
  53. \arg CMP_VREFINT: VREFINT input
  54. \arg CMP_PA4: PA4 input
  55. \arg CMP_PA5: PA5 input
  56. \arg CMP_PA0: PA0 input
  57. \arg CMP_PA2: PA2 input
  58. \param[in] hysteresis
  59. \arg CMP_HYSTERESIS_NO: output no hysteresis
  60. \arg CMP_HYSTERESIS_LOW: output low hysteresis
  61. \arg CMP_HYSTERESIS_MIDDLE: output middle hysteresis
  62. \arg CMP_HYSTERESIS_HIGH: output high hysteresis
  63. \param[out] none
  64. \retval none
  65. */
  66. void cmp_mode_init(operating_mode_enum operating_mode, inverting_input_enum inverting_input, cmp_hysteresis_enum output_hysteresis)
  67. {
  68. uint32_t CMPx_CS = 0;
  69. /* initialize comparator mode */
  70. CMPx_CS = CMP_CS;
  71. CMPx_CS &= ~(uint32_t)(CMP_CS_CMPM | CMP_CS_CMPMSEL | CMP_CS_CMPHST );
  72. CMPx_CS |= (uint32_t)(CS_CMPM(operating_mode) | CS_CMPMSEL(inverting_input) | CS_CMPHST(output_hysteresis));
  73. CMP_CS = CMPx_CS;
  74. }
  75. /*!
  76. \brief initialize comparator output
  77. \param[in] output_slection
  78. \arg CMP_OUTPUT_NONE: output no selection
  79. \arg CMP_OUTPUT_TIMER0BKIN: TIMER 0 break input
  80. \arg CMP_OUTPUT_TIMER0IC0: TIMER 0 channel0 input capture
  81. \arg CMP_OUTPUT_TIMER0OCPRECLR: TIMER 0 OCPRE_CLR input
  82. \arg CMP_OUTPUT_TIMER2IC0: TIMER 2 channel0 input capture
  83. \arg CMP_OUTPUT_TIMER2OCPRECLR: TIMER 2 OCPRE_CLR input
  84. \param[in] output_polarity
  85. \arg CMP_OUTPUT_POLARITY_INVERTED: output is inverted
  86. \arg CMP_OUTPUT_POLARITY_NOINVERTED: output is not inverted
  87. \param[out] none
  88. \retval none
  89. */
  90. void cmp_output_init(cmp_output_enum output_selection, uint32_t output_polarity)
  91. {
  92. uint32_t CMPx_CS = 0;
  93. /* initialize comparator output */
  94. CMPx_CS = CMP_CS;
  95. CMPx_CS &= ~(uint32_t)CMP_CS_CMPOSEL;
  96. CMPx_CS |= (uint32_t)CS_CMPOSEL(output_selection);
  97. /* output polarity */
  98. if(CMP_OUTPUT_POLARITY_INVERTED == output_polarity){
  99. CMPx_CS |= (uint32_t)CMP_CS_CMPPL;
  100. }else{
  101. CMPx_CS &= ~(uint32_t)CMP_CS_CMPPL;
  102. }
  103. CMP_CS = CMPx_CS;
  104. }
  105. /*!
  106. \brief enable comparator
  107. \param[in] none
  108. \param[out] none
  109. \retval none
  110. */
  111. void cmp_enable(void)
  112. {
  113. CMP_CS |= CMP_CS_CMPEN;
  114. }
  115. /*!
  116. \brief disable comparator
  117. \param[in] none
  118. \param[out] none
  119. \retval none
  120. */
  121. void cmp_disable(void)
  122. {
  123. CMP_CS &= ~CMP_CS_CMPEN;
  124. }
  125. /*!
  126. \brief enable comparator switch
  127. \param[in] none
  128. \param[out] none
  129. \retval none
  130. */
  131. void cmp_switch_enable(void)
  132. {
  133. CMP_CS |= CMP_CS_CMPSW;
  134. }
  135. /*!
  136. \brief disable comparator switch
  137. \param[in] none
  138. \param[out] none
  139. \retval none
  140. */
  141. void cmp_switch_disable(void)
  142. {
  143. CMP_CS &= ~CMP_CS_CMPSW;
  144. }
  145. /*!
  146. \brief get output level
  147. \param[in] none
  148. \param[out] none
  149. \retval the output level
  150. */
  151. uint32_t cmp_output_level_get(void)
  152. {
  153. if(CMP_CS & CMP_CS_CMPO){
  154. return CMP_OUTPUTLEVEL_HIGH;
  155. }else{
  156. return CMP_OUTPUTLEVEL_LOW;
  157. }
  158. }
  159. /*!
  160. \brief lock the comparator
  161. \param[in] none
  162. \param[out] none
  163. \retval none
  164. */
  165. void cmp_lock_enable(void)
  166. {
  167. CMP_CS |= CMP_CS_CMPLK;
  168. }