RTOS example - using stdio_mutex controlling the access to printf(). Note that this is necessary only for Cortex-M0 MCU-s

Dependencies:   mbed mbed-rtos

Files at this revision

API Documentation at this revision

Comitter:
cspista
Date:
Thu Feb 24 08:06:02 2022 +0000
Commit message:
final version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Feb 24 08:06:02 2022 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+#include "rtos.h"
+
+Serial pc(USBTX,USBRX);     //UART via ST-Link
+Mutex stdio_mutex;
+
+void notify(const char* name, int state)
+{
+    stdio_mutex.lock();
+    pc.printf("%s: %d\n\r", name, state);
+    stdio_mutex.unlock();
+}
+
+void test_thread(void const *args)
+{
+    while (true) {
+        notify((const char*)args, 0);
+        Thread::wait(1000);
+        notify((const char*)args, 1);
+        Thread::wait(1000);
+    }
+}
+
+int main()
+{
+    pc.baud(115200);
+    Thread t2(test_thread, (void *)"Th 2");
+    Thread t3(test_thread, (void *)"Th 3");
+    test_thread((void *)"Th 1");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Feb 24 08:06:02 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed-rtos/#5713cbbdb706
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Feb 24 08:06:02 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file