threads/threads.c

Date:
2004/10/25
Author:
Matthias Ringwald
This sample demonstrates Nut/OS multithreading.

Each thread is started with 192 bytes of stack. This is very low and doesn't provide much space for local variables.

00001 
00068 #include <stdio.h>
00069 #include <io.h>
00070 
00071 #include <sys/thread.h>
00072 #include <sys/timer.h>
00073 
00074 #include <dev/usartavr.h>
00075 #include <hardware/btn-hardware.h>
00076 #include <led/btn-led.h>
00077 
00078 
00079 /*
00080  * High priority thread.
00081  */
00082 THREAD(Thread1, arg)
00083 {
00084     /*
00085      * Endless loop in high priority thread.
00086      */
00087     NutThreadSetPriority(16);
00088     for (;;) {
00089         putchar('H');
00090         NutSleep(125);
00091     }
00092 }
00093 
00094 /*
00095  * Low priority thread.
00096  */
00097 THREAD(Thread2, arg)
00098 {
00099     /*
00100      * Endless loop in low priority thread.
00101      */
00102     NutThreadSetPriority(128);
00103     for (;;) {
00104         putchar('L');
00105         NutSleep(125);
00106     }
00107 }
00108 
00109 /*
00110  * Main application thread. 
00111  */
00112 int main(void)
00113 {
00114     u_long baud = 115200;
00115 
00116     /*
00117      * Register the UART device, open it, assign stdout to it and set 
00118      * the baudrate.
00119      */
00120     NutRegisterDevice(&APP_UART, 0, 0);
00121     freopen(APP_UART.dev_name, "w", stdout);
00122     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00123 
00124     puts("\nThread Test running on BTnut");
00125     u_long cpu_crystal = NutGetCpuClock();
00126     printf("CPU = %u.%04u MHz\n",(int) (cpu_crystal / 1000000UL), (int) ((cpu_crystal - (cpu_crystal / 1000000UL) * 1000000UL) / 100));
00127 
00128 
00129     // start heartbeet
00130     btn_led_init(1);
00131     
00132 
00133     /*
00134      * Start two additional threads. All threads are started with 
00135      * priority 64.
00136      */
00137     NutThreadCreate("t1", Thread1, 0, 192);
00138     NutThreadCreate("t2", Thread2, 0, 192);
00139 
00140     /*
00141      * Endless loop in main thread.
00142      */
00143     for (;;) {
00144         putchar('M');
00145         NutSleep(125);
00146     }
00147     return 0;
00148 }

Generated on Wed Apr 29 11:12:28 2009 for BTnut System Software by doxygen 1.5.1