summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/nuc980/Driver/Source/nu_etimer.c
blob: 41d927c122a09cef4d56148c16f46ccb5f865ff7 (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/**************************************************************************//**
 * @file     etimer.c
 * @brief    ETIMER driver source file
 *
 * SPDX-License-Identifier: Apache-2.0
 * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "nuc980.h"
#include "nu_sys.h"

/// @cond HIDDEN_SYMBOLS

/**
  * @brief This API is used to get the clock frequency of Timer
  * @param[in] timer ETIMER number. Range from 0 ~ 5
  * @return Timer clock frequency
  * @note This API cannot return correct clock rate if timer source is external clock input.
  */
UINT ETIMER_GetModuleClock(UINT timer)
{
    UINT src;

    src = (inpw(REG_CLK_DIVCTL8) >> (16 + timer * 2)) & 0x3;

    if (src == 0)
        return 12000000;
    else if (src == 1)
        return (sysGetClock(SYS_PCLK01) * 1000000);
    else if (src == 2)
        return (sysGetClock(SYS_PCLK01) * 1000000 / 4096);
    else
        return 32768;

}

/// @endcond /* HIDDEN_SYMBOLS */

/** @addtogroup Standard_Driver Standard Driver
  @{
*/

/** @addtogroup ETIMER_Driver ETIMER Driver
  @{
*/


/** @addtogroup ETIMER_EXPORTED_FUNCTIONS ETIMER Exported Functions
  @{
*/

/**
  * @brief This API is used to configure timer to operate in specified mode
  *        and frequency. If timer cannot work in target frequency, a closest
  *        frequency will be chose and returned.
  * @param[in] timer ETIMER number. Range from 0 ~ 5
  * @param[in] u32Mode Operation mode. Possible options are
  *                 - \ref ETIMER_ONESHOT_MODE
  *                 - \ref ETIMER_PERIODIC_MODE
  *                 - \ref ETIMER_TOGGLE_MODE
  *                 - \ref ETIMER_CONTINUOUS_MODE
  * @param[in] u32Freq Target working frequency
  * @return Real Timer working frequency
  * @note After calling this API, Timer is \b NOT running yet. But could start timer running be calling
  *       \ref ETIMER_Start macro or program registers directly
  */
UINT ETIMER_Open(UINT timer, UINT u32Mode, UINT u32Freq)
{
    UINT u32Clk = ETIMER_GetModuleClock(timer);
    UINT u32Cmpr = 0, u32Prescale = 0;

    // Fastest possible timer working freq is u32Clk / 2. While cmpr = 2, pre-scale = 0
    if (u32Freq > (u32Clk / 2))
    {
        u32Cmpr = 2;
    }
    else
    {
        if (u32Clk >= 0x8000000)
        {
            u32Prescale = 15;    // real prescaler value is 16
            u32Clk >>= 4;
        }
        else if (u32Clk >= 0x4000000)
        {
            u32Prescale = 7;    // real prescaler value is 8
            u32Clk >>= 3;
        }
        else if (u32Clk >= 0x2000000)
        {
            u32Prescale = 3;    // real prescaler value is 4
            u32Clk >>= 2;
        }
        else if (u32Clk >= 0x1000000)
        {
            u32Prescale = 1;    // real prescaler value is 2
            u32Clk >>= 1;
        }
        u32Cmpr = u32Clk / u32Freq;
    }

    if (timer == 0)
    {
        outpw(REG_ETMR0_CMPR, u32Cmpr);
        outpw(REG_ETMR0_PRECNT, u32Prescale);
        outpw(REG_ETMR0_CTL, 1 | u32Mode);
    }
    else if (timer == 1)
    {
        outpw(REG_ETMR1_CMPR, u32Cmpr);
        outpw(REG_ETMR1_PRECNT, u32Prescale);
        outpw(REG_ETMR1_CTL, 1 | u32Mode);
    }
    else if (timer == 2)
    {
        outpw(REG_ETMR2_CMPR, u32Cmpr);
        outpw(REG_ETMR2_PRECNT, u32Prescale);
        outpw(REG_ETMR2_CTL, 1 | u32Mode);
    }
    else if (timer == 3)
    {
        outpw(REG_ETMR3_CMPR, u32Cmpr);
        outpw(REG_ETMR3_PRECNT, u32Prescale);
        outpw(REG_ETMR3_CTL, 1 | u32Mode);
    }
    else if (timer == 4)
    {
        outpw(REG_ETMR4_CMPR, u32Cmpr);
        outpw(REG_ETMR4_PRECNT, u32Prescale);
        outpw(REG_ETMR4_CTL, 1 | u32Mode);
    }
    else
    {
        outpw(REG_ETMR5_CMPR, u32Cmpr);
        outpw(REG_ETMR5_PRECNT, u32Prescale);
        outpw(REG_ETMR5_CTL, 1 | u32Mode);
    }

    return (u32Clk / (u32Cmpr * (u32Prescale + 1)));
}

/**
  * @brief This API stops Timer counting and disable the Timer interrupt function
  * @param[in] timer ETIMER number. Range from 0 ~ 5
  * @return None
  */
void ETIMER_Close(UINT timer)
{
    if (timer == 0)
    {
        outpw(REG_ETMR0_CTL, 0);
        outpw(REG_ETMR0_IER, 0);
        outpw(REG_ETMR0_DR, 0);
    }
    else if (timer == 1)
    {
        outpw(REG_ETMR1_CTL, 0);
        outpw(REG_ETMR1_IER, 0);
        outpw(REG_ETMR1_DR, 0);
    }
    else if (timer == 2)
    {
        outpw(REG_ETMR2_CTL, 0);
        outpw(REG_ETMR2_IER, 0);
        outpw(REG_ETMR2_DR, 0);
    }
    else if (timer == 3)
    {
        outpw(REG_ETMR3_CTL, 0);
        outpw(REG_ETMR3_IER, 0);
        outpw(REG_ETMR3_DR, 0);
    }
    else if (timer == 4)
    {
        outpw(REG_ETMR4_CTL, 0);
        outpw(REG_ETMR4_IER, 0);
        outpw(REG_ETMR4_DR, 0);
    }
    else
    {
        outpw(REG_ETMR5_CTL, 0);
        outpw(REG_ETMR5_IER, 0);
        outpw(REG_ETMR5_DR, 0);
    }
}

/**
  * @brief This API is used to create a delay loop for u32usec micro seconds
  * @param[in] timer ETIMER number. Range from 0 ~ 5
  * @param[in] u32Usec Delay period in micro seconds with 10 usec every step. Valid values are between 10~1000000 (10 micro second ~ 1 second)
  * @return None
  * @note This API overwrites the register setting of the timer used to count the delay time.
  * @note This API use polling mode. So there is no need to enable interrupt for the timer module used to generate delay
  */
void ETIMER_Delay(UINT timer, UINT u32Usec)
{
    UINT u32Clk = ETIMER_GetModuleClock(timer);
    UINT u32Prescale = 0, delay = 300000000 / u32Clk;
    float fCmpr;

    // Clear current timer configuration
    if (timer == 0)
    {
        outpw(REG_ETMR0_CTL, 0);
    }
    else if (timer == 1)
    {
        outpw(REG_ETMR1_CTL, 0);
    }
    else if (timer == 2)
    {
        outpw(REG_ETMR2_CTL, 0);
    }
    else if (timer == 3)
    {
        outpw(REG_ETMR3_CTL, 0);
    }
    else if (timer == 4)
    {
        outpw(REG_ETMR4_CTL, 0);
    }
    else
    {
        outpw(REG_ETMR5_CTL, 0);
    }

    if (u32Clk == 10000)          // min delay is 100us if timer clock source is LIRC 10k
    {
        u32Usec = ((u32Usec + 99) / 100) * 100;
    }
    else        // 10 usec every step
    {
        u32Usec = ((u32Usec + 9) / 10) * 10;
    }

    if (u32Clk >= 0x4000000)
    {
        u32Prescale = 7;    // real prescaler value is 8
        u32Clk >>= 3;
    }
    else if (u32Clk >= 0x2000000)
    {
        u32Prescale = 3;    // real prescaler value is 4
        u32Clk >>= 2;
    }
    else if (u32Clk >= 0x1000000)
    {
        u32Prescale = 1;    // real prescaler value is 2
        u32Clk >>= 1;
    }

    // u32Usec * u32Clk might overflow if using UINT
    fCmpr = ((float)u32Usec * (float)u32Clk) / 1000000.0;

    if (timer == 0)
    {
        outpw(REG_ETMR0_CMPR, (UINT)fCmpr);
        outpw(REG_ETMR0_PRECNT, u32Prescale);
        outpw(REG_ETMR0_CTL, 1);
    }
    else if (timer == 1)
    {
        outpw(REG_ETMR1_CMPR, (UINT)fCmpr);
        outpw(REG_ETMR1_PRECNT, u32Prescale);
        outpw(REG_ETMR1_CTL, 1);
    }
    else if (timer == 2)
    {
        outpw(REG_ETMR2_CMPR, (UINT)fCmpr);
        outpw(REG_ETMR2_PRECNT, u32Prescale);
        outpw(REG_ETMR2_CTL, 1);
    }
    else if (timer == 3)
    {
        outpw(REG_ETMR3_CMPR, (UINT)fCmpr);
        outpw(REG_ETMR3_PRECNT, u32Prescale);
        outpw(REG_ETMR3_CTL, 1);
    }
    else if (timer == 4)
    {
        outpw(REG_ETMR4_CMPR, (UINT)fCmpr);
        outpw(REG_ETMR4_PRECNT, u32Prescale);
        outpw(REG_ETMR4_CTL, 1);
    }
    else
    {
        outpw(REG_ETMR5_CMPR, (UINT)fCmpr);
        outpw(REG_ETMR5_PRECNT, u32Prescale);
        outpw(REG_ETMR5_CTL, 1);
    }

    // When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.
    // And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag.
    for (; delay > 0; delay--)
    {
#if defined (__GNUC__) && !(__CC_ARM)
        __asm__ __volatile__
        (
            "nop  \n"
        );
#else
        __asm
        {
            NOP
        }
#endif
    }

    if (timer == 0)
    {
        while (inpw(REG_ETMR0_CTL) & 0x80);
    }
    else if (timer == 1)
    {
        while (inpw(REG_ETMR1_CTL) & 0x80);
    }
    else if (timer == 2)
    {
        while (inpw(REG_ETMR2_CTL) & 0x80);
    }
    else if (timer == 3)
    {
        while (inpw(REG_ETMR3_CTL) & 0x80);
    }
    else if (timer == 4)
    {
        while (inpw(REG_ETMR4_CTL) & 0x80);
    }
    else
    {
        while (inpw(REG_ETMR5_CTL) & 0x80);
    }
}

/**
  * @brief This API is used to enable timer capture function with specified mode and capture edge
  * @param[in] timer ETIMER number. Range from 0 ~ 5
  * @param[in] u32CapMode Timer capture mode. Could be
  *                 - \ref ETIMER_CAPTURE_FREE_COUNTING_MODE
  *                 - \ref ETIMER_CAPTURE_TRIGGER_COUNTING_MODE
  *                 - \ref ETIMER_CAPTURE_COUNTER_RESET_MODE
  * @param[in] u32Edge Timer capture edge. Possible values are
  *                 - \ref ETIMER_CAPTURE_FALLING_EDGE
  *                 - \ref ETIMER_CAPTURE_RISING_EDGE
  *                 - \ref ETIMER_CAPTURE_FALLING_THEN_RISING_EDGE
  *                 - \ref ETIMER_CAPTURE_RISING_THEN_FALLING_EDGE
  * @return None
  * @note Timer frequency should be configured separately by using \ref ETIMER_Open API, or program registers directly
  */
void ETIMER_EnableCapture(UINT timer, UINT u32CapMode, UINT u32Edge)
{
    if (timer == 0)
    {
        outpw(REG_ETMR0_CTL, (inpw(REG_ETMR0_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
    }
    else if (timer == 1)
    {
        outpw(REG_ETMR1_CTL, (inpw(REG_ETMR1_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
    }
    else if (timer == 2)
    {
        outpw(REG_ETMR2_CTL, (inpw(REG_ETMR2_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
    }
    else if (timer == 3)
    {
        outpw(REG_ETMR3_CTL, (inpw(REG_ETMR3_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
    }
    else if (timer == 4)
    {
        outpw(REG_ETMR4_CTL, (inpw(REG_ETMR4_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
    }
    else
    {
        outpw(REG_ETMR5_CTL, (inpw(REG_ETMR5_CTL) & ~0x1E0000) | u32CapMode | u32Edge | 0x10000);
    }
}

/**
  * @brief This API is used to disable the Timer capture function
  * @param[in] timer ETIMER number. Range from 0 ~ 5
  * @return None
  */
void ETIMER_DisableCapture(UINT timer)
{
    if (timer == 0)
    {
        outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~0x10000);
    }
    else if (timer == 1)
    {
        outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~0x10000);
    }
    else if (timer == 2)
    {
        outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~0x10000);
    }
    else if (timer == 3)
    {
        outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~0x10000);
    }
    else if (timer == 4)
    {
        outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) & ~0x10000);
    }
    else
    {
        outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) & ~0x10000);
    }

}

/**
  * @brief This function is used to enable the Timer counter function with specify detection edge
  * @param[in] timer ETIMER number. Range from 0 ~ 5
  * @param[in] u32Edge Detection edge of counter pin. Could be ether
  *             - \ref TIMER_COUNTER_RISING_EDGE, or
  *             - \ref TIMER_COUNTER_FALLING_EDGE
  * @return None
  * @note Timer compare value should be configured separately by using \ref ETIMER_SET_CMP_VALUE macro or program registers directly
  */
void ETIMER_EnableEventCounter(UINT timer, uint32_t u32Edge)
{

    if (timer == 0)
    {
        outpw(REG_ETMR0_CTL, (inpw(REG_ETMR0_CTL) & ~0x2000) | u32Edge | 0x1000);
    }
    else if (timer == 1)
    {
        outpw(REG_ETMR1_CTL, (inpw(REG_ETMR1_CTL) & ~0x2000) | u32Edge | 0x1000);
    }
    else if (timer == 2)
    {
        outpw(REG_ETMR2_CTL, (inpw(REG_ETMR2_CTL) & ~0x2000) | u32Edge | 0x1000);
    }
    else if (timer == 3)
    {
        outpw(REG_ETMR3_CTL, (inpw(REG_ETMR3_CTL) & ~0x2000) | u32Edge | 0x1000);
    }
    else if (timer == 4)
    {
        outpw(REG_ETMR4_CTL, (inpw(REG_ETMR4_CTL) & ~0x2000) | u32Edge | 0x1000);
    }
    else
    {
        outpw(REG_ETMR5_CTL, (inpw(REG_ETMR5_CTL) & ~0x2000) | u32Edge | 0x1000);
    }

}

/**
  * @brief This API is used to disable the Timer event counter function.
  * @param[in] timer ETIMER number. Range from 0 ~ 5
  * @return None
  */
void ETIMER_DisableEventCounter(UINT timer)
{
    if (timer == 0)
    {
        outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~0x1000);
    }
    else if (timer == 1)
    {
        outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~0x1000);
    }
    else if (timer == 2)
    {
        outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~0x1000);
    }
    else if (timer == 3)
    {
        outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~0x1000);
    }
    else if (timer == 4)
    {
        outpw(REG_ETMR4_CTL, inpw(REG_ETMR4_CTL) & ~0x1000);
    }
    else
    {
        outpw(REG_ETMR5_CTL, inpw(REG_ETMR5_CTL) & ~0x1000);
    }
}


/*@}*/ /* end of group ETIMER_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group ETIMER_Driver */

/*@}*/ /* end of group Standard_Driver */

/*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/