events.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*!
  2. * Copyright (C) 2001-2006 by egnite Software 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. * $Log$
  36. * Revision 1.3 2008/01/31 09:38:15 haraldkipp
  37. * Added return statement in main to avoid warnings with latest GCC.
  38. *
  39. * Revision 1.2 2006/07/21 09:07:19 haraldkipp
  40. * Use dev/board.h to determine output device.
  41. *
  42. * Revision 1.1 2005/07/23 13:54:39 haraldkipp
  43. * First check in
  44. *
  45. */
  46. /*!
  47. * \example events/events.c
  48. *
  49. * This sample demonstrates the usage of Nut/OS event queues. It further
  50. * shows how an event queue can be used as a mutual exclusion semaphore.
  51. *
  52. * Two additional threads are started, one with a higher and another one
  53. * with a lower priority than the main thread.
  54. *
  55. * The results are printed on the debug device. Each thread prints the
  56. * current action in one of three columns. The first column is used by
  57. * the highest priority, the last column by the lowest priority thread.
  58. */
  59. #include <cfg/os.h>
  60. #include <stdio.h>
  61. #include <io.h>
  62. #include <dev/board.h>
  63. #include <sys/thread.h>
  64. #include <sys/timer.h>
  65. #include <sys/event.h>
  66. /*
  67. * A global event queue, used as a mutex semaphore.
  68. */
  69. static HANDLE mutex;
  70. /*
  71. * High priority background thread.
  72. */
  73. THREAD(High, arg)
  74. {
  75. NutThreadSetPriority(32);
  76. for(;;) {
  77. puts("Request");
  78. if (NutEventWait(&mutex, 2000)) {
  79. puts("Timeout");
  80. }
  81. else {
  82. puts("Acquired");
  83. NutSleep(2500);
  84. puts("Release");
  85. NutEventPost(&mutex);
  86. }
  87. NutSleep(1000);
  88. }
  89. }
  90. /*
  91. * Low priority background thread.
  92. */
  93. THREAD(Low, arg)
  94. {
  95. NutThreadSetPriority(96);
  96. for(;;) {
  97. puts(" Request");
  98. if (NutEventWait(&mutex, 3000)) {
  99. puts(" Timeout");
  100. }
  101. else {
  102. puts(" Acquired");
  103. NutSleep(3500);
  104. puts(" Release");
  105. NutEventPost(&mutex);
  106. }
  107. }
  108. }
  109. /*
  110. * Main application routine.
  111. */
  112. int main(void)
  113. {
  114. uint32_t baud = 115200;
  115. /*
  116. * Register the UART device, open it, assign stdout to it and set
  117. * the baudrate.
  118. */
  119. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  120. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  121. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  122. /*
  123. * Print title.
  124. */
  125. puts("\nNut/OS Event Queue Demo");
  126. puts("High Main Low ");
  127. /*
  128. * Post an initial event. This will put the queue into signaled
  129. * state and immediately grant the next call to NutEventWait().
  130. */
  131. NutEventPost(&mutex);
  132. /*
  133. * Start two background threads.
  134. */
  135. NutThreadCreate("high", High, 0, 256);
  136. NutThreadCreate("low", Low, 0, 256);
  137. for(;;) {
  138. puts(" Request");
  139. if (NutEventWait(&mutex, 1000)) {
  140. puts(" Timeout");
  141. }
  142. else {
  143. puts(" Acquired");
  144. NutSleep(1500);
  145. puts(" Release");
  146. NutEventPost(&mutex);
  147. }
  148. NutSleep(1000);
  149. }
  150. return 0;
  151. }