RTOS example - using stdio_mutex controlling the access to printf(). Note that this is necessary only for Cortex-M0 MCU-s
main.cpp@0:fe1656248d2b, 2022-02-24 (annotated)
- Committer:
- cspista
- Date:
- Thu Feb 24 08:06:02 2022 +0000
- Revision:
- 0:fe1656248d2b
final version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cspista | 0:fe1656248d2b | 1 | #include "mbed.h" |
| cspista | 0:fe1656248d2b | 2 | #include "rtos.h" |
| cspista | 0:fe1656248d2b | 3 | |
| cspista | 0:fe1656248d2b | 4 | Serial pc(USBTX,USBRX); //UART via ST-Link |
| cspista | 0:fe1656248d2b | 5 | Mutex stdio_mutex; |
| cspista | 0:fe1656248d2b | 6 | |
| cspista | 0:fe1656248d2b | 7 | void notify(const char* name, int state) |
| cspista | 0:fe1656248d2b | 8 | { |
| cspista | 0:fe1656248d2b | 9 | stdio_mutex.lock(); |
| cspista | 0:fe1656248d2b | 10 | pc.printf("%s: %d\n\r", name, state); |
| cspista | 0:fe1656248d2b | 11 | stdio_mutex.unlock(); |
| cspista | 0:fe1656248d2b | 12 | } |
| cspista | 0:fe1656248d2b | 13 | |
| cspista | 0:fe1656248d2b | 14 | void test_thread(void const *args) |
| cspista | 0:fe1656248d2b | 15 | { |
| cspista | 0:fe1656248d2b | 16 | while (true) { |
| cspista | 0:fe1656248d2b | 17 | notify((const char*)args, 0); |
| cspista | 0:fe1656248d2b | 18 | Thread::wait(1000); |
| cspista | 0:fe1656248d2b | 19 | notify((const char*)args, 1); |
| cspista | 0:fe1656248d2b | 20 | Thread::wait(1000); |
| cspista | 0:fe1656248d2b | 21 | } |
| cspista | 0:fe1656248d2b | 22 | } |
| cspista | 0:fe1656248d2b | 23 | |
| cspista | 0:fe1656248d2b | 24 | int main() |
| cspista | 0:fe1656248d2b | 25 | { |
| cspista | 0:fe1656248d2b | 26 | pc.baud(115200); |
| cspista | 0:fe1656248d2b | 27 | Thread t2(test_thread, (void *)"Th 2"); |
| cspista | 0:fe1656248d2b | 28 | Thread t3(test_thread, (void *)"Th 3"); |
| cspista | 0:fe1656248d2b | 29 | test_thread((void *)"Th 1"); |
| cspista | 0:fe1656248d2b | 30 | } |