From: Bob Herlien <hebo@mbari.org>
Newsgroups: comp.os.vxworks
Subject: re: MVME167 usec timer?
Date: Wed, 7 Oct 92 15:59:25 PDT
Organization: Lawrence Berkeley Laboratory, Berkeley CA
NNTP-Posting-Host: 128.3.112.16
Originator: daemon@vxw.ee.lbl.gov

My usrTime functions (in ntpvx* on the archive, part of my NTP port)
includes gettimeofday() function that returns time in a struct timeval.
Struct timeval has a precision of microseconds.  To actually get this 
precision, you need to implement microtime() for your hardware.  If you
don't write a microtime(), you'll get time to the nearest tick (usually
167 milliseconds).

UsrTime has drivers for the SBE VPU-30.  For time of day clock, that includes
a driver for the MK48T02 time of day chip, which, as you noted, has a
precision of seconds.  By chaining onto the system tick, we increase the
resolution to 1 tick.  The microtime() function is needed for microsecond
(actually 4 usec for SBE VPU-30) precision.  The drivers I wrote and included,
for the SBE VPU-30, talk to the MC68230 PIT chip to derive microseconds.
The VxWorks BSP uses the PIT to generate 60 Hz for the system clock, and
my microtime() simply reads the current value in the count register to
determine the microsecond offset from the clock tick.

Here's the microtime() module for the MC63230 for SBE.  You can pick up
usrTime from the archives, in which case you get a bunch of Unix compatible
time functions, or you can simply look at what I've done and use a similar
trick for your hardware.  In either case, you'll need to write some software
that pokes around in the hardware that generates your clock tick.

/* microtime.s - get microseconds from clock tick	*/

/*
modification history
--------------------
14 sept 90, rah, created

DESCRIPTION
This module contains a routine to get high resolution data about current
time.  It returns the number of microseconds until the next clock tick.
It is specific to the SBE VPU30, and has a resolution of 4 microseconds.
*/

#define ASMLANGUAGE
#include "vpu30.h"

#define PIT_CNT_REG	(PIT_BASE + 0x2c)


	.text
	.even

	.globl	_microtime


/*******************************************************************************
*
* microtime - get microseconds until next clock tick
* This routine gets number of microseconds until the next clock tick.
* Since the movepl takes 4 bus cycles, and the clock is running during
* this time, the answer can reflect some bytes in which a carry took
* place and other bytes in which it didn't.  However, since the movepl
* takes much less than a microsecond, and the clock counts at 4 microsecond
* intervals, this carry error could only have happened if the LSB is 00.
* So, we check for an LSB of zero and try again in that case.
*
* RETURNS: Number of microseconds until next clock tick (unsigned long)
*/

_microtime:
	movel	a0, a7@-		/* save a0		*/
	moveal	#PIT_CNT_REG, a0	/* get address of PIT_CNT*/
mt_retry:
	movepl	(0,a0), d0		/* get current count	*/
	orb	d0, d0			/* test for LSB == 0	*/
	beq	mt_retry		/* if so, answer may be wrong*/

	asll	#2, d0			/* multiply count by 4 to get usec*/
	movel	a7@+, a0		/* restore a0		*/
	rts


--------------------------------------------------------------
Bob Herlien
MBARI (Monterey Bay Aquarium Research Institute)
hebo@mbari.org
"Limit congressmen to two terms.  One in office.  One in jail."