Skip to content

Commit 37e254f

Browse files
authored
Merge pull request #2204 from fvincenzo/master
Lp_ticker and Us_ticker time count fix
2 parents 781fda0 + 4f5b191 commit 37e254f

File tree

6 files changed

+138
-12
lines changed

6 files changed

+138
-12
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/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/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
}

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)