ostimer.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2004 by Jan Dubiec. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY JAN DUBIEC AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JAN DUBIEC
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * $Log$
  31. * Revision 1.1 2005/07/26 18:02:40 haraldkipp
  32. * Moved from dev.
  33. *
  34. * Revision 1.1 2005/05/27 17:18:41 drsung
  35. * Moved the file.
  36. *
  37. * Revision 1.1 2004/03/16 16:48:46 haraldkipp
  38. * Added Jan Dubiec's H8/300 port.
  39. *
  40. */
  41. #include <h83068f.h>
  42. /*!
  43. * \brief Loop for a specified number of milliseconds.
  44. *
  45. * This call will not release the CPU and will
  46. * not switch to another thread. However, because
  47. * of absent thread switching, this delay time is
  48. * very exact.
  49. *
  50. * Use NutSleep() to avoid blocking the CPU, if no
  51. * exact timing is needed.
  52. *
  53. * \param ms Delay time in milliseconds, maximum is 255.
  54. */
  55. void NutDelay(u_char ms)
  56. {
  57. u_short delay_cnt = 1843; //*KU* for 22.118400 MHz Clock
  58. u_short delay_cnt_buffer;
  59. while (ms--) {
  60. delay_cnt_buffer = delay_cnt;
  61. while (delay_cnt_buffer--);
  62. }
  63. }
  64. /*
  65. * Timer 0 interrupt handler.
  66. */
  67. static void NutTimer0Intr(void *arg)
  68. {
  69. NUTTIMERINFO *tnp;
  70. /* Clear timer 0, channnel A compare/match bit */
  71. ITU.TISRA.BIT.IMFA0 = 0;
  72. /*
  73. * Increment the tick counter used by Michael Fischer's
  74. * NutGetTickCount() routine.
  75. */
  76. milli_ticks++;
  77. #ifdef NUT_CPU_FREQ
  78. millis++;
  79. if (ms1++ >= 999) {
  80. ms1 = 0;
  81. seconds++;
  82. }
  83. #else /* NUT_CPU_FREQ */
  84. if (ms62_5 & 1) {
  85. millis += 62;
  86. } else {
  87. millis += 63;
  88. }
  89. /*
  90. * Update RTC. We do a post increment here, because
  91. * of an ImageCraft bug with volatiles. Thanks to
  92. * Michael Fischer.
  93. */
  94. if (ms62_5++ >= 15) {
  95. ms62_5 = 0;
  96. seconds++;
  97. }
  98. #endif /* NUT_CPU_FREQ */
  99. if (nutTimerList) {
  100. if (nutTimerList->tn_ticks_left)
  101. nutTimerList->tn_ticks_left--;
  102. /*
  103. * Process all elapsed timers.
  104. * Bugfix by KU: Avoid crash on empty timer list.
  105. */
  106. while ((nutTimerList != 0) && (nutTimerList->tn_ticks_left == 0)) {
  107. /*
  108. * Execute the callback.
  109. */
  110. if (nutTimerList->tn_callback)
  111. (*nutTimerList->tn_callback) (nutTimerList, (void *) nutTimerList->tn_arg);
  112. /*
  113. * Remove the timer from the list.
  114. */
  115. tnp = nutTimerList;
  116. //fprintf(__os_trs, "RemTmr<%04X>\r\n", tnp);
  117. nutTimerList = nutTimerList->tn_next;
  118. /*
  119. * Periodic timers are re-inserted.
  120. */
  121. if ((tnp->tn_ticks_left = tnp->tn_ticks) != 0)
  122. NutTimerInsert(tnp);
  123. /*
  124. * We can't touch the heap while running in
  125. * interrupt context. Oneshot timers are added
  126. * to a pool of available timers.
  127. */
  128. else {
  129. tnp->tn_next = nutTimerPool;
  130. nutTimerPool = tnp;
  131. //NutKDumpTimerList();
  132. }
  133. }
  134. }
  135. }
  136. #ifndef NUT_CPU_FREQ
  137. /*!
  138. * \brief Compute CPU clock in Hertz.
  139. *
  140. * This function determines the CPU clock by running
  141. * a counter loop between two timer interrupts.
  142. *
  143. * \return CPU clock in Hertz.
  144. *
  145. */
  146. static u_long NutComputeCpuClock(void)
  147. {
  148. return NUT_CPU_FREQ;
  149. }
  150. #endif /* #ifndef NUT_CPU_FREQ */
  151. /*!
  152. * \brief Initialize system timer.
  153. *
  154. * This function is automatically called by Nut/OS
  155. * during system initialization.
  156. *
  157. * Nut/OS uses on-chip timer 0 for its timer services.
  158. * Applications should not modify any registers of this
  159. * timer, but make use of the Nut/OS timer API. Timer 1
  160. * and timer 2 are available to applications.
  161. */
  162. void NutTimerInit(void)
  163. {
  164. #ifdef NUT_CPU_FREQ
  165. /* 16 bit timer ch. 0 high priority */
  166. INTC.IPRA.BIT._ITU0 = 1;
  167. /* auto clear TCNT, clock is phi/4 */
  168. ITU0.TCR.BYTE = 0x22;
  169. /* no output on GRA */
  170. ITU0.TIOR.BYTE = 0x00;
  171. /* generate interrupt every 10ms */
  172. /* ITU0.GRA = 0xd7ff; */
  173. /* generate interrupt every 1ms */
  174. ITU0.GRA = 0x159a;
  175. /* clear counter register */
  176. ITU0.TCNT = 0x0000;
  177. /* enable IMIA0 interrupt */
  178. ITU.TISRA.BIT.IMIEA0 = 1;
  179. NutRegisterIrqHandler(&sig_IMIA0, NutTimer0Intr, 0);
  180. /* start timer 0 */
  181. ITU.TSTR.BIT.STR0 = 1;
  182. #else /* #ifdef NUT_CPU_FREQ */
  183. /* TODO !!! */
  184. #error "NUT_CPU_FREQ must be defined"
  185. #endif /* #ifdef NUT_CPU_FREQ */
  186. }