20
20
#include "PeripheralNames.h"
21
21
22
22
#define TICK_READ_FROM_CPU 0 // 1: read tick from CPU, 0: read tick from G-Timer
23
- #define SYS_TIM_ID 1 // the G-Timer ID for System
24
- #define APP_TIM_ID 6 // the G-Timer ID for Application
23
+ #define SYS_TIM_ID 1 // the G-Timer ID for System
24
+ #define APP_TIM_ID 6 // the G-Timer ID for Application
25
25
26
- #define TICK_TO_US (x ) (uint64_t)(((x)/2) * 61 + ((x)%2) * TIMER_TICK_US)
26
+ /*
27
+ * For RTL8195AM, clock source is 32k
28
+ *
29
+ * us per tick: 30.5
30
+ * tick per ms: 32.7
31
+ * tick per us: 0.032
32
+ * tick per sec: 32768
33
+ *
34
+ * Define the following macros to convert between TICK and US.
35
+ */
36
+ #define MS_TO_TICK (x ) (uint64_t)(((x)*327) / 10)
37
+ #define US_TO_TICK (x ) (uint64_t)(((x)*32) / 1000)
38
+ #define TICK_TO_US (x ) (uint64_t)(((x)/2) * 61 + ((x)%2) * TIMER_TICK_US)
27
39
28
40
static int us_ticker_inited = 0 ;
29
41
static TIMER_ADAPTER TimerAdapter ;
@@ -34,23 +46,22 @@ extern HAL_TIMER_OP_EXT HalTimerOpExt;
34
46
VOID _us_ticker_irq_handler (void * Data )
35
47
{
36
48
us_ticker_irq_handler ();
37
- HalTimerOp .HalTimerDis ((u32 )TimerAdapter .TimerId );
38
49
}
39
50
40
- void us_ticker_init (void )
51
+ void us_ticker_init (void )
41
52
{
42
-
43
- if (us_ticker_inited ){
53
+ if (us_ticker_inited ) {
44
54
return ;
45
55
}
46
-
56
+
47
57
us_ticker_inited = 1 ;
48
-
58
+
59
+ // Reload and restart sys-timer
49
60
HalTimerOp .HalTimerDis (SYS_TIM_ID );
50
61
HalTimerOpExt .HalTimerReLoad (SYS_TIM_ID , 0xFFFFFFFFUL );
51
62
HalTimerOp .HalTimerEn (SYS_TIM_ID );
52
-
53
- // Initial a G-Timer
63
+
64
+ // Initial a app-timer
54
65
TimerAdapter .IrqDis = 0 ; // Enable Irq @ initial
55
66
TimerAdapter .IrqHandle .IrqFun = (IRQ_FUN ) _us_ticker_irq_handler ;
56
67
TimerAdapter .IrqHandle .IrqNum = TIMER2_7_IRQ ;
@@ -66,22 +77,22 @@ void us_ticker_init(void)
66
77
DBG_TIMER_INFO ("%s: Timer_Id=%d\n" , __FUNCTION__ , APP_TIM_ID );
67
78
}
68
79
69
- uint32_t us_ticker_read (void )
80
+ uint32_t us_ticker_read (void )
70
81
{
71
82
uint32_t tick_cnt ;
72
83
uint64_t tick_us ;
73
-
84
+
74
85
if (!us_ticker_inited ) {
75
86
us_ticker_init ();
76
87
}
77
-
88
+
78
89
tick_cnt = HalTimerOp .HalTimerReadCount (SYS_TIM_ID );
79
90
tick_us = TICK_TO_US (0xFFFFFFFFUL - tick_cnt );
80
91
81
92
return ((uint32_t )tick_us ); //return ticker value in micro-seconds (us)
82
93
}
83
94
84
- void us_ticker_set_interrupt (timestamp_t timestamp )
95
+ void us_ticker_set_interrupt (timestamp_t timestamp )
85
96
{
86
97
uint32_t time_cur ;
87
98
uint32_t time_cnt ;
@@ -95,9 +106,9 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
95
106
HalTimerOp .HalTimerEn ((u32 )TimerAdapter .TimerId );
96
107
us_ticker_fire_interrupt ();
97
108
return ;
98
- }
109
+ }
99
110
100
- TimerAdapter .TimerLoadValueUs = time_cnt / TIMER_TICK_US ;
111
+ TimerAdapter .TimerLoadValueUs = MAX ( MS_TO_TICK ( time_cnt / 1000 ) + US_TO_TICK ( time_cnt % 1000 ), 1 ) ;
101
112
HalTimerOpExt .HalTimerReLoad ((u32 )TimerAdapter .TimerId , TimerAdapter .TimerLoadValueUs );
102
113
HalTimerOp .HalTimerEn ((u32 )TimerAdapter .TimerId );
103
114
}
@@ -107,12 +118,12 @@ void us_ticker_fire_interrupt(void)
107
118
NVIC_SetPendingIRQ (TIMER2_7_IRQ );
108
119
}
109
120
110
- void us_ticker_disable_interrupt (void )
121
+ void us_ticker_disable_interrupt (void )
111
122
{
112
123
HalTimerOp .HalTimerDis ((u32 )TimerAdapter .TimerId );
113
124
}
114
125
115
- void us_ticker_clear_interrupt (void )
126
+ void us_ticker_clear_interrupt (void )
116
127
{
117
128
HalTimerOp .HalTimerIrqClear ((u32 )TimerAdapter .TimerId );
118
129
}
0 commit comments