mcf5225x_ostimer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2012 by Embedded Technologies s.r.o
  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. #include <cfg/os.h>
  33. #include <cfg/clock.h>
  34. #include <arch/m68k.h>
  35. #include <dev/irqreg.h>
  36. #include <sys/timer.h>
  37. /*!
  38. * \addtogroup xgNutArchM68kMCF5225XOsTimer
  39. */
  40. /*@{*/
  41. #ifndef NUT_TICK_FREQ
  42. /*!
  43. * \brief System timer interrupt frequency.
  44. *
  45. * Specifies the number of interrupts per second, typically 1000.
  46. * In order to reduce overhead, you may choose lower values. Note,
  47. * that Nut/OS API timer values are given in milliseconds. Thus,
  48. * lower values will reduce the available resolution, while
  49. * larger values may not provide any benefit.
  50. */
  51. #define NUT_TICK_FREQ 1000UL
  52. #endif
  53. #define EXT_CLOCK_HZ 48000000UL
  54. #define CHANEL 0 /* PIT0 */
  55. /*!
  56. * \brief Initialize system timer.
  57. *
  58. * Applications must not call this function.
  59. *
  60. * It is automatically called by Nut/OS during initialization to register
  61. * the system timer interrupt handler. It is an essential part of the
  62. * hardware dependant code and must be available for any platform that is
  63. * running Nut/OS.
  64. *
  65. * The number of system timer interrupts is define by \ref NUT_TICK_FREQ.
  66. *
  67. * Timer interrupts are enabled when this function returns.
  68. *
  69. * \param handler This routine should be called each time, when a
  70. * system timer interrupt occurs.
  71. */
  72. void NutRegisterTimer(void (*handler) (void *))
  73. {
  74. /*
  75. * Note, that this timer will no longer be available for
  76. * applications.
  77. */
  78. uint8_t prescaler = 0;
  79. uint32_t modulus;
  80. /*
  81. * It is recommended to use NutClockGet() to determine the input
  82. * clock frequency.
  83. *
  84. * Count PIT Count Register value.
  85. * Timeout Frequency = fsys/2 / (2^PCSRn[PRE] * (PMRn[PM] + 1)) in Hertz
  86. * PMRn[PM] = (fsys/2 / (2^PCSRn[PRE] * NUT_TICK_FREQ)) - 1
  87. */
  88. modulus = NutClockGet() / 2;
  89. modulus /= 1 << prescaler;
  90. modulus /= NUT_TICK_FREQ;
  91. modulus--;
  92. /* The prescaler bits select the internal bus clock divisor to generate the PIT clock. */
  93. MCF_PIT_PCSR(CHANEL) = 0x00 | MCF_PIT_PCSR_PRE(prescaler);
  94. /* Set PIT Count Register value. */
  95. MCF_PIT_PMR(CHANEL) = modulus;
  96. /* Enable reload for periodic mode */
  97. MCF_PIT_PCSR(CHANEL) |= MCF_PIT_PCSR_RLD;
  98. /* Init Interrupt */
  99. NutRegisterIrqHandler(&sig_PIT0, handler, NULL);
  100. /*
  101. * Enable PIT interrup and clear interrupt flag
  102. * NOTE: Interrupt is still disabled by interrupt controller.
  103. */
  104. MCF_PIT_PCSR(CHANEL) |= MCF_PIT_PCSR_PIE | MCF_PIT_PCSR_PIF;
  105. /* Enable timer */
  106. MCF_PIT_PCSR(CHANEL) |= MCF_PIT_PCSR_EN | MCF_PIT_PCSR_DBG;
  107. }
  108. #ifndef NUT_CPU_FREQ
  109. /*!
  110. * \brief Return the specified clock frequency.
  111. *
  112. * Applications must not call this function, but use NutClockGet()
  113. * instead.
  114. *
  115. * Simple implementations may not provide this function, in which case
  116. * \ref NUT_CPU_FREQ must define the CPU frequency in Hertz. This can
  117. * be done in the Configurator.
  118. *
  119. * \param idx This zero based index specifies the clock to retrieve. The
  120. * number of available hardware clocks depends on the target
  121. * harware and is specified by NUT_HWCLK_MAX + 1. Typically
  122. * \ref NUT_HWCLK_CPU is used to retrieve the current CPU
  123. * clock. Additional indices may be available to retrieve one
  124. * or more peripheral clocks or a special slow clock.
  125. *
  126. * \return Clock frequency in Hertz.
  127. */
  128. uint32_t NutArchClockGet(int idx)
  129. {
  130. int fout, fref, mfd, rfd, divider, lpdr;
  131. /* The PLL pre divider - 48MHz / (CCHR + 1) = 8MHz */
  132. divider = MCF_CLOCK_CCHR + 1;
  133. fref = EXT_CLOCK_HZ / divider;
  134. /* The LPDR divides down the system clock by a factor of 2^LPDR. */
  135. lpdr = 1 << MCF_CLOCK_LPDR;
  136. mfd = (MCF_CLOCK_SYNCR & 0x7000) >> 12;
  137. mfd = 2 * (mfd + 2); // MIN_MFD
  138. rfd = (MCF_CLOCK_SYNCR & 0x0700) >> 8;
  139. rfd = 1 << rfd;
  140. /* Return current PLL output */
  141. if ((MCF_CLOCK_SYNCR & MCF_CLOCK_SYNCR_CLKSRC) && (MCF_CLOCK_SYNCR & MCF_CLOCK_SYNCR_PLLEN)) {
  142. fout = (fref * (mfd / rfd)) / lpdr;
  143. } else {
  144. fout = fref / lpdr;
  145. }
  146. return fout;
  147. }
  148. #endif
  149. /*!
  150. * \brief Return the number of system ticks per second.
  151. *
  152. * This routine is used by Nut/OS to convert tick counts into
  153. * milliseconds.
  154. *
  155. * Applications typically do not deal with system ticks. Instead,
  156. * they use milliseconds to specify timeouts or call NutGetMillis()
  157. * and NutGetSeconds() to retrieve an elapsed time.
  158. *
  159. * \return System tick frequency in Hertz, typically the value of
  160. * \ref NUT_TICK_FREQ.
  161. */
  162. uint32_t NutGetTickClock(void)
  163. {
  164. return NUT_TICK_FREQ;
  165. }
  166. /*!
  167. * \brief Calculate system ticks for a given number of milliseconds.
  168. *
  169. * This routine is used by Nut/OS to retrieve the number of system
  170. * ticks for a given timeout.
  171. *
  172. * \param ms Number of milliseconds.
  173. *
  174. * \return Number of system ticks. The resolution is limited to the
  175. * granularity of the system timer.
  176. */
  177. uint32_t NutTimerMillisToTicks(uint32_t ms)
  178. {
  179. return (ms * NutGetTickClock()) / 1000;
  180. }
  181. /*@}*/