Skip to content

Commit 1b364a1

Browse files
committed
[BEETLE] Fix Lp_Ticker read
MBED OS requires an lp_ticker_read function that returns a 32bit value in microseconds. This can not be represented directly on the Beetle Dual 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 48c1d2e commit 1b364a1

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ void DualTimer_SetInterrupt_1(uint32_t timer, uint32_t time_us,
228228
timerenable_t mode)
229229
{
230230
uint32_t dualtimerControl = 0;
231+
uint32_t load_time_us = 0;
231232
/* Verify if the Timer is enabled */
232233
if (DualTimer_isEnabled(timer) == 1) {
233234
/* Disable Timer */
@@ -237,9 +238,15 @@ void DualTimer_SetInterrupt_1(uint32_t timer, uint32_t time_us,
237238
(DualTimers[timer].dualtimer1)->TimerControl =
238239
CMSDK_DUALTIMER_CTRL_INTEN_Msk
239240
| dualtimerControl;
241+
242+
/* Check time us condition */
243+
if(time_us == DUALTIMER_DEFAULT_RELOAD)
244+
load_time_us = DUALTIMER_MAX_VALUE;
245+
else
246+
load_time_us = time_us * DUALTIMER_TICKS_US;
247+
240248
/* Reload Value */
241-
DualTimers[timer].dualtimer1Reload = (time_us)
242-
* DUALTIMER_TICKS_US;
249+
DualTimers[timer].dualtimer1Reload = load_time_us;
243250
(DualTimers[timer].dualtimer1)->TimerLoad =
244251
DualTimers[timer].dualtimer1Reload;
245252
/* Enable Counter */
@@ -260,6 +267,7 @@ void DualTimer_SetInterrupt_2(uint32_t timer, uint32_t time_us,
260267
timerenable_t mode)
261268
{
262269
uint32_t dualtimerControl = 0;
270+
uint32_t load_time_us = 0;
263271
/* Verify if the Timer is enabled */
264272
if (DualTimer_isEnabled(timer) == 1) {
265273
/* Disable Timer */
@@ -269,9 +277,15 @@ void DualTimer_SetInterrupt_2(uint32_t timer, uint32_t time_us,
269277
(DualTimers[timer].dualtimer2)->TimerControl =
270278
CMSDK_DUALTIMER_CTRL_INTEN_Msk
271279
| dualtimerControl;
280+
281+
/* Check time us condition */
282+
if(time_us == DUALTIMER_DEFAULT_RELOAD)
283+
load_time_us = DUALTIMER_MAX_VALUE;
284+
else
285+
load_time_us = time_us * DUALTIMER_TICKS_US;
286+
272287
/* Reload Value */
273-
DualTimers[timer].dualtimer2Reload = (time_us)
274-
* DUALTIMER_TICKS_US;
288+
DualTimers[timer].dualtimer2Reload = load_time_us;
275289
(DualTimers[timer].dualtimer2)->TimerLoad =
276290
DualTimers[timer].dualtimer2Reload;
277291
/* Enable Counter */
@@ -358,3 +372,22 @@ uint32_t DualTimer_GetTicksUS(uint32_t timer)
358372
}
359373
return 0;
360374
}
375+
376+
/*
377+
* DualTimer_GetReloadValue(): returns the load value of the selected
378+
* singletimer.
379+
* timer: timer associated with the Ticks per us
380+
* singletimer: selected singletimer
381+
* @return: reload value of the selected singletimer - 0 if timer is disabled
382+
*/
383+
uint32_t DualTimer_GetReloadValue(uint32_t timer, uint32_t singletimer)
384+
{
385+
/* Verify if the Timer is enabled */
386+
if (DualTimer_isEnabled(timer) == 1) {
387+
if (singletimer == SINGLETIMER1)
388+
return DualTimers[timer].dualtimer1Reload / DUALTIMER_TICKS_US;
389+
else
390+
return DualTimers[timer].dualtimer2Reload / DUALTIMER_TICKS_US;
391+
}
392+
return 0;
393+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ typedef uint8_t timerenable_t;
5050
#define DUALTIMER_ONESHOT_MASK (3)
5151
#define DUALTIMER_ONESHOT (1 << DUALTIMER_ONESHOT_MASK)
5252

53+
/* Default reload */
54+
#define DUALTIMER_DEFAULT_RELOAD 0xFFFFFFFF
55+
5356
/*
5457
* DualTimer_Enable(): Enables a hardware timer
5558
* timer: timer to be enabled
@@ -136,6 +139,15 @@ uint32_t DualTimer_GetIRQInfo(uint32_t dualtimer);
136139
*/
137140
uint32_t DualTimer_GetTicksUS(uint32_t timer);
138141

142+
/*
143+
* DualTimer_GetReloadValue(): returns the load value of the selected
144+
* singletimer.
145+
* timer: timer associated with the Ticks per us
146+
* singletimer: selected singletimer
147+
* @return: reload value of the selected singletimer
148+
*/
149+
uint32_t DualTimer_GetReloadValue(uint32_t timer, uint32_t singletimer);
150+
139151
#ifdef __cplusplus
140152
}
141153
#endif

hal/targets/hal/TARGET_ARM_SSG/TARGET_BEETLE/lp_ticker.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
static uint32_t lp_ticker_initialized = 0;
2626
/* lp_ticker reload value */
2727
static uint32_t lp_ticker_reload = 0x0; /* Max Value */
28-
/* Store Overflow Count */
29-
static uint32_t lp_ticker_overflows_count = 0;
28+
/* Store Overflow Delta */
29+
static uint32_t lp_ticker_overflows_delta = 0;
30+
/* lp_ticker Overflow limit */
31+
static uint32_t lp_ticker_overflow_limit = 0;
3032

3133
#if DEVICE_LOWPOWERTIMER
3234
/**
@@ -36,7 +38,13 @@ void __lp_ticker_irq_handler(void)
3638
{
3739
if (DualTimer_GetIRQInfo(DUALTIMER0) == SINGLETIMER2) {
3840
DualTimer_ClearInterrupt(DUALTIMER0);
39-
lp_ticker_overflows_count++;
41+
/*
42+
* For each overflow event adds the timer max represented value to
43+
* the delta. This allows the lp_ticker to keep track of the elapsed
44+
* time:
45+
* elapsed_time = (num_overflow * overflow_limit) + current_time
46+
*/
47+
lp_ticker_overflows_delta += lp_ticker_overflow_limit;
4048
} else {
4149
lp_ticker_irq_handler();
4250
}
@@ -67,6 +75,20 @@ void lp_ticker_init(void)
6775
NVIC_SetVector((IRQn_Type)lp_ticker_irqn,
6876
(uint32_t)__lp_ticker_irq_handler);
6977
NVIC_EnableIRQ((IRQn_Type)lp_ticker_irqn);
78+
79+
/* DualTimer set interrupt on SINGLETIMER2 */
80+
DualTimer_SetInterrupt_2(DUALTIMER0, DUALTIMER_DEFAULT_RELOAD,
81+
DUALTIMER_COUNT_32 | DUALTIMER_PERIODIC);
82+
83+
/*
84+
* Set lp_ticker Overflow limit. The lp_ticker overflow limit is required
85+
* to calculated the return value of the lp_ticker read function in us
86+
* on 32bit.
87+
* A 32bit us value cannot be represented directly in the Dual Timer Load
88+
* register if it is greater than (0xFFFFFFFF ticks)/DUALTIMER_DIVIDER_US.
89+
*/
90+
lp_ticker_overflow_limit = DualTimer_GetReloadValue(DUALTIMER0,
91+
SINGLETIMER2);
7092
}
7193

7294
/**
@@ -82,7 +104,7 @@ uint32_t lp_ticker_read(void)
82104
lp_ticker_init();
83105

84106
/* Read Timer Value */
85-
microseconds = DualTimer_Read_2(DUALTIMER0);
107+
microseconds = lp_ticker_overflows_delta + DualTimer_Read_2(DUALTIMER0);
86108

87109
return microseconds;
88110
}

0 commit comments

Comments
 (0)