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

Shared Memory Communication in VxWorks
·466 words·3 mins
VxWorks RTOS IPC Shared Memory Embedded Systems Programming Tutorial
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