events.c

Go to the documentation of this file.
00001 
00059 #include <cfg/os.h>
00060 
00061 #include <stdio.h>
00062 #include <io.h>
00063 
00064 #include <dev/board.h>
00065 
00066 #include <sys/thread.h>
00067 #include <sys/timer.h>
00068 #include <sys/event.h>
00069 
00070 /*
00071  * A global event queue, used as a mutex semaphore.
00072  */
00073 static HANDLE mutex;
00074 
00075 /*
00076  * High priority background thread. 
00077  */
00078 THREAD(High, arg)
00079 {
00080     NutThreadSetPriority(32);
00081     for(;;) {
00082         puts("Request");
00083         if (NutEventWait(&mutex, 2000)) {
00084             puts("Timeout");
00085         }
00086         else {
00087             puts("Acquired");
00088             NutSleep(2500);
00089             puts("Release");
00090             NutEventPost(&mutex);
00091         }
00092         NutSleep(1000);
00093     }
00094 }
00095 
00096 /*
00097  * Low priority background thread. 
00098  */
00099 THREAD(Low, arg)
00100 {
00101     NutThreadSetPriority(96);
00102     for(;;) {
00103         puts("                  Request");
00104         if (NutEventWait(&mutex, 3000)) {
00105             puts("                  Timeout");
00106         }
00107         else {
00108             puts("                  Acquired");
00109             NutSleep(3500);
00110             puts("                  Release");
00111             NutEventPost(&mutex);
00112         }
00113     }
00114 }
00115 
00116 /*
00117  * Main application routine. 
00118  */
00119 int main(void)
00120 {
00121     u_long baud = 115200;
00122 
00123     /*
00124      * Register the UART device, open it, assign stdout to it and set 
00125      * the baudrate.
00126      */
00127     NutRegisterDevice(&DEV_DEBUG, 0, 0);
00128     freopen(DEV_DEBUG_NAME, "w", stdout);
00129     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00130 
00131     /*
00132      * Print title.
00133      */
00134     puts("\nNut/OS Event Queue Demo");
00135     puts("High     Main     Low      ");
00136 
00137     /*
00138      * Post an initial event. This will put the queue into signaled 
00139      * state and immediately grant the next call to NutEventWait().
00140      */
00141     NutEventPost(&mutex);
00142 
00143     /*
00144      * Start two background threads.
00145      */
00146     NutThreadCreate("high", High, 0, 256);
00147     NutThreadCreate("low", Low, 0, 256);
00148 
00149     for(;;) {
00150         puts("         Request");
00151         if (NutEventWait(&mutex, 1000)) {
00152             puts("         Timeout");
00153         }
00154         else {
00155             puts("         Acquired");
00156             NutSleep(1500);
00157             puts("         Release");
00158             NutEventPost(&mutex);
00159         }
00160         NutSleep(1000);
00161     }
00162     return 0;
00163 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/