1 /*!
2 \file gd32e50x_cmp.c
3 \brief CMP driver
4
5 \version 2020-03-10, V1.0.0, firmware for GD32E50x
6 \version 2020-08-26, V1.1.0, firmware for GD32E50x
7 \version 2021-03-23, V1.2.0, firmware for GD32E50x
8 \version 2021-05-19, V1.2.1, firmware for GD32E50x
9 */
10
11 /*
12 Copyright (c) 2021, GigaDevice Semiconductor Inc.
13
14 Redistribution and use in source and binary forms, with or without modification,
15 are permitted provided that the following conditions are met:
16
17 1. Redistributions of source code must retain the above copyright notice, this
18 list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright notice,
20 this list of conditions and the following disclaimer in the documentation
21 and/or other materials provided with the distribution.
22 3. Neither the name of the copyright holder nor the names of its contributors
23 may be used to endorse or promote products derived from this software without
24 specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 OF SUCH DAMAGE.
36 */
37
38 #include "gd32e50x_cmp.h"
39
40 #ifdef GD32E50X_CL
41 /*!
42 \brief deinitialize comparator
43 \param[in] none
44 \param[out] none
45 \retval none
46 */
cmp_deinit(void)47 void cmp_deinit(void)
48 {
49 rcu_periph_reset_enable(RCU_CMPRST);
50 rcu_periph_reset_disable(RCU_CMPRST);
51 }
52
53 /*!
54 \brief initialize comparator input
55 \param[in] cmp_periph: CMPx(x=1,3,5)
56 \param[in] inverting_input: CMP inverting input select
57 only one parameter can be selected which is shown as below:
58 \arg CMP_1_4VREFINT: VREFINT *1/4 input
59 \arg CMP_1_2VREFINT: VREFINT *1/2 input
60 \arg CMP_3_4VREFINT: VREFINT *3/4 input
61 \arg CMP_VREFINT: VREFINT input
62 \arg CMP_PA4: PA4 input
63 \arg CMP_PA5: PA5 input
64 \arg CMP_PA2: PA2 input(only CMP1)
65 \arg CMP_PB_2_15: PB2 input(only CMP1),PB15 input(only CMP3)
66 \param[out] none
67 \retval none
68 */
cmp_input_init(cmp_enum cmp_periph,inverting_input_enum inverting_input)69 void cmp_input_init(cmp_enum cmp_periph,inverting_input_enum inverting_input)
70 {
71 uint32_t CMPx_CS = 0;
72 if(CMP1 == cmp_periph){
73 /* initialize comparator 1 mode */
74 CMPx_CS = CMP_CS(cmp_periph);
75 CMPx_CS &= ~(uint32_t)(CMP1_CS_CMP1MSEL);
76 CMPx_CS |= (uint32_t)(CS_CMPMSEL(inverting_input));
77 CMP_CS(cmp_periph) = CMPx_CS;
78 }else if(CMP3 == cmp_periph){
79 /* initialize comparator 3 mode */
80 CMPx_CS = CMP_CS(cmp_periph);
81 CMPx_CS &= ~(uint32_t)(CMP3_CS_CMP3MSEL);
82 CMPx_CS |= (uint32_t)(CS_CMPMSEL(inverting_input));
83 CMP_CS(cmp_periph) = CMPx_CS;
84 }else if(CMP5 == cmp_periph){
85 /* initialize comparator 5 mode */
86 CMPx_CS = CMP_CS(cmp_periph);
87 CMPx_CS &= ~(uint32_t)(CMP5_CS_CMP5MSEL);
88 CMPx_CS |= (uint32_t)(CS_CMPMSEL(inverting_input));
89 CMP_CS(cmp_periph) = CMPx_CS;
90 }
91 }
92
93 /*!
94 \brief initialize comparator output
95 \param[in] cmp_periph: CMPx(x=1,3,5)
96 \param[in] output_slection: CMP output select
97 only one parameter can be selected which is shown as below:
98 \arg CMP_OUTPUT_NONE: output no selection
99 \arg CMP_OUTPUT_TIMER0_BKIN: TIMER0 break input(CMP1,CMP3,CMP5)
100 \arg CMP_OUTPUT_TIMER2IC2_TIMER1IC1: TIMER2 channel2 input capture(CMP3),TIMER1 channel1 input capture(CMP5)
101 \arg CMP_OUTPUT_TIMER0IC0: TIMER0 channel0 input capture(CMP1)
102 \arg CMP_OUTPUT_TIMER1IC3: TIMER1 channel3 input capture(CMP1)
103 \arg CMP_OUTPUT_TIMER2IC0: TIMER2 channel0 input capture(CMP1)
104 \param[in] output_polarity: CMP output polarity select
105 only one parameter can be selected which is shown as below:
106 \arg CMP_OUTPUT_POLARITY_INVERTED: output is inverted
107 \arg CMP_OUTPUT_POLARITY_NOINVERTED: output is not inverted
108 \param[out] none
109 \retval none
110 */
cmp_output_init(cmp_enum cmp_periph,cmp_output_enum output_slection,uint32_t output_polarity)111 void cmp_output_init(cmp_enum cmp_periph,cmp_output_enum output_slection, uint32_t output_polarity)
112 {
113 uint32_t CMPx_CS = 0;
114 if(CMP1 == cmp_periph){
115 /* initialize comparator 1 output */
116 CMPx_CS = CMP_CS(cmp_periph);
117 CMPx_CS &= ~(uint32_t)CMP1_CS_CMP1OSEL;
118 CMPx_CS |= (uint32_t)CS_CMPOSEL(output_slection);
119 /* output polarity */
120 if(CMP_OUTPUT_POLARITY_INVERTED == output_polarity){
121 CMPx_CS |= (uint32_t)CMP1_CS_CMP1PL;
122 }else{
123 CMPx_CS &= ~(uint32_t)CMP1_CS_CMP1PL;
124 }
125 CMP_CS(cmp_periph) = CMPx_CS;
126 }else if(CMP3 == cmp_periph){
127 /* initialize comparator 3 output */
128 CMPx_CS = CMP_CS(cmp_periph);
129 CMPx_CS &= ~(uint32_t)CMP3_CS_CMP3OSEL;
130 CMPx_CS |= (uint32_t)CS_CMPOSEL(output_slection);
131 /* output polarity */
132 if(CMP_OUTPUT_POLARITY_INVERTED == output_polarity){
133 CMPx_CS |= (uint32_t)CMP3_CS_CMP3PL;
134 }else{
135 CMPx_CS &= ~(uint32_t)CMP3_CS_CMP3PL;
136 }
137 CMP_CS(cmp_periph) = CMPx_CS;
138 }else if(CMP5 == cmp_periph){
139 /* initialize comparator 5 output */
140 CMPx_CS = CMP_CS(cmp_periph);
141 CMPx_CS &= ~(uint32_t)CMP5_CS_CMP5OSEL;
142 CMPx_CS |= (uint32_t)CS_CMPOSEL(output_slection);
143 /* output polarity */
144 if(CMP_OUTPUT_POLARITY_INVERTED == output_polarity){
145 CMPx_CS |= (uint32_t)CMP5_CS_CMP5PL;
146 }else{
147 CMPx_CS &= ~(uint32_t)CMP5_CS_CMP5PL;
148 }
149 CMP_CS(cmp_periph) = CMPx_CS;
150 }
151 }
152
153 /*!
154 \brief initialize CMP output blanking
155 \param[in] cmp_periph: CMPx(x=1,3,5)
156 \param[in] output_blank: CMP output blank select
157 only one parameter can be selected which is shown as below:
158 \arg CMP_OUTPUTBLANK_NONE: no blanking
159 \arg CMP_OUTPUTBLANK_TIMER2_IC3: select TIMER2_CH3 as blanking source(CMP3)
160 \arg CMP_OUTPUTBLANK_TIMER1IC2: select TIMER1_CH2 as blanking source(CMP1)
161 \arg CMP_OUTPUTBLANK_TIMER2IC2_TIMER1IC3: Select TIMER2_CH2 as blanking source(CMP1), select TIMER1_CH3 as blanking source(CMP5)
162 \param[out] none
163 \retval none
164 */
cmp_outputblank_init(cmp_enum cmp_periph,cmp_outputblank_enum output_blank)165 void cmp_outputblank_init(cmp_enum cmp_periph,cmp_outputblank_enum output_blank)
166 {
167 uint32_t CMPx_CS = 0;
168 if(CMP1 == cmp_periph){
169 CMPx_CS = CMP_CS(cmp_periph);
170 CMPx_CS &= ~(uint32_t)(CMP1_CS_CMP1BLK);
171 CMPx_CS |= (uint32_t)(CS_CMPMBLK(output_blank));
172 CMP_CS(cmp_periph) = CMPx_CS;
173 }else if(CMP3 == cmp_periph){
174 CMPx_CS = CMP_CS(cmp_periph);
175 CMPx_CS &= ~(uint32_t)(CMP3_CS_CMP3BLK);
176 CMPx_CS |= (uint32_t)(CS_CMPMBLK(output_blank));
177 CMP_CS(cmp_periph) = CMPx_CS;
178 }else if(CMP5 == cmp_periph){
179 CMPx_CS = CMP_CS(cmp_periph);
180 CMPx_CS &= ~(uint32_t)(CMP5_CS_CMP5BLK);
181 CMPx_CS |= (uint32_t)(CS_CMPMBLK(output_blank));
182 CMP_CS(cmp_periph) = CMPx_CS;
183 }
184 }
185
186 /*!
187 \brief enable comparator
188 \param[in] cmp_periph: CMPx(x=1,3,5)
189 \param[out] none
190 \retval none
191 */
cmp_enable(cmp_enum cmp_periph)192 void cmp_enable(cmp_enum cmp_periph)
193 {
194 CMP_CS(cmp_periph) |= CMP_CS_CMPEN;
195 }
196
197 /*!
198 \brief disable comparator
199 \param[in] cmp_periph: CMPx(x=1,3,5)
200 \param[out] none
201 \retval none
202 */
cmp_disable(cmp_enum cmp_periph)203 void cmp_disable(cmp_enum cmp_periph)
204 {
205 CMP_CS(cmp_periph) &= ~CMP_CS_CMPEN;
206 }
207
208 /*!
209 \brief lock the comparator
210 \param[in] cmp_periph: CMPx(x=1,3,5)
211 \param[out] none
212 \retval none
213 */
cmp_lock_enable(cmp_enum cmp_periph)214 void cmp_lock_enable(cmp_enum cmp_periph)
215 {
216 CMP_CS(cmp_periph) |= CMP_CS_CMPLK;
217 }
218
219 /*!
220 \brief get output level
221 \param[in] cmp_periph: CMPx(x=1,3,5)
222 \param[out] none
223 \retval the output level
224 */
cmp_output_level_get(cmp_enum cmp_periph)225 uint32_t cmp_output_level_get(cmp_enum cmp_periph)
226 {
227 if(CMP_CS(cmp_periph) & CMP_CS_CMPO){
228 return CMP_OUTPUTLEVEL_HIGH;
229 }else{
230 return CMP_OUTPUTLEVEL_LOW;
231 }
232 }
233
234 #endif /* GD32E50x_CL */
235