Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TEN_mbedos_threads by
main.cpp
00001 /** 00002 * @brief first thread control program with MBED OS 00003 */ 00004 #include "mbed.h" 00005 #include "rtos.h" 00006 00007 #define THREAD_A "thread_a" 00008 #define THREAD_B "thread_b" 00009 #define THREAD_C "thread_c" 00010 00011 InterruptIn checkinIntr(PC_13); 00012 InterruptIn motionIntr(PC_14); 00013 00014 DigitalOut led1(LED1); 00015 DigitalOut led2(PA_11); 00016 DigitalOut led3(PA_12); 00017 00018 /* reserve the debbuger uart to shell interface */ 00019 Serial pc_serial(USBTX,USBRX); 00020 00021 /* declares threads for this demo: */ 00022 const size_t a_stk_size = 512; 00023 uint8_t a_stk[a_stk_size]; 00024 Thread a_thread(osPriorityNormal, a_stk_size, &a_stk[0]); 00025 00026 const size_t b_stk_size = 512; 00027 uint8_t b_stk[b_stk_size]; 00028 Thread b_thread(osPriorityNormal, b_stk_size, &b_stk[0]); 00029 00030 const size_t c_stk_size = 512; 00031 uint8_t c_stk[c_stk_size]; 00032 Thread c_thread(osPriorityNormal, c_stk_size, &c_stk[0]); 00033 00034 const size_t e_stk_size = 512; 00035 uint8_t e_stk[e_stk_size]; 00036 Thread checkinEventThread(osPriorityNormal, e_stk_size, &e_stk[0]); 00037 Thread motionEventThread(osPriorityNormal, e_stk_size, &e_stk[0]); 00038 00039 // create an event queue 00040 EventQueue chechinEventQueue; 00041 EventQueue motionEventQueue; 00042 00043 uint32_t count = 0; 00044 00045 void checkinFunction() { 00046 // this now runs in the context of checkinEventThread, instead of in the ISR 00047 count++; 00048 printf("Toggling LED!\r\n"); 00049 for(int i = 0 ; i < 0xFFFFFF; i++); 00050 led3 = !led3; 00051 printf("Toggled LED count = %d \r\n", count); 00052 } 00053 00054 void motionFunction() { 00055 // this now runs in the context of checkinEventThread, instead of in the ISR 00056 count++; 00057 printf("Toggling LED!\r\n"); 00058 for(int i = 0 ; i < 0xFFFFFF; i++); 00059 led3 = !led3; 00060 printf("Toggled LED count = %d \r\n", count); 00061 } 00062 00063 /** 00064 * @brief thread a function 00065 */ 00066 static void thread(void const *buf) 00067 { 00068 uint32_t execs = 0; 00069 pc_serial.printf("## started %s execution! ##\n\r", (char *)buf); 00070 00071 for(;;) { 00072 execs++; 00073 /* adds dummy processing */ 00074 for(int i = 0 ; i < 0xFFFFFF; i++); 00075 pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs); 00076 led1 = !led1; 00077 Thread::yield(); 00078 } 00079 } 00080 00081 static void thread2(void const *buf) 00082 { 00083 uint32_t execs = 0; 00084 pc_serial.printf("## started %s execution! ##\n\r", (char *)buf); 00085 00086 for(;;) { 00087 execs++; 00088 /* adds dummy processing */ 00089 for(int i = 0 ; i < 0xF00000; i++); 00090 pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs); 00091 led2 = !led2; 00092 Thread::yield(); 00093 } 00094 } 00095 00096 static void thread3(void const *buf) 00097 { 00098 uint32_t execs = 0; 00099 pc_serial.printf("## started %s execution! ##\n\r", (char *)buf); 00100 00101 for(;;) { 00102 execs++; 00103 /* adds dummy processing */ 00104 for(int i = 0 ; i < 0xFFFFFF; i++); 00105 pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs); 00106 led3 = !led3; 00107 Thread::yield(); 00108 } 00109 } 00110 00111 /** 00112 * @brief main application loop 00113 */ 00114 int main(void) 00115 { 00116 pc_serial.baud(115200); 00117 a_thread.start(callback(thread, (void *)THREAD_A)); 00118 b_thread.start(callback(thread2, (void *)THREAD_B)); 00119 /*c_thread.start(callback(thread3, (void *)THREAD_C));*/ 00120 00121 checkinEventThread.start(callback(&chechinEventQueue, &EventQueue::dispatch_forever)); 00122 motionEventThread.start(callback(&motionEventQueue, &EventQueue::dispatch_forever)); 00123 00124 // wrap calls in queue.event to automatically defer to the queue's thread 00125 checkinIntr.fall(chechinEventQueue.event(&checkinFunction)); 00126 motionIntr.fall(motionEventQueue.event(&motionFunction)); 00127 00128 00129 return 0; 00130 }
Generated on Sun Jul 17 2022 01:40:37 by
1.7.2
