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

VxWorks

开发平台:

C/C++

  1. /* m68340Timer.c - MC68340 timer library */
  2. /* Copyright 1984-1992 Wind River Systems, Inc. */
  3. #include "copyright_wrs.h"
  4. /*
  5. modification history
  6. --------------------
  7. 01a,07aug92,caf  moved clock routines from ver 01i of evs340/sysLib.c, ansified.
  8. */
  9. /*
  10. DESCRIPTION
  11. This library contains routines to manipulate the timer functions on the
  12. Motorola MC68340.  This library handles both the system clock and the auxiliary
  13. clock functions.
  14. The macros SYS_CLK_RATE_MIN, SYS_CLK_RATE_MAX, AUX_CLK_RATE_MIN, and
  15. AUX_CLK_RATE_MAX must be defined to provide parameter checking for the
  16. sys[Aux]ClkRateSet() routines.
  17. */
  18. /* locals */
  19. LOCAL FUNCPTR sysClkRoutine = NULL; /* routine to call on clock interrupt */
  20. LOCAL int sysClkArg = NULL; /* its argument */
  21. LOCAL int sysClkRunning = FALSE;
  22. LOCAL int sysClkConnected = FALSE;
  23. LOCAL int sysClkTicksPerSecond = 60;
  24. LOCAL int sysClkPITR = 0x182;/* 512 prescale... 60.09 Hz */
  25. LOCAL FUNCPTR sysAuxClkRoutine = NULL; /* routine to call on clock interrupt */
  26. LOCAL int sysAuxClkArg = NULL; /* its argument */
  27. LOCAL int sysAuxClkRunning = FALSE;
  28. LOCAL int sysAuxClkConnected = FALSE;
  29. LOCAL int auxClkTicksPerSecond = 60;
  30. /*******************************************************************************
  31. *
  32. * sysClkInt - handle system clock interrupts
  33. *
  34. * This routine handles system clock interrupts.
  35. *
  36. * RETURNS: N/A
  37. */
  38. LOCAL void sysClkInt (void)
  39.     {
  40.     if (sysClkRoutine != NULL)
  41.         (*sysClkRoutine) (sysClkArg);
  42.     }
  43. /*******************************************************************************
  44. *
  45. * sysClkConnect - connect a routine to the system clock interrupt
  46. *
  47. * This routine specifies the interrupt service routine to be called at each
  48. * clock interrupt.  It does not enable system clock interrupts.  Normally,
  49. * it is called from usrRoot() in usrConfig.c to connect usrClock() to the
  50. * system clock interrupt.
  51. *
  52. * RETURNS: OK, or ERROR if the routine cannot be connected to the interrupt.
  53. *
  54. * SEE ALSO: intConnect(), usrClock(), sysClkEnable()
  55. */
  56. STATUS sysClkConnect
  57.     (
  58.     FUNCPTR routine,    /* routine called at each system clock interrupt */
  59.     int arg             /* argument with which to call routine           */
  60.     )
  61.     {
  62.     sysHwInit2 (); /* XXX for now -- needs to be in usrConfig.c */
  63.     sysClkRoutine   = routine;
  64.     sysClkArg       = arg;
  65.     sysClkConnected = TRUE;
  66.     return (OK);
  67.     }
  68. /*******************************************************************************
  69. *
  70. * sysClkDisable - turn off system clock interrupts
  71. *
  72. * This routine disables system clock interrupts.
  73. *
  74. * RETURNS: N/A
  75. *
  76. * SEE ALSO: sysClkEnable()
  77. */
  78. void sysClkDisable (void)
  79.     {
  80.     if (sysClkRunning)
  81. {
  82. *M340_SIM_PICR &= ~(SIM_PICR_PIRQL_MASK); /* disable interrupts */
  83. sysClkRunning = FALSE; /* clock is stopped */
  84. }
  85.     }
  86. /*******************************************************************************
  87. *
  88. * sysClkEnable - turn on system clock interrupts
  89. *
  90. * This routine enables system clock interrupts.
  91. *
  92. * RETURNS: N/A
  93. *
  94. * SEE ALSO: sysClkConnect(), sysClkDisable(), sysClkRateSet()
  95. */
  96. void sysClkEnable (void)
  97.     {
  98.     if (!sysClkRunning)
  99. {
  100. *M340_SIM_PITR = sysClkPITR; /* set clock rate */
  101. *M340_SIM_PICR |= INT_LVL_SIM << 8; /* enable interrupts */
  102. sysClkRunning = TRUE;
  103. }
  104.     }
  105. /*******************************************************************************
  106. *
  107. * sysClkRateGet - get the system clock rate
  108. *
  109. * This routine returns the interrupt rate of the system clock.
  110. *
  111. * RETURNS: The number of ticks per second of the system clock.
  112. *
  113. * SEE ALSO: sysClkEnable(), sysClkRateSet()
  114. */
  115. int sysClkRateGet (void)
  116.     {
  117.     return (sysClkTicksPerSecond);
  118.     }
  119. /*******************************************************************************
  120. *
  121. * sysClkRateSet - set the system clock rate
  122. *
  123. * This routine sets the interrupt rate of the system clock.  It does not
  124. * enable system clock interrupts.  Normally, it is called by usrRoot() in
  125. * usrConfig.c.
  126. *
  127. * RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be set.
  128. *
  129. * SEE ALSO: sysClkEnable(), sysClkRateGet()
  130. */
  131. STATUS sysClkRateSet
  132.     (
  133.     int ticksPerSecond     /* number of clock interrupts per second */
  134.     )
  135.     {
  136.     FAST int count;
  137.     if (ticksPerSecond < SYS_CLK_RATE_MIN || ticksPerSecond > SYS_CLK_RATE_MAX)
  138.         return (ERROR);
  139.     /* round to best PITR count value using integer arithmetic */
  140.     count = ((((10 * SYS_EXTAL_FREQ) / (4 * ticksPerSecond)) + 5) / 10);
  141.     if (count < 256)
  142.         sysClkPITR = count;
  143.     else
  144. {
  145. count = ((((10 * SYS_EXTAL_FREQ) / (2048 * ticksPerSecond)) + 5) / 10);
  146.         sysClkPITR = SIM_PITR_PTP | count;
  147. }
  148.     sysClkTicksPerSecond = ticksPerSecond;
  149.     if (sysClkRunning)
  150.         {
  151.         sysClkDisable ();
  152.         sysClkEnable ();
  153.         }
  154.     return (OK);
  155.     }
  156. /*******************************************************************************
  157. *
  158. * sysAuxClkInt - interrupt level processing for auxiliary clock
  159. *
  160. * This routine handles the auxiliary clock interrupt.  It is attached to the
  161. * clock interrupt vector by the routine sysAuxClkConnect().
  162. *
  163. * RETURNS: N/A
  164. */
  165. LOCAL void sysAuxClkInt (void)
  166.     {
  167.     /* write 1's to clear these bits. only using the TO interrupt */
  168.     *M340_TMR1_SR    |= (TMR_SR_TC | TMR_SR_TG | TMR_SR_TO);
  169.     if (sysAuxClkRoutine != NULL)
  170.         (*sysAuxClkRoutine) (sysAuxClkArg);
  171.     }
  172. /*******************************************************************************
  173. *
  174. * sysAuxClkConnect - connect a routine to the auxiliary clock interrupt
  175. *
  176. * This routine specifies the interrupt service routine to be called at each
  177. * auxiliary clock interrupt.  It does not enable auxiliary clock
  178. * interrupts.
  179. *
  180. * RETURNS: OK, or ERROR if the routine cannot be connected to the interrupt.
  181. *
  182. * SEE ALSO: intConnect(), sysAuxClkEnable()
  183. */
  184. STATUS sysAuxClkConnect
  185.     (
  186.     FUNCPTR routine,    /* routine called at each aux clock interrupt    */
  187.     int arg             /* argument to auxiliary clock interrupt routine */
  188.     )
  189.     {
  190.     if (!sysAuxClkConnected &&
  191.         (intConnect (INUM_TO_IVEC (INT_VEC_TMR1), sysAuxClkInt, 0) == ERROR))
  192.         {
  193.         return (ERROR);
  194.         }
  195.     sysAuxClkConnected = TRUE;
  196.     sysAuxClkRoutine   = routine;
  197.     sysAuxClkArg       = arg;
  198.     return (OK);
  199.     }
  200. /*******************************************************************************
  201. *
  202. * sysAuxClkDisable - turn off auxiliary clock interrupts
  203. *
  204. * This routine disables auxiliary clock interrupts.
  205. *
  206. * RETURNS: N/A
  207. *
  208. * SEE ALSO: sysAuxClkEnable()
  209. */
  210. void sysAuxClkDisable (void)
  211.     {
  212.     *M340_TMR1_CR  = 0; /* reset timer 1 */
  213.     /* write 1's to clear these bits. only using the TO interrupt */
  214.     *M340_TMR1_SR    |= (TMR_SR_TC | TMR_SR_TG | TMR_SR_TO);
  215.     sysAuxClkRunning = FALSE;
  216.     }
  217. /*******************************************************************************
  218. *
  219. * sysAuxClkEnable - turn on auxiliary clock interrupts
  220. *
  221. * This routine enables auxiliary clock interrupts.
  222. *
  223. * RETURNS: N/A
  224. *
  225. * SEE ALSO: sysAuxClkConnect(), sysAuxClkDisable(), sysAuxClkRateSet()
  226. */
  227. void sysAuxClkEnable (void)
  228.     {
  229.     /* initiate software reset of timer 1 */
  230.     *M340_TMR1_CR    &= ~TMR_CR_SWR;
  231.     *M340_TMR1_SR    |= (TMR_SR_TC | TMR_SR_TG | TMR_SR_TO);
  232.     /* fill in the preload value of timer 1 */
  233.     *M340_TMR1_PREL1 = ((10 * SYS_CLOCK_FREQ) /
  234.                         (512 * auxClkTicksPerSecond) + 5) / 10 - 1;
  235.     /* configure timer 1 with x256 prescale and timeout interrupt */
  236.     *M340_TMR1_CR = TMR_CR_PSE | TMR_CR_CPE | TMR_CR_X256 | TMR_CR_TO;
  237.     /* release software reset of timer 1 */
  238.     *M340_TMR1_CR |= TMR_CR_SWR;
  239.     sysAuxClkRunning = TRUE;
  240.     }
  241. /*******************************************************************************
  242. *
  243. * sysAuxClkRateGet - get the auxiliary clock rate
  244. *
  245. * This routine returns the interrupt rate of the auxiliary clock.
  246. *
  247. * RETURNS: The number of ticks per second of the auxiliary clock.
  248. *
  249. * SEE ALSO: sysAuxClkEnable(), sysAuxClkRateSet()
  250. */
  251. int sysAuxClkRateGet (void)
  252.     {
  253.     return (auxClkTicksPerSecond);
  254.     }
  255. /*******************************************************************************
  256. *
  257. * sysAuxClkRateSet - set auxiliary clock rate
  258. *
  259. * This routine sets the interrupt rate of the auxiliary clock.  It does not
  260. * enable auxiliary clock interrupts.
  261. *
  262. * RETURNS: OK, or ERROR if the tick rate is invalid or the timer cannot be set.
  263. *
  264. * SEE ALSO: sysAuxClkEnable(), sysAuxClkRateGet()
  265. */
  266. STATUS sysAuxClkRateSet
  267.     (
  268.     int ticksPerSecond     /* number of clock interrupts per second */
  269.     )
  270.     {
  271.     if (ticksPerSecond < AUX_CLK_RATE_MIN || ticksPerSecond > AUX_CLK_RATE_MAX)
  272.         return (ERROR);
  273.     auxClkTicksPerSecond = ticksPerSecond;
  274.     if (sysAuxClkRunning)
  275.         {
  276.         sysAuxClkDisable ();
  277.         sysAuxClkEnable ();
  278.         }
  279.     return (OK);
  280.     }