4.3.1 Application Timer

Use the application timers to schedule periodic tasks or execute specific actions after a certain delay. A timer created with the HAL API either hits once or fires repeatedly at a given interval.

To start a timer, call the HAL_StartAppTimer(HAL_AppTimer_t* params) function, where params represents the function’s parameters. Specify the timer interval, Timer mode (TIMER_REPEAT_MODE or TIMER_ONE_SHOT_MODE) and the callback function in the parameters. The timer system invokes the callback function each time the timer expires, according to the specified interval and mode.

To stop the timer, call the HAL_StopAppTimer(HAL_AppTimer_t* params) function, providing the pointer to the same instance of the HAL_AppTimer_t type that was used to configure the timer. This action stops a periodic timer and prevents a one-shot timer from executing if it did not yet trigger.

The following example provides details on how to use this function:
//Callback definition
void timerFired()
{
  //Do whatever you need
}
//Define the parameters instance in the outer scope
HAL_AppTimer_t timer;
...
timer.interval = 5000; //Timer interval in milliseconds
timer.mode     = TIMER_REPEAT_MODE;
timer.callback = timerFired;
HAL_StartAppTimer(&timer);

When a timer fires, the MCU passes control to the callback function registered for the timer, then continues execution from the place it was interrupted by the timer’s firing.