Skip to content

File tree

10 files changed

+203
-0
lines changed

10 files changed

+203
-0
lines changed

APIs_RTOS/Flags/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Flags example
2+
3+
This example shows how to use Flags for threads communication.
4+

APIs_RTOS/Flags/main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
Thread thread;
8+
DigitalOut led(LED1);
9+
10+
void led_thread() {
11+
while (true) {
12+
// Signal flags that are reported as event are automatically cleared.
13+
ThisThread::flags_wait_any(0x1);
14+
led = !led;
15+
}
16+
}
17+
18+
int main (void) {
19+
thread.start(callback(led_thread));
20+
21+
while (true) {
22+
ThisThread::sleep_for(1000);
23+
thread.flags_set(0x1);
24+
}
25+
}

APIs_RTOS/Isr/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Isr example
2+
3+
This example shows how to use various isr mechanisms in MbedOS.
4+

APIs_RTOS/Isr/main.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
Ticker ticker;
8+
Thread thread;
9+
Queue<const char*, 5> trail;
10+
11+
// Since we're printing from multiple threads, we need a mutex
12+
Mutex print_lock;
13+
14+
enum ExecutionTypes {
15+
IDLE,
16+
USER,
17+
ISR
18+
};
19+
20+
const char* ExecutionMessages[] = {
21+
"the idle thread",
22+
"a user thread",
23+
"interrupt context"
24+
};
25+
26+
void handler() {
27+
// Check to see if we're in interrupt context
28+
if (core_util_is_isr_active()) {
29+
// Do not print since we're in interrupt context
30+
trail.put(&(ExecutionMessages[ISR]));
31+
} else {
32+
// Safe to print since we're in a user thread
33+
print_lock.lock();
34+
printf("Starting user thread\r\n");
35+
print_lock.unlock();
36+
while(true) {
37+
trail.put(&(ExecutionMessages[USER]));
38+
ThisThread::sleep_for(500);
39+
}
40+
}
41+
}
42+
43+
void custom_idle_function() {
44+
// Custom idle behavior would go here
45+
// We won't print here since the default idle thread's stack is too small
46+
trail.put(&(ExecutionMessages[IDLE]));
47+
48+
// Switch back to the default idle behavior
49+
Kernel::attach_idle_hook(NULL);
50+
}
51+
52+
int main() {
53+
printf("Starting execution example\r\n");
54+
55+
// Attach the custom idle thread function
56+
Kernel::attach_idle_hook(custom_idle_function);
57+
58+
// Trigger the interrupt every 3 seconds
59+
ticker.attach(handler, 3);
60+
61+
// Start the user thread
62+
thread.start(handler);
63+
64+
// Get the past exectuion trail
65+
while (true) {
66+
osEvent evt = trail.get();
67+
if (evt.status != osEventMessage) {
68+
print_lock.lock();
69+
printf("Failed to retrieve the execution trail (returned %02lx)\r\n", evt.status);
70+
print_lock.unlock();
71+
} else {
72+
print_lock.lock();
73+
printf("Execution was in %s\r\n", *(const char**)evt.value.v);
74+
print_lock.unlock();
75+
}
76+
}
77+
}

APIs_RTOS/Mutex/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Mutex example
2+
3+
This example shows how to use Mutex for threads synchronization.
4+

APIs_RTOS/Mutex/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
Mutex stdio_mutex;
8+
Thread t2;
9+
Thread t3;
10+
11+
void notify(const char* name, int state) {
12+
stdio_mutex.lock();
13+
printf("%s: %d\n\r", name, state);
14+
stdio_mutex.unlock();
15+
}
16+
17+
void test_thread(void const *args) {
18+
while (true) {
19+
notify((const char*)args, 0);
20+
ThisThread::sleep_for(1000);
21+
notify((const char*)args, 1);
22+
ThisThread::sleep_for(1000);
23+
}
24+
}
25+
26+
int main() {
27+
t2.start(callback(test_thread, (void *)"Th 2"));
28+
t3.start(callback(test_thread, (void *)"Th 3"));
29+
30+
test_thread((void *)"Th 1");
31+
}

APIs_RTOS/Semaphore/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Semaphore example
2+
3+
This example shows how to use Semaphore for threads synchronization.
4+

APIs_RTOS/Semaphore/main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
Semaphore one_slot(1);
8+
Thread t2;
9+
Thread t3;
10+
11+
void test_thread(void const *name) {
12+
while (true) {
13+
one_slot.acquire();
14+
printf("%s\n\r", (const char*)name);
15+
ThisThread::sleep_for(1000);
16+
one_slot.release();
17+
}
18+
}
19+
20+
int main (void) {
21+
t2.start(callback(test_thread, (void *)"Th 2"));
22+
t3.start(callback(test_thread, (void *)"Th 3"));
23+
24+
test_thread((void *)"Th 1");
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Simple threading example with callback
2+
3+
This example shows how to run thread using callbacks.
4+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
Thread thread;
8+
DigitalOut led1(LED1);
9+
volatile bool running = true;
10+
11+
// Blink function toggles the led in a long running loop
12+
void blink(DigitalOut *led) {
13+
while (running) {
14+
*led = !*led;
15+
ThisThread::sleep_for(1000);
16+
}
17+
}
18+
19+
// Spawns a thread to run blink for 5 seconds
20+
int main() {
21+
thread.start(callback(blink, &led1));
22+
ThisThread::sleep_for(5000);
23+
running = false;
24+
thread.join();
25+
}

0 commit comments

Comments
 (0)