Timer3 disable/enable operation
We are using Timer3 to increment a uint64_t counter for serial port operation and need to be able to grab the counter value in the main loop.
//----------------------------------------------------------------
// -------------- Timer 3 ----------------------------------------
//----------------------------------------------------------------
// Setup for timer 3, set defaults
uint32_t tim_prescaler = 0;
uint32_t tim_period = 0;
uint32_t TimOutClock = 1;
// Timer 3 configuration. We want a timer 3 frequency of 2Khz(a interrupt
// every .5msec
tim_prescaler = 0;
TimOutClock = SystemCoreClock/1;
tim_period = __LL_TIM_CALC_ARR(TimOutClock, tim_prescaler, 2000);
TIM_TimeBaseStructure.Prescaler = tim_prescaler;
TIM_TimeBaseStructure.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_TimeBaseStructure.Autoreload = tim_period;
TIM_TimeBaseStructure.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
TIM_TimeBaseStructure.RepetitionCounter = 0;
LL_TIM_Init(TIM3, &TIM_TimeBaseStructure);
Because the counter can not be retrieved in a atomic operation we initially disabled the interrupt and turned it back on.
LL_TIM_DisableIT_UPDATE(TIM3);
rc = timer3Tick;
LL_TIM_EnableIT_UPDATE(TIM3);
The issue with this is that if the timer rolled over with the interrupt off, we would lose that tick count as the timer never stopped. We want to use the timer disable/enable to stop the timer, grab the counter and restart the timer exactly where we stopped it. Is there a document that can verify that when we restart the timer it does not reset the counter register Timer3->CNT?
uint64_t timer3_get_count(void)
{
uint64_t rc;
// Turn off the timer 3 counter
LL_TIM_DisableCounter(TIM3);
rc = timer3Tick;
// Turn on the timer 3 counter
LL_TIM_EnableCounter(TIM3);
return(rc);
}
