1 /******************************************************************************
2 * Copyright (C) 2019, Huada Semiconductor Co.,Ltd All rights reserved.
3 *
4 * This software is owned and published by:
5 * Huada Semiconductor Co.,Ltd ("HDSC").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with HDSC
11 * components. This software is licensed by HDSC to be adapted only
12 * for use in systems utilizing HDSC components. HDSC shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. HDSC is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * Disclaimer:
19 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
20 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
21 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
22 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
23 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
24 * WARRANTY OF NONINFRINGEMENT.
25 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
26 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
27 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
28 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
29 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
31 * SAVINGS OR PROFITS,
32 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
34 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
35 * FROM, THE SOFTWARE.
36 *
37 * This software may be replicated in part or whole for the licensed use,
38 * with the restriction that this Disclaimer and Copyright notice must be
39 * included with each copy of this software, whether used in part or whole,
40 * at all times.
41 */
42 /******************************************************************************/
43 /** \file vc.c
44  **
45  ** voltage comparator driver API.
46  ** @link VC Group Some description @endlink
47  **
48  **   - 2019-04-10   First Version
49  **
50  ******************************************************************************/
51 
52 /******************************************************************************
53  * Include files
54  ******************************************************************************/
55 #include "hc32l196_vc.h"
56 
57 /**
58  ******************************************************************************
59  ** \addtogroup VcGroup
60  ******************************************************************************/
61 //@{
62 
63 /******************************************************************************
64  * Local pre-processor symbols/macros ('#define')
65  ******************************************************************************/
66 
67 /******************************************************************************
68  * Global variable definitions (declared in header file with 'extern')
69  ******************************************************************************/
70 
71 
72 /******************************************************************************
73  * Local type definitions ('typedef')
74  ******************************************************************************/
75 
76 /******************************************************************************
77  * Local function prototypes ('static')
78  ******************************************************************************/
79 
80 /******************************************************************************
81  * Local variable definitions ('static')
82  ******************************************************************************/
83 
84 /*****************************************************************************
85  * Function implementation - global ('extern') and local ('static')
86  *****************************************************************************/
87 
88 /**
89 ******************************************************************************
90     ** \brief  配置VC中断触发方式
91     **
92     ** @param  Channelx : VcChannelx  x=0、1、2
93     ** @param  enSel : VcIrqRise、VcIrqFall、VcIrqHigh
94     ** \retval 无
95     **
96 ******************************************************************************/
Vc_CfgItType(en_vc_channel_t Channelx,en_vc_irq_sel_t ItType)97 void Vc_CfgItType(en_vc_channel_t Channelx, en_vc_irq_sel_t ItType)
98 {
99     stc_vc_vc0_cr_field_t *stcVcnCr;
100         switch(Channelx)
101         {
102             case VcChannel0:
103                 stcVcnCr = (stc_vc_vc0_cr_field_t*)&M0P_VC->VC0_CR_f;
104                 break;
105             case VcChannel1:
106                 stcVcnCr = (stc_vc_vc0_cr_field_t*)&M0P_VC->VC1_CR_f;
107                 break;
108             case VcChannel2:
109                 stcVcnCr = (stc_vc_vc0_cr_field_t*)&M0P_VC->VC2_CR_f;
110                 break;
111             default:
112                 break;
113         }
114     switch (ItType)
115     {
116         case VcIrqNone:
117             stcVcnCr->RISING  = 0u;
118             stcVcnCr->FALLING = 0u;
119             stcVcnCr->LEVEL   = 0u;
120             break;
121         case VcIrqRise:
122             stcVcnCr->RISING  = 1u;
123             break;
124         case VcIrqFall:
125             stcVcnCr->FALLING = 1u;
126             break;
127         case VcIrqHigh:
128             stcVcnCr->LEVEL   = 1u;
129             break;
130         default:
131             break;
132     }
133 }
134 
135 /**
136 ******************************************************************************
137     ** \brief  VC 中断使能与禁止
138     **
139     ** @param  Channelx : VcChannelx  x=0、1、2
140     ** @param  NewStatus : TRUE 或 FALSE
141     ** \retval 无
142     **
143 ******************************************************************************/
Vc_ItCfg(en_vc_channel_t Channelx,boolean_t NewStatus)144 void  Vc_ItCfg(en_vc_channel_t Channelx, boolean_t NewStatus)
145 {
146     switch(Channelx)
147     {
148         case VcChannel0:
149             SetBit((uint32_t)(&(M0P_VC->VC0_CR)), 15, NewStatus);
150         break;
151         case VcChannel1:
152             SetBit((uint32_t)(&(M0P_VC->VC1_CR)), 15, NewStatus);
153         break;
154         case VcChannel2:
155             SetBit((uint32_t)(&(M0P_VC->VC2_CR)), 15, NewStatus);
156         break;
157         default:
158             break;
159     }
160 }
161 
162 /**
163 ******************************************************************************
164     ** \brief  VC 比较结果获取,包含中断标志位和滤波结果
165     **
166     ** @param  Result : 所要读取的结果
167     ** \retval TRUE 或  FALSE
168     **
169 ******************************************************************************/
Vc_GetItStatus(en_vc_ifr_t Result)170 boolean_t Vc_GetItStatus(en_vc_ifr_t Result)
171 {
172     boolean_t bFlag;
173     bFlag = GetBit((uint32_t)(&(M0P_VC->IFR)), Result);
174     return bFlag;
175 }
176 
177 /**
178 ******************************************************************************
179     ** \brief  VC 清除中断标志位
180     **
181     ** @param  NewStatus : Vc0_Intf、Vc1_Intf、Vc2_Intf
182     ** \retval 无
183     **
184 ******************************************************************************/
Vc_ClearItStatus(en_vc_ifr_t NewStatus)185 void Vc_ClearItStatus(en_vc_ifr_t NewStatus)
186 {
187     SetBit((uint32_t)(&(M0P_VC->IFR)), NewStatus, 0);
188 }
189 
190 /**
191 ******************************************************************************
192     ** \brief  VC 配置DAC相关的内容  VC_CR中 VC_REF2P5_SEL VC_DIV_EN VC_DIV
193     **
194     ** @param  pstcDacCfg :
195     ** \retval Ok 或 ErrorInvalidParameter
196     **
197 ******************************************************************************/
Vc_DacInit(stc_vc_dac_cfg_t * pstcDacCfg)198 en_result_t Vc_DacInit(stc_vc_dac_cfg_t *pstcDacCfg)
199 {
200     if (NULL == pstcDacCfg)
201     {
202         return ErrorInvalidParameter;
203     }
204 
205     M0P_VC->CR_f.DIV_EN = pstcDacCfg->bDivEn;
206     M0P_VC->CR_f.REF2P5_SEL = pstcDacCfg->enDivVref;
207 
208     if (pstcDacCfg->u8DivVal < 0x40)
209     {
210         M0P_VC->CR_f.DIV = pstcDacCfg->u8DivVal;
211     }
212     else
213     {
214         return ErrorInvalidParameter;
215     }
216 
217     return Ok;
218 }
219 
220 /**
221 ******************************************************************************
222     ** \brief  VC通道初始化
223     **
224     ** @param  pstcDacCfg :
225     ** \retval 无
226     **
227 ******************************************************************************/
Vc_Init(stc_vc_channel_cfg_t * pstcChannelCfg)228 void Vc_Init(stc_vc_channel_cfg_t *pstcChannelCfg)
229 {
230     if (VcChannel0 == pstcChannelCfg->enVcChannel)
231     {
232         M0P_VC->CR_f.VC0_HYS_SEL = pstcChannelCfg->enVcCmpDly;
233         M0P_VC->CR_f.VC0_BIAS_SEL = pstcChannelCfg->enVcBiasCurrent;
234         M0P_VC->VC0_CR_f.DEBOUNCE_TIME = pstcChannelCfg->enVcFilterTime;
235         M0P_VC->VC0_CR_f.P_SEL = pstcChannelCfg->enVcInPin_P;
236         M0P_VC->VC0_CR_f.N_SEL = pstcChannelCfg->enVcInPin_N;
237                 M0P_VC->VC0_CR_f.FLTEN = pstcChannelCfg->bFlten;
238         M0P_VC->VC0_OUT_CFG = 1<<pstcChannelCfg->enVcOutCfg;
239     }
240     else if (VcChannel1 == pstcChannelCfg->enVcChannel)
241     {
242         M0P_VC->CR_f.VC1_HYS_SEL = pstcChannelCfg->enVcCmpDly;
243         M0P_VC->CR_f.VC1_BIAS_SEL = pstcChannelCfg->enVcBiasCurrent;
244         M0P_VC->VC1_CR_f.DEBOUNCE_TIME = pstcChannelCfg->enVcFilterTime;
245         M0P_VC->VC1_CR_f.P_SEL = pstcChannelCfg->enVcInPin_P;
246         M0P_VC->VC1_CR_f.N_SEL = pstcChannelCfg->enVcInPin_N;
247                 M0P_VC->VC1_CR_f.FLTEN = pstcChannelCfg->bFlten;
248         M0P_VC->VC1_OUT_CFG = 1<<pstcChannelCfg->enVcOutCfg;
249     }
250     else if(VcChannel2 ==  pstcChannelCfg->enVcChannel)
251     {
252     M0P_VC->CR_f.VC2_HYS_SEL = pstcChannelCfg->enVcCmpDly;
253     M0P_VC->CR_f.VC2_BIAS_SEL = pstcChannelCfg->enVcBiasCurrent;
254     M0P_VC->VC2_CR_f.DEBOUNCE_TIME = pstcChannelCfg->enVcFilterTime;
255     M0P_VC->VC2_CR_f.P_SEL = pstcChannelCfg->enVcInPin_P;
256     M0P_VC->VC2_CR_f.N_SEL = pstcChannelCfg->enVcInPin_N;
257             M0P_VC->VC2_CR_f.FLTEN = pstcChannelCfg->bFlten;
258     M0P_VC->VC2_OUT_CFG = 1<<pstcChannelCfg->enVcOutCfg;
259     }
260     else
261     {
262         ;
263     }
264 }
265 
266 /**
267 ******************************************************************************
268     ** \brief  VC 通道使能
269     **
270     ** \param  enChannel :  通道号VcChannel0 VcChannel1 VcChannel2
271     ** \param  NewStatus : TRUE FALSE
272     ** \retval NewStatus : TRUE FALSE
273     **
274 ******************************************************************************/
Vc_Cmd(en_vc_channel_t enChannel,boolean_t NewStatus)275 void Vc_Cmd(en_vc_channel_t enChannel, boolean_t NewStatus)
276 {
277     switch(enChannel)
278     {
279         case VcChannel0:
280             SetBit((uint32_t)(&(M0P_VC->VC0_CR)), 16, NewStatus);
281             break;
282         case VcChannel1:
283             SetBit((uint32_t)(&(M0P_VC->VC1_CR)), 16, NewStatus);
284             break;
285         case VcChannel2:
286             SetBit((uint32_t)(&(M0P_VC->VC2_CR)), 16, NewStatus);
287             break;
288         default:
289             break;
290     }
291 }
292 
293 //@} // VcGroup
294 
295 /******************************************************************************
296  * EOF (not truncated)
297  ******************************************************************************/
298 
299