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
|
/******************************************************************************
* Copyright (C) 2019, Huada Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/******************************************************************************/
/** \file vc.c
**
** voltage comparator driver API.
** @link VC Group Some description @endlink
**
** - 2019-04-10 First Version
**
******************************************************************************/
/******************************************************************************
* Include files
******************************************************************************/
#include "hc32l196_vc.h"
/**
******************************************************************************
** \addtogroup VcGroup
******************************************************************************/
//@{
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
*****************************************************************************/
/**
******************************************************************************
** \brief 配置VC中断触发方式
**
** @param Channelx : VcChannelx x=0、1、2
** @param enSel : VcIrqRise、VcIrqFall、VcIrqHigh
** \retval 无
**
******************************************************************************/
void Vc_CfgItType(en_vc_channel_t Channelx, en_vc_irq_sel_t ItType)
{
stc_vc_vc0_cr_field_t *stcVcnCr;
switch(Channelx)
{
case VcChannel0:
stcVcnCr = (stc_vc_vc0_cr_field_t*)&M0P_VC->VC0_CR_f;
break;
case VcChannel1:
stcVcnCr = (stc_vc_vc0_cr_field_t*)&M0P_VC->VC1_CR_f;
break;
case VcChannel2:
stcVcnCr = (stc_vc_vc0_cr_field_t*)&M0P_VC->VC2_CR_f;
break;
default:
break;
}
switch (ItType)
{
case VcIrqNone:
stcVcnCr->RISING = 0u;
stcVcnCr->FALLING = 0u;
stcVcnCr->LEVEL = 0u;
break;
case VcIrqRise:
stcVcnCr->RISING = 1u;
break;
case VcIrqFall:
stcVcnCr->FALLING = 1u;
break;
case VcIrqHigh:
stcVcnCr->LEVEL = 1u;
break;
default:
break;
}
}
/**
******************************************************************************
** \brief VC 中断使能与禁止
**
** @param Channelx : VcChannelx x=0、1、2
** @param NewStatus : TRUE 或 FALSE
** \retval 无
**
******************************************************************************/
void Vc_ItCfg(en_vc_channel_t Channelx, boolean_t NewStatus)
{
switch(Channelx)
{
case VcChannel0:
SetBit((uint32_t)(&(M0P_VC->VC0_CR)), 15, NewStatus);
break;
case VcChannel1:
SetBit((uint32_t)(&(M0P_VC->VC1_CR)), 15, NewStatus);
break;
case VcChannel2:
SetBit((uint32_t)(&(M0P_VC->VC2_CR)), 15, NewStatus);
break;
default:
break;
}
}
/**
******************************************************************************
** \brief VC 比较结果获取,包含中断标志位和滤波结果
**
** @param Result : 所要读取的结果
** \retval TRUE 或 FALSE
**
******************************************************************************/
boolean_t Vc_GetItStatus(en_vc_ifr_t Result)
{
boolean_t bFlag;
bFlag = GetBit((uint32_t)(&(M0P_VC->IFR)), Result);
return bFlag;
}
/**
******************************************************************************
** \brief VC 清除中断标志位
**
** @param NewStatus : Vc0_Intf、Vc1_Intf、Vc2_Intf
** \retval 无
**
******************************************************************************/
void Vc_ClearItStatus(en_vc_ifr_t NewStatus)
{
SetBit((uint32_t)(&(M0P_VC->IFR)), NewStatus, 0);
}
/**
******************************************************************************
** \brief VC 配置DAC相关的内容 VC_CR中 VC_REF2P5_SEL VC_DIV_EN VC_DIV
**
** @param pstcDacCfg :
** \retval Ok 或 ErrorInvalidParameter
**
******************************************************************************/
en_result_t Vc_DacInit(stc_vc_dac_cfg_t *pstcDacCfg)
{
if (NULL == pstcDacCfg)
{
return ErrorInvalidParameter;
}
M0P_VC->CR_f.DIV_EN = pstcDacCfg->bDivEn;
M0P_VC->CR_f.REF2P5_SEL = pstcDacCfg->enDivVref;
if (pstcDacCfg->u8DivVal < 0x40)
{
M0P_VC->CR_f.DIV = pstcDacCfg->u8DivVal;
}
else
{
return ErrorInvalidParameter;
}
return Ok;
}
/**
******************************************************************************
** \brief VC通道初始化
**
** @param pstcDacCfg :
** \retval 无
**
******************************************************************************/
void Vc_Init(stc_vc_channel_cfg_t *pstcChannelCfg)
{
if (VcChannel0 == pstcChannelCfg->enVcChannel)
{
M0P_VC->CR_f.VC0_HYS_SEL = pstcChannelCfg->enVcCmpDly;
M0P_VC->CR_f.VC0_BIAS_SEL = pstcChannelCfg->enVcBiasCurrent;
M0P_VC->VC0_CR_f.DEBOUNCE_TIME = pstcChannelCfg->enVcFilterTime;
M0P_VC->VC0_CR_f.P_SEL = pstcChannelCfg->enVcInPin_P;
M0P_VC->VC0_CR_f.N_SEL = pstcChannelCfg->enVcInPin_N;
M0P_VC->VC0_CR_f.FLTEN = pstcChannelCfg->bFlten;
M0P_VC->VC0_OUT_CFG = 1<<pstcChannelCfg->enVcOutCfg;
}
else if (VcChannel1 == pstcChannelCfg->enVcChannel)
{
M0P_VC->CR_f.VC1_HYS_SEL = pstcChannelCfg->enVcCmpDly;
M0P_VC->CR_f.VC1_BIAS_SEL = pstcChannelCfg->enVcBiasCurrent;
M0P_VC->VC1_CR_f.DEBOUNCE_TIME = pstcChannelCfg->enVcFilterTime;
M0P_VC->VC1_CR_f.P_SEL = pstcChannelCfg->enVcInPin_P;
M0P_VC->VC1_CR_f.N_SEL = pstcChannelCfg->enVcInPin_N;
M0P_VC->VC1_CR_f.FLTEN = pstcChannelCfg->bFlten;
M0P_VC->VC1_OUT_CFG = 1<<pstcChannelCfg->enVcOutCfg;
}
else if(VcChannel2 == pstcChannelCfg->enVcChannel)
{
M0P_VC->CR_f.VC2_HYS_SEL = pstcChannelCfg->enVcCmpDly;
M0P_VC->CR_f.VC2_BIAS_SEL = pstcChannelCfg->enVcBiasCurrent;
M0P_VC->VC2_CR_f.DEBOUNCE_TIME = pstcChannelCfg->enVcFilterTime;
M0P_VC->VC2_CR_f.P_SEL = pstcChannelCfg->enVcInPin_P;
M0P_VC->VC2_CR_f.N_SEL = pstcChannelCfg->enVcInPin_N;
M0P_VC->VC2_CR_f.FLTEN = pstcChannelCfg->bFlten;
M0P_VC->VC2_OUT_CFG = 1<<pstcChannelCfg->enVcOutCfg;
}
else
{
;
}
}
/**
******************************************************************************
** \brief VC 通道使能
**
** \param enChannel : 通道号VcChannel0 VcChannel1 VcChannel2
** \param NewStatus : TRUE FALSE
** \retval NewStatus : TRUE FALSE
**
******************************************************************************/
void Vc_Cmd(en_vc_channel_t enChannel, boolean_t NewStatus)
{
switch(enChannel)
{
case VcChannel0:
SetBit((uint32_t)(&(M0P_VC->VC0_CR)), 16, NewStatus);
break;
case VcChannel1:
SetBit((uint32_t)(&(M0P_VC->VC1_CR)), 16, NewStatus);
break;
case VcChannel2:
SetBit((uint32_t)(&(M0P_VC->VC2_CR)), 16, NewStatus);
break;
default:
break;
}
}
//@} // VcGroup
/******************************************************************************
* EOF (not truncated)
******************************************************************************/
|