Repeat timers are similar to oneshot timers, but instead of getting called only once, they get called repeatedly. After a repeat timer has been started, its callback will be executed repeatedly every xTimerPeriod ticks after being started. Since repeat timers are executed within the TmrSvc task, they can provide a lightweight alternative to tasks for short, non-blocking functions that need to be run periodically. The same considerations regarding stack usage and execution time apply to oneshot timers.
The steps are essentially the same for repeat timers: just set the value of the auto-reload flag to pdTRUE.
Let's take a look at the code in mainSoftwareTimers.c:
TimerHandle_t repeatHandle =
xTimerCreate( "myRepeatTimer", //name for timer
500 /portTICK_PERIOD_MS, //period of timer in ticks
pdTRUE, //auto-reload flag
NULL, //unique ID for timer
repeatCallBack); //callback function
assert_param(repeatHandle != NULL);
xTimerStart(repeatHandle , 0);
The repeating timer will toggle the green LED:
void repeatCallBack( TimerHandle_t xTimer )
{
static uint32_t counter = 0;
if(counter++ % 2)
{
GreenLed.On();
}
else
{
GreenLed.Off();
}
}
In the preceding code, a static variable is used for the counter variable so that its value persists across function calls, while still hiding the variable from all the code outside of the repeatCallBack() function.