1 /**
2 ******************************************************************************
3 * @file ft32f0xx_comp.c
4 * @author FMD AE
5 * @brief This file provides firmware functions to manage the following
6 * functionalities of the comparators (COMP1 and COMP2) peripheral:
7 * + Comparators configuration
8 * + Window mode control
9 * @version V1.0.0
10 * @data 2021-07-01
11 ******************************************************************************
12 */
13
14 /* Includes ------------------------------------------------------------------*/
15 #include "ft32f0xx_comp.h"
16
17 #define COMP_CSR_CLEAR_MASK ((uint32_t)0x00003FFE)
18
19 /**
20 * @brief Deinitializes COMP peripheral registers to their default reset values.
21 * @note Deinitialization can't be performed if the COMP configuration is locked.
22 * To unlock the configuration, perform a system reset.
23 * @param None
24 * @retval None
25 */
COMP_DeInit(void)26 void COMP_DeInit(void)
27 {
28 COMP->CSR = ((uint32_t)0x00000000); /*!< Set COMP_CSR register to reset value */
29
30 #if defined(FT32F072xB)
31 COMP->CSR2 = ((uint32_t)0x00000000);
32 #endif
33 }
34
35 /**
36 * @brief Initializes the COMP peripheral according to the specified parameters
37 * in COMP_InitStruct
38 * @note If the selected comparator is locked, initialization can't be performed.
39 * To unlock the configuration, perform a system reset.
40 * @note By default, PA1 is selected as COMP1 non inverting input.
41 *
42 * @param COMP_Selection: the selected comparator.
43 * This parameter can be one of the following values:
44 * @arg COMP_Selection_COMP1: COMP1 selected
45 * @arg COMP_Selection_COMP2: COMP2 selected
46 * @arg COMP_Selection_COMP3: COMP3 selected
47 * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure that contains
48 * the configuration information for the specified COMP peripheral.
49 * @retval None
50 */
COMP_Init(uint32_t COMP_Selection,COMP_InitTypeDef * COMP_InitStruct)51 void COMP_Init(uint32_t COMP_Selection, COMP_InitTypeDef* COMP_InitStruct)
52 {
53 uint32_t tmpreg = 0;
54
55 /* Check the parameters */
56 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
57 assert_param(IS_COMP_VIP_SEL(COMP_InitStruct->COMP_VipSel));
58 assert_param(IS_COMP_VINSEL(COMP_InitStruct->COMP_VinSel));
59 assert_param(IS_COMP_OUTPUT_SEL(COMP_InitStruct->COMP_OutputSel));
60 assert_param(IS_COMP_POL(COMP_InitStruct->COMP_Pol));
61
62 if (COMP_Selection != COMP_Selection_COMP3)
63 {
64 /*!< Get the COMP_CSR register value */
65 tmpreg = COMP->CSR;
66
67 /*!< Clear the bits */
68 tmpreg &= (uint32_t) ~(COMP_CSR_CLEAR_MASK<<COMP_Selection);
69
70 /*!< Configure COMP: COMP_VipSel, COMP_VinSel, COMP_OutputSel value and COMP_Pol */
71 tmpreg |= (uint32_t)((COMP_InitStruct->COMP_VipSel | COMP_InitStruct->COMP_VinSel|
72 COMP_InitStruct->COMP_OutputSel | COMP_InitStruct->COMP_Pol));
73
74 /*!< Write to COMP_CSR register */
75 COMP->CSR = tmpreg;
76 }
77 else
78 {
79 #if defined(FT32F072xB)
80 /*!< Get the COMP_CSR register value */
81 tmpreg = COMP->CSR2;
82
83 /*!< Clear the bits */
84 tmpreg &= (uint32_t) ~(COMP_CSR_CLEAR_MASK);
85
86 /*!< Configure COMP: COMP_VipSel, COMP_VinSel, COMP_OutputSel value and COMP_Pol */
87 tmpreg |= (uint32_t)((COMP_InitStruct->COMP_VipSel | COMP_InitStruct->COMP_VinSel|
88 COMP_InitStruct->COMP_OutputSel | COMP_InitStruct->COMP_Pol));
89
90 /*!< Write to COMP_CSR2 register */
91 COMP->CSR2 = tmpreg;
92 #endif
93 }
94 }
95
96 /**
97 * @brief Fills each COMP_InitStruct member with its default value.
98 * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure which will
99 * be initialized.
100 * @retval None
101 */
COMP_StructInit(COMP_InitTypeDef * COMP_InitStruct)102 void COMP_StructInit(COMP_InitTypeDef* COMP_InitStruct)
103 {
104 #if defined(FT32F072xB)
105 COMP_InitStruct->COMP_VipSel = 0;
106 COMP_InitStruct->COMP_VinSel = 0;
107 COMP_InitStruct->COMP_OutputSel = 0;
108 COMP_InitStruct->COMP_Pol = 0;
109 #else
110 COMP_InitStruct->COMP_VipSel = NCOMP_VIP_SEL_PAD_PA1;
111 COMP_InitStruct->COMP_VinSel = NCOMP_VIN_SEL_PAD_PA0 | PCOMP_VIN_SEL_PAD_PA2;
112 COMP_InitStruct->COMP_OutputSel = 0;
113 COMP_InitStruct->COMP_Pol = 0;
114 #endif
115 }
116
117 /**
118 * @brief Enable or disable the COMP peripheral.
119 * @note If the selected comparator is locked, enable/disable can't be performed.
120 * To unlock the configuration, perform a system reset.
121 * @param COMP_Selection: the selected comparator.
122 * This parameter can be one of the following values:
123 * @arg COMP_Selection_COMP1: COMP1 selected
124 * @arg COMP_Selection_COMP2: COMP2 selected
125 * @arg COMP_Selection_COMP3: COMP3 selected
126 * @param NewState: new state of the COMP peripheral.
127 * This parameter can be: ENABLE or DISABLE.
128 * @note When enabled, the comparator compares the non inverting input with
129 * the inverting input and the comparison result is available on comparator output.
130 * @note When disabled, the comparator doesn't perform comparison and the
131 * output level is low.
132 * @retval None
133 */
COMP_Cmd(uint32_t COMP_Selection,FunctionalState NewState)134 void COMP_Cmd(uint32_t COMP_Selection, FunctionalState NewState)
135 {
136 /* Check the parameters */
137 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
138 assert_param(IS_FUNCTIONAL_STATE(NewState));
139
140 if(COMP_Selection != COMP_Selection_COMP3)
141 {
142 if (NewState != DISABLE)
143 {
144 /* Enable the selected COMP peripheral */
145 COMP->CSR |= (uint32_t) (1<<COMP_Selection);
146 }
147 else
148 {
149 /* Disable the selected COMP peripheral */
150 COMP->CSR &= (uint32_t)(~((uint32_t)1<<COMP_Selection));
151 }
152 }
153 else
154 {
155 #if defined(FT32F072xB)
156 if (NewState != DISABLE)
157 {
158 /* Enable the selected COMP peripheral */
159 COMP->CSR2 |= (uint32_t) (1);
160 }
161 else
162 {
163 /* Disable the selected COMP peripheral */
164 COMP->CSR2 &= (uint32_t)(~((uint32_t)1));
165 }
166 #endif
167
168 }
169 }
170
171 /**
172 * @brief Return the output level (high or low) of the selected comparator.
173 * @note The output level depends on the selected polarity.
174 * @note If the polarity is not inverted:
175 * - Comparator output is low when the non-inverting input is at a lower
176 * voltage than the inverting input
177 * - Comparator output is high when the non-inverting input is at a higher
178 * voltage than the inverting input
179 * @note If the polarity is inverted:
180 * - Comparator output is high when the non-inverting input is at a lower
181 * voltage than the inverting input
182 * - Comparator output is low when the non-inverting input is at a higher
183 * voltage than the inverting input
184 * @param COMP_Selection: the selected comparator.
185 * This parameter can be one of the following values:
186 * @arg COMP_Selection_COMP1: COMP1 selected
187 * @arg COMP_Selection_COMP2: COMP2 selected
188 * @arg COMP_Selection_COMP3: COMP3 selected
189 * @retval Returns the selected comparator output level: low or high.
190 *
191 */
COMP_GetOutputLevel(uint32_t COMP_Selection)192 uint32_t COMP_GetOutputLevel(uint32_t COMP_Selection)
193 {
194 uint32_t compout = 0x0;
195
196 /* Check the parameters */
197 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
198
199 if(COMP_Selection != COMP_Selection_COMP3)
200 {
201 /* Check if selected comparator output is high */
202 if ((COMP->CSR & (COMP_CSR_COMP1OUT<<COMP_Selection)) != 0)
203 {
204 compout = COMP_OutputLevel_High;
205 }
206 else
207 {
208 compout = COMP_OutputLevel_Low;
209 }
210 }
211 else
212 {
213 #if defined(FT32F072xB)
214 /* Check if selected comparator output is high */
215 if ( (COMP->CSR2 & COMP_CSR_COMP3OUT) != 0)
216 {
217 compout = COMP_OutputLevel_High;
218 }
219 else
220 {
221 compout = COMP_OutputLevel_Low;
222 }
223 #endif
224 }
225
226 /* Return the comparator output level */
227 return (uint32_t)(compout);
228 }
229
230 /**
231 * @}
232 */
233 /**
234 * @brief Enables or disables the window mode.
235 * @note In window mode, COMP1 and COMP2 non inverting inputs are connected
236 * together and only COMP1 non inverting input (PA1) can be used.
237 * @param NewState: new state of the window mode.
238 * This parameter can be :
239 * @arg ENABLE: COMP1 and COMP2 non inverting inputs are connected together.
240 * @arg DISABLE: OMP1 and COMP2 non inverting inputs are disconnected.
241 * @retval None
242 */
COMP_WindowCmd(FunctionalState NewState)243 void COMP_WindowCmd(FunctionalState NewState)
244 {
245 /* Check the parameters */
246 assert_param(IS_FUNCTIONAL_STATE(NewState));
247
248 if (NewState != DISABLE)
249 {
250 /* Enable the window mode */
251 COMP->CSR |= (uint32_t) COMP_CSR_WNDWEN;
252 }
253 else
254 {
255 /* Disable the window mode */
256 COMP->CSR &= (uint32_t)(~COMP_CSR_WNDWEN);
257 }
258 }
259
260 /**
261 * @}
262 */
263 /**
264 * @brief Lock the selected comparator (COMP1/COMP2) configuration.
265 * @note Locking the configuration means that all control bits are read-only.
266 * To unlock the comparator configuration, perform a system reset.
267 * @param COMP_Selection: selects the comparator to be locked
268 * This parameter can be a value of the following values:
269 * @arg COMP_Selection_COMP1: COMP1 configuration is locked.
270 * @arg COMP_Selection_COMP2: COMP2 configuration is locked.
271 * @arg COMP_Selection_COMP3: COMP3 configuration is locked.
272 * @retval None
273 */
COMP_LockConfig(uint32_t COMP_Selection)274 void COMP_LockConfig(uint32_t COMP_Selection)
275 {
276 /* Check the parameter */
277 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
278
279 if(COMP_Selection != COMP_Selection_COMP3)
280 {
281 /* Set the lock bit corresponding to selected comparator */
282 COMP->CSR |= (uint32_t) (COMP_CSR_NCOMPLOCK<<COMP_Selection);
283 }
284 else
285 {
286 #if defined(FT32F072xB)
287 /* Set the lock bit corresponding to selected comparator */
288 COMP->CSR2 |= (uint32_t) (COMP_CSR_COMP3LOCK);
289 #endif
290 }
291 }
292
293 /**
294 * @}
295 */
296
297 /**
298 * @}
299 */
300
301 /**
302 * @}
303 */
304
305 /**
306 * @}
307 */
308
309 /************************ (C) COPYRIGHT FMD *****END OF FILE****/
310