ixp425Timer.c
资源名称:ixp425BSP.rar [点击查看]
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:17k
源码类别:
VxWorks
开发平台:
C/C++
- /* ixp425Timer.c - ixp425 processor timer library */
- /* Copyright 2002 Wind River Systems, Inc. */
- #include "copyright_wrs.h"
- /*
- modification history
- --------------------
- 01a,05jun02,jb initial version...
- */
- /*
- DESCRIPTION
- This library contains routines to manipulate the timer functions.
- _______
- hardware TIMER 0
- clock _______
- |
- interface -----------------------------------------------------
- | | |
- | | |
- software _______ ______ _____________
- clock(s) sysClk auxClk timeStampClk
- _______ ______ _____________
- interface functions:
- clkInt() - clock interrupt handler
- clkConnect() - connect a routine to the clock interrupt
- clkDisable() - turn off system clock interrupts
- clkEnable() - turn on system clock interrupts
- clkRateGet() - get the system clock rate oscillations per second
- clkPeriod() - get the period of the timer (tick counter rollover)
- clkFreq() - get a timer clock frequency
- clkTimestamp() - get a tick counter count
- Note: There are two different types of ticks referred to. The frequency
- tick and the counter tick. A frequency tick refers to the timer clock
- oscillations per second and the counter ticks refer to the number of times
- (ticks) a register decreases until roll over.
- The macros SYS_CLK_RATE_MIN, SYS_CLK_RATE_MAX must be defined to
- provide parameter checking for the sysClkRateSet() routine.
- */
- #include "ixp425.h"
- #include "ixp425Timer.h"
- #include "drv/timer/timerDev.h"
- #ifdef INCLUDE_TIMESTAMP
- #include "drv/timer/timestampDev.h"
- #endif
- /*
- * The APB timer block is not based on the CPU speed, but on
- * the APB core clock of 66Mhz.
- */
- #define TIMER_APB_CLOCK_MHZ ( IXP425_PERIPHERAL_BUS_CLOCK )
- /* hardware access methods */
- #ifndef IXP425_REG_TIMER_READ
- #define IXP425_REG_TIMER_READ(reg,result)
- ((result) = *(volatile UINT32 *)(reg))
- #endif /*IXP425_REG_TIMER_READ*/
- #ifndef IXP425_REG_TIMER_WRITE
- #define IXP425_REG_TIMER_WRITE(reg,data)
- (*((volatile UINT32 *)(reg)) = (data))
- #endif /*IXP425_REG_TIMER_WRITE*/
- /* Locals */
- LOCAL int clockTicksPerSecond = IXP425_OSST_TICKS_PER_SECOND;
- LOCAL UINT32 clockTimerRollOver = IXP425_OSST_ROLLOVER;
- LOCAL BOOL clockConnected = FALSE;
- LOCAL BOOL sysClkRunning = FALSE;
- LOCAL FUNCPTR sysClkRoutine = (FUNCPTR) NULL;
- LOCAL int sysClkArg = (int) NULL;
- LOCAL BOOL sysClkConnected = FALSE;
- /* We do not actually have an auxiliary clock available. */
- LOCAL BOOL sysAuxClkRunning = FALSE;
- LOCAL FUNCPTR sysAuxClkRoutine = (FUNCPTR) NULL;
- LOCAL int sysAuxClkArg = (int) NULL;
- LOCAL BOOL sysAuxClkConnected = FALSE;
- #ifdef INCLUDE_TIMESTAMP
- LOCAL BOOL sysTimestampRunning = FALSE;
- #endif
- LOCAL void clkInt (void)
- {
- /* Clear Pending Interrupt by writing '1' to it */
- IXP425_REG_TIMER_WRITE(IXP425_OSST, IXP425_OSST_TIMER_1_PEND);
- if ((sysClkRoutine != NULL) && sysClkRunning)
- (*(FUNCPTR) sysClkRoutine) (sysClkArg);
- /* The timer is free running and as such it has already reloaded
- * and is counting down
- */
- }
- /***************************************************************************
- *
- * clkConnect - connect a routine to the clock interrupt
- *
- * This routine specifies the interrupt service routine to be called at each
- * clock interrupt.
- *
- * RETURN: OK
- *
- * SEE ALSO: intConnect(), usrClock(), clkEnable()
- */
- LOCAL STATUS clkConnect (void)
- {
- if(!clockConnected)
- {
- sysHwInit2 ();
- (void)intConnect ((void *)INT_VEC_TIMER1, clkInt, 0);
- clockConnected = TRUE;
- }
- return (OK);
- }
- /***************************************************************************
- *
- * clkDisable - turn off system clock interrupts
- *
- * This routine disables clock interrupts. In order for the hardware clock
- * to be disables all the software clocks must be disabled.
- *
- * RETURNS: N/A
- *
- * SEE ALSO: clkEnable()
- */
- LOCAL void clkDisable (void)
- {
- intDisable(INT_VEC_TIMER1);
- /* Clear the OST register for this timer, This disabled the
- timer */
- IXP425_REG_TIMER_WRITE( IXP425_OSRT1 , IXP425_OST_DISABLED );
- }
- /***************************************************************************
- *
- * clkEnable - turn on system clock interrupts
- *
- * This routine enables system clock interrupts. Any software clock can
- * enable the hardware clock
- *
- * RETURNS: N/A
- *
- * SEE ALSO: sysClkDisable(), sysClkRateSet()
- */
- LOCAL void clkEnable (void)
- {
- IXP425_REG_TIMER_WRITE( IXP425_OSRT1 , ((clockTimerRollOver & ~IXP425_OST_RELOAD_MASK) | IXP425_OST_ENABLE ) );
- intEnable(INT_VEC_TIMER1);
- }
- /***************************************************************************
- *
- * clkRateGet - get the system clock rate
- *
- * This routine returns the oscillations clock rate.
- *
- * RETURNS: The number of oscillations per second of the clock.
- *
- * SEE ALSO: sysClkEnable(), sysClkRateSet()
- */
- LOCAL int clkRateGet (void)
- {
- return (clockTicksPerSecond);
- }
- /**************************************************************************
- *
- * clkPeriod - get the period of the tick counter
- *
- * This routine gets the period of the timer, in ticks. The
- * period, or terminal count, is the number of ticks to which the tick
- * counter counts before rolling over and restarting the counting process.
- *
- * RETURNS: The period of the timer in counter ticks.
- */
- LOCAL UINT32 clkPeriod (void)
- {
- return (clockTimerRollOver);
- }
- /**************************************************************************
- *
- * clkFreq - get a timer clock frequency
- *
- * This routine gets the frequency of the timer clock, in ticks per
- * second. The rate of the timer is set explicitly by the
- * hardware and typically cannot be altered.
- *
- * RETURNS: The timer clock frequency, in counter ticks per second.
- */
- LOCAL UINT32 clkFreq (void)
- {
- return ( TIMER_APB_CLOCK_MHZ * 1000000);
- }
- /**************************************************************************
- *
- * clkTimestamp - get a time stamp count
- *
- * This routine returns the current value of the timer tick counter.
- * The tick count can be converted to seconds by dividing it by the return of
- * clkFreq().
- *
- *
- * RETURNS: The current time stamp count.
- *
- * SEE ALSO: clkFreq()
- */
- LOCAL UINT32 clkTimestamp (void)
- {
- UINT32 count;
- /* Read the free running up-timer from peripherial block */
- IXP425_REG_TIMER_READ(IXP425_OSTS,count);
- return(count);
- }
- /***************************************************************************
- *
- * sysClkConnect - connect a routine to the system clock interrupt
- *
- * This routine specifies the interrupt service routine to be called at each
- * clock interrupt. Normally it is called from usrRoot() in usrConfig.c to
- * connect usrClock() to the system clock interrupt.
- *
- * RETURN: OK
- *
- * SEE ALSO: intConnect(), usrClock(), sysClkEnable()
- */
- STATUS sysClkConnect
- (
- FUNCPTR routine, /* routine called at each system clock interrupt */
- int arg /* argument with which to call routine */
- )
- {
- sysClkConnected = TRUE;
- sysClkRoutine = routine;
- sysClkArg = arg;
- clkConnect();
- return (OK);
- }
- /***************************************************************************
- *
- * sysClkDisable - turn off system clock interrupts
- *
- * This routine disables system clock interrupts.
- *
- * RETURNS: N/A
- *
- * SEE ALSO: sysClkEnable()
- */
- void sysClkDisable (void)
- {
- if (sysClkRunning)
- {
- clkDisable();
- sysClkRunning = FALSE;
- }
- }
- /***************************************************************************
- *
- * sysClkEnable - turn on system clock interrupts
- *
- * This routine enables system clock interrupts.
- *
- * RETURNS: N/A
- *
- * SEE ALSO: sysClkDisable(), sysClkRateSet()
- */
- void sysClkEnable (void)
- {
- if (!sysClkRunning)
- {
- clkEnable();
- sysClkRunning = TRUE;
- }
- }
- /***************************************************************************
- *
- * sysClkRateGet - get the system clock rate
- *
- * This routine returns the system clock rate.
- *
- * RETURNS: The number of ticks per second of the system clock.
- *
- * SEE ALSO: sysClkEnable(), sysClkRateSet()
- */
- int sysClkRateGet (void)
- {
- return(clkRateGet());
- }
- /***************************************************************************
- *
- * sysClkRateSet - set the system clock rate
- *
- * This routine sets the interrupt rate of the system clock. It is called by
- * usrRoot() in usrConfig.c.
- *
- * RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be set.
- *
- * SEE ALSO: sysClkEnable(), sysClkRateGet()
- */
- STATUS sysClkRateSet
- (
- int ticksPerSecond /* number of clock interrupts per second */
- )
- {
- if (ticksPerSecond < SYS_CLK_RATE_MIN || ticksPerSecond > SYS_CLK_RATE_MAX)
- return (ERROR);
- clockTicksPerSecond = ticksPerSecond;
- clockTimerRollOver = ((TIMER_APB_CLOCK_MHZ * 1000000) / clockTicksPerSecond);
- if (sysClkRunning)
- {
- sysClkDisable ();
- sysClkEnable ();
- }
- return (OK);
- }
- /***************************************************************************
- * We do not actually have an auxiliary clock available.
- ***************************************************************************/
- /***************************************************************************
- *
- * sysAuxClkConnect - connect a routine to the auxiliary clock interrupt
- *
- * This routine specifies the interrupt service routine to be called at each
- * auxiliary clock interrupt. It does not enable auxiliary clock interrupts.
- *
- * RETURNS: OK
- *
- * SEE ALSO: intConnect(), sysAuxClkEnable()
- */
- STATUS sysAuxClkConnect
- (
- FUNCPTR routine, /* routine called at each aux. clock interrupt */
- int arg /* argument with which to call routine */
- )
- {
- sysAuxClkConnected = TRUE;
- sysAuxClkRoutine = routine;
- sysAuxClkArg = arg;
- clkConnect();
- return (OK);
- }
- /***************************************************************************
- *
- * sysAuxClkDisconnect - clear the auxiliary clock routine
- *
- * This routine disables the auxiliary clock interrupt, stops the timer,
- * and disconnects the routine currently connected to the auxiliary clock
- * interrupt.
- *
- * RETURNS: N/A
- *
- * SEE ALSO: sysAuxClkConnect(), sysAuxClkEnable()
- */
- void sysAuxClkDisconnect (void)
- {
- /* disable the auxiliary clock interrupt */
- sysAuxClkDisable ();
- }
- /***************************************************************************
- *
- * sysAuxClkDisable - turn off auxiliary clock interrupts
- *
- * This routine disables auxiliary clock interrupts.
- *
- * RETURNS: N/A
- *
- * SEE ALSO: sysAuxClkEnable()
- */
- void sysAuxClkDisable (void)
- {
- if (sysAuxClkRunning)
- {
- sysAuxClkRunning = FALSE;
- }
- }
- /***************************************************************************
- *
- * sysAuxClkEnable - turn on auxiliary clock interrupts
- *
- * This routine enables auxiliary clock interrupts.
- *
- * RETURNS: N/A
- *
- * SEE ALSO: sysAuxClkDisable()
- */
- void sysAuxClkEnable (void)
- {
- if (!sysAuxClkRunning)
- {
- sysAuxClkRunning = TRUE;
- }
- }
- /***************************************************************************
- *
- * sysAuxClkRateGet - get the auxiliary clock rate
- *
- * This routine returns the interrupt rate of the auxiliary clock.
- *
- * RETURNS: The number of ticks per second of the auxiliary clock.
- *
- * SEE ALSO: sysAuxClkEnable(), sysAuxClkRateSet()
- */
- int sysAuxClkRateGet (void)
- {
- return(clkRateGet());
- }
- /***************************************************************************
- *
- * sysAuxClkRateSet - set the auxiliary clock rate
- *
- * This routine sets the interrupt rate of the auxiliary clock. It does not
- * enable auxiliary clock interrupts.
- *
- * RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be set.
- *
- * SEE ALSO: sysAuxClkEnable(), sysAuxClkRateGet()
- */
- STATUS sysAuxClkRateSet
- (
- int ticksPerSecond /* number of clock interrupts per second */
- )
- {
- return (OK);
- }
- #ifdef INCLUDE_TIMESTAMP
- /**************************************************************************
- NOTE: The current system has no suitable spare timers, thus requiring
- that timestamps be derived from vxWorks system clock timer.
- **************************************************************************/
- /**************************************************************************
- *
- * sysTimestampConnect - connect a user routine to a timestamp timer interrupt
- *
- * This routine specifies the user interrupt routine to be called at each
- * timestamp timer interrupt.
- *
- * RETURNS: OK, or ERROR if sysTimestampInt() has not been used.
- *
- * SEE ALSO: sysTimestampEnable()
- */
- STATUS sysTimestampConnect
- (
- FUNCPTR routine, /* routine called at each timestamp timer interrupt */
- int arg /* argument with which to call routine */
- )
- {
- return (ERROR);
- }
- /**************************************************************************
- *
- * sysTimestampEnable - enable a timestamp timer interrupt
- *
- * This routine enables timestamp timer interrupts and resets the counter.
- *
- * RETURNS: OK, or ERROR if the timestamp timer cannot be enabled.
- *
- * SEE ALSO: sysTimestampDisable()
- */
- STATUS sysTimestampEnable(void)
- {
- if(!sysTimestampRunning)
- {
- sysTimestampRunning = TRUE;
- }
- return (OK);
- }
- /**************************************************************************
- *
- * sysTimestampDisable - disable a timestamp timer interrupt
- *
- * This routine disables the timestamp timer. It does not directly disable
- * interrupts. However, the tick counter does not increment once the
- * timestamp timer is disabled, thus, interrupts are no longer generated.
- * This routine merely resets the timer counter.
- *
- * RETURNS: OK, ERROR if the timestamp timer cannot be disabled.
- *
- * SEE ALSO: sysTimestampEnable()
- */
- STATUS sysTimestampDisable (void)
- {
- if (sysTimestampRunning)
- {
- sysTimestampRunning = FALSE;
- }
- return (ERROR);
- }
- /**************************************************************************
- *
- * sysTimestampPeriod - get the period of a timestamp timer
- *
- * This routine gets the period of the timestamp timer, in 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 timestamp timer in counter 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.
- *
- */
- return (clkPeriod());
- }
- /**************************************************************************
- *
- * sysTimestampFreq - get a timestamp timer clock frequency
- *
- * This routine gets the frequency of the timer clock, in ticks per
- * second. The rate of the timestamp timer is set explicitly by the
- * hardware and typically cannot be altered.
- *
- * RETURNS: The timestamp timer clock frequency, in ticks per second.
- */
- UINT32 sysTimestampFreq (void)
- {
- /*
- * 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 (clkFreq());
- }
- /**************************************************************************
- *
- * sysTimestamp - get a 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 it by the return of
- * sysTimestampFreq().
- *
- * This routine should be called with interrupts locked. If interrupts are
- * not locked, sysTimestampLock() should be used instead.
- *
- * RETURNS: The current timestamp timer tick count.
- *
- * SEE ALSO: sysTimestampFreq(), sysTimestampLock()
- */
- UINT32 sysTimestamp (void)
- {
- UINT32 ticks = 0;
- if (sysTimestampRunning)
- {
- /* Read the timer counter register */
- ticks = clkTimestamp();
- }
- /* return the timestamp timer tick count here */
- return (ticks);
- }
- /**************************************************************************
- *
- * sysTimestampLock - lock interrupts and get the timestamp timer tick count
- *
- * This routine locks interrupts when the tick counter must be stopped
- * in order to read it or when two independent counters must be read.
- * It then returns the current value of the timestamp timer tick
- * counter.
- *
- * The tick count can be converted to seconds by dividing it by the return of
- * sysTimestampFreq().
- *
- * If interrupts are already locked, sysTimestamp() should be
- * used instead.
- *
- * RETURNS: The current timestamp timer tick count.
- *
- * SEE ALSO: sysTimestampFreq(), sysTimestamp()
- */
- UINT32 sysTimestampLock (void)
- {
- int locKey;
- UINT32 ticks = 0;
- if (sysTimestampRunning)
- {
- /* Lock Interrupts */
- locKey = intLock();
- /* Read the timer counter register */
- ticks = clkTimestamp();
- /* UnLock Interrupts */
- intUnlock (locKey);
- }
- /* return the timestamp timer tick count here */
- return (ticks);
- }
- void reset()
- {
- IXP425_REG_TIMER_WRITE(IXP425_OSWK, 0x482e);
- IXP425_REG_TIMER_WRITE(IXP425_OSWT, 0);
- IXP425_REG_TIMER_WRITE(IXP425_OSWE, 0x3);
- }
- #endif /* INCLUDE_TIMESTAMP */