| Software Timer API Function | Description |
|---|---|
| xTimerCreate() | Creates a software timer using dynamic memory |
| xTimerCreateStatic() | Creates a software timer using user-provided memory |
| xTimerStart() | Starts or restarts a timer from a task |
| xTimerStartFromISR() | Starts or restarts a timer from an ISR |
| xTimerStop() | Stops a running timer from a task |
| xTimerStopFromISR() | Stops a running timer from an ISR |
| xTimerReset() | Resets a timer and restarts its period from zero |
| xTimerResetFromISR() | Resets a timer from an ISR |
One-shot timer: once started, it will execute its callback function once only. It can be restarted manually, but will not restart itself

Auto-reload timer: once started, it will re-start itself each time it expires, resulting in periodic execution of its callback function

#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES-1)
#define configTIMER_QUEUE_LENGTH 5
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE*2)Creates a FreeRTOS software timer using dynamically allocated memory from the FreeRTOS heap. It defines the timer name, period, one-shot or auto-reload mode, timer ID, and callback function. The newly created timer remains dormant until it is started.
TimerHandle_t xTimerCreate(const char * const pcTimerName,
TickType_t xTimerPeriodInTicks,
UBaseType_t uxAutoReload,
void * pvTimerID,
TimerCallbackFunction_t pxCallbackFunction)
// ---------------------- Example ----------------------
TimerHandle_t OneShotTimer_Handle;
TimerHandle_t AutoReloadTimer_Handle;
void OneShotTimerCallback(TimerHandle_t xTimer);
void AutoReloadTimerCallback(TimerHandle_t xTimer);
OneShotTimer_Handle = xTimerCreate("OneShotTimer",
mainONE_SHOT_TIMER_PERIOD,
pdFALSE,
0,
OneShotTimerCallback);
AutoReloadTimer_Handle = xTimerCreate("AutoReloadTimer",
mainAUTO_RELOAD_TIMER_PERIOD,
pdTRUE,
0,
AutoReloadTimerCallback);Creates a software timer using a StaticTimer_t memory buffer supplied by the application. Unlike xTimerCreate(), it does not allocate memory from the FreeRTOS heap, making memory usage predictable and suitable for systems that avoid dynamic allocation.
TimerHandle_t xTimerCreateStatic(const char * const pcTimerName,
TickType_t xTimerPeriodInTicks,
UBaseType_t uxAutoReload,
void * pvTimerID,
TimerCallbackFunction_t pxCallbackFunction,
StaticTimer_t * pxTimerBuffer)Starts a previously created software timer from normal task context. After the specified timer period expires, the timer’s callback function executes. For an auto-reload timer, this repeats periodically; for a one-shot timer, it occurs only once.
BaseType_t xTimerStart(TimerHandle_t xTimer,
TickType_t xTicksToWait)
// ---------------------- Example ----------------------
TimerHandle_t OneShotTimer_Handle;
TimerHandle_t AutoReloadTimer_Handle;
xTimerStart(OneShotTimer_Handle, 0);
xTimerStart(AutoReloadTimer_Handle, 0);Starts or restarts a software timer from an interrupt service routine. It sends a start command to the timer command queue and uses pxHigherPriorityTaskWoken to indicate whether a context switch should occur when the ISR exits.
BaseType_t xTimerStartFromISR(TimerHandle_t xTimer,
BaseType_t * pxHigherPriorityTaskWoken)Stops an active software timer from normal task context. Once stopped, the timer will not expire or execute its callback function, but the timer object remains valid and can be started again later.
BaseType_t xTimerStop(TimerHandle_t xTimer,
TickType_t xTicksToWait)
// ---------------------- Example ----------------------
TimerHandle_t AutoReloadTimer_Handle;
xTimerStop(AutoReloadTimer_Handle, 0);Stops an active software timer from an interrupt service routine. It is the interrupt-safe version of xTimerStop() and can request a context switch through the pxHigherPriorityTaskWoken parameter.
BaseType_t xTimerStopFromISR(TimerHandle_t xTimer,
BaseType_t * pxHigherPriorityTaskWoken)Resets a timer’s expiration time from normal task context. If the timer is active, it restarts the full timer period from the moment the reset is requested; if it is dormant, the reset operation also starts it.
BaseType_t xTimerReset(TimerHandle_t xTimer,
TickType_t xTicksToWait)Resets or starts a software timer from an interrupt service routine. It recalculates the expiration time from the moment it is called and uses pxHigherPriorityTaskWoken to indicate whether an ISR-level context switch is required.
BaseType_t xTimerResetFromISR(TimerHandle_t xTimer,
BaseType_t * pxHigherPriorityTaskWoken)Back to top of the page