Skip to content

Commit 07646db

Browse files
tung7970Archcady
authored andcommitted
rtl8195am - fix us_ticker interrupt handling
TIMER2_7_IRQ is shared among several timer sources, including us_ticker. Raising TIMER2_7_IRQ pending bit will trigger the timer interrupt, but the timer interrupt handler will not know which timer source this interrupt is for. This patch sets timer load value to one tick and force us_ticker to fire almost "immediately". TIMER2_7_IRQ is handled through a common interrupt handler, and is automatically cleared. Therefore, there is no need to clear IRQ. The underlying timer HAL treats load value as micro-seconds and does conversion internally. Therefore, simply pass micro-seconds to timer HAL without converting to tick first. Signed-off-by: Tony Wu <[email protected]>
1 parent 863b3fd commit 07646db

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

targets/TARGET_Realtek/TARGET_AMEBA/us_ticker.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "us_ticker_api.h"
2020
#include "PeripheralNames.h"
2121

22-
#define TICK_READ_FROM_CPU 0 // 1: read tick from CPU, 0: read tick from G-Timer
2322
#define SYS_TIM_ID 1 // the G-Timer ID for System
2423
#define APP_TIM_ID 2 // the G-Timer ID for Application
2524

@@ -33,8 +32,6 @@
3332
*
3433
* Define the following macros to convert between TICK and US.
3534
*/
36-
#define MS_TO_TICK(x) (uint64_t)(((x)*327) / 10)
37-
#define US_TO_TICK(x) (uint64_t)(((x)*32) / 1000)
3835
#define TICK_TO_US(x) (uint64_t)(((x)/2) * 61 + ((x)%2) * TIMER_TICK_US)
3936

4037
static int us_ticker_inited = 0;
@@ -73,8 +70,6 @@ void us_ticker_init(void)
7370
TimerAdapter.TimerMode = USER_DEFINED;
7471

7572
HalTimerOp.HalTimerInit((void *) &TimerAdapter);
76-
77-
DBG_TIMER_INFO("%s: Timer_Id=%d\n", __FUNCTION__, APP_TIM_ID);
7873
}
7974

8075
uint32_t us_ticker_read(void)
@@ -95,27 +90,26 @@ uint32_t us_ticker_read(void)
9590
void us_ticker_set_interrupt(timestamp_t timestamp)
9691
{
9792
uint32_t time_cur;
98-
uint32_t time_cnt;
9993

100-
HalTimerOp.HalTimerDis((u32)TimerAdapter.TimerId);
10194
time_cur = us_ticker_read();
10295
if (timestamp > time_cur + TIMER_TICK_US) {
103-
time_cnt = timestamp - time_cur;
96+
TimerAdapter.TimerLoadValueUs = timestamp - time_cur;
10497
} else {
105-
HalTimerOpExt.HalTimerReLoad((u32)TimerAdapter.TimerId, 0xffffffff);
106-
HalTimerOp.HalTimerEn((u32)TimerAdapter.TimerId);
107-
us_ticker_fire_interrupt();
108-
return;
98+
TimerAdapter.TimerLoadValueUs = TIMER_TICK_US;
10999
}
110100

111-
TimerAdapter.TimerLoadValueUs = MAX(MS_TO_TICK(time_cnt/1000) + US_TO_TICK(time_cnt%1000), 1);
101+
HalTimerOp.HalTimerDis((u32)TimerAdapter.TimerId);
112102
HalTimerOpExt.HalTimerReLoad((u32)TimerAdapter.TimerId, TimerAdapter.TimerLoadValueUs);
113103
HalTimerOp.HalTimerEn((u32)TimerAdapter.TimerId);
114104
}
115105

116106
void us_ticker_fire_interrupt(void)
117107
{
118-
NVIC_SetPendingIRQ(TIMER2_7_IRQ);
108+
TimerAdapter.TimerLoadValueUs = TIMER_TICK_US;
109+
110+
HalTimerOp.HalTimerDis((u32)TimerAdapter.TimerId);
111+
HalTimerOpExt.HalTimerReLoad((u32)TimerAdapter.TimerId, TimerAdapter.TimerLoadValueUs);
112+
HalTimerOp.HalTimerEn((u32)TimerAdapter.TimerId);
119113
}
120114

121115
void us_ticker_disable_interrupt(void)
@@ -125,5 +119,4 @@ void us_ticker_disable_interrupt(void)
125119

126120
void us_ticker_clear_interrupt(void)
127121
{
128-
HalTimerOp.HalTimerIrqClear((u32)TimerAdapter.TimerId);
129122
}

0 commit comments

Comments
 (0)