2323#define SYS_TIM_ID 1 // the G-Timer ID for System
2424#define APP_TIM_ID 6 // the G-Timer ID for Application
2525
26+ #define TICK_TO_US (x ) (uint64_t)(((x)/2) * 61 + ((x)%2) * TIMER_TICK_US)
27+
2628static int us_ticker_inited = 0 ;
2729static TIMER_ADAPTER TimerAdapter ;
2830
2931extern HAL_TIMER_OP HalTimerOp ;
3032extern HAL_TIMER_OP_EXT HalTimerOpExt ;
3133
32- VOID _us_ticker_irq_handler (IN VOID * Data )
34+ VOID _us_ticker_irq_handler (void * Data )
3335{
3436 us_ticker_irq_handler ();
37+ HalTimerOp .HalTimerDis ((u32 )TimerAdapter .TimerId );
3538}
3639
3740void us_ticker_init (void )
3841{
3942
40- if (us_ticker_inited ) return ;
43+ if (us_ticker_inited ){
44+ return ;
45+ }
46+
4147 us_ticker_inited = 1 ;
4248
43-
49+ HalTimerOp .HalTimerDis (SYS_TIM_ID );
50+ HalTimerOpExt .HalTimerReLoad (SYS_TIM_ID , 0xFFFFFFFFUL );
51+ HalTimerOp .HalTimerEn (SYS_TIM_ID );
52+
4453 // Initial a G-Timer
4554 TimerAdapter .IrqDis = 0 ; // Enable Irq @ initial
4655 TimerAdapter .IrqHandle .IrqFun = (IRQ_FUN ) _us_ticker_irq_handler ;
@@ -52,50 +61,45 @@ void us_ticker_init(void)
5261 TimerAdapter .TimerLoadValueUs = 0xFFFFFFFF ;
5362 TimerAdapter .TimerMode = USER_DEFINED ;
5463
55- HalTimerOp .HalTimerInit ((VOID * ) & TimerAdapter );
64+ HalTimerOp .HalTimerInit ((void * ) & TimerAdapter );
5665
5766 DBG_TIMER_INFO ("%s: Timer_Id=%d\n" , __FUNCTION__ , APP_TIM_ID );
5867}
5968
60- uint32_t us_ticker_read ()
69+ uint32_t us_ticker_read (void )
6170{
6271 uint32_t tick_cnt ;
63- uint32_t ticks_125ms ;
64- uint32_t ticks_remain ;
65- uint64_t us_tick ;
66-
72+ uint64_t tick_us ;
73+
74+ if (!us_ticker_inited ) {
75+ us_ticker_init ();
76+ }
77+
6778 tick_cnt = HalTimerOp .HalTimerReadCount (SYS_TIM_ID );
68- tick_cnt = 0xffffffff - tick_cnt ; // it's a down counter
69- ticks_125ms = tick_cnt /(GTIMER_CLK_HZ /8 ); //use 125ms as a intermediate unit;
70- ticks_remain = tick_cnt - (ticks_125ms * (GTIMER_CLK_HZ /8 )); //calculate the remainder
71- us_tick = ticks_125ms * 125000 ; //change unit to us, 125ms is 125000 us
72- us_tick += (ticks_remain * 1000000 )/GTIMER_CLK_HZ ; //also use us as unit
79+ tick_us = TICK_TO_US (0xFFFFFFFFUL - tick_cnt );
7380
74- return ((uint32_t )us_tick ); //return ticker value in micro-seconds (us)
81+ return ((uint32_t )tick_us ); //return ticker value in micro-seconds (us)
7582}
7683
7784void us_ticker_set_interrupt (timestamp_t timestamp )
7885{
79- uint32_t cur_time_us ;
80- uint32_t time_dif ;
86+ uint32_t time_cur ;
87+ uint32_t time_cnt ;
8188
8289 HalTimerOp .HalTimerDis ((u32 )TimerAdapter .TimerId );
83- cur_time_us = us_ticker_read ();
84- if (( uint32_t ) timestamp > cur_time_us ) {
85- time_dif = ( uint32_t ) timestamp - cur_time_us ;
90+ time_cur = us_ticker_read ();
91+ if (timestamp > time_cur + TIMER_TICK_US ) {
92+ time_cnt = timestamp - time_cur ;
8693 } else {
8794 HalTimerOpExt .HalTimerReLoad ((u32 )TimerAdapter .TimerId , 0xffffffff );
88- HalTimerOpExt .HalTimerIrqEn ((u32 )TimerAdapter .TimerId );
8995 HalTimerOp .HalTimerEn ((u32 )TimerAdapter .TimerId );
90- NVIC_SetPendingIRQ ( TIMER2_7_IRQ );
96+ us_ticker_fire_interrupt ( );
9197 return ;
9298 }
9399
94- TimerAdapter .TimerLoadValueUs = time_dif ;
95- HalTimerOpExt .HalTimerReLoad ((u32 )TimerAdapter .TimerId , time_dif / TIMER_TICK_US );
96- HalTimerOpExt .HalTimerIrqEn ((u32 )TimerAdapter .TimerId );
100+ TimerAdapter .TimerLoadValueUs = time_cnt / TIMER_TICK_US ;
101+ HalTimerOpExt .HalTimerReLoad ((u32 )TimerAdapter .TimerId , TimerAdapter .TimerLoadValueUs );
97102 HalTimerOp .HalTimerEn ((u32 )TimerAdapter .TimerId );
98-
99103}
100104
101105void us_ticker_fire_interrupt (void )
0 commit comments