timer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef _SYS_TIMER_H
  2. #define _SYS_TIMER_H
  3. /*
  4. * Copyright (C) 2001-2006 by egnite Software GmbH. 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. * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
  36. *
  37. * This file is distributed in the hope that it will be useful, but WITHOUT
  38. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  39. * FITNESS FOR A PARTICULAR PURPOSE.
  40. *
  41. * You can redistribute this file and/or modify it under the terms of the GNU
  42. * General Public License (GPL) as published by the Free Software Foundation;
  43. * either version 2 of the License, or (at your discretion) any later version.
  44. * See the accompanying file "copying-gpl.txt" for more details.
  45. *
  46. * As a special exception to the GPL, permission is granted for additional
  47. * uses of the text contained in this file. See the accompanying file
  48. * "copying-liquorice.txt" for details.
  49. */
  50. /*
  51. * $Log$
  52. * Revision 1.13 2008/08/22 09:25:34 haraldkipp
  53. * Clock value caching and new functions NutArchClockGet, NutClockGet and
  54. * NutClockSet added.
  55. *
  56. * Revision 1.12 2008/08/11 07:00:28 haraldkipp
  57. * BSD types replaced by stdint types (feature request #1282721).
  58. *
  59. * Revision 1.11 2008/07/08 08:25:05 haraldkipp
  60. * NutDelay is no more architecture specific.
  61. * Number of loops per millisecond is configurable or will be automatically
  62. * determined.
  63. * A new function NutMicroDelay provides shorter delays.
  64. *
  65. * Revision 1.10 2006/06/28 14:36:34 haraldkipp
  66. * Event/thread/timer re-design.
  67. *
  68. * Revision 1.9 2005/07/26 16:04:02 haraldkipp
  69. * Hardware dependent timer code moved to nutlibarch.
  70. *
  71. * Revision 1.8 2005/07/12 16:36:53 freckle
  72. * made NutTimerInsert public
  73. *
  74. * Revision 1.7 2005/07/12 16:26:08 freckle
  75. * extracted timer creation from NutTimerStartTicks into NutTimerCreate
  76. *
  77. * Revision 1.6 2005/06/12 16:54:14 haraldkipp
  78. * New function processes elapsed timers.
  79. *
  80. * Revision 1.5 2005/01/19 17:59:42 freckle
  81. * Improved interrupt performance by reducing some critical section
  82. *
  83. * Revision 1.4 2004/03/16 16:48:44 haraldkipp
  84. * Added Jan Dubiec's H8/300 port.
  85. *
  86. * Revision 1.3 2003/11/03 16:35:18 haraldkipp
  87. * New API returns system uptime in miliseconds
  88. *
  89. * Revision 1.2 2003/10/13 10:17:11 haraldkipp
  90. * Seconds counter added
  91. *
  92. * Revision 1.1.1.1 2003/05/09 14:41:22 haraldkipp
  93. * Initial using 3.2.1
  94. *
  95. * Revision 1.12 2003/02/04 18:00:54 harald
  96. * Version 3 released
  97. *
  98. * Revision 1.11 2003/01/14 16:38:18 harald
  99. * New tick counter query
  100. *
  101. * Revision 1.10 2002/06/26 17:29:29 harald
  102. * First pre-release with 2.4 stack
  103. *
  104. */
  105. #include <sys/types.h>
  106. #include <arch/timer.h>
  107. #include <time.h>
  108. #include <stdint.h>
  109. /*!
  110. * \file sys/timer.h
  111. * \brief Timer management definitions.
  112. */
  113. /*!
  114. * \brief Timer type.
  115. */
  116. typedef struct _NUTTIMERINFO NUTTIMERINFO;
  117. /*!
  118. * \struct _NUTTIMERINFO timer.h sys/timer.h
  119. * \brief Timer information structure.
  120. */
  121. struct _NUTTIMERINFO {
  122. /*! \brief Link to next timer.
  123. */
  124. NUTTIMERINFO *tn_next;
  125. /*! \brief Link to previous timer.
  126. */
  127. NUTTIMERINFO *tn_prev;
  128. /*! \brief Number of system ticks.
  129. * Set to zero on one-shot timers.
  130. */
  131. uint32_t tn_ticks;
  132. /*! \brief Decremented by one on each system tick intervall.
  133. */
  134. uint32_t tn_ticks_left;
  135. /*! \brief Callback function.
  136. */
  137. void (*tn_callback)(HANDLE, void *);
  138. /*! \brief Argument pointer passed to callback function.
  139. */
  140. volatile void *tn_arg;
  141. };
  142. extern NUTTIMERINFO* nutTimerList;
  143. /*! \brief The system_time struct holds the seconds and microseconds since
  144. * system startup
  145. */
  146. extern volatile struct timeval system_time;
  147. #define TM_ONESHOT 0x01
  148. #define NUT_CACHE_LVALID 0x80000000UL
  149. /* Set defaults. */
  150. #ifndef NUT_HWCLK_CPU
  151. #define NUT_HWCLK_CPU 0
  152. #endif
  153. #ifndef NUT_HWCLK_PERIPHERAL
  154. #define NUT_HWCLK_PERIPHERAL NUT_HWCLK_CPU
  155. #endif
  156. #ifndef NUT_HWCLK_MAX
  157. #if defined(MCU_STM32F10X)
  158. #define NUT_HWCLK_MAX NUT_HWCLK_PCLK2
  159. #else
  160. #define NUT_HWCLK_MAX NUT_HWCLK_PERIPHERAL
  161. #endif
  162. #endif
  163. /*
  164. * Functions used by the kernel.
  165. */
  166. extern void NutTimerIntr(void *arg);
  167. extern void NutTimerInit(void);
  168. extern NUTTIMERINFO * NutTimerCreate(uint32_t ticks, void (*callback) (HANDLE, void *), void *arg, uint8_t flags);
  169. extern void NutTimerInsert(NUTTIMERINFO * tn);
  170. extern void NutTimerProcessElapsed(void);
  171. /*
  172. * API declarations.
  173. */
  174. extern void NutSleep(uint32_t ms);
  175. extern void NutDelay(uint8_t ms);
  176. extern void NutMicroDelay(uint32_t us);
  177. extern uint32_t NutGetTickCount(void);
  178. extern uint32_t NutGetSeconds(void);
  179. extern uint32_t NutGetMillis(void);
  180. extern HANDLE NutTimerStart(uint32_t ms, void (*callback)(HANDLE, void *), void *arg, uint8_t flags);
  181. extern HANDLE NutTimerStartTicks(uint32_t ticks, void (*callback) (HANDLE, void *), void *arg, uint8_t flags);
  182. extern void NutTimerStop(HANDLE handle);
  183. /*
  184. * Clock frequencies.
  185. */
  186. #if NUT_HWCLK_MAX
  187. /* More than 1 clock available: Implement function. */
  188. extern uint32_t NutClockGet(int idx);
  189. #else
  190. /* Only 1 clock available: Map to CPU clock. */
  191. #define NutClockGet(i) NutGetCpuClock()
  192. #endif
  193. extern int NutClockSet(int idx, uint32_t freq);
  194. /* On some platforms the clock query functions may be defined by
  195. ** a preprocessor macro to avoid function call overhead. */
  196. #if !defined(NutGetCpuClock)
  197. extern uint32_t NutGetCpuClock(void);
  198. #endif
  199. #endif