1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
******************************************************************************
* @file HAL_COMP.c
* @version V1.0.0
* @date 2020
* @brief COMP HAL module driver.
* This file provides firmware functions to manage the following
* functionalities of the Comparator Peripheral (COMP).
* @ Initialization and de-initialization functions
* @ IO operation functions
* @ Peripheral Control functions
******************************************************************************
*/
#include "ACM32Fxx_HAL.h"
/************************************************************************
* function : HAL_COMP_MspInit
* Description: Inition the comparator gpio and clock
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
* output : none
************************************************************************/
__weak void HAL_COMP_MspInit(COMP_HandleTypeDef* hcomp)
{
/*
NOTE : This function should be modified by the user.
*/
/* For Example */
GPIO_InitTypeDef GPIO_Handle;
System_Module_Enable(EN_COMP);
if(hcomp->Init.Comparator == COMP1 )
{
/* COMP1 GPIO inition VINP:PC4(INP_0)*/
/* COMP1 GPIO inition VINM:PA4(INM_0)*/
/* COMP1 GPIO inition VOUT:PA0(FUNCTION_7)*/
GPIO_Handle.Pin = GPIO_PIN_4;
GPIO_Handle.Mode = GPIO_MODE_ANALOG;
GPIO_Handle.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_Handle);
GPIO_Handle.Pin = GPIO_PIN_4;
GPIO_Handle.Mode = GPIO_MODE_ANALOG;
GPIO_Handle.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_Handle);
GPIO_Handle.Pin = GPIO_PIN_0;
GPIO_Handle.Mode = GPIO_MODE_AF_PP;
GPIO_Handle.Alternate = GPIO_FUNCTION_7;
GPIO_Handle.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_Handle);
}
else if(hcomp->Init.Comparator == COMP2 )
{
/* COMP2 GPIO inition VINP:PA3(INP_1)*/
/* COMP2 GPIO inition VOUT:PA7(FUNCTION_7)*/
GPIO_Handle.Pin = GPIO_PIN_3;
GPIO_Handle.Mode = GPIO_MODE_ANALOG;
GPIO_Handle.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_Handle);
GPIO_Handle.Pin = GPIO_PIN_7;
GPIO_Handle.Mode = GPIO_MODE_AF_PP;
GPIO_Handle.Alternate = GPIO_FUNCTION_7;
GPIO_Handle.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_Handle);
}
}
/************************************************************************
* function : HAL_COMP_MspDeInit
* Description: De-Inition the comparator gpio and clock
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
* output : none
************************************************************************/
__weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef* hcomp)
{
/*
NOTE : This function should be modified by the user.
*/
/* For Example */
System_Module_Reset(RST_COMP);
System_Module_Enable(EN_COMP);
}
/************************************************************************
* function : HAL_COMP_Init
* Description: Inition the comparator
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
************************************************************************/
HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef* hcomp)
{
uint32_t u32RegTemp;
__IO uint32_t *gu32RegCrx;
/* Check the COMP handle allocation */
if (hcomp == NULL)
{
return HAL_ERROR;
}
/* Check the parameters */
if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
if(!IS_COMP_ALL_CRV_EN(hcomp->Init.Crv_En)) return HAL_ERROR;
if(!IS_COMP_ALL_CRV_SEL(hcomp->Init.Crv_Sel)) return HAL_ERROR;
if(!IS_COMP_ALL_CRV_CFG(hcomp->Init.Crv_Cfg)) return HAL_ERROR;
if(!IS_COMP_ALL_WINMODE(hcomp->Init.WinMode)) return HAL_ERROR;
if(!IS_COMP_ALL_WINOUT(hcomp->Init.WinOut)) return HAL_ERROR;
if(!IS_COMP_ALL_POLARITY(hcomp->Init.Polarity)) return HAL_ERROR;
if(!IS_COMP_ALL_FLTEN(hcomp->Init.FltEn)) return HAL_ERROR;
if(!IS_COMP_ALL_FLTTIME(hcomp->Init.FltTime)) return HAL_ERROR;
if(!IS_COMP_ALL_BLANKTIME(hcomp->Init.BlankTime)) return HAL_ERROR;
if(!IS_COMP_ALL_BLANKSEL(hcomp->Init.BlankSel)) return HAL_ERROR;
if(!IS_COMP_ALL_INPSEL(hcomp->Init.InPSel)) return HAL_ERROR;
if(!IS_COMP_ALL_INMSEL(hcomp->Init.InMSel)) return HAL_ERROR;
if(!IS_COMP_ALL_HYS(hcomp->Init.HYS)) return HAL_ERROR;
/* Init the low level hardware : GPIO, CLOCK */
HAL_COMP_MspInit(hcomp);
if(hcomp->Init.Comparator == COMP1 )
gu32RegCrx = &hcomp->Instance->CR1;
else
gu32RegCrx = &hcomp->Instance->CR2;
//Check if the register is locked
if(READ_BIT(*gu32RegCrx , COMP_CR_LOCK))
{
System_Module_Reset(RST_COMP);
}
//Check if the comparetor is enable
if(READ_BIT(*gu32RegCrx , COMP_CR_EN))
CLEAR_BIT(*gu32RegCrx , COMP_CR_EN);
u32RegTemp = *gu32RegCrx ;
u32RegTemp = u32RegTemp | ((hcomp->Init.Crv_Cfg << COMP_CR_CRV_CFG_POS)& COMP_CR_CRV_CFG_MASK) | \
((hcomp->Init.Crv_Sel << 24) & COMP_CR_CRV_SEL) | \
((hcomp->Init.Crv_En << 23) & COMP_CR_CRV_EN) | \
((hcomp->Init.WinMode << 22) & COMP_CR_WINMODE) | \
((hcomp->Init.WinOut << 21) & COMP_CR_WINOUT) | \
((hcomp->Init.Polarity << 20) & COMP_CR_POLARITY) | \
((hcomp->Init.FltEn << 19) & COMP_CR_FLTEN) | \
((hcomp->Init.FltTime << COMP_CR_FLTTIME_POS)& COMP_CR_FLTTIME_MASK) | \
((hcomp->Init.BlankTime << COMP_CR_BLANKTIME_POS)& COMP_CR_BLANKTIME_MASK) | \
((hcomp->Init.BlankSel << COMP_CR_BLANKSEL_POS)& COMP_CR_BLANKSEL_MASK) | \
((hcomp->Init.InPSel << COMP_CR_INPSEL_POS)& COMP_CR_INPSEL_MASK) | \
((hcomp->Init.InMSel << COMP_CR_INMSEL_POS)& COMP_CR_INMSEL_MASK) | \
((hcomp->Init.HYS << COMP_CR_HYS_POS)& COMP_CR_HYS_MASK);
//Write the COMP_CR register .
WRITE_REG(*gu32RegCrx,u32RegTemp);
SET_BIT(*gu32RegCrx , COMP_CR_EN); //enable
return HAL_OK;
}
/************************************************************************
* function : HAL_COMP_Enable
* Description: Enable the comparator
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
************************************************************************/
HAL_StatusTypeDef HAL_COMP_Enable(COMP_HandleTypeDef* hcomp)
{
__IO uint32_t *gu32RegCrx;
/* Check the COMP handle allocation */
if (hcomp == NULL)
{
return HAL_ERROR;
}
/* Check the parameters */
if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
if(hcomp->Init.Comparator == COMP1 )
gu32RegCrx = &hcomp->Instance->CR1;
else
gu32RegCrx = &hcomp->Instance->CR2;
SET_BIT(*gu32RegCrx , COMP_CR_EN); //enable
/* Return function status */
return HAL_OK;
}
/************************************************************************
* function : HAL_COMP_DeInit
* Description: Deinit and reset the comparator
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
************************************************************************/
HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef* hcomp)
{
/* Check the COMP handle allocation */
if (hcomp == NULL)
{
return HAL_ERROR;
}
HAL_COMP_MspDeInit(hcomp);
memset(&hcomp->Init, 0, sizeof(hcomp->Init));
/* Return function status */
return HAL_OK;
}
/************************************************************************
* function : HAL_COMP_Disable
* Description: Disable the comparator
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
************************************************************************/
HAL_StatusTypeDef HAL_COMP_Disable(COMP_HandleTypeDef* hcomp)
{
__IO uint32_t *gu32RegCrx;
/* Check the COMP handle allocation */
if (hcomp == NULL)
{
return HAL_ERROR;
}
/* Check the parameters */
if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
if(hcomp->Init.Comparator == COMP1 )
gu32RegCrx = &hcomp->Instance->CR1;
else
gu32RegCrx = &hcomp->Instance->CR2;
CLEAR_BIT(*gu32RegCrx , COMP_CR_EN); //disable
/* Return function status */
return HAL_OK;
}
/************************************************************************
* function : HAL_COMP_GetOutputLevel
* Description: Get the output level of the comparator
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
************************************************************************/
HAL_StatusTypeDef HAL_COMP_GetOutputLevel(COMP_HandleTypeDef* hcomp)
{
uint32_t u32RegTemp;
/* Check the parameters */
if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
u32RegTemp = READ_REG(hcomp->Instance->SR);
if(hcomp->Init.Comparator == COMP1 )
{
hcomp->OutputLevel_Org = (u32RegTemp & COMP_SR_VCOUT1_ORG)? 1:0;
hcomp->OutputLevel = (u32RegTemp & COMP_SR_VCOUT1)? 1:0;
}
else
{
hcomp->OutputLevel_Org = (u32RegTemp & COMP_SR_VCOUT2_ORG)? 1:0;
hcomp->OutputLevel = (u32RegTemp & COMP_SR_VCOUT2)? 1:0;
}
/* Return function status */
return HAL_OK;
}
/************************************************************************
* function : HAL_COMP_Lock
* Description: Lock the comparator
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
************************************************************************/
HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef* hcomp)
{
__IO uint32_t *gu32RegCrx;
/* Check the COMP handle allocation */
if (hcomp == NULL)
{
return HAL_ERROR;
}
/* Check the parameters */
if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
if(hcomp->Init.Comparator == COMP1 )
gu32RegCrx = &hcomp->Instance->CR1;
else
gu32RegCrx = &hcomp->Instance->CR2;
SET_BIT(*gu32RegCrx , COMP_CR_LOCK); //lock
/* Return function status */
return HAL_OK;
}
/************************************************************************
* function : HAL_COMP_Start
* Description: Start the comparator
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
************************************************************************/
HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
{
return(HAL_COMP_Enable(hcomp));
}
/************************************************************************
* function : HAL_COMP_Stop
* Description: Stop the comparator
* input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
************************************************************************/
HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
{
return(HAL_COMP_Disable(hcomp));
}
|