๐ Introduction #
Timers are critical in real-time operating systems (RTOS) like VxWorks.
They are used for:
- Periodic tasks (e.g., sensor polling every 100 ms).
- Timeouts (e.g., network retries).
- Watchdog mechanisms to detect software hangs.
VxWorks provides Watchdog Timers, which can be used for both one-shot and periodic timer functionality.
๐งฉ Types of Timers in VxWorks #
-
One-Shot Timer
- Executes once after a specified delay.
-
Periodic Timer
- Re-arms itself after expiration for repeated execution.
๐ป Example: Using a Watchdog Timer #
Weโll create:
- A One-Shot Timer โ executes once after 5 seconds.
- A Periodic Timer โ executes every 2 seconds.
Code Example #
#include <vxWorks.h>
#include <wdLib.h>
#include <taskLib.h>
#include <stdio.h>
WDOG_ID oneShotWd;
WDOG_ID periodicWd;
// One-shot callback
void oneShotHandler(int arg)
{
printf("One-shot Timer expired! arg=%d\n", arg);
}
// Periodic callback
void periodicHandler(int arg)
{
printf("Periodic Timer triggered! arg=%d\n", arg);
wdStart(periodicWd, sysClkRateGet() * 2, (FUNCPTR)periodicHandler, arg);
}
// Init function
void usrAppInit(void)
{
oneShotWd = wdCreate();
periodicWd = wdCreate();
// One-shot: 5 seconds
wdStart(oneShotWd, sysClkRateGet() * 5, (FUNCPTR)oneShotHandler, 42);
// Periodic: first trigger in 2 seconds
wdStart(periodicWd, sysClkRateGet() * 2, (FUNCPTR)periodicHandler, 99);
}
๐ Explanation of the Code #
-
wdCreate()- Creates a watchdog timer instance.
-
wdStart()- Starts the timer with a given delay (in system ticks).
sysClkRateGet()โ gets system clock ticks per second.
-
One-Shot Timer (
oneShotWd)- Fires once after 5 seconds.
-
Periodic Timer (
periodicWd)- Re-arms itself inside the callback, firing every 2 seconds.
โก What Youโll See #
When running, youโll see something like:
Periodic Timer triggered! arg=99
Periodic Timer triggered! arg=99
One-shot Timer expired! arg=42
Periodic Timer triggered! arg=99
...
๐ Key Takeaways #
- VxWorks timers are implemented using watchdog timers.
- One-shot timers fire once, while periodic timers must be restarted inside their callback.
- Timers are crucial for timeouts, scheduling, and periodic tasks.
โ Wrap-Up #
In this tutorial, you learned:
- How to create and use one-shot timers.
- How to implement periodic timers in VxWorks.
- How watchdog timers can handle both use cases.
In the next blog, weโll explore Semaphores in VxWorks: Binary, Counting, and Mutexes to manage synchronization between tasks.
๐ Stay tuned for Blog 13: โSemaphores in VxWorks: Binary, Counting, and Mutexes.โ