vme2Timer.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:7k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* vme2Timer.c - vmechip2 Timestamp Timer driver library */
  2. /* Copyright 1994-1996 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 01e,06aug96,dat  merged into main 5_3_x tree.
  8. 01d,28nov94,rdc  fixed spr 3023.
  9. 01c,16apr94,dzb  changed file name from vme2Timer.c to vme2TS.c
  10. 01b,10jan94,dzb  added conditional compilation for INCLUDE_TIMESTAMP macro.
  11. 01a,17sep93,dzb  written.
  12. */
  13. /*
  14. DESCRIPTION
  15. This library contains routines to manipulate the timer functions on the
  16. VMEchip2 with a board-independent interface.  This library handles
  17. the timestamp timer facility.
  18. To include the timestamp timer facility, the macro INCLUDE_TIMESTAMP must be
  19. defined.  The macro TIMESTAMP_LEVEL must be defined to provide the timestamp
  20. timer's interrupt level.
  21. For proper operation the macro CPU_SPEED is presumed to be the correct
  22. speed, in hertz, for the CPU clock.
  23. Other macros used by this driver:
  24.     VMECHIP2_TTCOUNT1  - timer counter 1, 32 bits
  25.     VMECHIP2_TIMEOUTCR - Prescaler count in byte 0
  26.     VMECHIP2_TTCOMP1   - timer 1 compare reg, 32 bits
  27.     VMECHIP2_ILR1      - interrupt level register 1
  28.     VMECHIP2_LBIER     - local bus int enable 1
  29.     VMECHIP2_ICLR      - interrupt clear register
  30. */
  31. #ifdef INCLUDE_TIMESTAMP
  32. #include "drv/timer/timestampDev.h"
  33. #include "drv/vme/vmechip2.h"
  34. /* Locals */ 
  35. LOCAL BOOL    sysTimestampRunning = FALSE; /* running flag */
  36. LOCAL FUNCPTR sysTimestampRoutine = NULL; /* user rollover routine */
  37. LOCAL int     sysTimestampArg     = NULL; /* arg to user routine */
  38. /*******************************************************************************
  39. *
  40. * sysTimestampInt - timestamp timer interrupt handler
  41. *
  42. * This rountine handles the timestamp timer interrupt.  A user routine is
  43. * called, if one was connected by sysTimestampConnect().
  44. *
  45. * RETURNS: N/A   
  46. *
  47. * SEE ALSO: sysTimestampConnect()
  48. */
  49. LOCAL void sysTimestampInt (void)
  50.     {
  51.     *VMECHIP2_ICLR = ICLR_CTIC1; /* acknowledge timer interrupt */
  52.     if (sysTimestampRoutine != NULL) /* call user routine */
  53.         (*sysTimestampRoutine) (sysTimestampArg);
  54.     }
  55. /*******************************************************************************
  56. *
  57. * sysTimestampConnect - connect a user routine to the timestamp timer interrupt
  58. *
  59. * This routine specifies the user interrupt routine to be called at each
  60. * timestamp timer interrupt.  It does not enable the timestamp timer itself.
  61. *
  62. * RETURNS: OK, or ERROR if sysTimestampInt() interrupt handler is not used.
  63. */
  64.  
  65. STATUS sysTimestampConnect
  66.     (
  67.     FUNCPTR routine, /* routine called at each timestamp timer interrupt */
  68.     int arg /* argument with which to call routine */
  69.     )
  70.     {
  71.     sysTimestampRoutine = routine;
  72.     sysTimestampArg = arg;
  73.  
  74.     return (OK);
  75.     }
  76. /*******************************************************************************
  77. *
  78. * sysTimestampEnable - initialize and enable the timestamp timer
  79. *
  80. * This routine connects the timestamp timer interrupt and initializes the
  81. * counter registers.  If the timestamp timer is already running, this routine
  82. * merely resets the timer counter.
  83. *
  84. * The rate of the timestamp timer should be set explicitly within the BSP,
  85. * in the sysHwInit() routine.  This routine does not intialize the timer
  86. * rate.  
  87. *
  88. * RETURNS: OK, or ERROR if the timestamp timer cannot be enabled.
  89. */
  90.  
  91. STATUS sysTimestampEnable (void)
  92.     {
  93.     if (sysTimestampRunning)
  94. {
  95.         *VMECHIP2_TTCOUNT1 = 0; /* clear the counter */
  96. return (OK);
  97. }
  98.     
  99.     (void) intConnect (INUM_TO_IVEC (UTIL_INT_VEC_BASE0 + LBIV_TT1),
  100.                        sysTimestampInt, NULL);
  101.     sysTimestampRunning = TRUE;
  102.     /* reset & enable the timestamp timer interrupt */
  103.     *VMECHIP2_ICLR  = ICLR_CTIC1;
  104.     *VMECHIP2_LBIER |= LBIER_ETIC1;
  105.     *VMECHIP2_ILR1  |= TIMESTAMP_LEVEL;
  106.     *VMECHIP2_TTCOMP1 = sysTimestampPeriod(); /* set the timer period */
  107.     *VMECHIP2_TTCOUNT1 = 0; /* clear the counter */
  108.     *VMECHIP2_TIMERCR |= TIMERCR_TT1_EN; /* enable the timer */
  109.     return (OK);
  110.     }
  111. /********************************************************************************
  112. * sysTimestampDisable - disable the timestamp timer
  113. *
  114. * This routine disables the timestamp timer.  Interrupts are not disabled,
  115. * although the tick counter will not increment after the timestamp timer
  116. * is disabled, thus interrupts will no longer be generated.
  117. *
  118. * RETURNS: OK, or ERROR if the timestamp timer cannot be disabled.
  119. */
  120.  
  121. STATUS sysTimestampDisable (void)
  122.     {
  123.     if (sysTimestampRunning)
  124. {
  125.         *VMECHIP2_TIMERCR &= ~TIMERCR_TT1_EN; /* disable the timer */
  126.         sysTimestampRunning = FALSE;
  127. }
  128.     return (OK);
  129.     }
  130. /*******************************************************************************
  131. *
  132. * sysTimestampPeriod - get the timestamp timer period
  133. *
  134. * This routine returns the period of the timestamp timer in ticks.
  135. * The period, or terminal count, is the number of ticks to which the timestamp
  136. * timer will count before rolling over and restarting the counting process.
  137. *
  138. * RETURNS: The period of the timestamp timer in counter ticks.
  139. */
  140.  
  141. UINT32 sysTimestampPeriod (void)
  142.     {
  143.     return (0xffffffff); /* highest period -> freerunning */
  144.     }
  145. /*******************************************************************************
  146. *
  147. * sysTimestampFreq - get the timestamp timer clock frequency
  148. *
  149. * This routine returns the frequency of the timer clock, in ticks per second.
  150. * The rate of the timestamp timer should be set explicitly within the BSP,
  151. * in the sysHwInit() routine.
  152. *
  153. * RETURNS: The timestamp timer clock frequency, in ticks per second.
  154. */
  155.  
  156. UINT32 sysTimestampFreq (void)
  157.     {
  158.     UINT32 timerFreq;
  159.     UINT32 Bclk;
  160. #ifdef CPU_SPEED
  161.     Bclk = CPU_SPEED;
  162. #else
  163.     Bclk = 25000000;           /* use 25MHz if not valid speed */
  164. #endif
  165.     timerFreq = Bclk/(256 - (*VMECHIP2_TIMEOUTCR & 0xff));
  166.     return (timerFreq);
  167.     }
  168.  
  169. /*******************************************************************************
  170. *
  171. * sysTimestamp - get the timestamp timer tick count
  172. *
  173. * This routine returns the current value of the timestamp timer tick counter.
  174. * The tick count can be converted to seconds by dividing by the return of
  175. * sysTimestampFreq().
  176. *
  177. * This routine should be called with interrupts locked.  If interrupts are
  178. * not already locked, sysTimestampLock() should be used instead.
  179. *
  180. * RETURNS: The current timestamp timer tick count.
  181. *
  182. * SEE ALSO: sysTimestampLock()
  183. */
  184.  
  185. UINT32 sysTimestamp (void)
  186.     {
  187.     return (*VMECHIP2_TTCOUNT1);
  188.     }
  189. /*******************************************************************************
  190. *
  191. * sysTimestampLock - get the timestamp timer tick count
  192. *
  193. * This routine returns the current value of the timestamp timer tick counter.
  194. * The tick count can be converted to seconds by dividing by the return of
  195. * sysTimestampFreq().
  196. *
  197. * This routine locks interrupts for cases where it is necessary to stop the
  198. * tick counter in order to read it, or when two independent counters must
  199. * be read.  If interrupts are already locked, sysTimestamp() should be
  200. * used instead.
  201. *
  202. * RETURNS: The current timestamp timer tick count.
  203. *
  204. * SEE ALSO: sysTimestamp()
  205. */
  206.  
  207. UINT32 sysTimestampLock (void)
  208.     {
  209.     return (*VMECHIP2_TTCOUNT1);
  210.     }
  211. #endif /* INCLUDE_TIMESTAMP */