gd32e23x_cmp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*!
  2. \file gd32f23x_cmp.c
  3. \brief CMP driver
  4. \version 2024-02-22, V2.1.0, firmware for GD32E23x
  5. */
  6. /*
  7. Copyright (c) 2022, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32e23x_cmp.h"
  30. /*!
  31. \brief CMP deinit
  32. \param[in] cmp_periph
  33. \arg CMP0: comparator 0
  34. \param[out] none
  35. \retval none
  36. */
  37. void cmp_deinit(cmp_enum cmp_periph)
  38. {
  39. if(CMP0 == cmp_periph){
  40. CMP_CS &= ((uint32_t)0xFFFF0000U);
  41. }else{
  42. }
  43. }
  44. /*!
  45. \brief CMP mode init
  46. \param[in] cmp_periph
  47. \arg CMP0: comparator 0
  48. \param[in] operating_mode
  49. \arg CMP_MODE_HIGHSPEED: high speed mode
  50. \arg CMP_MODE_MIDDLESPEED: medium speed mode
  51. \arg CMP_MODE_LOWSPEED: low speed mode
  52. \arg CMP_MODE_VERYLOWSPEED: very-low speed mode
  53. \param[in] inverting_input
  54. \arg CMP_INVERTING_INPUT_1_4VREFINT: VREFINT *1/4 input
  55. \arg CMP_INVERTING_INPUT_1_2VREFINT: VREFINT *1/2 input
  56. \arg CMP_INVERTING_INPUT_3_4VREFINT: VREFINT *3/4 input
  57. \arg CMP_INVERTING_INPUT_VREFINT: VREFINT input
  58. \arg CMP_INVERTING_INPUT_PA4: PA4 input
  59. \arg CMP_INVERTING_INPUT_PA5: PA5 input
  60. \arg CMP_INVERTING_INPUT_PA0_PA2: PA0 for CMP0 or PA2 for CMP1 as inverting input
  61. \param[in] output_hysteresis
  62. \arg CMP_HYSTERESIS_NO: output no hysteresis
  63. \arg CMP_HYSTERESIS_LOW: output low hysteresis
  64. \arg CMP_HYSTERESIS_MIDDLE: output middle hysteresis
  65. \arg CMP_HYSTERESIS_HIGH: output high hysteresis
  66. \param[out] none
  67. \retval none
  68. */
  69. void cmp_mode_init(cmp_enum cmp_periph, uint32_t operating_mode, uint32_t inverting_input, uint32_t output_hysteresis)
  70. {
  71. uint32_t temp = 0U;
  72. if(CMP0 == cmp_periph){
  73. /* initialize comparator 0 mode */
  74. temp = CMP_CS;
  75. temp &= ~(uint32_t)(CMP_CS_CMP0M | CMP_CS_CMP0MSEL | CMP_CS_CMP0HST);
  76. temp |= (uint32_t)(operating_mode | inverting_input | output_hysteresis);
  77. CMP_CS = temp;
  78. }else{
  79. }
  80. }
  81. /*!
  82. \brief CMP output init
  83. \param[in] cmp_periph
  84. \arg CMP0: comparator 0
  85. \param[in] output_selection
  86. \arg CMP_OUTPUT_NONE: CMP output none
  87. \arg CMP_OUTPUT_TIMER0_BKIN: CMP output TIMER0 break input
  88. \arg CMP_OUTPUT_TIMER0_IC0: CMP output TIMER0_CH0 input capture
  89. \arg CMP_OUTPUT_TIMER0_OCPRECLR: CMP output TIMER0 OCPRE_CLR input
  90. \arg CMP_OUTPUT_TIMER2_IC0: CMP output TIMER2_CH0 input capture
  91. \arg CMP_OUTPUT_TIMER2_OCPRECLR: CMP output TIMER2 OCPRE_CLR input
  92. \param[in] output_polarity
  93. \arg CMP_OUTPUT_POLARITY_INVERTED: output is inverted
  94. \arg CMP_OUTPUT_POLARITY_NONINVERTED: output is not inverted
  95. \param[out] none
  96. \retval none
  97. */
  98. void cmp_output_init(cmp_enum cmp_periph, uint32_t output_selection, uint32_t output_polarity)
  99. {
  100. uint32_t temp = 0U;
  101. if(CMP0 == cmp_periph){
  102. /* initialize comparator 0 output */
  103. temp = CMP_CS;
  104. temp &= ~(uint32_t)CMP_CS_CMP0OSEL;
  105. temp |= (uint32_t)output_selection;
  106. /* output polarity */
  107. if(CMP_OUTPUT_POLARITY_INVERTED == output_polarity){
  108. temp |= (uint32_t)CMP_CS_CMP0PL;
  109. }else{
  110. temp &= ~(uint32_t)CMP_CS_CMP0PL;
  111. }
  112. CMP_CS = temp;
  113. }else{
  114. }
  115. }
  116. /*!
  117. \brief enable CMP
  118. \param[in] cmp_periph
  119. \arg CMP0: comparator 0
  120. \param[out] none
  121. \retval none
  122. */
  123. void cmp_enable(cmp_enum cmp_periph)
  124. {
  125. if(CMP0 == cmp_periph){
  126. CMP_CS |= (uint32_t)CMP_CS_CMP0EN;
  127. }else{
  128. }
  129. }
  130. /*!
  131. \brief disable CMP
  132. \param[in] cmp_periph
  133. \arg CMP0: comparator 0
  134. \param[out] none
  135. \retval none
  136. */
  137. void cmp_disable(cmp_enum cmp_periph)
  138. {
  139. if(CMP0 == cmp_periph){
  140. CMP_CS &= ~(uint32_t)CMP_CS_CMP0EN;
  141. }else{
  142. }
  143. }
  144. /*!
  145. \brief enable CMP switch
  146. \param[in] none
  147. \param[out] none
  148. \retval none
  149. */
  150. void cmp_switch_enable(void)
  151. {
  152. CMP_CS |= (uint32_t)CMP_CS_CMP0SW;
  153. }
  154. /*!
  155. \brief disable CMP switch
  156. \param[in] none
  157. \param[out] none
  158. \retval none
  159. */
  160. void cmp_switch_disable(void)
  161. {
  162. CMP_CS &= ~(uint32_t)CMP_CS_CMP0SW;
  163. }
  164. /*!
  165. \brief lock the CMP
  166. \param[in] cmp_periph
  167. \arg CMP0: comparator 0
  168. \param[out] none
  169. \retval none
  170. */
  171. void cmp_lock_enable(cmp_enum cmp_periph)
  172. {
  173. if(CMP0 == cmp_periph){
  174. /* lock CMP0 */
  175. CMP_CS |= (uint32_t)CMP_CS_CMP0LK;
  176. }else{
  177. }
  178. }
  179. /*!
  180. \brief get output level
  181. \param[in] cmp_periph
  182. \arg CMP0: comparator 0
  183. \param[out] none
  184. \retval the output level
  185. */
  186. uint32_t cmp_output_level_get(cmp_enum cmp_periph)
  187. {
  188. /* get output level of CMP0 */
  189. if((uint32_t)RESET != (CMP_CS & CMP_CS_CMP0O)) {
  190. return CMP_OUTPUTLEVEL_HIGH;
  191. }else{
  192. return CMP_OUTPUTLEVEL_LOW;
  193. }
  194. }