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 pcnt.c
44  **
45  ** pcnt driver API.
46  ** @link pcnt Group Some description @endlink
47  **
48  **   - 2019-04-08       First Version
49  **
50  ******************************************************************************/
51 
52 /******************************************************************************
53  * Include files
54  ******************************************************************************/
55 #include "hc32l196_pcnt.h"
56 
57 /**
58  ******************************************************************************
59  ** \addtogroup PCNTGroup
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 /**
86 ******************************************************************************
87     ** \brief  PCNT的启动和停止控制
88     ** @param  NewState : Run_Enable 或者 Run_Disable
89     ** @param  NewState : FALSE或者TRUE
90     ** \retval 无
91     **
92 ******************************************************************************/
Pcnt_Cmd(boolean_t NewState)93 boolean_t Pcnt_Cmd(boolean_t NewState)
94 {
95     SetBit((uint32_t)(&(M0P_PCNT->RUN)), 0, NewState);
96     return GetBit((uint32_t)(&(M0P_PCNT->RUN)), 0);
97 }
98 
99 
100 /**
101 ******************************************************************************
102     ** \brief  将BUF中的值同步到CNT
103     ** @param  value : 要同步到TOP的数值
104     ** \retval ok 或 ErrorTimeout
105     **
106 ******************************************************************************/
Pcnt_SetB2T(uint16_t value)107 en_result_t Pcnt_SetB2T(uint16_t value)
108 {
109     uint16_t u16TimeOut;
110 
111     u16TimeOut = 1000;
112     M0P_PCNT->BUF = value;
113     M0P_PCNT->CMD_f.B2T = 1;
114 
115     while(u16TimeOut--)
116     {
117         if(M0P_PCNT->SR2_f.B2T == FALSE)
118         {
119             break;
120         }
121     }
122     if(u16TimeOut == 0)
123     {
124         return ErrorTimeout;
125     }
126         return Ok;
127 }
128 
129 /**
130 ******************************************************************************
131     ** \brief  将BUF中的值同步到CNT
132     ** @param  value : 要同步到CNT的数值
133     ** \retval ok 或 ErrorTimeout
134     **
135 ******************************************************************************/
Pcnt_SetB2C(uint16_t value)136 en_result_t Pcnt_SetB2C(uint16_t value)
137 {
138     uint16_t u16TimeOut;
139     u16TimeOut = 1000;
140     M0P_PCNT->BUF = value;
141     M0P_PCNT->CMD_f.B2C = 1;
142 
143     while(u16TimeOut--)
144     {
145         if(M0P_PCNT->SR2_f.B2C == FALSE)
146         {
147             break;
148         }
149     }
150     if(u16TimeOut == 0)
151     {
152         return ErrorTimeout;
153     }
154     return Ok;
155 }
156 
157 /**
158 ******************************************************************************
159     ** \brief  将TOP中的值同步到CNT
160     ** @param  value : 要同步到CNT的数值
161     ** \retval ok 或 ErrorTimeout
162     **
163 ******************************************************************************/
Pcnt_SetT2C(void)164 en_result_t Pcnt_SetT2C(void)
165 {
166     uint16_t u16TimeOut;
167     u16TimeOut = 1000;
168     M0P_PCNT->CMD_f.T2C = 1;
169     while(u16TimeOut--)
170     {
171         if(M0P_PCNT->SR2_f.T2C == FALSE)
172         {
173             break;
174         }
175     }
176     if(u16TimeOut == 0)
177     {
178         return ErrorTimeout;
179     }
180     return Ok;
181 }
182 
183 
184 /**
185 ******************************************************************************
186     ** \brief  赋值BUF
187     ** @param  value : 要赋值给BUF的数值
188     ** \retval 无
189     **
190 ******************************************************************************/
Pcnt_SetBuf(uint16_t value)191 void Pcnt_SetBuf(uint16_t value)
192 {
193     M0P_PCNT->TOP_f.TOP = value;
194 }
195 
196 /**
197 ******************************************************************************
198     ** \brief  初始化
199     ** @param  start : 要同步到TOP的数值
200     ** @param  end   : 要同步到CNT的数值
201     ** \retval ok 或 ErrorTimeout
202     **
203 ******************************************************************************/
Pcnt_Init(stc_pcnt_initstruct_t * InitStruct)204 void Pcnt_Init(stc_pcnt_initstruct_t*  InitStruct)
205 {
206     M0P_PCNT->CTRL_f.S1P = InitStruct->Pcnt_S1Sel;
207     M0P_PCNT->CTRL_f.S0P = InitStruct->Pcnt_S0Sel;
208     M0P_PCNT->CTRL_f.CLKSEL = InitStruct->Pcnt_Clk;
209     M0P_PCNT->CTRL_f.MODE = InitStruct->Pcnt_Mode;
210     if(InitStruct->Pcnt_Mode == PcntDoubleMode)//如果是双通道正交脉冲计数模式
211     {
212         M0P_PCNT->SR1_f.DIR = InitStruct->Pcnt_Dir;
213     }
214     else
215     {
216         M0P_PCNT->CTRL_f.DIR = InitStruct->Pcnt_Dir;
217     }
218     M0P_PCNT->FLT_f.EN = InitStruct->Pcnt_FltEn;
219     M0P_PCNT->FLT_f.DEBTOP = InitStruct->Pcnt_DebTop;
220     M0P_PCNT->FLT_f.CLKDIV = InitStruct->Pcnt_ClkDiv;
221     M0P_PCNT->TOCR_f.EN    = InitStruct->Pcnt_TocrEn;
222     M0P_PCNT->TOCR_f.TH    = InitStruct->Pcnt_TocrTh;
223 
224     M0P_PCNT->DBG_f.DBG = InitStruct->Pcnt_Dbg;
225 }
226 
227 /**
228 ******************************************************************************
229     ** \brief  配置中断源的使能
230     ** @param  IT_Src : 中断源再PCNT_IEN内部的位位置
231     ** @param  NewState   : FALSE 或TRUE
232     ** \retval 无
233     **
234 ******************************************************************************/
Pcnt_ItCfg(en_pcnt_itfce_t IT_Src,boolean_t NewState)235 void Pcnt_ItCfg(en_pcnt_itfce_t IT_Src, boolean_t NewState)
236 {
237     if(NewState == TRUE)
238     {
239         M0P_PCNT->IEN |= (uint32_t)(1<<IT_Src);
240     }
241     else if(NewState == FALSE)
242     {
243         M0P_PCNT->IEN &= ~(uint32_t)(1<<IT_Src);
244     }
245     else
246     {
247         ;
248     }
249 }
250 
251 /**
252 ******************************************************************************
253     ** \brief  获取中断源的标志位
254     ** @param  IT_Src : 中断源标志位
255     ** \retval FALSE 或TRUE
256     **
257 ******************************************************************************/
Pcnt_GetItStatus(en_pcnt_itfce_t IT_Src)258 boolean_t Pcnt_GetItStatus(en_pcnt_itfce_t IT_Src)
259 {
260     return ((M0P_PCNT->IFR >> IT_Src) & 1u) > 0 ? TRUE : FALSE;
261 }
262 
263 /**
264 ******************************************************************************
265     ** \brief  清除中断源的标志位
266     ** @param  IT_Src : 中断源标志位
267     ** \retval 无
268     **
269 ******************************************************************************/
Pcnt_ClrItStatus(en_pcnt_itfce_t IT_Src)270 void Pcnt_ClrItStatus(en_pcnt_itfce_t IT_Src)
271 {
272     M0P_PCNT->ICR &= ~(uint32_t)(1<<(uint32_t)IT_Src);
273 }
274 
275 
276 /**
277 ******************************************************************************
278     ** \brief  获取PCNT_CNT寄存器的数值
279     ** @param  无
280     ** \retval PCNT_CNT数值
281     **
282 ******************************************************************************/
Pcnt_GetCnt(void)283 uint16_t Pcnt_GetCnt(void)
284 {
285     return  (uint16_t)(M0P_PCNT->CNT);
286 }
287 
288 /**
289 ******************************************************************************
290     ** \brief  获取PCNT_TOP寄存器的数值
291     ** @param  无
292     ** \retval PCNT_TOP数值
293     **
294 ******************************************************************************/
Pcnt_GetTop(void)295 uint16_t Pcnt_GetTop(void)
296 {
297    return  (uint16_t)(M0P_PCNT->TOP);
298 }
299 
300 /**
301 ******************************************************************************
302     ** \brief  获取PCNT_BUF寄存器的数值
303     ** @param  无
304     ** \retval PCNT_BUF数值
305     **
306 ******************************************************************************/
Pcnt_GetBuf(void)307 uint16_t Pcnt_GetBuf(void)
308 {
309     return (uint16_t)(M0P_PCNT->BUF);
310 }
311 
312 //@} // Group
313 /******************************************************************************
314  * EOF (not truncated)
315  ******************************************************************************/
316 
317