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