timers.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*!
  2. * Copyright (C) 2012 by egnite GmbH
  3. * Copyright (C) 2001-2005 by egnite Software GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * $Id: timers.c 4717 2012-10-07 17:50:39Z haraldkipp $
  37. */
  38. /*!
  39. * \example timers/timers.c
  40. *
  41. * This sample demonstrates the usage of Nut/OS timer functions.
  42. */
  43. #include <cfg/os.h>
  44. #include <cfg/arch.h>
  45. #include <dev/board.h>
  46. #include <sys/thread.h>
  47. #include <sys/timer.h>
  48. #include <sys/event.h>
  49. #include <sys/heap.h>
  50. #include <stdio.h>
  51. #include <io.h>
  52. /*
  53. * Timer callback function.
  54. *
  55. * Timer callbacks are called during context switch processing. They must
  56. * return as soon as possible and must not call any potentially blocking
  57. * function.
  58. *
  59. * It would be great to be able to start a thread here, but unfortunately
  60. * this doesn't work in Nut/OS. :-(
  61. */
  62. static void TimerCallback(HANDLE timer, void *arg)
  63. {
  64. NutEventPostAsync(arg);
  65. }
  66. /*
  67. * Demonstrate Nut/OS one-shot timer.
  68. *
  69. * One-shot timers will call the callback function once and then stop
  70. * the timer automatically.
  71. *
  72. * Note that this function also demonstrates event timeouts.
  73. */
  74. static void OneShotDemo(int s)
  75. {
  76. HANDLE t;
  77. HANDLE e = NULL;
  78. int w;
  79. printf("Start %d s one-shot timer\n", s);
  80. t = NutTimerStart(s * 1000UL, TimerCallback, &e, TM_ONESHOT);
  81. for (w = 1; w < s + 1; w++) {
  82. printf(" Waiting %d s...", w);
  83. if (NutEventWait(&e, 1000UL) == 0) {
  84. puts("elapsed");
  85. break;
  86. }
  87. puts("timed out");
  88. }
  89. }
  90. /*
  91. * Demonstrate Nut/OS periodic timer.
  92. *
  93. * Periodic timers will call the callback function and then restart
  94. * the timer automatically. They must be explicitly stopped, if no
  95. * longer needed.
  96. */
  97. static void PeriodicDemo(int s)
  98. {
  99. HANDLE t;
  100. HANDLE e = NULL;
  101. int i;
  102. uint32_t ms;
  103. printf("Start %d s periodic timer\n", s);
  104. t = NutTimerStart(s * 1000UL, TimerCallback, &e, 0);
  105. for (i = 0; i < 5; i++) {
  106. ms = NutGetMillis();
  107. printf(" Waiting...");
  108. NutEventWait(&e, NUT_WAIT_INFINITE);
  109. ms = NutGetMillis() - ms;
  110. printf("elapsed after %lu ms\n", ms);
  111. }
  112. puts(" Stop periodic timer");
  113. NutTimerStop(t);
  114. }
  115. /*
  116. * Demonstrate Nut/OS delay timer.
  117. *
  118. * The delay function will not release the CPU, keeping all other threads
  119. * blocked. It is a good choice for very short delays, where context
  120. * switching is undesirable or would take too long. Use it sparingly and
  121. * for short delays only.
  122. */
  123. static void DelayDemo(void)
  124. {
  125. uint32_t req;
  126. uint32_t act;
  127. for (req = 1024UL; req <= 8UL * 1024UL * 1024UL; req *= 2) {
  128. printf("Delaying %lu us, ", req);
  129. act = NutGetMillis();
  130. NutMicroDelay(req);
  131. act = NutGetMillis() - act;
  132. printf("delayed %lu ms\n", act);
  133. }
  134. }
  135. /*
  136. * Demonstrate Nut/OS sleep timer.
  137. *
  138. * This often used function serves two purposes: Delaying the current
  139. * thread and releasing the CPU.
  140. *
  141. * This demo will run endlessly.
  142. */
  143. static void SleepDemo(void)
  144. {
  145. uint32_t req = 1;
  146. uint32_t act;
  147. for (;;) {
  148. printf("Sleeping %lu ms, ", req);
  149. act = NutGetMillis();
  150. NutSleep(req);
  151. act = NutGetMillis() - act;
  152. printf("slept %lu ms\n", act);
  153. req *= 2;
  154. }
  155. }
  156. /*
  157. * Main application routine.
  158. */
  159. int main(void)
  160. {
  161. uint32_t baud = 115200;
  162. /*
  163. * Register the UART device, open it, assign stdout to it and set
  164. * the baudrate.
  165. */
  166. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  167. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  168. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  169. puts("\n\nNut/OS timer sample");
  170. /*
  171. * Display some general info.
  172. */
  173. printf("CPU running at %lu Hz\n", NutGetCpuClock());
  174. printf("%lu system ticks per second\n", NutTimerMillisToTicks(1000));
  175. /*
  176. * Run the demos.
  177. */
  178. OneShotDemo(10);
  179. PeriodicDemo(2);
  180. DelayDemo();
  181. SleepDemo(); /* Never returns. */
  182. return 0;
  183. }