1 /*********************************************************************************************************//**
2 * @file ht32f5xxxx_cmp.c
3 * @version $Rev:: 8260 $
4 * @date $Date:: 2024-11-05 #$
5 * @brief This file provides all the CMP firmware functions.
6 *************************************************************************************************************
7 * @attention
8 *
9 * Firmware Disclaimer Information
10 *
11 * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
12 * code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
13 * proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
14 * other intellectual property laws.
15 *
16 * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
17 * code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
18 * other than HOLTEK and the customer.
19 *
20 * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
21 * only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
22 * the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
23 * the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
24 *
25 * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
26 ************************************************************************************************************/
27
28 /* Includes ------------------------------------------------------------------------------------------------*/
29 #include "ht32f5xxxx_cmp.h"
30
31 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
32 * @{
33 */
34
35 /** @defgroup CMP CMP
36 * @brief CMP driver modules
37 * @{
38 */
39
40
41 /* Global functions ----------------------------------------------------------------------------------------*/
42 /** @defgroup CMP_Exported_Functions CMP exported functions
43 * @{
44 */
45 /*********************************************************************************************************//**
46 * @brief Deinitialize the CMP0 and CMP1 peripheral registers to their default reset values.
47 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
48 * @retval None
49 ************************************************************************************************************/
CMP_DeInit(HT_CMP_TypeDef * HT_CMPn)50 void CMP_DeInit(HT_CMP_TypeDef* HT_CMPn)
51 {
52 RSTCU_PeripReset_TypeDef RSTCUReset = {{0}};
53
54 if (HT_CMPn == NULL) // Remove the compiler warning
55 {
56 }
57
58 RSTCUReset.Bit.CMP = 1;
59 RSTCU_PeripReset(RSTCUReset, ENABLE);
60 }
61
62 /*********************************************************************************************************//**
63 * @brief Unprotect the selected comparator configuration before setting the Comparator Control Register.
64 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
65 * @retval None
66 ************************************************************************************************************/
CMP_UnprotectConfig(HT_CMP_TypeDef * HT_CMPn)67 void CMP_UnprotectConfig(HT_CMP_TypeDef* HT_CMPn)
68 {
69 /* Check the parameters */
70 Assert_Param(IS_CMP(HT_CMPn));
71
72 /* Set the unlock code corresponding to selected comparator */
73 HT_CMPn->CR = CMP_PROTECT_KEY;
74 }
75
76 /*********************************************************************************************************//**
77 * @brief Initialize the CMP peripheral according to the specified parameters in the CMP_InitStruct.
78 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
79 * @param CMP_InitStruct: pointer to a CMP_InitTypeDef structure.
80 * @retval None
81 ************************************************************************************************************/
CMP_Init(HT_CMP_TypeDef * HT_CMPn,CMP_InitTypeDef * CMP_InitStruct)82 void CMP_Init(HT_CMP_TypeDef* HT_CMPn, CMP_InitTypeDef* CMP_InitStruct)
83 {
84 /* Check the parameters */
85 Assert_Param(IS_CMP(HT_CMPn));
86 Assert_Param(IS_CMP_Wakeup_Set(CMP_InitStruct->CMP_Wakeup));
87 Assert_Param(IS_CMP_OutputSelection(CMP_InitStruct->CMP_OutputSelection));
88 Assert_Param(IS_CMP_ScalerSource(CMP_InitStruct->CMP_ScalerSource));
89 Assert_Param(IS_CMP_ScalerOutputBuf(CMP_InitStruct->CMP_ScalerOutputBuf));
90 Assert_Param(IS_CMP_ScalerEnable(CMP_InitStruct->CMP_ScalerEnable));
91 Assert_Param(IS_CMP_CoutSynchronized(CMP_InitStruct->CMP_CoutSync));
92 Assert_Param(IS_CMP_OutputPol_Set(CMP_InitStruct->CMP_OutputPol));
93 #if (LIBCFG_CMP_65x_66x_VER)
94 Assert_Param(IS_CMP_InputSelection(CMP_InitStruct->CMP_InputSelection));
95 #endif
96 Assert_Param(IS_CMP_InvInputSelection(CMP_InitStruct->CMP_InvInputSelection));
97 Assert_Param(IS_CMP_Hysteresis_Set(CMP_InitStruct->CMP_Hysteresis));
98 Assert_Param(IS_CMP_Speed_Set(CMP_InitStruct->CMP_Speed));
99
100 HT_CMPn->CR |= CMP_InitStruct->CMP_Wakeup | CMP_InitStruct->CMP_OutputSelection | CMP_InitStruct->CMP_ScalerSource | \
101 CMP_InitStruct->CMP_ScalerOutputBuf | CMP_InitStruct->CMP_ScalerEnable | CMP_InitStruct->CMP_CoutSync | \
102 CMP_InitStruct->CMP_OutputPol | CMP_InitStruct->CMP_InvInputSelection | CMP_InitStruct->CMP_Hysteresis | \
103 CMP_InitStruct->CMP_Speed;
104
105 #if (LIBCFG_CMP_65x_66x_VER)
106 HT_CMPn->CI = CMP_InitStruct->CMP_InputSelection;
107 #endif
108 }
109
110 /*********************************************************************************************************//**
111 * @brief Fill each CMP_InitStruct member with its default value.
112 * @param CMP_InitStruct: pointer to an CMP_InitTypeDef structure.
113 * @retval None
114 ************************************************************************************************************/
CMP_StructInit(CMP_InitTypeDef * CMP_InitStruct)115 void CMP_StructInit(CMP_InitTypeDef* CMP_InitStruct)
116 {
117 /* CMP_InitStruct members default value */
118 CMP_InitStruct->CMP_Wakeup = CMP_WUP_DISABLE;
119 CMP_InitStruct->CMP_OutputSelection = CMP_TRIG_NONE;
120 #if (LIBCFG_CMP_NOSCALER_SRC)
121 CMP_InitStruct->CMP_ScalerSource = 0;
122 #else
123 CMP_InitStruct->CMP_ScalerSource = CMP_SCALER_SRC_VDDA;
124 #endif
125 CMP_InitStruct->CMP_ScalerOutputBuf = CMP_SCALER_OBUF_DISABLE;
126 CMP_InitStruct->CMP_ScalerEnable = CMP_SCALER_DISABLE;
127 CMP_InitStruct->CMP_CoutSync = CMP_ASYNC_OUTPUT;
128 CMP_InitStruct->CMP_OutputPol = CMP_NONINV_OUTPUT;
129 CMP_InitStruct->CMP_InvInputSelection = CMP_EXTERNAL_CN_IN;
130 CMP_InitStruct->CMP_Hysteresis = CMP_NO_HYSTERESIS;
131 CMP_InitStruct->CMP_Speed = CMP_LOW_SPEED;
132 }
133
134 /*********************************************************************************************************//**
135 * @brief Enable or Disable the specified CMP peripheral.
136 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
137 * @param NewState: This parameter can be ENABLE or DISABLE.
138 * @retval None
139 ************************************************************************************************************/
CMP_Cmd(HT_CMP_TypeDef * HT_CMPn,ControlStatus NewState)140 void CMP_Cmd(HT_CMP_TypeDef* HT_CMPn, ControlStatus NewState)
141 {
142 /* Check the parameters */
143 Assert_Param(IS_CMP(HT_CMPn));
144 Assert_Param(IS_CONTROL_STATUS(NewState));
145
146 if (NewState != DISABLE)
147 {
148 HT_CMPn->CR |= CMP_ENABLE;
149 }
150 else
151 {
152 HT_CMPn->CR &= ~(u32)CMP_ENABLE;
153 }
154 }
155
156 /*********************************************************************************************************//**
157 * @brief Enable or Disable the specified CMP interrupts.
158 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
159 * @param CMP_INT_x: specify the CMP interrupt sources that is to be enabled or disabled.
160 * This parameter can be any combination of the following values:
161 * @arg CMP_INT_RE : CMP rising edge interrupt
162 * @arg CMP_INT_FE : CMP falling edge interrupt
163 * @param NewState: This parameter can be ENABLE or DISABLE.
164 * @retval None
165 ************************************************************************************************************/
CMP_IntConfig(HT_CMP_TypeDef * HT_CMPn,u32 CMP_INT_x,ControlStatus NewState)166 void CMP_IntConfig(HT_CMP_TypeDef* HT_CMPn, u32 CMP_INT_x, ControlStatus NewState)
167 {
168 /* Check the parameters */
169 Assert_Param(IS_CMP(HT_CMPn));
170 Assert_Param(IS_CMP_INT(CMP_INT_x));
171 Assert_Param(IS_CONTROL_STATUS(NewState));
172
173 if (NewState != DISABLE)
174 {
175 HT_CMPn->IER |= CMP_INT_x;
176 }
177 else
178 {
179 HT_CMPn->IER &= ~CMP_INT_x;
180 }
181 }
182
183 /*********************************************************************************************************//**
184 * @brief Enable or Disable the specified CMP edge detection.
185 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
186 * @param CMP_xE_Detect: specify the CMP edge detection that is to be enabled or disabled.
187 * This parameter can be any combination of the following values:
188 * @arg CMP_RE_Detect : CMP rising edge detection
189 * @arg CMP_FE_Detect : CMP falling edge detection
190 * @param NewState: This parameter can be ENABLE or DISABLE.
191 * @retval None
192 ************************************************************************************************************/
CMP_EdgeDetectConfig(HT_CMP_TypeDef * HT_CMPn,u32 CMP_xE_Detect,ControlStatus NewState)193 void CMP_EdgeDetectConfig(HT_CMP_TypeDef* HT_CMPn, u32 CMP_xE_Detect, ControlStatus NewState)
194 {
195 /* Check the parameters */
196 Assert_Param(IS_CMP(HT_CMPn));
197 Assert_Param(IS_CMP_EdgeDetect(CMP_xE_Detect));
198 Assert_Param(IS_CONTROL_STATUS(NewState));
199
200 if (NewState != DISABLE)
201 {
202 HT_CMPn->TFR = (HT_CMPn->TFR | CMP_xE_Detect) & 0xfffffffc;
203 }
204 else
205 {
206 HT_CMPn->TFR = (HT_CMPn->TFR & (~CMP_xE_Detect)) & 0xfffffffc;
207 }
208 }
209
210 /*********************************************************************************************************//**
211 * @brief Check whether the specified CM flag has been set.
212 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
213 * @param CMP_FLAG_x: specify the flag to be checked.
214 * This parameter can be any combination of the following values:
215 * @arg CMP_FLAG_RE : CMP rising edge flag
216 * @arg CMP_FLAG_FE : CMP falling edge flag
217 * @retval SET or RESET
218 ************************************************************************************************************/
CMP_GetFlagStatus(HT_CMP_TypeDef * HT_CMPn,u32 CMP_FLAG_x)219 FlagStatus CMP_GetFlagStatus(HT_CMP_TypeDef* HT_CMPn, u32 CMP_FLAG_x)
220 {
221 /* Check the parameters */
222 Assert_Param(IS_CMP(HT_CMPn));
223 Assert_Param(IS_CMP_FLAG(CMP_FLAG_x));
224
225 if ((HT_CMPn->TFR & CMP_FLAG_x) != 0)
226 {
227 return SET;
228 }
229 else
230 {
231 return RESET;
232 }
233 }
234
235 /*********************************************************************************************************//**
236 * @brief Clear flags of the specified CMP.
237 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
238 * @param CMP_FLAG_x: specify the flag to be checked.
239 * This parameter can be any combination of the following values:
240 * @arg CMP_FLAG_RE : CMP rising edge flag
241 * @arg CMP_FLAG_FE : CMP falling edge flag
242 * @retval None
243 ************************************************************************************************************/
CMP_ClearFlag(HT_CMP_TypeDef * HT_CMPn,u32 CMP_FLAG_x)244 void CMP_ClearFlag(HT_CMP_TypeDef* HT_CMPn, u32 CMP_FLAG_x)
245 {
246 /* Check the parameters */
247 Assert_Param(IS_CMP(HT_CMPn));
248 Assert_Param(IS_CMP_FLAG(CMP_FLAG_x));
249
250 /* Clear the flags */
251 HT_CMPn->TFR = (HT_CMPn->TFR & 0xfffffffc) | CMP_FLAG_x;
252
253 /*--------------------------------------------------------------------------------------------------------*/
254 /* DSB instruction is added in this function to ensure the write operation which is for clearing interrupt*/
255 /* flag is actually completed before exiting ISR. It prevents the NVIC from detecting the interrupt again */
256 /* since the write register operation may be pended in the internal write buffer of Cortex-Mx when program*/
257 /* has exited interrupt routine. This DSB instruction may be masked if this function is called in the */
258 /* beginning of ISR and there are still some instructions before exiting ISR. */
259 /*--------------------------------------------------------------------------------------------------------*/
260 __DSB();
261 }
262
263 /*********************************************************************************************************//**
264 * @brief Get the output status of the specified CMP.
265 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
266 * @retval SET or RESET
267 ************************************************************************************************************/
CMP_GetOutputStatus(HT_CMP_TypeDef * HT_CMPn)268 FlagStatus CMP_GetOutputStatus(HT_CMP_TypeDef* HT_CMPn)
269 {
270 /* Check the parameters */
271 Assert_Param(IS_CMP(HT_CMPn));
272
273 if ((HT_CMPn-> CR & CMP_OUTPUT_HIGH) != 0)
274 {
275 return SET;
276 }
277 else
278 {
279 return RESET;
280 }
281 }
282
283 /*********************************************************************************************************//**
284 * @brief Set the specified reference value in the data register of the scaler.
285 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
286 * @param Scaler_Value: value to be loaded in the selected data register
287 * @retval None
288 ************************************************************************************************************/
CMP_SetScalerValue(HT_CMP_TypeDef * HT_CMPn,u8 Scaler_Value)289 void CMP_SetScalerValue(HT_CMP_TypeDef* HT_CMPn, u8 Scaler_Value)
290 {
291 /* Check the parameters */
292 Assert_Param(IS_CMP(HT_CMPn));
293 Assert_Param(IS_SCALER_VALUE(Scaler_Value));
294
295 /* Set the scaler reference value register */
296 HT_CMPn->VALR = (u32)Scaler_Value;
297 }
298
299 #if (LIBCFG_CMP_CO)
300 /*********************************************************************************************************//**
301 * @brief Select the synchronous source with the CMPnO signal.
302 * @param HT_CMPn: where HT_CMPn is the selected CMP from the CMP peripherals.
303 * @param uCOUTSEL: Comparator Sync Output Select.
304 * This parameter can be one of the following value:
305 * @arg CMP_SYNCOUT_CMPnO :
306 * @arg CMP_SYNCOUT_MCTM_CH0O :
307 * @arg CMP_SYNCOUT_MCTM_CH0NO :
308 * @arg CMP_SYNCOUT_MCTM_CH1O :
309 * @arg CMP_SYNCOUT_MCTM_CH1NO :
310 * @arg CMP_SYNCOUT_MCTM_CH2O :
311 * @arg CMP_SYNCOUT_MCTM_CH2NO :
312 * @arg CMP_SYNCOUT_MCTM_CH3O :
313 * @arg CMP_SYNCOUT_MCTM_CH3OB :
314 * @retval None
315 ************************************************************************************************************/
CMP_Output_SyncSource_Select(HT_CMP_TypeDef * HT_CMPn,CMP_SYNCOUT_Enum CMP_SYNCOUT_x)316 void CMP_Output_SyncSource_Select(HT_CMP_TypeDef* HT_CMPn, CMP_SYNCOUT_Enum CMP_SYNCOUT_x)
317 {
318 /* Check the parameters */
319 Assert_Param(IS_CMP(HT_CMPn));
320 Assert_Param(IS_CMP_SYNC_SOURCE(CMP_SYNCOUT_x));
321
322 HT_CMPn->CO = (HT_CMPn->CO & 0xFFFFFFF0) | CMP_SYNCOUT_x;
323 }
324 #endif
325 /**
326 * @}
327 */
328
329
330 /**
331 * @}
332 */
333
334 /**
335 * @}
336 */
337