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