1. Concept of Software Timers
A software timer is a mechanism used to trigger events after a specific time, similar to an alarm clock in real life. Users can set the timer to specify when and how often an event occurs, and call a predefined callback function when the time is reached. Unlike hardware timers, software timers are provided by the operating system and built on top of hardware timers, offering greater flexibility.
2. Hardware Timer vs. Software Timer
- Hardware Timer:
- Function: Timer functionality built into the chip, typically clock input provided by an external crystal oscillator, triggers an interrupt after counting.
- Characteristics: High precision (can reach nanosecond levels), interrupt-triggered, bound to hardware resources.
- Use: Precise time control, such as PWM, counters, clocks, etc.
- Software Timer:
- Function: Extends more timing functionalities based on the interface provided by the operating system, built on hardware timers.
- Characteristics: Not limited by hardware resources, high flexibility, can create multiple timers.
- Use: Scenarios requiring flexible timing operations, such as scheduled tasks, event timing triggers, etc.
3. Working Mechanism of Software Timers
- Callback Function: The user specifies a callback function when creating a software timer; FreeRTOS will call this function to execute user-defined tasks when the timer expires.
- Timing Precision: The precision of a software timer depends on the system clock cycle, typically based on SysTick as the base clock. The callback function needs to execute quickly and should not include operations that block task execution, such as
<span>vTaskDelay()</span>
. - Timer Period: The interval between two triggers of the callback function is called the timer’s period,
<span>xTimerPeriodInTicks</span>
.
4. Features of FreeRTOS Software Timers
The software timers provided by FreeRTOS support the following features:
- Create Timer: Create a timer using the
<span>xTimerCreate()</span>
function. - Start Timer: Start the timer using the
<span>xTimerStart()</span>
function, allowing it to begin timing. - Stop Timer: Stop the timer using the
<span>xTimerStop()</span>
function, halting the timing. - Reset Timer: Reset the timer using the
<span>xTimerReset()</span>
function, restarting the timing. - Delete Timer: Delete the timer using the
<span>xTimerDelete()</span>
function, freeing resources.
5. Timer Modes
- One-shot Mode: The timer enters a sleep state after triggering the callback function once and does not automatically restart. Suitable for timing operations that only need to trigger once.
- Periodic Mode: The timer repeatedly triggers the callback function at set time intervals until the user manually stops or deletes the timer. Suitable for tasks that need to be executed periodically.
6. Implementation of FreeRTOS Software Timers
FreeRTOS manages software timers through a task called <span>prvTimerTask</span>
(also known as the Daemon task). This task is automatically created when the scheduler starts, responsible for checking timer overflows and calling the corresponding callback functions. To use software timer functionality, the macro <span>configUSE_TIMERS</span>
must be set to <span>1</span>
in <span>FreeRTOSConfig.h</span>
.
7. API Functions for Software Timers
1. Create Timer
TimerHandle_t xTimerCreate(const char *pcTimerName, TickType_t xTimerPeriodInTicks, UBaseType_t uxAutoReload, void *pvTimerID, TimerCallbackFunction_t pxCallbackFunction);
- pcTimerName: The name of the timer, mainly for debugging.
- xTimerPeriodInTicks: Timer period, measured in system clock ticks.
- uxAutoReload: Whether to auto-reload,
<span>pdTRUE</span>
for periodic mode,<span>pdFALSE</span>
for one-shot mode. - pvTimerID: User-defined timer identifier.
- pxCallbackFunction: The callback function called when the timer overflows.
2. Start Timer
BaseType_t xTimerStart(TimerHandle_t xTimer, TickType_t xTicksToWait);// Start software timer in interrupt xTimerStartFromISR( xTimer, pxHigherPriorityTaskWoken )
- xTimer: The handle of the timer to start.
- xTicksToWait: Maximum time to wait for the start (in ticks).
3. Stop Timer
BaseType_t xTimerStop(TimerHandle_t xTimer, TickType_t xTicksToWait);// Stop software timer in interrupt BaseType_t xTimerStopFromISR(TimerHandle_t xTimer,BaseType_t *pxHigherPriorityTaskWoken);
- xTimer: The handle of the timer to stop.
- xTicksToWait: Maximum time to wait for the stop.
4. Reset Timer
BaseType_t xTimerReset(TimerHandle_t xTimer, TickType_t xTicksToWait);
- xTimer: The handle of the timer to reset.
- xTicksToWait: Maximum time to wait for the reset.
5. Delete Timer
BaseType_t xTimerDelete(TimerHandle_t xTimer, TickType_t xTicksToWait);
- xTimer: The handle of the timer to delete.
- xTicksToWait: Maximum time to wait for the delete.
8. Example of Using Software Timers
// Timer handle TimerHandle_t xExampleTimer = NULL; // Timer callback function void vTimerCallback(TimerHandle_t xTimer){ // Actions after timer triggers printf("Timer callback executed!\n"); } void vTaskFunction(void *pvParameters){ for (;;) { // Main task content printf("Task running...\n"); vTaskDelay(pdMS_TO_TICKS(1000)); // Delay 1 second }} int main(void){ // Create a timer, with a period of 500ms, and not auto-reloading xExampleTimer = xTimerCreate("ExampleTimer", pdMS_TO_TICKS(500), pdTRUE, (void *)0, vTimerCallback); if (xExampleTimer != NULL) { // Start the timer if (xTimerStart(xExampleTimer, 0) != pdPASS) { // Failed to start timer printf("Failed to start timer.\n"); } } else { // Failed to create timer printf("Failed to create timer.\n"); } // Create a task xTaskCreate(vTaskFunction, "Task", configMINIMAL_STACK_SIZE, NULL, 1, NULL); // Start the scheduler vTaskStartScheduler(); // If the scheduler fails to start, the code will continue running here while(1) { } return 0;}
9. Conclusion
Software timers in FreeRTOS provide a flexible and powerful timing mechanism, capable of extending the functionalities of hardware timers and supporting more timing requirements. By using software timers effectively, complex timing control and task scheduling can be achieved in embedded systems.