hobbielektronika
/
10_rtos_signals
Synchronize two tasks by the help of event signals. Watch the output in a Terminal window!
main.cpp@0:6c4ab9386095, 2016-02-23 (annotated)
- Committer:
- icserny
- Date:
- Tue Feb 23 13:27:53 2016 +0000
- Revision:
- 0:6c4ab9386095
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icserny | 0:6c4ab9386095 | 1 | /** 10_rtos_signals |
icserny | 0:6c4ab9386095 | 2 | * |
icserny | 0:6c4ab9386095 | 3 | * Synchronize two tasks by the help of event signals. |
icserny | 0:6c4ab9386095 | 4 | * Thread2 changes the state of LED1 when any event flag arrives. |
icserny | 0:6c4ab9386095 | 5 | * Thread1 (the main thread) sends event flags at reguar intervals. |
icserny | 0:6c4ab9386095 | 6 | * You may experimenting by changeing this program: |
icserny | 0:6c4ab9386095 | 7 | * - Wait for a given flag: for example Thread::signal_wait(0x8); |
icserny | 0:6c4ab9386095 | 8 | * - Wait for multiple events: for example Thread::signal_wait(0x5); |
icserny | 0:6c4ab9386095 | 9 | * - Wait for a nonexistent events: for example Thread::signal_wait(0x10000); |
icserny | 0:6c4ab9386095 | 10 | * - Wait for a limited time: for example Thread::signal_wait(0x3,500); |
icserny | 0:6c4ab9386095 | 11 | * |
icserny | 0:6c4ab9386095 | 12 | * Watch the output in a Terminal window! |
icserny | 0:6c4ab9386095 | 13 | */ |
icserny | 0:6c4ab9386095 | 14 | |
icserny | 0:6c4ab9386095 | 15 | #include "mbed.h" |
icserny | 0:6c4ab9386095 | 16 | #include "rtos.h" |
icserny | 0:6c4ab9386095 | 17 | |
icserny | 0:6c4ab9386095 | 18 | DigitalOut led(LED1); |
icserny | 0:6c4ab9386095 | 19 | |
icserny | 0:6c4ab9386095 | 20 | void led_thread(void const *argument) |
icserny | 0:6c4ab9386095 | 21 | { |
icserny | 0:6c4ab9386095 | 22 | while (true) { |
icserny | 0:6c4ab9386095 | 23 | // Signal flags that are reported as event are automatically cleared. |
icserny | 0:6c4ab9386095 | 24 | osEvent evt = Thread::signal_wait(0); //Wait for any signal |
icserny | 0:6c4ab9386095 | 25 | switch(evt.status) { |
icserny | 0:6c4ab9386095 | 26 | case osOK: |
icserny | 0:6c4ab9386095 | 27 | printf("osOK\n"); //no error or event occurred |
icserny | 0:6c4ab9386095 | 28 | break; |
icserny | 0:6c4ab9386095 | 29 | case osEventSignal: |
icserny | 0:6c4ab9386095 | 30 | printf("osEventSignal = %#05x\n",evt.value.signals); //signal event occurred |
icserny | 0:6c4ab9386095 | 31 | break; |
icserny | 0:6c4ab9386095 | 32 | case osEventTimeout: |
icserny | 0:6c4ab9386095 | 33 | printf("osEventTimeout\n"); //timeout occurred |
icserny | 0:6c4ab9386095 | 34 | break; |
icserny | 0:6c4ab9386095 | 35 | case osErrorValue: |
icserny | 0:6c4ab9386095 | 36 | printf("osErrorValue\n"); |
icserny | 0:6c4ab9386095 | 37 | break; //value of a parameter is out of range. |
icserny | 0:6c4ab9386095 | 38 | default: |
icserny | 0:6c4ab9386095 | 39 | printf("Unknown error flag: %#08x\n",(uint32_t)evt.status); |
icserny | 0:6c4ab9386095 | 40 | break; |
icserny | 0:6c4ab9386095 | 41 | }; |
icserny | 0:6c4ab9386095 | 42 | led = !led; |
icserny | 0:6c4ab9386095 | 43 | } |
icserny | 0:6c4ab9386095 | 44 | } |
icserny | 0:6c4ab9386095 | 45 | |
icserny | 0:6c4ab9386095 | 46 | int main (void) |
icserny | 0:6c4ab9386095 | 47 | { |
icserny | 0:6c4ab9386095 | 48 | int32_t signal_mask = 0x1; |
icserny | 0:6c4ab9386095 | 49 | Thread thread2(led_thread); |
icserny | 0:6c4ab9386095 | 50 | |
icserny | 0:6c4ab9386095 | 51 | while (true) { |
icserny | 0:6c4ab9386095 | 52 | Thread::wait(1000); |
icserny | 0:6c4ab9386095 | 53 | thread2.signal_set(signal_mask); //send signals from 0x0001 to 0x8000 |
icserny | 0:6c4ab9386095 | 54 | signal_mask <<=1; //switch to next signal |
icserny | 0:6c4ab9386095 | 55 | if(signal_mask > 0x8000) signal_mask=0x1; |
icserny | 0:6c4ab9386095 | 56 | } |
icserny | 0:6c4ab9386095 | 57 | } |