![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TEN MBED OS threads lab
main.cpp@0:95bee34b8805, 2017-01-29 (annotated)
- Committer:
- uLipe
- Date:
- Sun Jan 29 20:42:49 2017 +0000
- Revision:
- 0:95bee34b8805
first;
Who changed what in which revision?
User | Revision | Line number | New 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 | |
uLipe | 0:95bee34b8805 | 7 | /* declares threads for this demo: */ |
uLipe | 0:95bee34b8805 | 8 | const size_t a_stk_size = 1024; |
uLipe | 0:95bee34b8805 | 9 | uint8_t a_stk[a_stk_size]; |
uLipe | 0:95bee34b8805 | 10 | Thread a_thread(osPriorityNormal, a_stk_size, &a_stk[0]); |
uLipe | 0:95bee34b8805 | 11 | |
uLipe | 0:95bee34b8805 | 12 | const size_t b_stk_size = 1024; |
uLipe | 0:95bee34b8805 | 13 | uint8_t b_stk[b_stk_size]; |
uLipe | 0:95bee34b8805 | 14 | Thread b_thread(osPriorityNormal, b_stk_size, &b_stk[0]); |
uLipe | 0:95bee34b8805 | 15 | |
uLipe | 0:95bee34b8805 | 16 | /* reserve the debbuger uart to shell interface */ |
uLipe | 0:95bee34b8805 | 17 | Serial pc_serial(USBTX,USBRX); |
uLipe | 0:95bee34b8805 | 18 | |
uLipe | 0:95bee34b8805 | 19 | |
uLipe | 0:95bee34b8805 | 20 | /** |
uLipe | 0:95bee34b8805 | 21 | * @brief thread a function |
uLipe | 0:95bee34b8805 | 22 | */ |
uLipe | 0:95bee34b8805 | 23 | static void thread_a(void) |
uLipe | 0:95bee34b8805 | 24 | { |
uLipe | 0:95bee34b8805 | 25 | uint32_t execs = 0; |
uLipe | 0:95bee34b8805 | 26 | pc_serial.printf("## started thread_a execution! ##\n\r"); |
uLipe | 0:95bee34b8805 | 27 | |
uLipe | 0:95bee34b8805 | 28 | for(;;) { |
uLipe | 0:95bee34b8805 | 29 | execs++; |
uLipe | 0:95bee34b8805 | 30 | /* adds dummy processing */ |
uLipe | 0:95bee34b8805 | 31 | for(int i = 0 ; i < 0xFFFFFF; i++); |
uLipe | 0:95bee34b8805 | 32 | pc_serial.printf("## thread_a executed %d times! ##\n\r", execs); |
uLipe | 0:95bee34b8805 | 33 | a_thread.yield(); |
uLipe | 0:95bee34b8805 | 34 | } |
uLipe | 0:95bee34b8805 | 35 | } |
uLipe | 0:95bee34b8805 | 36 | |
uLipe | 0:95bee34b8805 | 37 | |
uLipe | 0:95bee34b8805 | 38 | /** |
uLipe | 0:95bee34b8805 | 39 | * @brief thread a function |
uLipe | 0:95bee34b8805 | 40 | */ |
uLipe | 0:95bee34b8805 | 41 | static void thread_b(void) |
uLipe | 0:95bee34b8805 | 42 | { |
uLipe | 0:95bee34b8805 | 43 | uint32_t execs = 0; |
uLipe | 0:95bee34b8805 | 44 | pc_serial.printf("## started thread_b execution! ##\n\r"); |
uLipe | 0:95bee34b8805 | 45 | |
uLipe | 0:95bee34b8805 | 46 | for(;;) { |
uLipe | 0:95bee34b8805 | 47 | execs++; |
uLipe | 0:95bee34b8805 | 48 | /* adds dummy processing */ |
uLipe | 0:95bee34b8805 | 49 | for(int i = 0 ; i < 0xFFFFFF; i++); |
uLipe | 0:95bee34b8805 | 50 | pc_serial.printf("## thread_b executed %d times! ##\n\r", execs); |
uLipe | 0:95bee34b8805 | 51 | b_thread.yield(); |
uLipe | 0:95bee34b8805 | 52 | } |
uLipe | 0:95bee34b8805 | 53 | } |
uLipe | 0:95bee34b8805 | 54 | |
uLipe | 0:95bee34b8805 | 55 | |
uLipe | 0:95bee34b8805 | 56 | |
uLipe | 0:95bee34b8805 | 57 | /** |
uLipe | 0:95bee34b8805 | 58 | * @brief main application loop |
uLipe | 0:95bee34b8805 | 59 | */ |
uLipe | 0:95bee34b8805 | 60 | int main(void) |
uLipe | 0:95bee34b8805 | 61 | { |
uLipe | 0:95bee34b8805 | 62 | pc_serial.baud(115200); |
uLipe | 0:95bee34b8805 | 63 | a_thread.start(thread_a); |
uLipe | 0:95bee34b8805 | 64 | b_thread.start(thread_b); |
uLipe | 0:95bee34b8805 | 65 | return 0; |
uLipe | 0:95bee34b8805 | 66 | } |