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
Timers: how to set the next delay in the callback
-
- Posts: 35
- Joined: Fri Feb 09, 2018 12:44 am
- Has thanked: 2 times
- Been thanked: 5 times
- wurstnase
- Posts: 121
- Joined: Tue Oct 17, 2017 2:24 pm
- Has thanked: 43 times
- Been thanked: 30 times
- Contact:
Re: Timers: how to set the next delay in the callback
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
Bit7 ARPE:Auto-reloadpreloadenable
0: TIMx_ARR register is not buffered
1: TIMx_ARR register is buffered
\o/ Nico
- alex31
- Posts: 374
- Joined: Fri May 25, 2012 10:23 am
- Location: toulouse, france
- Has thanked: 38 times
- Been thanked: 61 times
- Contact:
Re: Timers: how to set the next delay in the callback
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
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
-
- Posts: 35
- Joined: Fri Feb 09, 2018 12:44 am
- Has thanked: 2 times
- Been thanked: 5 times
Re: Timers: how to set the next delay in the callback
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?
- Giovanni
- Site Admin
- Posts: 14444
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1074 times
- Been thanked: 921 times
- Contact:
Re: Timers: how to set the next delay in the callback
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
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
Who is online
Users browsing this forum: No registered users and 13 guests