-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathbl602_tim.c
More file actions
727 lines (613 loc) · 18 KB
/
bl602_tim.c
File metadata and controls
727 lines (613 loc) · 18 KB
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/****************************************************************************
* arch/risc-v/src/bl602/bl602_tim.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <hardware/bl602_timer.h>
#include "bl602_tim.h"
#include "riscv_internal.h"
/****************************************************************************
* Name: bl602_data_setbits
****************************************************************************/
static void bl602_wdt_access(void)
{
modifyreg32(BL602_TIMER_WFAR, TIMER_WFAR_MASK, 0xbaba);
modifyreg32(BL602_TIMER_WSAR, TIMER_WSAR_MASK, 0xeb10);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: bl602_timer_getcompvalue
*
* Description:
* Get the specified channel and match comparator value.
*
* Input Parameters:
* timer_ch - TIMER channel type.
* cmp_no - TIMER comparator ID type.
*
* Returned Value:
* Match compare register value
*
****************************************************************************/
uint32_t bl602_timer_getcompvalue(uint8_t timer_ch, uint8_t cmp_no)
{
return getreg32(BL602_TIMER_TMR2_0 + \
4 * (TIMER_MAX_MATCH * timer_ch + cmp_no));
}
/****************************************************************************
* Name: bl602_timer_setcompvalue
*
* Description:
* TIMER set specified channel and comparator compare value
*
* Input Parameters:
* timer_ch - TIMER channel type.
* cmp_no - TIMER comparator ID type.
* val - TIMER match compare register value.
*
* Returned Value:
* None
*
****************************************************************************/
void bl602_timer_setcompvalue(uint8_t timer_ch,
uint8_t cmp_no,
uint32_t val)
{
putreg32(val,
BL602_TIMER_TMR2_0 + 4 * (TIMER_MAX_MATCH * timer_ch + cmp_no));
}
/****************************************************************************
* Name: bl602_timer_getcountervalue
*
* Description:
* TIMER get the specified channel count value.
*
* Input Parameters:
* timer_ch - TIMER channel type
*
* Returned Value:
* TIMER count register value
*
****************************************************************************/
uint32_t bl602_timer_getcountervalue(uint32_t timer_ch)
{
uint32_t count;
uint32_t tcvwr_addr;
/* TO avoid risk of reading, don't read TCVWR directly
* request for read. Why? This register is read-only.
*/
tcvwr_addr = BL602_TIMER_TCVWR2 + 4 * timer_ch;
putreg32(1, tcvwr_addr);
/* Need wait */
count = getreg32(tcvwr_addr);
count = getreg32(tcvwr_addr);
count = getreg32(tcvwr_addr);
return count;
}
/****************************************************************************
* Name: bl602_timer_getmatchstatus
*
* Description:
* TIMER get specified channel and comparator match status
*
* Input Parameters:
* timer_ch - TIMER channel type.
* cmp_no - TIMER comparator ID type.
*
* Returned Value:
* match status
*
****************************************************************************/
bool bl602_timer_getmatchstatus(uint32_t timer_ch, uint8_t cmp_no)
{
uint32_t status = getreg32(BL602_TIMER_TMSR2 + 4 * timer_ch);
switch (cmp_no)
{
case TIMER_COMP_ID_0:
return ((status & TIMER_TMSR2_TMSR_0) != 0);
case TIMER_COMP_ID_1:
return ((status & TIMER_TMSR2_TMSR_1) != 0);
case TIMER_COMP_ID_2:
return ((status & TIMER_TMSR2_TMSR_2) != 0);
default:
break;
}
return false;
}
/****************************************************************************
* Name: bl602_timer_getpreloadvalue
*
* Description:
* TIMER get specified channel preload value
*
* Input Parameters:
* timer_ch - TIMER channel type.
*
* Returned Value:
* Preload register value.
*
****************************************************************************/
uint32_t bl602_timer_getpreloadvalue(uint32_t timer_ch)
{
return getreg32(BL602_TIMER_TPLVR2 + 4 * timer_ch);
}
/****************************************************************************
* Name: bl602_timer_setpreloadvalue
*
* Description:
* TIMER set preload register low 32bits value
*
* Input Parameters:
* timer_ch - TIMER channel type.
* val - Preload register low 32bits value.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_timer_setpreloadvalue(uint8_t timer_ch, uint32_t val)
{
putreg32(val, BL602_TIMER_TPLVR2 + 4 * timer_ch);
}
/****************************************************************************
* Name: bl602_timer_setpreloadtrigsrc
*
* Description:
* TIMER set preload trigger source,COMP0,COMP1,COMP2 or None
*
* Input Parameters:
* timer_ch - TIMER channel type.
* pl_src - TIMER preload source type.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_timer_setpreloadtrigsrc(uint8_t timer_ch, uint32_t pl_src)
{
putreg32(pl_src, BL602_TIMER_TPLCR2 + 4 * timer_ch);
}
/****************************************************************************
* Name: bl602_timer_setcountmode
*
* Description:
* TIMER set count mode:preload or free run
*
* Input Parameters:
* timer_ch - TIMER channel type.
* count_mode - TIMER count mode: TIMER_COUNT_PRELOAD or
* TIMER_COUNT_FREERUN.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_timer_setcountmode(uint32_t timer_ch, uint8_t count_mode)
{
modifyreg32(BL602_TIMER_TCMR,
TIMER_TCMR_TIMER2_MODE << timer_ch,
count_mode ? TIMER_TCMR_TIMER2_MODE << timer_ch : 0);
}
/****************************************************************************
* Name: bl602_timer_clearintstatus
*
* Description:
* TIMER clear interrupt status
*
* Input Parameters:
* timer_ch - TIMER channel type.
* cmp_no - TIMER match comparator ID type.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_timer_clearintstatus(uint8_t timer_ch, uint32_t cmp_no)
{
putreg32(BL602_TIMER_TICR2 + 4 * timer_ch, 1 << cmp_no);
}
/****************************************************************************
* Name: bl602_timer_init
*
* Description:
* TIMER initialization function.
*
* Input Parameters:
* timer_cfg - TIMER configuration structure pointer.
*
* Returned Value:
* None
*
****************************************************************************/
void bl602_timer_init(struct timer_cfg_s *timer_cfg)
{
uint8_t timer_ch = timer_cfg->timer_ch;
/* Configure timer clock source */
if (timer_ch == TIMER_CH0)
{
modifyreg32(BL602_TIMER_TCCR, TIMER_TCCR_CS_1_MASK,
timer_cfg->clk_src << TIMER_TCCR_CS_1_SHIFT);
}
else
{
modifyreg32(BL602_TIMER_TCCR, TIMER_TCCR_CS_2_MASK,
timer_cfg->clk_src << TIMER_TCCR_CS_2_SHIFT);
}
/* Configure timer clock division */
if (timer_ch == TIMER_CH0)
{
modifyreg32(BL602_TIMER_TCDR, TIMER_TCDR_TCDR2_MASK,
timer_cfg->clock_division << TIMER_TCDR_TCDR2_SHIFT);
}
else
{
modifyreg32(BL602_TIMER_TCDR, TIMER_TCDR_TCDR3_MASK,
timer_cfg->clock_division << TIMER_TCDR_TCDR3_SHIFT);
}
/* Configure timer count mode: preload or free run */
bl602_timer_setcountmode(timer_ch, timer_cfg->count_mode);
/* Configure timer preload trigger src */
bl602_timer_setpreloadtrigsrc(timer_ch, timer_cfg->pl_trig_src);
if (timer_cfg->count_mode == TIMER_COUNT_PRELOAD)
{
/* Configure timer preload value */
bl602_timer_setpreloadvalue(timer_ch, timer_cfg->pre_load_val);
}
/* Configure match compare values */
bl602_timer_setcompvalue(timer_ch, 0, timer_cfg->match_val0);
bl602_timer_setcompvalue(timer_ch, 1, timer_cfg->match_val1);
bl602_timer_setcompvalue(timer_ch, 2, timer_cfg->match_val2);
}
/****************************************************************************
* Name: bl602_timer_enable
*
* Description:
* TIMER enable one channel function.
*
* Input Parameters:
* timer_ch - TIMER channel type.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_timer_enable(uint8_t timer_ch)
{
modifyreg32(BL602_TIMER_TCER, 0, (TIMER_TCER_TIMER2_EN << timer_ch));
}
/****************************************************************************
* Name: bl602_timer_disable
*
* Description:
* TIMER disable one channel function.
*
* Input Parameters:
* timer_ch - TIMER channel type.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_timer_disable(uint8_t timer_ch)
{
modifyreg32(BL602_TIMER_TCER, (TIMER_TCER_TIMER2_EN << timer_ch), 0);
}
/****************************************************************************
* Name: bl602_timer_intmask
*
* Description:
* TIMER mask or unmask certain or all interrupt.
*
* Input Parameters:
* timer_ch - TIMER channel type.
* int_type - TIMER interrupt type.
* int_mask - TIMER interrupt mask value:1:disable interrupt.0:enable
* interrupt.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_timer_intmask(uint8_t timer_ch, uint8_t int_type,
uint8_t int_mask)
{
uint32_t tier_addr = BL602_TIMER_TIER2 + 4 * timer_ch;
switch (int_type)
{
case TIMER_INT_COMP_0:
if (int_mask == 0)
{
/* Enable this interrupt */
modifyreg32(tier_addr, 0, TIMER_TIER2_TIER_0);
}
else
{
/* Disable this interrupt */
modifyreg32(tier_addr, TIMER_TIER2_TIER_0, 0);
}
break;
case TIMER_INT_COMP_1:
if (int_mask == 0)
{
/* Enable this interrupt */
modifyreg32(tier_addr, 0, TIMER_TIER2_TIER_1);
}
else
{
/* Disable this interrupt */
modifyreg32(tier_addr, TIMER_TIER2_TIER_1, 0);
}
break;
case TIMER_INT_COMP_2:
if (int_mask == 0)
{
/* Enable this interrupt */
modifyreg32(tier_addr, 0, TIMER_TIER2_TIER_2);
}
else
{
/* Disable this interrupt */
modifyreg32(tier_addr, TIMER_TIER2_TIER_2, 0);
}
break;
case TIMER_INT_ALL:
if (int_mask == 0)
{
/* Enable this interrupt */
modifyreg32(tier_addr, 0,
TIMER_TIER2_TIER_0 | TIMER_TIER2_TIER_1 | TIMER_TIER2_TIER_2);
}
else
{
/* Disable this interrupt */
modifyreg32(tier_addr,
TIMER_TIER2_TIER_0 | TIMER_TIER2_TIER_1 | TIMER_TIER2_TIER_2, 0);
}
break;
default:
break;
}
}
/****************************************************************************
* Name: bl602_wdt_set_clock
*
* Description:
* TIMER set watchdog clock source and clock division.
*
* Input Parameters:
* clk_src - Watchdog timer clock source type.
* div - Watchdog timer clock division value.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_wdt_set_clock(uint8_t clk_src, uint8_t div)
{
/* Configure watchdog timer clock source */
modifyreg32(BL602_TIMER_TCCR, TIMER_TCCR_CS_WDT_MASK,
clk_src << TIMER_TCCR_CS_WDT_SHIFT);
/* Configure watchdog timer clock division */
modifyreg32(BL602_TIMER_TCDR, TIMER_TCDR_WCDR_MASK,
div << TIMER_TCDR_WCDR_SHIFT);
}
/****************************************************************************
* Name: bl602_wdt_getmatchvalue
*
* Description:
* TIMER get watchdog match compare value.
*
* Input Parameters:
* None.
*
* Returned Value:
* Watchdog match compare register value.
*
****************************************************************************/
uint16_t bl602_wdt_getmatchvalue(void)
{
bl602_wdt_access();
/* Get watchdog timer match register value */
return (uint16_t)(getreg32(BL602_TIMER_WMR) & TIMER_WMR_WMR_MASK);
}
/****************************************************************************
* Name: bl602_wdt_setcompvalue
*
* Description:
* TIMER set watchdog match compare value.
*
* Input Parameters:
* val - Watchdog match compare value
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_wdt_setcompvalue(uint16_t val)
{
bl602_wdt_access();
/* Set watchdog timer match register value */
putreg32(val, BL602_TIMER_WMR);
}
/****************************************************************************
* Name: bl602_wdt_getcountervalue
*
* Description:
* TIMER get watchdog count register value.
*
* Input Parameters:
* None.
*
* Returned Value:
* Watchdog count register value.
*
****************************************************************************/
uint16_t bl602_wdt_getcountervalue(void)
{
bl602_wdt_access();
/* Get watchdog timer count register value */
return (uint16_t)(getreg32(BL602_TIMER_WVR) & TIMER_WVR_WVR_MASK);
}
/****************************************************************************
* Name: bl602_wdt_resetcountervalue
*
* Description:
* TIMER reset watchdog count register value.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_wdt_resetcountervalue(void)
{
/* Reset watchdog timer count register value */
bl602_wdt_access();
/* Set watchdog counter reset register bit0 to 1 */
modifyreg32(BL602_TIMER_WCR, 0, TIMER_WCR_WCR);
}
/****************************************************************************
* Name: bl602_wdt_getresetstatus
*
* Description:
* TIMER get watchdog reset status.
*
* Input Parameters:
* None.
*
* Returned Value:
* 0 or 1.
*
****************************************************************************/
bool bl602_wdt_getresetstatus(void)
{
bl602_wdt_access();
/* Get watchdog status register */
return ((getreg32(BL602_TIMER_WSR) & TIMER_WSR_WTS) != 0);
}
/****************************************************************************
* Name: bl602_wdt_clearresetstatus
*
* Description:
* TIMER clear watchdog reset status.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_wdt_clearresetstatus(void)
{
bl602_wdt_access();
/* Set watchdog status register */
modifyreg32(BL602_TIMER_WSR, TIMER_WSR_WTS, 0);
}
/****************************************************************************
* Name: bl602_wdt_enable
*
* Description:
* TIMER enable watchdog function.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_wdt_enable(void)
{
bl602_wdt_access();
modifyreg32(BL602_TIMER_WMER, 0, TIMER_WMER_WE);
}
/****************************************************************************
* Name: bl602_wdt_disable
*
* Description:
* Watchdog timer disable function.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_wdt_disable(void)
{
bl602_wdt_access();
modifyreg32(BL602_TIMER_WMER, TIMER_WMER_WE, 0);
}
/****************************************************************************
* Name: bl602_wdt_intmask
*
* Description:
* Watchdog timer mask or unmask certain or all interrupt.
*
* Input Parameters:
* int_type - Watchdog interrupt type.
* int_mask - Watchdog interrupt mask value:BL_STD_MASK:disable
* interrupt.BL_STD_UNMASK:enable interrupt.
*
* Returned Value:
* None.
*
****************************************************************************/
void bl602_wdt_intmask(uint8_t int_type, uint8_t int_mask)
{
bl602_wdt_access();
/* Deal with watchdog match/interrupt enable register,WRIE:watchdog
* reset/interrupt enable
*/
switch (int_type)
{
case WDT_INT:
if (int_mask == 0)
{
/* Enable this interrupt */
/* 0 means generates a watchdog interrupt,a watchdog timer reset is
* not generated
*/
modifyreg32(BL602_TIMER_WMER, TIMER_WMER_WRIE, 0);
}
else
{
/* Disable this interrupt */
/* 1 means generates a watchdog timer reset,a watchdog interrupt is
* not generated
*/
modifyreg32(BL602_TIMER_WMER, 0, TIMER_WMER_WRIE);
}
break;
default:
break;
}
}