events/events.c

This sample demonstrates the usage of Nut/OS event queues. It further shows how an event queue can be used as a mutual exclusion semaphore.

Two additional threads are started, one with a higher and another one with a lower priority than the main thread.

The results are printed on the debug device. Each thread prints the current action in one of three columns. The first column is used by the highest priority, the last column by the lowest priority thread.

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

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