os_timer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2011 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file arch/arm/dev/zero/os_timer.c
  36. * \brief System timer for Zero CPU.
  37. *
  38. * This code implements a system timer for an imaginary CPU.
  39. * It may serve as a template when porting Nut/OS to a new target.
  40. * Implementers should look out for 'TODO' comments.
  41. *
  42. * \verbatim
  43. * $Id$
  44. * \endverbatim
  45. */
  46. /* The Configurator will create header files with configuration
  47. parameters in nutbld/include/cfg/ (build tree). If nothing specific
  48. has been configured, the compiler will include the original files
  49. from nut/include/cfg/ (source tree). */
  50. #include <cfg/os.h>
  51. #include <cfg/clock.h>
  52. /*
  53. * TODO: Include architecture specific header files, if required.
  54. *
  55. * To implement your timer hardware, you need to access several
  56. * hardware registers and therefore need to include the header files,
  57. * where they are defined. However, if properly configured, this is usually
  58. * done automatically via compiler.h, which in turn is included almost
  59. * anywhere.
  60. *
  61. * Sometimes the deep level of included headers may become tricky to
  62. * follow or something may get broken by later changes. It is legitimate
  63. * to include essential header files, even if they are already included
  64. * indirectly by other header files.
  65. */
  66. #include <sys/timer.h>
  67. /*!
  68. * \addtogroup xgNutArchArmZeroOsTimer
  69. */
  70. /*@{*/
  71. #ifndef NUT_TICK_FREQ
  72. /*!
  73. * \brief System timer interrupt frequency.
  74. *
  75. * Specifies the number of interrupts per second, typically 1000.
  76. * In order to reduce overhead, you may choose lower values. Note,
  77. * that Nut/OS API timer values are given in milliseconds. Thus,
  78. * lower values will reduce the available resolution, while
  79. * larger values may not provide any benefit.
  80. */
  81. #define NUT_TICK_FREQ 1000UL
  82. #endif
  83. /*!
  84. * \brief Initialize system timer.
  85. *
  86. * Applications must not call this function.
  87. *
  88. * It is automatically called by Nut/OS during initialization to register
  89. * the system timer interrupt handler. It is an essential part of the
  90. * hardware dependant code and must be available for any platform that is
  91. * running Nut/OS.
  92. *
  93. * The number of system timer interrupts is define by \ref NUT_TICK_FREQ.
  94. *
  95. * Timer interrupts are enabled when this function returns.
  96. *
  97. * \param handler This routine should be called each time, when a
  98. * system timer interrupt occurs.
  99. */
  100. void NutRegisterTimer(void (*handler) (void *))
  101. {
  102. /*
  103. * TODO: Select the timer hardware.
  104. *
  105. * Note, that this timer will no longer be available for
  106. * applications.
  107. */
  108. /*
  109. * TODO: Initialize the timer hardware.
  110. *
  111. * It is recommended to use NutClockGet() to determine the input
  112. * clock frequency.
  113. */
  114. /*
  115. * TODO: Enable timer interrupts.
  116. *
  117. * You may, but do not need to use NutRegisterIrqHandler() and
  118. * NutIrqEnable(). Implementing Nut/OS interrupt handler
  119. * registration is not always trivial, therefore you may hard code
  120. * the handler call for a first port. However, many platform
  121. * independent drivers use it as well and sooner or later you need
  122. * to implement interrupt handling anyway.
  123. */
  124. }
  125. #ifndef NUT_CPU_FREQ
  126. /*!
  127. * \brief Return the specified clock frequency.
  128. *
  129. * Applications must not call this function, but use NutClockGet()
  130. * instead.
  131. *
  132. * Simple implementations may not provide this function, in which case
  133. * \ref NUT_CPU_FREQ must define the CPU frequency in Hertz. This can
  134. * be done in the Configurator.
  135. *
  136. * \param idx This zero based index specifies the clock to retrieve. The
  137. * number of available hardware clocks depends on the target
  138. * harware and is specified by NUT_HWCLK_MAX + 1. Typically
  139. * \ref NUT_HWCLK_CPU is used to retrieve the current CPU
  140. * clock. Additional indices may be available to retrieve one
  141. * or more peripheral clocks or a special slow clock.
  142. *
  143. * \return Clock frequency in Hertz.
  144. */
  145. uint32_t NutArchClockGet(int idx)
  146. {
  147. /* TODO: Calculate the specified clock from current register settings. */
  148. return 100000000;
  149. }
  150. #endif
  151. /*!
  152. * \brief Return the number of system ticks per second.
  153. *
  154. * This routine is used by Nut/OS to convert tick counts into
  155. * milliseconds.
  156. *
  157. * Applications typically do not deal with system ticks. Instead,
  158. * they use milliseconds to specify timeouts or call NutGetMillis()
  159. * and NutGetSeconds() to retrieve an elapsed time.
  160. *
  161. * \return System tick frequency in Hertz, typically the value of
  162. * \ref NUT_TICK_FREQ.
  163. */
  164. uint32_t NutGetTickClock(void)
  165. {
  166. return NUT_TICK_FREQ;
  167. }
  168. /*!
  169. * \brief Calculate system ticks for a given number of milliseconds.
  170. *
  171. * This routine is used by Nut/OS to retrieve the number of system
  172. * ticks for a given timeout.
  173. *
  174. * \param ms Number of milliseconds.
  175. *
  176. * \return Number of system ticks. The resolution is limited to the
  177. * granularity of the system timer.
  178. */
  179. uint32_t NutTimerMillisToTicks(uint32_t ms)
  180. {
  181. return (ms * NutGetTickClock()) / 1000;
  182. }
  183. /*@}*/