First I setup the timer
static uint32_t added_val = 0;
void LPTMR_Setup(void)
{
lptmr_config_t lptmr_config;
lptmr_config.timerMode = kLPTMR_TimerModeTimeCounter;
lptmr_config.pinSelect = kLPTMR_PinSelectInput_0;
lptmr_config.pinPolarity = kLPTMR_PinPolarityActiveHigh;
lptmr_config.enableFreeRunning = true;
lptmr_config.bypassPrescaler = true;
lptmr_config.prescalerClockSource = kLPTMR_PrescalerClock_1; //1Khz - 1ms
lptmr_config.value = kLPTMR_Prescale_Glitch_0;
LPTMR_Init(LPTMR0, &lptmr_config);
LPTMR_SetTimerPeriod(LPTMR0, 0xFFFF);
LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
EnableIRQ(LPTMR0_IRQn);
LPTMR_StartTimer(LPTMR0);
}
uint32_t LPTMR_GetCount(void)
{
uint32_t count = LPTMR_GetCurrentTimerCount(LPTMR0);
uint32_t total_ms = count + added_val;
return total_ms;
}
void LPTMR0_IRQHandler(void)
{
LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
added_val += 0xFFFF;
/*
Workaround for TWR-KV58: because write buffer is enabled, adding
memory barrier instructions to make sure clearing interrupt flag completed
before go out ISR
*/
__DSB();
__ISB();
}
Then I check it
while(1)
{
time_stamp = LPTMR_GetCount();
if (time_stamp >= t1)
{
t1 = time_stamp + PERIOD_1;
GPIO_PortToggle(LED_PORT, (1<<LEDR_PIN));
}
if (time_stamp >= t2)
{
t2 = time_stamp + PERIOD_2;
GPIO_PortToggle(LED_PORT, (1<<LEDB_PIN));
}
if (time_stamp >= t3)
{
t3 = time_stamp + PERIOD_3;
GPIO_PortToggle(LED_PORT, (1<<LEDG_PIN));
}
if (time_stamp >= t4)
{
t4 = time_stamp + PERIOD_4;
GPIO_PortToggle(LED_PORT, (1<<LEDO_PIN));
}
}
And I see LEDs blinking at right intervals...till first interrupt occurs.
So the fix in the interrupt routine doesn't work?