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

VxWorks

开发平台:

C/C++

  1. /* swTimer.c - software timer library */
  2. /* Copyright 1984-1994 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 01a,02feb94,dzb
  8. */
  9. /*
  10. DESCRIPTION
  11. */
  12. /* includes */
  13. #include "semLib.h"
  14. #include "taskLib.h"
  15. /* defines */
  16. #ifndef SYS_SW_CLK_PRIORITY
  17. #define SYS_SW_CLK_PRIORITY 255
  18. #endif /* SYS_SW_CLK_PRIORITY */
  19. /* locals */
  20. LOCAL FUNCPTR sysClkRoutine = NULL; /* routine to call on clock interrupt */
  21. LOCAL int sysClkArg = NULL; /* its argument */
  22. LOCAL int sysClkRunning = FALSE;
  23. LOCAL int sysClkTicksPerSecond = 1;
  24. LOCAL SEM_ID semDId;
  25. /*******************************************************************************
  26. *
  27. * sysClkConnect - connect a routine to the system clock interrupt
  28. *
  29. * This routine specifies the interrupt service routine to be called at each
  30. * system clock interrupt.  It does not enable system clock interrupts.
  31. * Normally, it is called from usrRoot() in usrConfig.c to connect usrClock()
  32. * to the system clock interrupt.
  33. *
  34. * RETURN: OK, or ERROR if the routine cannot be connected to the interrupt.
  35. *
  36. * SEE ALSO: intConnect(), usrClock(), sysClkEnable()
  37. */
  38. STATUS sysClkConnect
  39.     (
  40.     FUNCPTR routine, /* routine to be called at each clock interrupt */
  41.     int     arg /* argument with which to call routine          */
  42.     )
  43.     {
  44.     sysHwInit2 (); /* XXX for now -- needs to be in usrConfig.c */
  45.     sysClkRoutine   = routine;
  46.     sysClkArg       = arg;
  47.     return (OK);
  48.     }
  49. /*******************************************************************************
  50. *
  51. * sysClkDisable - turn off system clock interrupts
  52. *
  53. * This routine disables system clock interrupts.
  54. *
  55. * RETURNS: N/A
  56. *
  57. * SEE ALSO: sysClkEnable()
  58. */
  59. void sysClkDisable (void)
  60.     {
  61.     sysClkRunning = FALSE;
  62.     semTake (semDId, WAIT_FOREVER);
  63.     }
  64. /*******************************************************************************
  65. *
  66. * sysSwClk - simulate a system clock
  67. *
  68. * RETURNS: N/A.
  69. *
  70. * SEE ALSO: sysClkEnable(), sysClkDisable()
  71. */
  72. void sysSwClk (void)
  73.     {
  74.     int ix;
  75.     while (sysClkRunning)
  76. {
  77. for (ix = 0; ix < sysClkTicksPerSecond; ix++)
  78.     ;
  79.         if (sysClkRoutine != NULL)
  80.            (*sysClkRoutine) (sysClkArg);
  81. }
  82.     semFlush (semDId);
  83.     }
  84. /*******************************************************************************
  85. *
  86. * sysClkEnable - turn on system clock interrupts
  87. *
  88. * This routine enables system clock interrupts.
  89. *
  90. * RETURNS: N/A
  91. *
  92. * SEE ALSO: sysClkConnect(), sysClkDisable(), sysClkRateSet()
  93. */
  94. void sysClkEnable (void)
  95.     {
  96.     static BOOL initialized = FALSE;
  97.     if (!sysClkRunning)
  98.         {
  99. if (!initialized)
  100.     {
  101.             semDId = semBCreate (SEM_Q_FIFO, SEM_EMPTY);
  102.     initialized = TRUE;
  103.     }
  104. sysClkRunning = TRUE;
  105. taskSpawn ("sysSwClk", SYS_SW_CLK_PRIORITY, VX_NO_STACK_FILL, 2000,
  106.     (FUNCPTR) sysSwClk, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  107. }
  108.     }
  109. /*******************************************************************************
  110. *
  111. * sysClkRateGet - get the system clock rate
  112. *
  113. * This routine returns the interrupt rate of the system clock.
  114. *
  115. * RETURNS: The number of ticks per second of the system clock.
  116. *
  117. * SEE ALSO: sysClkEnable(), sysClkRateSet()
  118. */
  119. int sysClkRateGet (void)
  120.     {
  121.     return (sysClkTicksPerSecond);
  122.     }
  123. /*******************************************************************************
  124. *
  125. * sysClkRateSet - set the system clock rate
  126. *
  127. * This routine sets the interrupt rate of the system clock.  It does not
  128. * enable system clock interrupts.  Normally, it is called by usrRoot() in
  129. * usrConfig.c.
  130. *
  131. * RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be
  132. * set.
  133. *
  134. * SEE ALSO: sysClkEnable(), sysClkRateGet()
  135. */
  136. STATUS sysClkRateSet
  137.     (
  138.     int ticksPerSecond     /* number of clock interrupts per second */
  139.     )
  140.     {
  141.     if (ticksPerSecond < SYS_CLK_RATE_MIN || ticksPerSecond > SYS_CLK_RATE_MAX)
  142. return (ERROR);
  143.     sysClkTicksPerSecond = ticksPerSecond;
  144.     if (sysClkRunning)
  145. {
  146. sysClkDisable ();
  147. sysClkEnable ();
  148. }
  149.     return (OK);
  150.     }