mbed_example
/
rtos_isr
isr example
main.cpp@7:985db97e8ae0, 2018-10-15 (annotated)
- Committer:
- bridadan
- Date:
- Mon Oct 15 17:26:47 2018 -0500
- Revision:
- 7:985db97e8ae0
- Parent:
- 4:40078e697304
Updating example to show usage of the idle thread. Also showing Thread and Handler modes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:3043ec651794 | 1 | #include "mbed.h" |
mbed_official | 0:4979ad0f5cb9 | 2 | |
bridadan | 7:985db97e8ae0 | 3 | Ticker ticker; |
mab5449 | 4:40078e697304 | 4 | Thread thread; |
bridadan | 7:985db97e8ae0 | 5 | Queue<const char*, 5> trail; |
bridadan | 7:985db97e8ae0 | 6 | |
bridadan | 7:985db97e8ae0 | 7 | // Since we're printing from multiple threads, we need a mutex |
bridadan | 7:985db97e8ae0 | 8 | Mutex print_lock; |
bridadan | 7:985db97e8ae0 | 9 | |
bridadan | 7:985db97e8ae0 | 10 | enum ExecutionTypes { |
bridadan | 7:985db97e8ae0 | 11 | IDLE, |
bridadan | 7:985db97e8ae0 | 12 | USER, |
bridadan | 7:985db97e8ae0 | 13 | ISR |
bridadan | 7:985db97e8ae0 | 14 | }; |
emilmont | 1:3043ec651794 | 15 | |
bridadan | 7:985db97e8ae0 | 16 | const char* ExecutionMessages[] = { |
bridadan | 7:985db97e8ae0 | 17 | "the idle thread", |
bridadan | 7:985db97e8ae0 | 18 | "a user thread", |
bridadan | 7:985db97e8ae0 | 19 | "interrupt context" |
bridadan | 7:985db97e8ae0 | 20 | }; |
emilmont | 1:3043ec651794 | 21 | |
bridadan | 7:985db97e8ae0 | 22 | void handler() { |
bridadan | 7:985db97e8ae0 | 23 | // Check to see if we're in interrupt context |
bridadan | 7:985db97e8ae0 | 24 | if (core_util_is_isr_active()) { |
bridadan | 7:985db97e8ae0 | 25 | // Do not print since we're in interrupt context |
bridadan | 7:985db97e8ae0 | 26 | trail.put(&(ExecutionMessages[ISR])); |
bridadan | 7:985db97e8ae0 | 27 | } else { |
bridadan | 7:985db97e8ae0 | 28 | // Safe to print since we're in a user thread |
bridadan | 7:985db97e8ae0 | 29 | print_lock.lock(); |
bridadan | 7:985db97e8ae0 | 30 | printf("Starting user thread\r\n"); |
bridadan | 7:985db97e8ae0 | 31 | print_lock.unlock(); |
bridadan | 7:985db97e8ae0 | 32 | while(true) { |
bridadan | 7:985db97e8ae0 | 33 | trail.put(&(ExecutionMessages[USER])); |
bridadan | 7:985db97e8ae0 | 34 | wait(5); |
bridadan | 7:985db97e8ae0 | 35 | } |
emilmont | 1:3043ec651794 | 36 | } |
emilmont | 1:3043ec651794 | 37 | } |
emilmont | 1:3043ec651794 | 38 | |
bridadan | 7:985db97e8ae0 | 39 | void custom_idle_function() { |
bridadan | 7:985db97e8ae0 | 40 | // Custom idle behavior would go here |
bridadan | 7:985db97e8ae0 | 41 | // We won't print here since the default idle thread's stack is too small |
bridadan | 7:985db97e8ae0 | 42 | trail.put(&(ExecutionMessages[IDLE])); |
bridadan | 7:985db97e8ae0 | 43 | |
bridadan | 7:985db97e8ae0 | 44 | // Switch back to the default idle behavior |
bridadan | 7:985db97e8ae0 | 45 | Kernel::attach_idle_hook(NULL); |
bridadan | 7:985db97e8ae0 | 46 | } |
mab5449 | 4:40078e697304 | 47 | |
bridadan | 7:985db97e8ae0 | 48 | int main() { |
bridadan | 7:985db97e8ae0 | 49 | printf("Starting execution example\r\n"); |
bridadan | 7:985db97e8ae0 | 50 | |
bridadan | 7:985db97e8ae0 | 51 | // Attach the custom idle thread function |
bridadan | 7:985db97e8ae0 | 52 | Kernel::attach_idle_hook(custom_idle_function); |
bridadan | 7:985db97e8ae0 | 53 | |
bridadan | 7:985db97e8ae0 | 54 | // Trigger the interrupt every 3 seconds |
bridadan | 7:985db97e8ae0 | 55 | ticker.attach(handler, 3); |
bridadan | 7:985db97e8ae0 | 56 | |
bridadan | 7:985db97e8ae0 | 57 | // Start the user thread |
bridadan | 7:985db97e8ae0 | 58 | thread.start(handler); |
bridadan | 7:985db97e8ae0 | 59 | |
bridadan | 7:985db97e8ae0 | 60 | // Get the past exectuion trail |
emilmont | 1:3043ec651794 | 61 | while (true) { |
bridadan | 7:985db97e8ae0 | 62 | osEvent evt = trail.get(); |
emilmont | 1:3043ec651794 | 63 | if (evt.status != osEventMessage) { |
bridadan | 7:985db97e8ae0 | 64 | print_lock.lock(); |
bridadan | 7:985db97e8ae0 | 65 | printf("Failed to retrieve the execution trail (returned %02x)\r\n", evt.status); |
bridadan | 7:985db97e8ae0 | 66 | print_lock.unlock(); |
emilmont | 1:3043ec651794 | 67 | } else { |
bridadan | 7:985db97e8ae0 | 68 | print_lock.lock(); |
bridadan | 7:985db97e8ae0 | 69 | printf("Execution was in %s\r\n", *(const char**)evt.value.v); |
bridadan | 7:985db97e8ae0 | 70 | print_lock.unlock(); |
emilmont | 1:3043ec651794 | 71 | } |
emilmont | 1:3043ec651794 | 72 | } |
emilmont | 1:3043ec651794 | 73 | } |