cancel
Showing results for 
Search instead for 
Did you mean: 

TIMER Update CNT value while running

Daniel Studer
Associate II
Posted on November 07, 2017 at 15:58

Hello

I have a problem with the Timer2 on STM32F767.

I use the Timer2 as simple timer in Countermode Down. With this timer I want to realize a countdown to start another process once after the underflow interrupt has occured. So I initialized the Timer as countdown without autoreload as following:

  TimHandle_Trigger.Init.Period                       = countDown_us - 1;

  TimHandle_Trigger.Init.Prescaler                  = uwPrescalerValue;

  TimHandle_Trigger.Init.ClockDivision            = TIM_CLOCKDIVISION_DIV1;

  TimHandle_Trigger.Init.CounterMode            = TIM_COUNTERMODE_DOWN;

  TimHandle_Trigger.Init.RepetitionCounter    = 0;

  TimHandle_Trigger.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

Next I start the timer to count down for example from 10 seconds to zero. Now I have to adjust/synchronize the counter value CNT (with a value I receive by RealTime Ethernet Protocol), therefore I have to check the actual CNT value and then adjust this value with my received one, if there is an offset.

For this I want to have a simple function to rewrite to the CNT value to adjust the counter. But here is the problem. This is not working. I did this as following:

inline void adjust_timer2(uint32_t countDown_us)

{

    /* Update Disabled to reinitialize counter value */

    TimHandle_Trigger.Instance->CR1 |= TIM_CR1_UDIS;

    /* Set new countdown value in us */

    TimHandle_Trigger.Instance->CNT = countDown_us;

    /* set UG bit for Update generation to reinitialize counter value */

    TimHandle_Trigger.Instance->EGR |= TIM_EGR_UG;

    /* Update Enabled */

    TimHandle_Trigger.Instance->CR1 &= ~TIM_CR1_UDIS;

    /* clear all interrupts */

    TimHandle_Trigger.Instance->SR = 0;

}

Debugging this, I can see in the register of the timer2 it generates an update interrupt immediately and I get an update interrupt. After this I get a second update interrupt but not after the time I set the new CNT value. The second interrupt occours just after the first one.

What could be the problem here? Do I have to reload the CNT value on an other way, while the Timer2 is already running?

Thanks for any help.

Dani

#update #synchronize #timer
2 REPLIES 2
Posted on November 07, 2017 at 17:22

What do you mean by debugging? Did you break/stop/single-step the code? If so, did you set the respective bit in DBGMCU Freeze registers?

JW

Posted on November 08, 2017 at 09:21

During initializing timer2 I set the period to value 9999 (0x270F).

Then I start timer2 and before starting it, I set CNT value to 10000000 (this means 10 seconds until underflow). This works well and the timer2 is counting down.

During counting down I call 'adjust_timer2()' with the value 1000000 (set timer2 to underflow in 1 second).

I have a breakpoint on the first line in adjust_timer2() and then stepping through the code (single steps) and checking the timer2 registers.Yes I set the freeze register of this timer and this works I think.

The values I set is just for testing the function now and they have no sense yet.

In the timer2 register I can see the new CNT value after line:

TimHandle_Trigger.Instance->CNT = countDown_us; (countDown_us is set to 1000000)

Then I execute the next line

TimHandle_Trigger.Instance->EGR |= TIM_EGR_UG;

and after this line the CNT value is set to 0x270F, which is the initial value.

I tried now to comment this line and without setting the UG bit it works well. In the reference manual I can see, by setting the UG bit, the CNT and Prescale value are reinitialized.

For now my adjust_timer2() function only has the following line:

inline void adjust_timer2(uint32_t countDown_us)

{

    /* Set new countdown value in us */

    TimHandle_Trigger.Instance->CNT = countDown_us;

}

I don't now why, but yeterday this did not work at all.

Dani