Skip to content

File tree

10 files changed

+215
-0
lines changed

10 files changed

+215
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
{
12+
while (true) {
13+
// Signal flags that are reported as event are automatically cleared.
14+
ThisThread::flags_wait_any(0x1);
15+
led = !led;
16+
}
17+
}
18+
19+
int main(void)
20+
{
21+
thread.start(callback(led_thread));
22+
23+
while (true) {
24+
ThisThread::sleep_for(1000);
25+
thread.flags_set(0x1);
26+
}
27+
}

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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
{
28+
// Check to see if we're in interrupt context
29+
if (core_util_is_isr_active()) {
30+
// Do not print since we're in interrupt context
31+
trail.put(&(ExecutionMessages[ISR]));
32+
} else {
33+
// Safe to print since we're in a user thread
34+
print_lock.lock();
35+
printf("Starting user thread\r\n");
36+
print_lock.unlock();
37+
while (true) {
38+
trail.put(&(ExecutionMessages[USER]));
39+
ThisThread::sleep_for(500);
40+
}
41+
}
42+
}
43+
44+
void custom_idle_function()
45+
{
46+
// Custom idle behavior would go here
47+
// We won't print here since the default idle thread's stack is too small
48+
trail.put(&(ExecutionMessages[IDLE]));
49+
50+
// Switch back to the default idle behavior
51+
Kernel::attach_idle_hook(NULL);
52+
}
53+
54+
int main()
55+
{
56+
printf("Starting execution example\r\n");
57+
58+
// Attach the custom idle thread function
59+
Kernel::attach_idle_hook(custom_idle_function);
60+
61+
// Trigger the interrupt every 3 seconds
62+
ticker.attach(handler, 3);
63+
64+
// Start the user thread
65+
thread.start(handler);
66+
67+
// Get the past exectuion trail
68+
while (true) {
69+
osEvent evt = trail.get();
70+
if (evt.status != osEventMessage) {
71+
print_lock.lock();
72+
printf("Failed to retrieve the execution trail (returned %02lx)\r\n", evt.status);
73+
print_lock.unlock();
74+
} else {
75+
print_lock.lock();
76+
printf("Execution was in %s\r\n", *(const char **)evt.value.v);
77+
print_lock.unlock();
78+
}
79+
}
80+
}

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
{
13+
stdio_mutex.lock();
14+
printf("%s: %d\n\r", name, state);
15+
stdio_mutex.unlock();
16+
}
17+
18+
void test_thread(void const *args)
19+
{
20+
while (true) {
21+
notify((const char *)args, 0);
22+
ThisThread::sleep_for(1000);
23+
notify((const char *)args, 1);
24+
ThisThread::sleep_for(1000);
25+
}
26+
}
27+
28+
int main()
29+
{
30+
t2.start(callback(test_thread, (void *)"Th 2"));
31+
t3.start(callback(test_thread, (void *)"Th 3"));
32+
33+
test_thread((void *)"Th 1");
34+
}

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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
{
13+
while (true) {
14+
one_slot.acquire();
15+
printf("%s\n\r", (const char *)name);
16+
ThisThread::sleep_for(1000);
17+
one_slot.release();
18+
}
19+
}
20+
21+
int main(void)
22+
{
23+
t2.start(callback(test_thread, (void *)"Th 2"));
24+
t3.start(callback(test_thread, (void *)"Th 3"));
25+
26+
test_thread((void *)"Th 1");
27+
}
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
{
14+
while (running) {
15+
*led = !*led;
16+
ThisThread::sleep_for(1000);
17+
}
18+
}
19+
20+
// Spawns a thread to run blink for 5 seconds
21+
int main()
22+
{
23+
thread.start(callback(blink, &led1));
24+
ThisThread::sleep_for(5000);
25+
running = false;
26+
thread.join();
27+
}

0 commit comments

Comments
 (0)