This section provides skeleton code for several types of timestamp drivers. These examples illustrate the issues discussed in G.4 Writing the Driver. However, they are only templates. In their current form they will not compile.
This example shows a skeleton timestamp device driver for a hardware timer that can be read while enabled. This type of timer is the simplest to configure for timestamp mode. See Timers That Can Be Read While Enabled, for a discussion of the most important details involved in writing this kind of driver.
/* sampleATimer.c - sample A timer library */ /* Copyright 1994 Wind River Systems, Inc. */ #include "copyright_wrs.h" /* modification history -------------------- 01a,23mar94,dzb written. */ /* DESCRIPTION This library contains sample routines to manipulate the timer functions on the sample A chip with a board-independent interface. This library handles the timestamp timer facility. To include the timestamp timer facility, the macro INCLUDE_TIMESTAMP must be defined. NOTE: This module provides an example of a VxWorks timestamp timer driver for a timer that can be read while enabled. It illustrates the structures and routines discussed in the Appendix, "Creating a VxWorks Timestamp Driver." This module is only a template. In its current form, it will not compile. */ #ifdef INCLUDE_TIMESTAMP #include "drv/timer/timestampDev.h" #include "drv/timer/sampleATimer.h" /* Locals */ LOCAL BOOL sysTimestampRunning = FALSE; /* running flag */ LOCAL FUNCPTR sysTimestampRoutine = NULL; /* user rollover routine */ LOCAL int sysTimestampArg = NULL; /* arg to user routine */ /*************************************************************************** * * sysTimestampInt - timestamp timer interrupt handler * * This routine handles the timestamp timer interrupt. A user routine is * called, if one was connected by sysTimestampConnect(). * * RETURNS: N/A * * SEE ALSO: sysTimestampConnect() */ LOCAL void sysTimestampInt (void) { /* acknowledge the timer rollover interrupt here */ if (sysTimestampRoutine != NULL) /* call user-connected routine */ (*sysTimestampRoutine) (sysTimestampArg); } /*************************************************************************** * * sysTimestampConnect - connect a user routine to the timestamp timer * interrupt * * This routine specifies the user interrupt routine to be called at each * timestamp timer interrupt. It does not enable the timestamp timer itself. * * RETURNS: OK, or ERROR if sysTimestampInt() interrupt handler is not used. */ STATUS sysTimestampConnect ( FUNCPTR routine, /* called at each timestamp timer interrupt */ int arg /* argument with which to call routine */ ) { sysTimestampRoutine = routine; sysTimestampArg = arg; return (OK); } /*************************************************************************** * * sysTimestampEnable - initialize and enable the timestamp timer * * This routine connects the timestamp timer interrupt and initializes the * counter registers. If the timestamp timer is already running, this routine * merely resets the timer counter. * * Set the rate of the timestamp timer input clock explicitly within the * BSP, in the sysHwInit() routine. This routine does not initialize * the timer clock rate. * * RETURNS: OK, or ERROR if the timestamp timer cannot be enabled. */ STATUS sysTimestampEnable (void) { if (sysTimestampRunning) { /* clear the timer counter here */ return (OK); } /* connect interrupt handler for the timestamp timer */ (void) intConnect (INUM_TO_IVEC (XXX), sysTimestampInt, NULL); sysTimestampRunning = TRUE; /* set the timestamp timer's interrupt vector to XXX (if necessary) */ /* reset & enable the timestamp timer interrupt */ /* set the period of timestamp timer (see sysTimestampPeriod()) */ /* clear the timer counter here */ /* enable the timestamp timer here */ return (OK); } /*************************************************************************** * * sysTimestampDisable - disable the timestamp timer * * This routine disables the timestamp timer. Interrupts are not disabled. * However, the tick counter will not increment after the timestamp timer * is disabled, ensuring that interrupts are no longer generated. * * RETURNS: OK, or ERROR if the timestamp timer cannot be disabled. */ STATUS sysTimestampDisable (void) { if (sysTimestampRunning) { /* disable the timestamp timer here */ sysTimestampRunning = FALSE; } return (OK); } /*************************************************************************** * * sysTimestampPeriod - get the timestamp timer period * * This routine returns the period of the timer in timestamp ticks. * The period, or terminal count, is the number of ticks to which the * timestamp timer counts before rolling over and restarting the counting * process. * * RETURNS: The period of the timer in timestamp ticks. */ UINT32 sysTimestampPeriod (void) { /* * Return the timestamp timer period here. * The highest period (maximum terminal count) should be used so * that rollover interrupts are kept to a minimum. */ } /*************************************************************************** * * sysTimestampFreq - get the timestamp timer clock frequency * * This routine returns the frequency of the timer clock, in ticks per second. * The rate of the timestamp timer should be set explicitly in the BSP, * in the sysHwInit() routine. * * RETURNS: The timestamp timer clock frequency, in ticks per second. */ UINT32 sysTimestampFreq (void) { UINT32 timerFreq; /* * Return the timestamp tick output frequency here. * This value can be determined from the following equation: * timerFreq = clock input frequency / prescaler * * When possible, read the clock input frequency and prescaler values * directly from chip registers. */ return (timerFreq); } /*************************************************************************** * * sysTimestamp - get the timestamp timer tick count * * This routine returns the current value of the timestamp timer tick counter. * The tick count can be converted to seconds by dividing by the return of * sysTimestampFreq(). * * Call this routine with interrupts locked. If interrupts are * not already locked, use sysTimestampLock() instead. * * RETURNS: The current timestamp timer tick count. * SEE ALSO: sysTimestampLock() */ UINT32 sysTimestamp (void) { /* return the timestamp timer tick count here */ } /*************************************************************************** * * sysTimestampLock - get the timestamp timer tick count * * This routine returns the current value of the timestamp timer tick counter. * The tick count can be converted to seconds by dividing by the return of * sysTimestampFreq(). * * This routine locks interrupts for cases where it is necessary to stop the * tick counter before reading it, or when two independent counters must * be read. If interrupts are already locked, use sysTimestamp() instead. * * RETURNS: The current timestamp timer tick count. * * SEE ALSO: sysTimestamp() */ UINT32 sysTimestampLock (void) { /* * Return the timestamp timer tick count here. * Interrupts do *not* need to be locked in this routine if * the counter need not be stopped before reading. */ } #endif /* INCLUDE_TIMESTAMP */
This example shows a skeleton timestamp device driver for a hardware timer that cannot be read while enabled, requires preloading, and counts down. See Working Around Deficiencies in Hardware Timers, for a discussion of the most important details involved in writing this kind of driver.
/* sampleBTimer.c - sample B timer library */ /* Copyright 1984-1994 Wind River Systems, Inc. */ #include "copyright_wrs.h" /*
This example shows a skeleton timestamp driver for systems that have no suitable spare timers, so that timestamps must be derived from the VxWorks system clock timer. See Using the VxWorks System Clock Timer, for a discussion of the most important details involved in writing this kind of driver.
/* sampleCTimer.c - sample C timer library */ /* Copyright 1994 Wind River Systems, Inc. */ #include "copyright_wrs.h" /* modification history -------------------- 01a,23mar94,dzb written. */ /* DESCRIPTION This library contains sample routines to manipulate the timer functions on the sample C chip with a board-independent interface. This library handles the timestamp timer facility. To include the timestamp timer facility, the macro INCLUDE_TIMESTAMP must be defined.