Merge pull request #2204 from fvincenzo/master

Lp_ticker and Us_ticker time count fix
pull/2218/head
Martin Kojtal 2016-07-22 09:50:05 +01:00 committed by GitHub
commit 37e254fa16
6 changed files with 138 additions and 12 deletions

View File

@ -228,6 +228,7 @@ void DualTimer_SetInterrupt_1(uint32_t timer, uint32_t time_us,
timerenable_t mode)
{
uint32_t dualtimerControl = 0;
uint32_t load_time_us = 0;
/* Verify if the Timer is enabled */
if (DualTimer_isEnabled(timer) == 1) {
/* Disable Timer */
@ -237,9 +238,15 @@ void DualTimer_SetInterrupt_1(uint32_t timer, uint32_t time_us,
(DualTimers[timer].dualtimer1)->TimerControl =
CMSDK_DUALTIMER_CTRL_INTEN_Msk
| dualtimerControl;
/* Check time us condition */
if(time_us == DUALTIMER_DEFAULT_RELOAD)
load_time_us = DUALTIMER_MAX_VALUE;
else
load_time_us = time_us * DUALTIMER_TICKS_US;
/* Reload Value */
DualTimers[timer].dualtimer1Reload = (time_us)
* DUALTIMER_TICKS_US;
DualTimers[timer].dualtimer1Reload = load_time_us;
(DualTimers[timer].dualtimer1)->TimerLoad =
DualTimers[timer].dualtimer1Reload;
/* Enable Counter */
@ -260,6 +267,7 @@ void DualTimer_SetInterrupt_2(uint32_t timer, uint32_t time_us,
timerenable_t mode)
{
uint32_t dualtimerControl = 0;
uint32_t load_time_us = 0;
/* Verify if the Timer is enabled */
if (DualTimer_isEnabled(timer) == 1) {
/* Disable Timer */
@ -269,9 +277,15 @@ void DualTimer_SetInterrupt_2(uint32_t timer, uint32_t time_us,
(DualTimers[timer].dualtimer2)->TimerControl =
CMSDK_DUALTIMER_CTRL_INTEN_Msk
| dualtimerControl;
/* Check time us condition */
if(time_us == DUALTIMER_DEFAULT_RELOAD)
load_time_us = DUALTIMER_MAX_VALUE;
else
load_time_us = time_us * DUALTIMER_TICKS_US;
/* Reload Value */
DualTimers[timer].dualtimer2Reload = (time_us)
* DUALTIMER_TICKS_US;
DualTimers[timer].dualtimer2Reload = load_time_us;
(DualTimers[timer].dualtimer2)->TimerLoad =
DualTimers[timer].dualtimer2Reload;
/* Enable Counter */
@ -358,3 +372,22 @@ uint32_t DualTimer_GetTicksUS(uint32_t timer)
}
return 0;
}
/*
* DualTimer_GetReloadValue(): returns the load value of the selected
* singletimer.
* timer: timer associated with the Ticks per us
* singletimer: selected singletimer
* @return: reload value of the selected singletimer - 0 if timer is disabled
*/
uint32_t DualTimer_GetReloadValue(uint32_t timer, uint32_t singletimer)
{
/* Verify if the Timer is enabled */
if (DualTimer_isEnabled(timer) == 1) {
if (singletimer == SINGLETIMER1)
return DualTimers[timer].dualtimer1Reload / DUALTIMER_TICKS_US;
else
return DualTimers[timer].dualtimer2Reload / DUALTIMER_TICKS_US;
}
return 0;
}

View File

@ -50,6 +50,9 @@ typedef uint8_t timerenable_t;
#define DUALTIMER_ONESHOT_MASK (3)
#define DUALTIMER_ONESHOT (1 << DUALTIMER_ONESHOT_MASK)
/* Default reload */
#define DUALTIMER_DEFAULT_RELOAD 0xFFFFFFFF
/*
* DualTimer_Enable(): Enables a hardware timer
* timer: timer to be enabled
@ -136,6 +139,15 @@ uint32_t DualTimer_GetIRQInfo(uint32_t dualtimer);
*/
uint32_t DualTimer_GetTicksUS(uint32_t timer);
/*
* DualTimer_GetReloadValue(): returns the load value of the selected
* singletimer.
* timer: timer associated with the Ticks per us
* singletimer: selected singletimer
* @return: reload value of the selected singletimer
*/
uint32_t DualTimer_GetReloadValue(uint32_t timer, uint32_t singletimer);
#ifdef __cplusplus
}
#endif

View File

@ -165,14 +165,22 @@ uint32_t Timer_Read(uint32_t timer)
*/
void Timer_SetInterrupt(uint32_t timer, uint32_t time_us)
{
uint32_t load_time_us = 0;
/* Verify if the Timer is enabled */
if (Timer_isEnabled(timer) == 1) {
/* Disable Timer */
Timer_Disable(timer);
/* Enable Interrupt */
(Timers[timer].timerN)->CTRL = CMSDK_TIMER_CTRL_IRQEN_Msk;
/* Check time us condition */
if(time_us == TIMER_DEFAULT_RELOAD)
load_time_us = TIMER_MAX_VALUE;
else
load_time_us = time_us * TIMER_TICKS_US;
/* Initialize Timer Value */
Timers[timer].timerReload = (time_us) * TIMER_TICKS_US;
Timers[timer].timerReload = load_time_us;
(Timers[timer].timerN)->RELOAD = Timers[timer].timerReload;
(Timers[timer].timerN)->VALUE = Timers[timer].timerReload;
/* Enable Counter */
@ -234,3 +242,21 @@ uint32_t Timer_GetTicksUS(uint32_t timer)
}
return 0;
}
/*
* Timer_GetReloadValue(): returns the load value of the selected
* timer.
* timer: timer associated with the Ticks per us
* @return: reload value of the selected singletimer
*/
uint32_t Timer_GetReloadValue(uint32_t timer)
{
/* Verify if the Timer is enabled */
if (Timer_isEnabled(timer) == 1) {
if (timer == TIMER1)
return Timers[timer].timerReload / TIMER_TICKS_US;
else
return Timers[timer].timerReload / TIMER_TICKS_US;
}
return 0;
}

View File

@ -26,6 +26,9 @@ extern "C" {
#define TIMER0 0
#define TIMER1 1
/* Default reload */
#define TIMER_DEFAULT_RELOAD 0xFFFFFFFF
/*
* Timer_Initialize(): Initializes an hardware timer
* timer: timer to be Initialized
@ -92,6 +95,14 @@ uint32_t Timer_GetIRQn(uint32_t timer);
*/
uint32_t Timer_GetTicksUS(uint32_t timer);
/*
* Timer_GetReloadValue(): returns the load value of the selected
* timer.
* timer: timer associated with the Ticks per us
* @return: reload value of the selected singletimer
*/
uint32_t Timer_GetReloadValue(uint32_t timer);
#ifdef __cplusplus
}
#endif

View File

@ -25,8 +25,10 @@
static uint32_t lp_ticker_initialized = 0;
/* lp_ticker reload value */
static uint32_t lp_ticker_reload = 0x0; /* Max Value */
/* Store Overflow Count */
static uint32_t lp_ticker_overflows_count = 0;
/* Store Overflow Delta */
static uint32_t lp_ticker_overflows_delta = 0;
/* lp_ticker Overflow limit */
static uint32_t lp_ticker_overflow_limit = 0;
#if DEVICE_LOWPOWERTIMER
/**
@ -36,7 +38,13 @@ void __lp_ticker_irq_handler(void)
{
if (DualTimer_GetIRQInfo(DUALTIMER0) == SINGLETIMER2) {
DualTimer_ClearInterrupt(DUALTIMER0);
lp_ticker_overflows_count++;
/*
* For each overflow event adds the timer max represented value to
* the delta. This allows the lp_ticker to keep track of the elapsed
* time:
* elapsed_time = (num_overflow * overflow_limit) + current_time
*/
lp_ticker_overflows_delta += lp_ticker_overflow_limit;
} else {
lp_ticker_irq_handler();
}
@ -67,6 +75,20 @@ void lp_ticker_init(void)
NVIC_SetVector((IRQn_Type)lp_ticker_irqn,
(uint32_t)__lp_ticker_irq_handler);
NVIC_EnableIRQ((IRQn_Type)lp_ticker_irqn);
/* DualTimer set interrupt on SINGLETIMER2 */
DualTimer_SetInterrupt_2(DUALTIMER0, DUALTIMER_DEFAULT_RELOAD,
DUALTIMER_COUNT_32 | DUALTIMER_PERIODIC);
/*
* Set lp_ticker Overflow limit. The lp_ticker overflow limit is required
* to calculated the return value of the lp_ticker read function in us
* on 32bit.
* A 32bit us value cannot be represented directly in the Dual Timer Load
* register if it is greater than (0xFFFFFFFF ticks)/DUALTIMER_DIVIDER_US.
*/
lp_ticker_overflow_limit = DualTimer_GetReloadValue(DUALTIMER0,
SINGLETIMER2);
}
/**
@ -82,7 +104,7 @@ uint32_t lp_ticker_read(void)
lp_ticker_init();
/* Read Timer Value */
microseconds = DualTimer_Read_2(DUALTIMER0);
microseconds = lp_ticker_overflows_delta + DualTimer_Read_2(DUALTIMER0);
return microseconds;
}

View File

@ -23,11 +23,19 @@ static uint32_t us_ticker_reload = 0x0; /* Max Value */
/* us ticker initialized */
static uint32_t us_ticker_inited = 0;
/* us ticker overflow */
static uint32_t us_ticker_overflow = 0;
static uint32_t us_ticker_overflow_delta = 0;
/* us ticker overflow limit */
static uint32_t us_ticker_overflow_limit = 0;
void __us_ticker_irq_handler(void) {
Timer_ClearInterrupt(TIMER1);
us_ticker_overflow++;
/*
* For each overflow event adds the timer max represented value to
* the delta. This allows the us_ticker to keep track of the elapsed
* time:
* elapsed_time = (num_overflow * overflow_limit) + current_time
*/
us_ticker_overflow_delta += us_ticker_overflow_limit;
}
void us_ticker_init(void) {
@ -57,6 +65,18 @@ void us_ticker_init(void) {
us_ticker_irqn1 = Timer_GetIRQn(TIMER1);
NVIC_SetVector((IRQn_Type)us_ticker_irqn1, (uint32_t)__us_ticker_irq_handler);
NVIC_EnableIRQ((IRQn_Type)us_ticker_irqn1);
/* Timer set interrupt on TIMER1 */
Timer_SetInterrupt(TIMER1, TIMER_DEFAULT_RELOAD);
/*
* Set us_ticker Overflow limit. The us_ticker overflow limit is required
* to calculated the return value of the us_ticker read function in us
* on 32bit.
* A 32bit us value cannot be represented directly in the Timer Load
* register if it is greater than (0xFFFFFFFF ticks)/TIMER_DIVIDER_US.
*/
us_ticker_overflow_limit = Timer_GetReloadValue(TIMER1);
}
uint32_t us_ticker_read() {
@ -64,7 +84,9 @@ uint32_t us_ticker_read() {
if (!us_ticker_inited)
us_ticker_init();
return_value = Timer_Read(TIMER1);
return_value = us_ticker_overflow_delta + Timer_Read(TIMER1);
return return_value;
}