First

Fork of TEN_mbedos_threads by Qmax

Committer:
edenrokey
Date:
Thu Mar 02 12:07:13 2017 +0000
Revision:
2:967778096fa0
Parent:
1:d1d5c33000fb
Robert first checkin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uLipe 0:95bee34b8805 1 /**
uLipe 0:95bee34b8805 2 * @brief first thread control program with MBED OS
uLipe 0:95bee34b8805 3 */
uLipe 0:95bee34b8805 4 #include "mbed.h"
uLipe 0:95bee34b8805 5 #include "rtos.h"
uLipe 0:95bee34b8805 6
narasimma23 1:d1d5c33000fb 7 #define THREAD_A "thread_a"
narasimma23 1:d1d5c33000fb 8 #define THREAD_B "thread_b"
narasimma23 1:d1d5c33000fb 9 #define THREAD_C "thread_c"
uLipe 0:95bee34b8805 10
edenrokey 2:967778096fa0 11 InterruptIn checkinIntr(PC_13);
edenrokey 2:967778096fa0 12 InterruptIn motionIntr(PC_14);
edenrokey 2:967778096fa0 13
edenrokey 2:967778096fa0 14 DigitalOut led1(LED1);
edenrokey 2:967778096fa0 15 DigitalOut led2(PA_11);
edenrokey 2:967778096fa0 16 DigitalOut led3(PA_12);
edenrokey 2:967778096fa0 17
uLipe 0:95bee34b8805 18 /* reserve the debbuger uart to shell interface */
uLipe 0:95bee34b8805 19 Serial pc_serial(USBTX,USBRX);
uLipe 0:95bee34b8805 20
narasimma23 1:d1d5c33000fb 21 /* declares threads for this demo: */
narasimma23 1:d1d5c33000fb 22 const size_t a_stk_size = 512;
narasimma23 1:d1d5c33000fb 23 uint8_t a_stk[a_stk_size];
narasimma23 1:d1d5c33000fb 24 Thread a_thread(osPriorityNormal, a_stk_size, &a_stk[0]);
narasimma23 1:d1d5c33000fb 25
narasimma23 1:d1d5c33000fb 26 const size_t b_stk_size = 512;
narasimma23 1:d1d5c33000fb 27 uint8_t b_stk[b_stk_size];
narasimma23 1:d1d5c33000fb 28 Thread b_thread(osPriorityNormal, b_stk_size, &b_stk[0]);
narasimma23 1:d1d5c33000fb 29
narasimma23 1:d1d5c33000fb 30 const size_t c_stk_size = 512;
narasimma23 1:d1d5c33000fb 31 uint8_t c_stk[c_stk_size];
narasimma23 1:d1d5c33000fb 32 Thread c_thread(osPriorityNormal, c_stk_size, &c_stk[0]);
uLipe 0:95bee34b8805 33
edenrokey 2:967778096fa0 34 const size_t e_stk_size = 512;
edenrokey 2:967778096fa0 35 uint8_t e_stk[e_stk_size];
edenrokey 2:967778096fa0 36 Thread checkinEventThread(osPriorityNormal, e_stk_size, &e_stk[0]);
edenrokey 2:967778096fa0 37 Thread motionEventThread(osPriorityNormal, e_stk_size, &e_stk[0]);
edenrokey 2:967778096fa0 38
edenrokey 2:967778096fa0 39 // create an event queue
edenrokey 2:967778096fa0 40 EventQueue chechinEventQueue;
edenrokey 2:967778096fa0 41 EventQueue motionEventQueue;
edenrokey 2:967778096fa0 42
edenrokey 2:967778096fa0 43 uint32_t count = 0;
edenrokey 2:967778096fa0 44
edenrokey 2:967778096fa0 45 void checkinFunction() {
edenrokey 2:967778096fa0 46 // this now runs in the context of checkinEventThread, instead of in the ISR
edenrokey 2:967778096fa0 47 count++;
edenrokey 2:967778096fa0 48 printf("Toggling LED!\r\n");
edenrokey 2:967778096fa0 49 for(int i = 0 ; i < 0xFFFFFF; i++);
edenrokey 2:967778096fa0 50 led3 = !led3;
edenrokey 2:967778096fa0 51 printf("Toggled LED count = %d \r\n", count);
edenrokey 2:967778096fa0 52 }
edenrokey 2:967778096fa0 53
edenrokey 2:967778096fa0 54 void motionFunction() {
edenrokey 2:967778096fa0 55 // this now runs in the context of checkinEventThread, instead of in the ISR
edenrokey 2:967778096fa0 56 count++;
edenrokey 2:967778096fa0 57 printf("Toggling LED!\r\n");
edenrokey 2:967778096fa0 58 for(int i = 0 ; i < 0xFFFFFF; i++);
edenrokey 2:967778096fa0 59 led3 = !led3;
edenrokey 2:967778096fa0 60 printf("Toggled LED count = %d \r\n", count);
edenrokey 2:967778096fa0 61 }
edenrokey 2:967778096fa0 62
uLipe 0:95bee34b8805 63 /**
uLipe 0:95bee34b8805 64 * @brief thread a function
uLipe 0:95bee34b8805 65 */
narasimma23 1:d1d5c33000fb 66 static void thread(void const *buf)
uLipe 0:95bee34b8805 67 {
uLipe 0:95bee34b8805 68 uint32_t execs = 0;
narasimma23 1:d1d5c33000fb 69 pc_serial.printf("## started %s execution! ##\n\r", (char *)buf);
uLipe 0:95bee34b8805 70
uLipe 0:95bee34b8805 71 for(;;) {
uLipe 0:95bee34b8805 72 execs++;
uLipe 0:95bee34b8805 73 /* adds dummy processing */
uLipe 0:95bee34b8805 74 for(int i = 0 ; i < 0xFFFFFF; i++);
narasimma23 1:d1d5c33000fb 75 pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs);
edenrokey 2:967778096fa0 76 led1 = !led1;
narasimma23 1:d1d5c33000fb 77 Thread::yield();
uLipe 0:95bee34b8805 78 }
uLipe 0:95bee34b8805 79 }
uLipe 0:95bee34b8805 80
edenrokey 2:967778096fa0 81 static void thread2(void const *buf)
edenrokey 2:967778096fa0 82 {
edenrokey 2:967778096fa0 83 uint32_t execs = 0;
edenrokey 2:967778096fa0 84 pc_serial.printf("## started %s execution! ##\n\r", (char *)buf);
edenrokey 2:967778096fa0 85
edenrokey 2:967778096fa0 86 for(;;) {
edenrokey 2:967778096fa0 87 execs++;
edenrokey 2:967778096fa0 88 /* adds dummy processing */
edenrokey 2:967778096fa0 89 for(int i = 0 ; i < 0xF00000; i++);
edenrokey 2:967778096fa0 90 pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs);
edenrokey 2:967778096fa0 91 led2 = !led2;
edenrokey 2:967778096fa0 92 Thread::yield();
edenrokey 2:967778096fa0 93 }
edenrokey 2:967778096fa0 94 }
edenrokey 2:967778096fa0 95
edenrokey 2:967778096fa0 96 static void thread3(void const *buf)
edenrokey 2:967778096fa0 97 {
edenrokey 2:967778096fa0 98 uint32_t execs = 0;
edenrokey 2:967778096fa0 99 pc_serial.printf("## started %s execution! ##\n\r", (char *)buf);
edenrokey 2:967778096fa0 100
edenrokey 2:967778096fa0 101 for(;;) {
edenrokey 2:967778096fa0 102 execs++;
edenrokey 2:967778096fa0 103 /* adds dummy processing */
edenrokey 2:967778096fa0 104 for(int i = 0 ; i < 0xFFFFFF; i++);
edenrokey 2:967778096fa0 105 pc_serial.printf("## %s executed %d times! ##\n\r", (char *)buf, execs);
edenrokey 2:967778096fa0 106 led3 = !led3;
edenrokey 2:967778096fa0 107 Thread::yield();
edenrokey 2:967778096fa0 108 }
edenrokey 2:967778096fa0 109 }
uLipe 0:95bee34b8805 110
uLipe 0:95bee34b8805 111 /**
uLipe 0:95bee34b8805 112 * @brief main application loop
uLipe 0:95bee34b8805 113 */
uLipe 0:95bee34b8805 114 int main(void)
narasimma23 1:d1d5c33000fb 115 {
uLipe 0:95bee34b8805 116 pc_serial.baud(115200);
narasimma23 1:d1d5c33000fb 117 a_thread.start(callback(thread, (void *)THREAD_A));
edenrokey 2:967778096fa0 118 b_thread.start(callback(thread2, (void *)THREAD_B));
edenrokey 2:967778096fa0 119 /*c_thread.start(callback(thread3, (void *)THREAD_C));*/
edenrokey 2:967778096fa0 120
edenrokey 2:967778096fa0 121 checkinEventThread.start(callback(&chechinEventQueue, &EventQueue::dispatch_forever));
edenrokey 2:967778096fa0 122 motionEventThread.start(callback(&motionEventQueue, &EventQueue::dispatch_forever));
edenrokey 2:967778096fa0 123
edenrokey 2:967778096fa0 124 // wrap calls in queue.event to automatically defer to the queue's thread
edenrokey 2:967778096fa0 125 checkinIntr.fall(chechinEventQueue.event(&checkinFunction));
edenrokey 2:967778096fa0 126 motionIntr.fall(motionEventQueue.event(&motionFunction));
edenrokey 2:967778096fa0 127
edenrokey 2:967778096fa0 128
uLipe 0:95bee34b8805 129 return 0;
edenrokey 2:967778096fa0 130 }