summaryrefslogtreecommitdiff
path: root/bsp/hc32l136/Libraries/HC32L136_StdPeriph_Driver/src/pcnt.c
blob: bdf0561aeb0faa1bf6f4caca93b675c2d8084cf1 (plain)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/******************************************************************************
* Copyright (C) 2017, 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 pcnt.c
 **
 ** pcnt driver API.
 ** @link pcnt Group Some description @endlink
 **
 **   - 2018-04-15   Devi    First Version
 **
 ******************************************************************************/

/******************************************************************************
 * Include files
 ******************************************************************************/
#include "pcnt.h"

/**
 ******************************************************************************
 ** \addtogroup PCNTGroup
 ******************************************************************************/
//@{

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

#define IS_VALID_pagagain(x)     ( (x) <= 7 )

#define IS_VALID_channel(x)      (  (OPA0 == (x)) ||\
                                    (OPA1 == (x)) ||\
                                    (OPA2 == (x)) )

#define IS_VALID_STAT(x)         (   (PCNT_S1E == (x)) ||\
                                     (PCNT_S0E == (x)) ||\
                                     (PCNT_BB == (x)) ||\
                                     (PCNT_FE == (x)) ||\
                                     (PCNT_DIR == (x)) ||\
                                     (PCNT_TO == (x)) ||\
                                     (PCNT_OV == (x)) ||\
                                     (PCNT_UF == (x)) )



/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/


/******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/
static func_ptr_t pfnPcntCallback = NULL; ///< callback function pointer for PCNT Irq
/******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*****************************************************************************
 * Function implementation - global ('extern') and local ('static')
 *****************************************************************************/

void Pcnt_IRQHandler(void)
{
	if(NULL != pfnPcntCallback)
	{
		pfnPcntCallback();
	}	
}

/**
 * \brief   
 *          PCNT 初始化
 *
 * \param   无
 * \param   无
 *
 * \retval  无
 * \retval  无
 */
en_result_t PCNT_Init(stc_pcnt_config_t*  pstcPcntConfig)
{

	
	  M0P_SYSCTRL->PERI_CLKEN_f.PCNT = 1; 
  
    M0P_PCNT->CR_f.S1P = pstcPcntConfig->bS1Sel; 
    M0P_PCNT->CR_f.S0P = pstcPcntConfig->bS0Sel;
    M0P_PCNT->CR_f.DIR = pstcPcntConfig->u8Direc;   //计数方式
    M0P_PCNT->CR_f.CLKSEL = pstcPcntConfig->u8Clk;
    M0P_PCNT->CR_f.MODE = pstcPcntConfig->u8Mode;
    
    M0P_PCNT->FLT_f.CLKDIV = pstcPcntConfig->u8FLTClk;
    
    if(pstcPcntConfig->bFLTEn)
    {
      if(pstcPcntConfig->u8FLTDep == 0)	
    	{
    	 	M0P_PCNT->FLT_f.DEBTOP = 2;
    	}
    	else
    	{
    		M0P_PCNT->FLT_f.DEBTOP = pstcPcntConfig->u8FLTDep;
    	}
    }
    M0P_PCNT->FLT_f.EN = pstcPcntConfig->bFLTEn;
    
    M0P_PCNT->TOCR_f.TH = pstcPcntConfig->u16TODep;
    M0P_PCNT->TOCR_f.EN = pstcPcntConfig->bTOEn;    
    
    if (TRUE == pstcPcntConfig->bIrqEn)
    {
        M0P_PCNT->IEN = pstcPcntConfig->u8IrqStatus;
        EnableNvic(PCNT_IRQn,IrqLevel3,TRUE);
    }
    else
    {
    	  M0P_PCNT->IEN = 0x00;
        EnableNvic(PCNT_IRQn,IrqLevel3,FALSE);
    }
    if(NULL != pstcPcntConfig->pfnIrqCb)
    {
        pfnPcntCallback = pstcPcntConfig->pfnIrqCb;
    }
   return Ok;
}

/**
 * \brief   
 *          PCNT 去初始化
 *
 * \param   无
 * \param   无
 *
 * \retval  无
 * \retval  无
 */
void PCNT_DeInit(void)
{
   M0P_PCNT->CR = 0;
   M0P_PCNT->RUN = 0;
   M0P_SYSCTRL->PERI_CLKEN_f.PCNT = 0; 

}

/**
 * \brief   
 *          PCNT 脉冲计数设置
 *
 * \param   [in]  start  开始计数设置
 * \param   [in]  end    结束计数设置
 *
 * \retval  无
 */
en_result_t PCNT_Parameter(uint8_t start,uint8_t end)
{
  uint32_t u32TimeOut;
    
   u32TimeOut = 1000;
   M0P_PCNT->BUF = end;     //加载结束溢出值
	 M0P_PCNT->CMD_f.B2T = 1;
	
	  while(u32TimeOut--)
    {
        if(FALSE == M0P_PCNT->SR2_f.B2T)
        {
            break;
        }
    }
    if(u32TimeOut == 0)
    {
        return ErrorTimeout;
    }

   u32TimeOut = 1000;
   M0P_PCNT->BUF = start;     //加载初始值
	 M0P_PCNT->CMD_f.B2C = 1;
	
	  while(u32TimeOut--)
    {
        if(FALSE == M0P_PCNT->SR2_f.B2C)
        {
            break;
        }
    }
    if(u32TimeOut == 0)
    {
        return ErrorTimeout;
    }
   return Ok;   
}

/**
 * \brief   
 *          获取PCNT计数方向
 * \param   [in]  
 *
 * \retval  无
 */
en_pcnt_direcsel_t PCNT_Direction(void)
{
   return  (en_pcnt_direcsel_t)M0P_PCNT->SR1_f.DIR;
}

/**
 * \brief   
 *          获取PCNT计数值
 * \param   [in]  
 *
 * \retval  无
 */
uint16_t PCNT_Count(void)
{
   return  M0P_PCNT->CNT;
}

/**
 * \brief   
 *          获取PCNT溢出值
 * \param   [in]  
 *
 * \retval  无
 */
uint16_t PCNT_TopCount(void)
{
   return  M0P_PCNT->TOP;
}

/**
 * \brief   
 *          PCNT使能
 * \param   [in]  
 *
 * \retval  无
 */
void PCNT_Run(boolean_t work)
{
   M0P_PCNT->RUN_f.RUN = work; 
}

/**
 * \brief   
 *          PCNT 读取状态
  * \param  [in]  en_pcnt_status_t  PCNT状态
 *
 * \retval  无
 */
boolean_t PCNT_GetStatus(en_pcnt_status_t enStatus)
{
    boolean_t bFlag = FALSE;
    
    ASSERT(IS_VALID_STAT(enStatus));

    switch (enStatus)
    {
        case PCNT_S1E:
            bFlag = M0P_PCNT->IFR_f.S1E;
            break;
        case PCNT_S0E:
            bFlag = M0P_PCNT->IFR_f.S0E;
            break;
        case PCNT_BB:
            bFlag = M0P_PCNT->IFR_f.BB;
            break;
        case PCNT_FE:
            bFlag = M0P_PCNT->IFR_f.FE;
            break;
        case PCNT_DIR:
            bFlag = M0P_PCNT->IFR_f.DIR;
            break;
        case PCNT_TO:
            bFlag = M0P_PCNT->IFR_f.TO;
            break;
        case PCNT_OV:
            bFlag = M0P_PCNT->IFR_f.OV;
            break;
        case PCNT_UF:
            bFlag = M0P_PCNT->IFR_f.UF;
            break;             
        default:
            break;
    }
    return bFlag;
}
/**
 * \brief   
 *          PCNT 清除状态
  * \param  [in]  en_pcnt_status_t  PCNT状态
 *
 * \retval  无
 */
void PCNT_ClrStatus(en_pcnt_status_t enStatus)
{

    ASSERT(IS_VALID_STAT(enStatus));

    switch (enStatus)
    {
        case PCNT_S1E:
            M0P_PCNT->ICR_f.S1E = 0;
            break;
        case PCNT_S0E:
            M0P_PCNT->ICR_f.S0E = 0;
            break;
        case PCNT_BB:
             M0P_PCNT->ICR_f.BB = 0;
            break;
        case PCNT_FE:
            M0P_PCNT->ICR_f.FE = 0;
            break;
        case PCNT_DIR:
            M0P_PCNT->ICR_f.DIR = 0;
            break;
        case PCNT_TO:
            M0P_PCNT->ICR_f.TO = 0;
            break;
        case PCNT_OV:
            M0P_PCNT->ICR_f.OV = 0;
            break;
        case PCNT_UF:
            M0P_PCNT->ICR_f.UF = 0;
            break;             
        default:
            break;
    }
}
/**
 * \brief   
 *          PCNT 中断设置
  * \param  [in]  en_pcnt_status_t  PCNT状态
 *
 * \retval  无
 */
void PCNT_SetIrqStatus(en_pcnt_status_t enStatus)
{

    ASSERT(IS_VALID_STAT(enStatus));

    switch (enStatus)
    {
        case PCNT_S1E:
            M0P_PCNT->IEN_f.S1E = 1;
            break;                
        case PCNT_S0E:            
            M0P_PCNT->IEN_f.S0E = 1;
            break;                
        case PCNT_BB:            
             M0P_PCNT->IEN_f.BB = 1;
            break;                
        case PCNT_FE:             
            M0P_PCNT->IEN_f.FE =  1;
            break;                
        case PCNT_DIR:            
            M0P_PCNT->IEN_f.DIR = 1;
            break;                
        case PCNT_TO:             
            M0P_PCNT->IEN_f.TO =  1;
            break;                
        case PCNT_OV:             
            M0P_PCNT->IEN_f.OV =  1;
            break;                
        case PCNT_UF:             
            M0P_PCNT->IEN_f.UF =  1;
            break;             
        default:
            break;
    }
}
//@} // OPAGroup


/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/