ostimer_gba.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH. 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 THE COPYRIGHT HOLDERS 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 THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.7 2008/08/22 09:25:33 haraldkipp
  36. * Clock value caching and new functions NutArchClockGet, NutClockGet and
  37. * NutClockSet added.
  38. *
  39. * Revision 1.6 2008/08/11 06:59:12 haraldkipp
  40. * BSD types replaced by stdint types (feature request #1282721).
  41. *
  42. * Revision 1.5 2008/07/08 08:25:04 haraldkipp
  43. * NutDelay is no more architecture specific.
  44. * Number of loops per millisecond is configurable or will be automatically
  45. * determined.
  46. * A new function NutMicroDelay provides shorter delays.
  47. *
  48. * Revision 1.4 2006/10/08 16:48:07 haraldkipp
  49. * Documentation fixed
  50. *
  51. * Revision 1.3 2005/10/24 17:59:19 haraldkipp
  52. * Use correct header file, arm, not gba.
  53. *
  54. * Revision 1.2 2005/08/02 17:46:45 haraldkipp
  55. * Major API documentation update.
  56. *
  57. * Revision 1.1 2005/07/26 18:02:26 haraldkipp
  58. * Moved from dev.
  59. *
  60. * Revision 1.2 2005/07/20 09:17:26 haraldkipp
  61. * Default NUT_CPU_FREQ and NUT_TICK_FREQ added.
  62. * NutTimerIntr() removed, because we can use the hardware independent code.
  63. *
  64. * Revision 1.1 2005/05/27 17:16:40 drsung
  65. * Moved the file.
  66. *
  67. * Revision 1.5 2005/04/05 17:50:46 haraldkipp
  68. * Use register names in gba.h.
  69. *
  70. * Revision 1.4 2004/11/08 19:16:37 haraldkipp
  71. * Hacked in Gameboy timer support
  72. *
  73. * Revision 1.3 2004/10/03 18:42:21 haraldkipp
  74. * No GBA support yet, but let the compiler run through
  75. *
  76. * Revision 1.2 2004/09/08 10:19:39 haraldkipp
  77. * Running on AT91 and S3C, thanks to James Tyou
  78. *
  79. */
  80. #include <cfg/arch.h>
  81. #include <arch/arm.h>
  82. #include <dev/irqreg.h>
  83. /*!
  84. * \addtogroup xgNutArchArmOsTimerGba
  85. */
  86. /*@{*/
  87. #ifndef NUT_CPU_FREQ
  88. #define NUT_CPU_FREQ 1000000UL
  89. #endif
  90. #ifndef NUT_TICK_FREQ
  91. #define NUT_TICK_FREQ 1000UL
  92. #endif
  93. static void (*os_handler) (void *);
  94. /*!
  95. * \brief Timer 0 interrupt entry.
  96. */
  97. void Timer3Entry(void *arg)
  98. {
  99. outw(REG_IF, INT_TMR3);
  100. os_handler(0);
  101. }
  102. /*!
  103. * \brief Initialize system timer.
  104. *
  105. * This function is automatically called by Nut/OS
  106. * during system initialization.
  107. *
  108. * Nut/OS uses on-chip timer 0 for its timer services.
  109. * Applications should not modify any registers of this
  110. * timer, but make use of the Nut/OS timer API. Timer 1
  111. * and timer 2 are available to applications.
  112. */
  113. void NutRegisterTimer(void (*handler) (void *))
  114. {
  115. os_handler = handler;
  116. /* Disable master interrupt. */
  117. outw(REG_IME, 0);
  118. /* Set global interrupt vector. */
  119. NutRegisterIrqHandler(&sig_TMR3, Timer3Entry, 0);
  120. /* Enable timer and timer interrupts. */
  121. outdw(REG_TMR3CNT, TMR_IRQ_ENA | TMR_ENA | 48756);
  122. /* Enable timer 3 interrupts. */
  123. outw(REG_IE, inw(REG_IE) | INT_TMR3);
  124. /* Enable master interrupt. */
  125. outw(REG_IME, 1);
  126. }
  127. /*!
  128. * \brief Return the CPU clock in Hertz.
  129. *
  130. * \return CPU clock frequency in Hertz, which is 16780000 for the GBA.
  131. */
  132. uint32_t NutArchClockGet(int idx)
  133. {
  134. return NUT_CPU_FREQ;
  135. }
  136. /*!
  137. * \brief Return the number of system ticks per second.
  138. *
  139. * \return System tick frequency in Hertz.
  140. */
  141. uint32_t NutGetTickClock(void)
  142. {
  143. return NUT_TICK_FREQ;
  144. }
  145. /*!
  146. * \brief Calculate system ticks for a given number of milliseconds.
  147. */
  148. uint32_t NutTimerMillisToTicks(uint32_t ms)
  149. {
  150. #if (NUT_TICK_FREQ % 1000)
  151. if (ms >= 0x3E8000UL)
  152. return (ms / 1000UL) * NUT_TICK_FREQ;
  153. return (ms * NUT_TICK_FREQ + 999UL) / 1000UL;
  154. #else
  155. return ms * (NUT_TICK_FREQ / 1000UL);
  156. #endif
  157. }
  158. /*@}*/