Skip to main content

Timers in VxWorks: Periodic and One-Shot Timers

·356 words·2 mins
VxWorks RTOS Timers Watchdog Embedded Systems Programming Tutorial
Table of Contents
VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 12: This Article

🚀 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
#

  1. One-Shot Timer

    • Executes once after a specified delay.
  2. 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
#

  1. wdCreate()

    • Creates a watchdog timer instance.
  2. wdStart()

    • Starts the timer with a given delay (in system ticks).
    • sysClkRateGet() → gets system clock ticks per second.
  3. One-Shot Timer (oneShotWd)

    • Fires once after 5 seconds.
  4. 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.”

VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 12: This Article

Related

Inter-Task Communication with Message Queues in VxWorks
·459 words·3 mins
VxWorks RTOS IPC Message Queues Embedded Systems Programming Tutorial
UDP Networking in VxWorks: Sending and Receiving Packets
·553 words·3 mins
VxWorks RTOS Networking Sockets UDP Embedded Systems Programming Tutorial
Handling Multiple Clients in VxWorks with select
·648 words·4 mins
VxWorks RTOS Networking Sockets Select() Multi-Client Embedded Systems Programming Tutorial