#include "gtCore.h"
#include "gtCntmr.h"
#include "vxCntmrIntCtrl.h"
#include "vxTimestamp.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined (INCLUDE_MVTCT_TIMESTAMP)
/* defines */
#define TIMESTAMP_COUT_NUM 0 /* Use Counter Number */
#if (TIMESTAMP_COUT_NUM < 4)
#define TIMESTAMP_COUT_CAUSE TIMESTAMP_COUT_NUM /* Cause 0- 3 */
#else
#define TIMESTAMP_COUT_CAUSE (TIMESTAMP_COUT_NUM%4) + 32 /* Cause 32-35 */
#endif
#define INIT_VALUE 0xFFFFFFFE /* Initial value of the counter */
#define TMSTMP_INT_PRIO 10 /* Timestamp interrupt priority */
/* typedefs */
/* locals */
LOCAL BOOL sysTimestampRunning = FALSE; /* running flag */
LOCAL BOOL sysTimestampIntConnected = FALSE; /* int connected flag */
LOCAL FUNCPTR sysTimestampRoutine = NULL; /* routine to call on intr */
LOCAL int sysTimestampArg = 0; /* arg for routine */
void sysTimestampInt (void); /* forward declaration */
/*******************************************************************************
*
* sysTimestampInt - timestamp timer interrupt handler
*
* This rountine handles the timestamp timer interrupt. A user routine is
* called, if one was connected by sysTimestampConnect().
*
* RETURNS: N/A
*
* SEE ALSO: sysTimestampConnect()
*/
void sysTimestampInt (void)
{
if ( sysTimestampRunning && sysTimestampRoutine != NULL )
{
(*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, int arg)
{
int lock;
lock = intLock();
sysTimestampArg = arg;
sysTimestampRoutine = routine;
intUnlock(lock);
return (OK);
}
/*******************************************************************************
*
* sysTimestampEnable - initialize and enable the timestamp timer
*
* This routine enables the MV Timer/Counter interrupt and initializes the
* counter registers. If the MV timestamp timer is already running, this routine
* merely resets the MV timer counter.
*
* Output:
* Manipulates the MV internal register in order to start timestamp counting.
*
* RETURNS: OK if output succeeded
* ERROR if failed to enable MV timer 0 expiration interrupt.
*/
STATUS sysTimestampEnable (void)
{
/* first - disable the timer (for further counter reload) */
gtCntmrDisable(TIMESTAMP_COUT_NUM);
if(!sysTimestampRunning)
{
/* configure the MV counter control register to timestamp (TIMER) mode*/
gtCntmrSetMode(TIMESTAMP_COUT_NUM, TIMER);
if ( !sysTimestampIntConnected )
{
vxCntmrIntCtrlInit();
if(vxCntmrIntConnect(TIMESTAMP_COUT_CAUSE,(VOIDFUNCPTR)sysTimestampInt,0,TMSTMP_INT_PRIO)!=OK)
{
return (ERROR);
}
sysTimestampIntConnected = TRUE;
}
}
/* set the counter register to a full count down*/
gtCntmrLoad(TIMESTAMP_COUT_NUM, INIT_VALUE);
/* Enable MV Timer/Counter expiration interrupt */
vxCntmrIntEnable(TIMESTAMP_COUT_CAUSE);
/* enable counter */
gtCntmrEnable(TIMESTAMP_COUT_NUM);
sysTimestampRunning = TRUE;
return(OK);
}
/*******************************************************************************
*
* sysTimestampDisable - disable the MV timestamp counter.
*
* This routine disables the MV timestamp timer and the timestamp timer
* interrupt.
*
* Output:
* Reset the appropriate counter bit in the MV counter control register.
*
* RETURNS: OK.
*/
STATUS sysTimestampDisable (void)
{
if(sysTimestampRunning)
{
/* Disable timestamp counter */
gtCntmrDisable(TIMESTAMP_COUT_NUM);
/* Disable timestamp counter expiration interrupt */
vxCntmrIntDisable(TIMESTAMP_COUT_CAUSE);
sysTimestampRunning = FALSE;
}
return(OK);
}
/*******************************************************************************
*
* sysTimestampPeriod - get the MV timestamp timer period
*
* This routine returns the period of the MV timestamp timer in ticks.
* The period, or terminal count, is the number of ticks to which the GT
* timestamp timer will count before rolling over and restarting the
* counting process.
*
* RETURNS: The period of the MV timestamp timer in counter ticks.
*/
UINT32 sysTimestampPeriod (void)
{
return(INIT_VALUE); /* 32 bit length of counter */
}
/*******************************************************************************
*
* sysTimestampFreq - get the MV timestamp timer clock frequency
*
* This routine returns the frequency of the MV timer clock, in ticks per second.
*
* RETURNS: The MV timestamp timer clock frequency, in ticks per second.
*/
UINT32 sysTimestampFreq (void)
{
return(TCLK_RATE); /* The clock is the TCLK on the development board. */
}
/*******************************************************************************
*
* sysTimestamp - get the MV timestamp timer tick count
*
* This routine returns the current value of the MV timestamp timer tick counter.
* The tick count can be converted to seconds by dividing by the return of
* auxTimestampFreq().
*
* RETURNS: The current MV timestamp timer tick count.
*
* SEE ALSO: sysTimestampLock()
*/
UINT32 sysTimestamp (void)
{
if(TIMESTAMP_COUT_NUM < 4) /* up counting */
return ~(GTREGREAD(TIMER_COUNTER0 + 4*TIMESTAMP_COUT_NUM));
#ifdef INCLUDE_CNTMR_4_7
else
return ~(GTREGREAD(TIMER_COUNTER4 + 4*(TIMESTAMP_COUT_NUM%4)));
#endif
}
/*******************************************************************************
*
* sysTimestampLock - get the MV timestamp tick count of a timer that can't
* be read while enabled
*
* This routine returns the current value of the MV timestamp timer tick counter.
* The tick count can be converted to seconds by dividing by the return of
* sysTimestampFreq(). Because the MV timers can be read while counting this
* function does the same as sysTimestamp().
*
*
* RETURNS: The current MV timestamp timer tick count.
*
* SEE ALSO: sysTimestamp()
*/
UINT32 sysTimestampLock (void)
{
return(sysTimestamp());
}
#endif /* INCLUDE_MVTCT_TIMESTAMP */
#ifdef __cplusplus
}
#endif
#include "gtCntmr.h"
#include "vxCntmrIntCtrl.h"
#include "vxTimestamp.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined (INCLUDE_MVTCT_TIMESTAMP)
/* defines */
#define TIMESTAMP_COUT_NUM 0 /* Use Counter Number */
#if (TIMESTAMP_COUT_NUM < 4)
#define TIMESTAMP_COUT_CAUSE TIMESTAMP_COUT_NUM /* Cause 0- 3 */
#else
#define TIMESTAMP_COUT_CAUSE (TIMESTAMP_COUT_NUM%4) + 32 /* Cause 32-35 */
#endif
#define INIT_VALUE 0xFFFFFFFE /* Initial value of the counter */
#define TMSTMP_INT_PRIO 10 /* Timestamp interrupt priority */
/* typedefs */
/* locals */
LOCAL BOOL sysTimestampRunning = FALSE; /* running flag */
LOCAL BOOL sysTimestampIntConnected = FALSE; /* int connected flag */
LOCAL FUNCPTR sysTimestampRoutine = NULL; /* routine to call on intr */
LOCAL int sysTimestampArg = 0; /* arg for routine */
void sysTimestampInt (void); /* forward declaration */
/*******************************************************************************
*
* sysTimestampInt - timestamp timer interrupt handler
*
* This rountine handles the timestamp timer interrupt. A user routine is
* called, if one was connected by sysTimestampConnect().
*
* RETURNS: N/A
*
* SEE ALSO: sysTimestampConnect()
*/
void sysTimestampInt (void)
{
if ( sysTimestampRunning && sysTimestampRoutine != NULL )
{
(*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, int arg)
{
int lock;
lock = intLock();
sysTimestampArg = arg;
sysTimestampRoutine = routine;
intUnlock(lock);
return (OK);
}
/*******************************************************************************
*
* sysTimestampEnable - initialize and enable the timestamp timer
*
* This routine enables the MV Timer/Counter interrupt and initializes the
* counter registers. If the MV timestamp timer is already running, this routine
* merely resets the MV timer counter.
*
* Output:
* Manipulates the MV internal register in order to start timestamp counting.
*
* RETURNS: OK if output succeeded
* ERROR if failed to enable MV timer 0 expiration interrupt.
*/
STATUS sysTimestampEnable (void)
{
/* first - disable the timer (for further counter reload) */
gtCntmrDisable(TIMESTAMP_COUT_NUM);
if(!sysTimestampRunning)
{
/* configure the MV counter control register to timestamp (TIMER) mode*/
gtCntmrSetMode(TIMESTAMP_COUT_NUM, TIMER);
if ( !sysTimestampIntConnected )
{
vxCntmrIntCtrlInit();
if(vxCntmrIntConnect(TIMESTAMP_COUT_CAUSE,(VOIDFUNCPTR)sysTimestampInt,0,TMSTMP_INT_PRIO)!=OK)
{
return (ERROR);
}
sysTimestampIntConnected = TRUE;
}
}
/* set the counter register to a full count down*/
gtCntmrLoad(TIMESTAMP_COUT_NUM, INIT_VALUE);
/* Enable MV Timer/Counter expiration interrupt */
vxCntmrIntEnable(TIMESTAMP_COUT_CAUSE);
/* enable counter */
gtCntmrEnable(TIMESTAMP_COUT_NUM);
sysTimestampRunning = TRUE;
return(OK);
}
/*******************************************************************************
*
* sysTimestampDisable - disable the MV timestamp counter.
*
* This routine disables the MV timestamp timer and the timestamp timer
* interrupt.
*
* Output:
* Reset the appropriate counter bit in the MV counter control register.
*
* RETURNS: OK.
*/
STATUS sysTimestampDisable (void)
{
if(sysTimestampRunning)
{
/* Disable timestamp counter */
gtCntmrDisable(TIMESTAMP_COUT_NUM);
/* Disable timestamp counter expiration interrupt */
vxCntmrIntDisable(TIMESTAMP_COUT_CAUSE);
sysTimestampRunning = FALSE;
}
return(OK);
}
/*******************************************************************************
*
* sysTimestampPeriod - get the MV timestamp timer period
*
* This routine returns the period of the MV timestamp timer in ticks.
* The period, or terminal count, is the number of ticks to which the GT
* timestamp timer will count before rolling over and restarting the
* counting process.
*
* RETURNS: The period of the MV timestamp timer in counter ticks.
*/
UINT32 sysTimestampPeriod (void)
{
return(INIT_VALUE); /* 32 bit length of counter */
}
/*******************************************************************************
*
* sysTimestampFreq - get the MV timestamp timer clock frequency
*
* This routine returns the frequency of the MV timer clock, in ticks per second.
*
* RETURNS: The MV timestamp timer clock frequency, in ticks per second.
*/
UINT32 sysTimestampFreq (void)
{
return(TCLK_RATE); /* The clock is the TCLK on the development board. */
}
/*******************************************************************************
*
* sysTimestamp - get the MV timestamp timer tick count
*
* This routine returns the current value of the MV timestamp timer tick counter.
* The tick count can be converted to seconds by dividing by the return of
* auxTimestampFreq().
*
* RETURNS: The current MV timestamp timer tick count.
*
* SEE ALSO: sysTimestampLock()
*/
UINT32 sysTimestamp (void)
{
if(TIMESTAMP_COUT_NUM < 4) /* up counting */
return ~(GTREGREAD(TIMER_COUNTER0 + 4*TIMESTAMP_COUT_NUM));
#ifdef INCLUDE_CNTMR_4_7
else
return ~(GTREGREAD(TIMER_COUNTER4 + 4*(TIMESTAMP_COUT_NUM%4)));
#endif
}
/*******************************************************************************
*
* sysTimestampLock - get the MV timestamp tick count of a timer that can't
* be read while enabled
*
* This routine returns the current value of the MV timestamp timer tick counter.
* The tick count can be converted to seconds by dividing by the return of
* sysTimestampFreq(). Because the MV timers can be read while counting this
* function does the same as sysTimestamp().
*
*
* RETURNS: The current MV timestamp timer tick count.
*
* SEE ALSO: sysTimestamp()
*/
UINT32 sysTimestampLock (void)
{
return(sysTimestamp());
}
#endif /* INCLUDE_MVTCT_TIMESTAMP */
#ifdef __cplusplus
}
#endif