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

VxWorks

开发平台:

C/C++

  1. /* sunEcTimer.c - Sun EC Timer library */
  2. /* Copyright 1984-1994 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 02c,19sep94,vin  fixed a bug in the sysAuxClkEnable, changed copyright.
  8.  removed the SUN_COUNTER_1 reset (SPR #3426)
  9. 02b,19sep94,vin  fixed a bug in the sysClkEnable,
  10.  changed the SUN_IR_PENDING to SUN_IR_MASK_SET (SPR #3400)
  11. 01c,11jun93,ccc  moved intConnect() to sysHwInit2().
  12. 01b,13may93,ccc  moved to final directory.
  13. 01a,17mar93,ccc  created.
  14.     26apr93,ccc  changed path to build in sunec directory.
  15. */
  16. /*
  17. DESCRIPTION
  18. This library contains routines to manipulate the timer functions on the
  19. sun1e with a board-independant interface.  This library handles both
  20. the system clock and the auxiliary clock functions.
  21. The macros SYS_CLK_RATE_MIN, SYS_CLK_RATE_MAX, AUX_CLK_RATE_MIN, and
  22. AUX_CLK_RATE_MAX must be defined to provide parameter checking for the
  23. sys[Aux]ClkRateSet() routines.
  24. */
  25. #include "drv/timer/timerDev.h"
  26. IMPORT void sysClkAck ();
  27. IMPORT void sysAuxClkAck ();
  28. /* Locals */ 
  29. LOCAL int     sysClkTicksPerSecond = 60; 
  30. LOCAL BOOL    sysClkRunning        = FALSE; 
  31. LOCAL FUNCPTR sysClkRoutine        = NULL; 
  32. LOCAL int     sysClkArg            = NULL; 
  33. LOCAL BOOL    sysClkConnected      = FALSE; 
  34. LOCAL int     auxClkTicksPerSecond = 60; 
  35. LOCAL BOOL    sysAuxClkRunning     = FALSE; 
  36. LOCAL FUNCPTR sysAuxClkRoutine     = NULL; 
  37. LOCAL int     sysAuxClkArg         = NULL;
  38. LOCAL BOOL    sysAuxClkConnected   = FALSE; 
  39. /**************************************************************************
  40. *
  41. * sysClkInt - clock interrupt handler
  42. *
  43. * This routine handles the clock interrupt.  It is attached to the clock
  44. * interrupt vector by the routine sysClkConnect().  The appropriate routine
  45. * is called and the interrupts are acknowleged.
  46. */
  47. LOCAL void sysClkInt (void)
  48.     {
  49.     if (sysClkRoutine != NULL)
  50.         (*(FUNCPTR) sysClkRoutine) (sysClkArg);
  51.     sysClkAck ();
  52.     }
  53. /***************************************************************************
  54. *
  55. * sysAuxClkInt - clock interrupt handler
  56. *
  57. * This routine handles the clock interrupt.  It is attached to the clock
  58. * interrupt vector by the routine sysAuxClkConnect().  The appropriate
  59. * routine is called and the interrupts are acknowleged.
  60. */
  61. LOCAL void sysAuxClkInt (void)
  62.     {
  63.     if (sysAuxClkRoutine != NULL)
  64.         (*(FUNCPTR) sysAuxClkRoutine) (sysAuxClkArg);
  65.     sysAuxClkAck ();
  66.     }
  67. /***************************************************************************
  68. *
  69. * sysClkConnect - connect a routine to the system clock interrupt
  70. *
  71. * This routine specifies the interrupt handler to be called at each clock
  72. * interrupt.  It does not enable system clock interrupts.  Normally, it is
  73. * called from usrRoot() in usrConfig.c to connect usrClock() to the system
  74. * clock interrupt.
  75. *
  76. * RETURNS: OK, or ERROR if the routine cannot be connected to the interrupt.
  77. *
  78. * SEE ALSO: usrClock(), sysClkEnable()
  79. */
  80. STATUS sysClkConnect
  81.     (
  82.     FUNCPTR routine,    /* routine called at each system clock interrupt */
  83.     int     arg         /* argument with which to call routine           */
  84.     )
  85.     {
  86.     sysHwInit2 ();
  87.     sysClkConnected   = TRUE;
  88.     sysClkRoutine     = routine;
  89.     sysClkArg         = arg;
  90.     return (OK);
  91.     }
  92.  
  93. /***************************************************************************
  94. *
  95. * sysClkDisable - turn off system clock interrupts
  96. *
  97. * This routine disables system clock interrupts.
  98. *
  99. * RETURNS: N/A
  100. *
  101. * SEE ALSO: sysClkEnable()
  102. */
  103.  
  104. void sysClkDisable (void)
  105.  
  106.     {
  107.     if (sysClkRunning)
  108.         {
  109.         *SUN_IR_MASK_SET = IR_ALL;     /* turn off interrupts */
  110.         *SUN_IR_MASK_SET = IR_CLOCK10; /* turn off lvl 10 */
  111.         *SUN_IR_MASK_CLR = IR_ALL;     /* turn on interrupts */
  112.         sysClkRunning    = FALSE;
  113.         }
  114.     }
  115. /***************************************************************************
  116. *
  117. * sysClkEnable - turn on system clock interrupts
  118. *
  119. * This routine enables system clock interrupts.
  120. *
  121. * RETURNS: N/A
  122. *
  123. * SEE ALSO: sysClkDisable(), sysClkRateSet()
  124. */
  125. void sysClkEnable (void)
  126.     {
  127.     unsigned int intRegister;
  128.     if (!sysClkRunning)
  129.         {
  130.         *SUN_IR_MASK_SET = IR_ALL;       /* turn off interrupts */
  131.         intRegister      = *SUN_LIMIT_0; /* Clear interrupt at clock */
  132.         *SUN_IR_MASK_SET = IR_CLOCK10;   /* Clear interrupt pending */
  133.         *SUN_IR_MASK_CLR = IR_CLOCK10;   /* turn on lvl 10 */
  134. *SUN_LIMIT_0     = (1000000 / sysClkTicksPerSecond) << 10;
  135.         *SUN_IR_MASK_CLR = IR_ALL;       /* turn on interrupts */
  136.         sysClkRunning    = TRUE;
  137.         }
  138.     }
  139. /***************************************************************************
  140. *
  141. * sysClkRateGet - get the system clock rate
  142. *
  143. * This routine returns the interrupt rate of the system clock.
  144. *
  145. * RETURNS: The number of ticks per second of the system clock.
  146. *
  147. * SEE ALSO: sysClkEnable(), sysClkRateSet()
  148. */
  149. int sysClkRateGet (void)
  150.     {
  151.     return (sysClkTicksPerSecond);
  152.     }
  153. /***************************************************************************
  154. *
  155. * sysClkRateSet - set the system clock rate
  156. *
  157. * This routine sets the interrupt rate of the system clock.  It does not
  158. * enable system clock interrupts.  Normally, it is called by usrRoot() in
  159. * usrConfig.c.
  160. *
  161. * RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be set.
  162. *
  163. * SEE ALSO: sysClkEnable(), sysClkRateGet()
  164. */
  165. STATUS sysClkRateSet
  166.     (
  167.     int ticksPerSecond      /* number of clock interrupts per second */
  168.     )
  169.     {
  170.     if (ticksPerSecond < SYS_CLK_RATE_MIN || ticksPerSecond > SYS_CLK_RATE_MAX)
  171. return (ERROR);
  172.     sysClkTicksPerSecond = ticksPerSecond;
  173.     if (sysClkRunning)
  174. {
  175. sysClkDisable ();
  176. sysClkEnable ();
  177. }
  178.     return (OK);
  179.     }
  180. /***************************************************************************
  181. *
  182. * sysAuxClkConnect - connect a routine to the auxiliary clock interrupt
  183. *
  184. * This routine specifies the interrupt handler to be called at each
  185. * auxiliary clock interrupt.  It does not enable auxiliary clock
  186. * interrupts.
  187. *
  188. * RETURNS: OK, or ERROR if the routine cannot be connected to the interrupt.
  189. *
  190. * SEE ALSO: sysAuxClkDisconnect(), sysAuxClkEnable()
  191. */
  192. STATUS sysAuxClkConnect
  193.     (
  194.     FUNCPTR routine,    /* routine called at each aux. clock interrupt */
  195.     int     arg         /* argument with which to call routine         */
  196.     )
  197.     {
  198.     sysAuxClkConnected = TRUE;
  199.     sysAuxClkRoutine   = routine;
  200.     sysAuxClkArg       = arg;
  201.     return (OK);
  202.     }
  203. /***************************************************************************
  204. *
  205. * sysAuxClkDisconnect - clear the auxiliary clock routine
  206. *
  207. * This routine disables the auxiliary clock interrupt, stops the timer,
  208. * and disconnects the routine currently connected to the auxiliary clock
  209. * interrupt.
  210. *
  211. * RETURNS: N/A
  212. *
  213. * SEE ALSO: sysAuxClkConnect(), sysAuxClkEnable()
  214. */
  215. void sysAuxClkDisconnect (void)
  216.     {
  217.     /* disable the auxiliary clock interrupt */
  218.     sysAuxClkDisable ();
  219.     /* connect dummy routine, just in case */
  220.     sysAuxClkConnect ((FUNCPTR) logMsg, (int) "auxiliary clock interruptn");
  221.     }
  222. /***************************************************************************
  223. *
  224. * sysAuxClkDisable - turn off auxiliary clock interrupts
  225. *
  226. * This routine disables auxiliary clock interrupts.
  227. *
  228. * RETURNS: N/A
  229. *
  230. * SEE ALSO: sysAuxClkEnable()
  231. */
  232. void sysAuxClkDisable (void)
  233.     {
  234.     if (sysAuxClkRunning)
  235.         {
  236.         *SUN_COUNTER_1_CR = 1;      /* disable timer */
  237.         sysAuxClkRunning = FALSE;
  238.         }
  239.     }
  240. /***************************************************************************
  241. *
  242. * sysAuxClkEnable - turn on auxiliary clock interrupts
  243. *
  244. * This routine enables auxiliary clock interrupts.
  245. *
  246. * RETURNS: N/A
  247. *
  248. * SEE ALSO: sysAuxClkDisable(), sysAuxClkRateSet()
  249. */
  250. void sysAuxClkEnable (void)
  251.     {
  252.     if (!sysAuxClkRunning)
  253.         {
  254. *SUN_COUNTER_1_CR = 0; /* enable timer */
  255. *SUN_LIMIT_1      = (1000000 / auxClkTicksPerSecond) << 10;
  256.         sysAuxClkRunning  = TRUE;
  257.         }
  258.     }
  259. /***************************************************************************
  260. *
  261. * sysAuxClkRateGet - get the auxiliary clock rate
  262. *
  263. * This routine returns the interrupt rate of the auxiliary clock.
  264. *
  265. * RETURNS: The number of ticks per second of the auxiliary clock.
  266. *
  267. * SEE ALSO: sysAuxClkEnable(), sysAuxClkRateSet()
  268. */
  269. int sysAuxClkRateGet (void)
  270.     {
  271.     return (auxClkTicksPerSecond);
  272.     }
  273. /***************************************************************************
  274. *
  275. * sysAuxClkRateSet - set the auxiliary clock rate
  276. *
  277. * This routine sets the interrupt rate of the auxiliary clock.  It does not
  278. * enable auxiliary clock interrupts.
  279. *
  280. * RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be set.
  281. *
  282. * SEE ALSO: sysAuxClkEnable(), sysAuxClkRateGet()
  283. */
  284. STATUS sysAuxClkRateSet
  285.     (
  286.     int ticksPerSecond      /* number of clock interrupts per second */
  287.     )
  288.     {
  289.     if (ticksPerSecond < AUX_CLK_RATE_MIN || ticksPerSecond > AUX_CLK_RATE_MAX)
  290. return (ERROR);
  291.     auxClkTicksPerSecond = ticksPerSecond;
  292.     if (sysAuxClkRunning)
  293. {
  294. sysAuxClkDisable ();
  295. sysAuxClkEnable ();
  296. }
  297.     return (OK);
  298.     }