Basic example showing the Queue API with a producer in an ISR and a consumer in a normal thread.
mbed 2 and mbed OS 5
This is an mbed 2 example. mbed OS 5 has integrated the mbed library with mbed-rtos. For an mbed-os example, please see:
Import programrtos_isr
isr example
main.cpp@5:23a456bf0487, 2017-02-15 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Feb 15 13:59:58 2017 -0600
- Revision:
- 5:23a456bf0487
- Parent:
- 4:40078e697304
Revert update to mbed-os.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:3043ec651794 | 1 | #include "mbed.h" |
mbed_official | 5:23a456bf0487 | 2 | #include "rtos.h" |
mbed_official | 0:4979ad0f5cb9 | 3 | |
emilmont | 1:3043ec651794 | 4 | Queue<uint32_t, 5> queue; |
mbed_official | 5:23a456bf0487 | 5 | |
emilmont | 1:3043ec651794 | 6 | DigitalOut myled(LED1); |
emilmont | 1:3043ec651794 | 7 | |
emilmont | 1:3043ec651794 | 8 | void queue_isr() { |
emilmont | 1:3043ec651794 | 9 | queue.put((uint32_t*)2); |
emilmont | 1:3043ec651794 | 10 | myled = !myled; |
emilmont | 1:3043ec651794 | 11 | } |
emilmont | 1:3043ec651794 | 12 | |
mbed_official | 5:23a456bf0487 | 13 | void queue_thread(void const *args) { |
emilmont | 1:3043ec651794 | 14 | while (true) { |
emilmont | 1:3043ec651794 | 15 | queue.put((uint32_t*)1); |
mbed_official | 5:23a456bf0487 | 16 | Thread::wait(1000); |
emilmont | 1:3043ec651794 | 17 | } |
emilmont | 1:3043ec651794 | 18 | } |
emilmont | 1:3043ec651794 | 19 | |
emilmont | 1:3043ec651794 | 20 | int main (void) { |
mbed_official | 5:23a456bf0487 | 21 | Thread thread(queue_thread); |
mbed_official | 5:23a456bf0487 | 22 | |
mbed_official | 5:23a456bf0487 | 23 | Ticker ticker; |
emilmont | 1:3043ec651794 | 24 | ticker.attach(queue_isr, 1.0); |
mbed_official | 5:23a456bf0487 | 25 | |
emilmont | 1:3043ec651794 | 26 | while (true) { |
emilmont | 1:3043ec651794 | 27 | osEvent evt = queue.get(); |
emilmont | 1:3043ec651794 | 28 | if (evt.status != osEventMessage) { |
emilmont | 1:3043ec651794 | 29 | printf("queue->get() returned %02x status\n\r", evt.status); |
emilmont | 1:3043ec651794 | 30 | } else { |
emilmont | 1:3043ec651794 | 31 | printf("queue->get() returned %d\n\r", evt.value.v); |
emilmont | 1:3043ec651794 | 32 | } |
emilmont | 1:3043ec651794 | 33 | } |
emilmont | 1:3043ec651794 | 34 | } |