Skip to content

Commit 4f5b191

Browse files
committed
[BEETLE] Fix US_Ticker read
MBED OS requires an us_ticker_read function that returns a 32bit value in microseconds. This can not be represented directly on the Beetle Timer Load register. max_us_on_reg = (0xFFFFFFFF ticks)/DIVIDER_US This patch introduces an intermediate layer that counts the timer wraps around and returns the correct value of us to the MBED library. Signed-off-by: Vincenzo Frascino <[email protected]>
1 parent 1b364a1 commit 4f5b191

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

hal/targets/cmsis/TARGET_ARM_SSG/TARGET_BEETLE/apb_timer.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,22 @@ uint32_t Timer_Read(uint32_t timer)
165165
*/
166166
void Timer_SetInterrupt(uint32_t timer, uint32_t time_us)
167167
{
168+
uint32_t load_time_us = 0;
168169
/* Verify if the Timer is enabled */
169170
if (Timer_isEnabled(timer) == 1) {
170171
/* Disable Timer */
171172
Timer_Disable(timer);
172173
/* Enable Interrupt */
173174
(Timers[timer].timerN)->CTRL = CMSDK_TIMER_CTRL_IRQEN_Msk;
175+
176+
/* Check time us condition */
177+
if(time_us == TIMER_DEFAULT_RELOAD)
178+
load_time_us = TIMER_MAX_VALUE;
179+
else
180+
load_time_us = time_us * TIMER_TICKS_US;
181+
174182
/* Initialize Timer Value */
175-
Timers[timer].timerReload = (time_us) * TIMER_TICKS_US;
183+
Timers[timer].timerReload = load_time_us;
176184
(Timers[timer].timerN)->RELOAD = Timers[timer].timerReload;
177185
(Timers[timer].timerN)->VALUE = Timers[timer].timerReload;
178186
/* Enable Counter */
@@ -234,3 +242,21 @@ uint32_t Timer_GetTicksUS(uint32_t timer)
234242
}
235243
return 0;
236244
}
245+
246+
/*
247+
* Timer_GetReloadValue(): returns the load value of the selected
248+
* timer.
249+
* timer: timer associated with the Ticks per us
250+
* @return: reload value of the selected singletimer
251+
*/
252+
uint32_t Timer_GetReloadValue(uint32_t timer)
253+
{
254+
/* Verify if the Timer is enabled */
255+
if (Timer_isEnabled(timer) == 1) {
256+
if (timer == TIMER1)
257+
return Timers[timer].timerReload / TIMER_TICKS_US;
258+
else
259+
return Timers[timer].timerReload / TIMER_TICKS_US;
260+
}
261+
return 0;
262+
}

hal/targets/cmsis/TARGET_ARM_SSG/TARGET_BEETLE/apb_timer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ extern "C" {
2626
#define TIMER0 0
2727
#define TIMER1 1
2828

29+
/* Default reload */
30+
#define TIMER_DEFAULT_RELOAD 0xFFFFFFFF
31+
2932
/*
3033
* Timer_Initialize(): Initializes an hardware timer
3134
* timer: timer to be Initialized
@@ -92,6 +95,14 @@ uint32_t Timer_GetIRQn(uint32_t timer);
9295
*/
9396
uint32_t Timer_GetTicksUS(uint32_t timer);
9497

98+
/*
99+
* Timer_GetReloadValue(): returns the load value of the selected
100+
* timer.
101+
* timer: timer associated with the Ticks per us
102+
* @return: reload value of the selected singletimer
103+
*/
104+
uint32_t Timer_GetReloadValue(uint32_t timer);
105+
95106
#ifdef __cplusplus
96107
}
97108
#endif

hal/targets/hal/TARGET_ARM_SSG/TARGET_BEETLE/us_ticker.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ static uint32_t us_ticker_reload = 0x0; /* Max Value */
2323
/* us ticker initialized */
2424
static uint32_t us_ticker_inited = 0;
2525
/* us ticker overflow */
26-
static uint32_t us_ticker_overflow = 0;
26+
static uint32_t us_ticker_overflow_delta = 0;
27+
/* us ticker overflow limit */
28+
static uint32_t us_ticker_overflow_limit = 0;
2729

2830
void __us_ticker_irq_handler(void) {
2931
Timer_ClearInterrupt(TIMER1);
30-
us_ticker_overflow++;
32+
/*
33+
* For each overflow event adds the timer max represented value to
34+
* the delta. This allows the us_ticker to keep track of the elapsed
35+
* time:
36+
* elapsed_time = (num_overflow * overflow_limit) + current_time
37+
*/
38+
us_ticker_overflow_delta += us_ticker_overflow_limit;
3139
}
3240

3341
void us_ticker_init(void) {
@@ -57,14 +65,28 @@ void us_ticker_init(void) {
5765
us_ticker_irqn1 = Timer_GetIRQn(TIMER1);
5866
NVIC_SetVector((IRQn_Type)us_ticker_irqn1, (uint32_t)__us_ticker_irq_handler);
5967
NVIC_EnableIRQ((IRQn_Type)us_ticker_irqn1);
68+
69+
/* Timer set interrupt on TIMER1 */
70+
Timer_SetInterrupt(TIMER1, TIMER_DEFAULT_RELOAD);
71+
72+
/*
73+
* Set us_ticker Overflow limit. The us_ticker overflow limit is required
74+
* to calculated the return value of the us_ticker read function in us
75+
* on 32bit.
76+
* A 32bit us value cannot be represented directly in the Timer Load
77+
* register if it is greater than (0xFFFFFFFF ticks)/TIMER_DIVIDER_US.
78+
*/
79+
us_ticker_overflow_limit = Timer_GetReloadValue(TIMER1);
6080
}
6181

6282
uint32_t us_ticker_read() {
6383
uint32_t return_value = 0;
6484

6585
if (!us_ticker_inited)
6686
us_ticker_init();
67-
return_value = Timer_Read(TIMER1);
87+
88+
return_value = us_ticker_overflow_delta + Timer_Read(TIMER1);
89+
6890
return return_value;
6991
}
7092

0 commit comments

Comments
 (0)