26
26
#include "PeripheralNames.h"
27
27
28
28
#define TIMER_TARGET_COUNT_DEFAULT 0xFFFFFFFF
29
+ #define TIMER0_UNIT (26*1000*1000) //1s
29
30
30
31
volatile uint32_t us_ticker_initialized = 0 ;
31
32
volatile uint32_t us_user_intset ;
32
33
volatile uint32_t g_us_last_return = 0 ;
34
+ uint64_t g_tick_sec = 0 ;
35
+ uint32_t g_timer0_pre_cnt ;
33
36
34
37
void us_ticker_enable_interrupt (void );
35
38
void us_ticker_disable_interrupt (void );
@@ -46,9 +49,9 @@ const ticker_info_t *us_ticker_get_info()
46
49
static void enable_timer0 (void )
47
50
{
48
51
putreg32 (1 , S5JS100_TIMER0_BASE + S5JS100_TIMER_UP_DOWN_SEL ); // Set Up count
49
- putreg32 (0x0 , S5JS100_TIMER0_BASE + S5JS100_TIMER_LOAD_VALUE );
52
+ putreg32 (g_timer0_pre_cnt , S5JS100_TIMER0_BASE + S5JS100_TIMER_LOAD_VALUE );
50
53
putreg32 (0x1 , S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_ENABLE );
51
- putreg32 (0 , S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE );
54
+ putreg32 (g_timer0_pre_cnt , S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE );
52
55
putreg32 (TIMER_TARGET_COUNT_DEFAULT , S5JS100_TIMER0_BASE + S5JS100_TIMER_INT_SEL );
53
56
putreg32 (0 , S5JS100_TIMER0_BASE + S5JS100_TIMER_INT_ENABLE );
54
57
putreg32 (3 , S5JS100_TIMER0_BASE + S5JS100_TIMER_CONTROL );
@@ -98,6 +101,7 @@ void us_ticker_init(void)
98
101
99
102
if (!us_ticker_initialized ) {
100
103
us_ticker_initialized = 1 ;
104
+ g_timer0_pre_cnt = 0 ;
101
105
/* Enable timer0 to timer */
102
106
enable_timer0 ();
103
107
/* Enable timer1 to compare and Disable timer1 interrupt */
@@ -114,11 +118,21 @@ void us_ticker_init(void)
114
118
115
119
uint32_t us_ticker_read ()
116
120
{
121
+ uint32_t cnt ;
117
122
/* from timer0 read count value */
118
123
if (!us_ticker_initialized ) {
119
124
us_ticker_init ();
120
125
}
121
- volatile uint32_t current_count = getreg32 (S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE ) / 26 ;
126
+ volatile uint32_t current_count = getreg32 (S5JS100_TIMER0_BASE + S5JS100_TIMER_CDC_COUNT_VALUE );
127
+ cnt = current_count /TIMER0_UNIT ;
128
+ if (cnt ) {
129
+ g_tick_sec = g_tick_sec + cnt ;
130
+ g_timer0_pre_cnt = current_count %TIMER0_UNIT ;
131
+ current_count = current_count - (cnt * TIMER0_UNIT );
132
+ disable_timer0 ();
133
+ enable_timer0 ();
134
+ }
135
+ current_count = (current_count /26 )+ (g_tick_sec * 1000 * 1000 );
122
136
g_us_last_return = current_count ;
123
137
return current_count ;
124
138
}
@@ -128,11 +142,10 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
128
142
if (timestamp < 0x70000000 && timestamp != 0 ) {
129
143
uint32_t temp = g_us_last_return ;
130
144
us_user_intset = timestamp - g_us_last_return ;
131
- /* keep to check
132
145
if (us_user_intset < 0 ) {
133
146
us_ticker_irq_handler ();
134
147
return ;
135
- }*/
148
+ }
136
149
us_user_intset = us_user_intset * 26 ;
137
150
uint32_t past_tick = us_ticker_read () - temp ;
138
151
putreg32 (0 , S5JS100_TIMER1_BASE + S5JS100_TIMER_LOAD_CON_VALUE );
0 commit comments