Basic example showing the CMSIS-RTOS queue API with a producer in an ISR and a consumer in a normal thread

Dependencies:   mbed mbed-rtos

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

Revision:
1:aa8ea13b6a07
Parent:
0:c8b166e5c984
Child:
2:39180377e223
--- a/main.cpp	Fri Jul 13 16:10:43 2012 +0000
+++ b/main.cpp	Fri Jul 13 16:13:14 2012 +0000
@@ -0,0 +1,40 @@
+#include "mbed.h"
+#include "cmsis_os.h"
+
+osMessageQDef(queue, 5, message_t);
+osMessageQId  queue;
+
+DigitalOut myled(LED1);
+
+void queue_isr() {
+    osMessagePut(queue, (uint32_t)2, 0);
+    
+    myled = !myled;
+}
+
+void queue_thread(void const *argument) {
+    while (true) {
+       osMessagePut(queue, 1, 0);
+       osDelay(1000);
+    }
+}
+
+osThreadDef(queue_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
+
+int main (void) {
+     queue = osMessageCreate(osMessageQ(queue), NULL);
+    
+    osThreadCreate(osThread(queue_thread), NULL);
+    
+    Ticker ticker;
+    ticker.attach(queue_isr, 1.0);
+    
+    while (true) {
+        osEvent evt = osMessageGet(queue, osWaitForever);
+        if (evt.status != osEventMessage) {
+            printf("queue->get() returned %02x status\n\r", evt.status);
+        } else {
+            printf("queue->get() returned %d\n\r", evt.value.v);
+        }
+    }
+}