Timers: how to set the next delay in the callback

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
tsichevski
Posts: 34
Joined: Fri Feb 09, 2018 12:44 am
Has thanked: 2 times
Been thanked: 4 times

Timers: how to set the next delay in the callback

Postby tsichevski » Tue Jun 19, 2018 10:59 pm

Hi,

I need a repetitive timer, with variable interval. I want the next interval be set in the timer callback. I.e. when the timer fires, I decide when to fire it next.

I implemented it using ChibiOS GPT. I start the timer with gptStartContinuous(), and set the next interval with gptChangeIntervalI() from the timer callback procedure.

The problem is that at the moment I call gptChangeIntervalI(), the next interval is already counting, and its length is not affected by the call. So the interval change is applied at the end of this next interval only.

Obviously, I can do that by creating a task and use the chThdSleepMilliseconds() or like.
But does anybody know how to implement this without allocating a task for it?

Regards,
Vladimir

User avatar
wurstnase
Posts: 69
Joined: Tue Oct 17, 2017 2:24 pm
Has thanked: 27 times
Been thanked: 9 times
Contact:

Re: Timers: how to set the next delay in the callback

Postby wurstnase » Wed Jun 20, 2018 4:34 am

You should check the reference manual for your chip. E.g. the STM32F446 and also others have a preload bit which enables the shadow register. (ARPE)

Bit7 ARPE:Auto-reloadpreloadenable
0: TIMx_ARR register is not buffered
1: TIMx_ARR register is buffered
\o/ Nico

User avatar
alex31
Posts: 237
Joined: Fri May 25, 2012 10:23 am
Location: toulouse, france
Has thanked: 10 times
Been thanked: 20 times
Contact:

Re: Timers: how to set the next delay in the callback

Postby alex31 » Wed Jun 20, 2018 7:21 am

Hello,

If you want to stick to ChibiOS API instead of directly tweaking MCU registers, you can use one shot timer instead of continuous :

you initially start timer by gptStartOneShot, and in the callback you call gptStartOneShotI with the new interval.

If you timing accuracy is lower than your systick period*, you can also use the virtual timer API , see chVTxxx functions.

* virtual timer will count at CH_CFG_ST_FREQUENCY (chconf.h) frequency.

Alexandre

tsichevski
Posts: 34
Joined: Fri Feb 09, 2018 12:44 am
Has thanked: 2 times
Been thanked: 4 times

Re: Timers: how to set the next delay in the callback

Postby tsichevski » Wed Jun 20, 2018 8:38 am

wurstnase wrote:You should check the reference manual for your chip. E.g. the STM32F446 and also others have a preload bit which enables the shadow register. (ARPE)
...


Thanks, it did the trick! The only downside is I have to fix it in the ChibiOS code, since the value 1 of the bit is hardwired in the gpt_lld_start_timer() function :(

PS Giovanni, may it is worth to make this flag configurable?

User avatar
Giovanni
Site Admin
Posts: 11382
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 430 times
Been thanked: 362 times
Contact:

Re: Timers: how to set the next delay in the callback

Postby Giovanni » Wed Jun 20, 2018 10:18 am

Hi,

I prefer no, because without buffering there is a dangerous race condition. The risk is to set the ARR register to a value lower than the current counter value, this would result in the driver having to wait for the counter to overflow and go back before match can occur.

Giovanni


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 2 guests