Robert Kennedy / Mbed OS TEN_mbedos_threads_robert

Fork of TEN_mbedos_threads by Qmax

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main_copy.cpp Source File

main_copy.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 button(PC_13);
00012 
00013 DigitalOut led1(LED1);
00014 DigitalOut led2(PA_11);
00015 DigitalOut led3(PA_12); 
00016 
00017 /* reserve the debbuger uart to shell interface */
00018 Serial pc_serial(USBTX,USBRX);
00019 
00020 /* declares threads for this demo: */
00021 const size_t a_stk_size = 512;
00022 uint8_t a_stk[a_stk_size];
00023 Thread a_thread(osPriorityNormal, a_stk_size, &a_stk[0]);
00024 
00025 const size_t b_stk_size = 512;
00026 uint8_t b_stk[b_stk_size];
00027 Thread b_thread(osPriorityNormal, b_stk_size, &b_stk[0]);
00028 
00029 const size_t c_stk_size = 512;
00030 uint8_t c_stk[c_stk_size];
00031 Thread c_thread(osPriorityNormal, c_stk_size, &c_stk[0]);
00032 
00033 const size_t e_stk_size = 512;
00034 uint8_t e_stk[e_stk_size];
00035 Thread eventThread(osPriorityNormal, e_stk_size, &e_stk[0]);
00036 
00037 // create an event queue
00038 EventQueue queue;
00039  
00040 uint32_t count = 0;
00041 
00042 void do_something() {
00043   // this now runs in the context of eventThread, instead of in the ISR
00044   count++;
00045   printf("Toggling LED!\r\n");
00046   for(int i = 0 ; i < 0xFFFFFF; i++);
00047   led3 = !led3;
00048   printf("Toggled LED count = %d \r\n", count);
00049 }
00050 
00051 /**
00052  * @brief thread a function 
00053  */
00054 static void thread(void const *buf)
00055 {
00056     uint32_t execs = 0;
00057     pc_serial.printf("## started %s execution! ##\n\r", (char *)buf);
00058  
00059     for(;;) {
00060         execs++;
00061         /* adds dummy processing */
00062         for(int i = 0 ; i < 0xFFFFFF; i++);
00063         pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs);
00064         led1 = !led1;
00065         Thread::yield();
00066     }
00067 }
00068 
00069 static void thread2(void const *buf)
00070 {
00071     uint32_t execs = 0;
00072     pc_serial.printf("## started %s execution! ##\n\r", (char *)buf);
00073  
00074     for(;;) {
00075         execs++;
00076         /* adds dummy processing */
00077         for(int i = 0 ; i < 0xF00000; i++);
00078         pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs);
00079         led2 = !led2;
00080         Thread::yield();
00081     }
00082 }
00083 
00084 static void thread3(void const *buf)
00085 {
00086     uint32_t execs = 0;
00087     pc_serial.printf("## started %s execution! ##\n\r", (char *)buf);
00088  
00089     for(;;) {
00090         execs++;
00091         /* adds dummy processing */
00092         for(int i = 0 ; i < 0xFFFFFF; i++);
00093         pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs);
00094         led3 = !led3;
00095         Thread::yield();
00096     }
00097 }
00098 
00099 /**
00100  * @brief main application loop
00101  */
00102 int main(void) 
00103 {      
00104     pc_serial.baud(115200);
00105     a_thread.start(callback(thread, (void *)THREAD_A));
00106     b_thread.start(callback(thread2, (void *)THREAD_B));
00107     /*c_thread.start(callback(thread3, (void *)THREAD_C));*/
00108     
00109     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
00110  
00111     // wrap calls in queue.event to automatically defer to the queue's thread
00112     button.fall(queue.event(&do_something));
00113   
00114   
00115     return 0;
00116 }