Demonstrating a ISR signalling a high-priority thread
Dependencies: ELEC350-Practicals-FZ429
Fork of Task622Solution-mbedos54 by
main.cpp@11:9dcbcda8ea12, 2017-11-14 (annotated)
- Committer:
- noutram
- Date:
- Tue Nov 14 14:22:03 2017 +0000
- Revision:
- 11:9dcbcda8ea12
- Parent:
- 10:730250526509
Streamlined version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
| noutram | 10:730250526509 | 2 | #include <iostream> |
| noutram | 10:730250526509 | 3 | #include "sample_hardware.hpp" |
| noutram | 10:730250526509 | 4 | |
| noutram | 2:70084af839d3 | 5 | #include "string.h" |
| noutram | 2:70084af839d3 | 6 | #include <stdio.h> |
| noutram | 2:70084af839d3 | 7 | #include <ctype.h> |
| noutram | 0:f916cefba2f4 | 8 | |
| noutram | 8:b28defacd894 | 9 | #define SWITCH1_RELEASE 1 |
| noutram | 8:b28defacd894 | 10 | |
| noutram | 9:c46e831f8e4a | 11 | void thread1(); |
| noutram | 9:c46e831f8e4a | 12 | void thread2(); |
| noutram | 8:b28defacd894 | 13 | void switchISR(); |
| noutram | 0:f916cefba2f4 | 14 | |
| noutram | 10:730250526509 | 15 | InterruptIn sw(PE_12); |
| noutram | 8:b28defacd894 | 16 | |
| noutram | 8:b28defacd894 | 17 | //Threads |
| noutram | 10:730250526509 | 18 | Thread t1(osPriorityRealtime); //Higher priority |
| noutram | 10:730250526509 | 19 | Thread t2(osPriorityNormal); |
| noutram | 0:f916cefba2f4 | 20 | |
| noutram | 8:b28defacd894 | 21 | |
| noutram | 8:b28defacd894 | 22 | //Called on the falling edge of a switch |
| noutram | 8:b28defacd894 | 23 | void switchISR() { |
| noutram | 10:730250526509 | 24 | t1.signal_set(SWITCH1_RELEASE); //Very short |
| noutram | 8:b28defacd894 | 25 | } |
| noutram | 8:b28defacd894 | 26 | |
| noutram | 8:b28defacd894 | 27 | //High priority thread |
| noutram | 9:c46e831f8e4a | 28 | void thread1() |
| noutram | 4:dae8898e55fe | 29 | { |
| noutram | 8:b28defacd894 | 30 | redLED = 1; |
| noutram | 4:dae8898e55fe | 31 | while (true) { |
| noutram | 11:9dcbcda8ea12 | 32 | //Block (WAITING) for signal from the ISR |
| noutram | 8:b28defacd894 | 33 | Thread::signal_wait(SWITCH1_RELEASE); |
| noutram | 11:9dcbcda8ea12 | 34 | |
| noutram | 11:9dcbcda8ea12 | 35 | //Flash LED |
| noutram | 8:b28defacd894 | 36 | redLED = !redLED; |
| noutram | 11:9dcbcda8ea12 | 37 | |
| noutram | 11:9dcbcda8ea12 | 38 | //Wait for switch bounce to settle |
| noutram | 10:730250526509 | 39 | Thread::wait(200); |
| noutram | 10:730250526509 | 40 | |
| noutram | 10:730250526509 | 41 | //Block using BUSY-WAIT |
| noutram | 10:730250526509 | 42 | // Timer t; |
| noutram | 10:730250526509 | 43 | // t.start(); |
| noutram | 10:730250526509 | 44 | // while (t < 2); |
| noutram | 10:730250526509 | 45 | |
| noutram | 11:9dcbcda8ea12 | 46 | //Clear any additional signals (caused by switch bounce and ISR) |
| noutram | 10:730250526509 | 47 | t1.signal_clr(SWITCH1_RELEASE); //Debounce - clear pending signals |
| noutram | 4:dae8898e55fe | 48 | } |
| noutram | 2:70084af839d3 | 49 | } |
| noutram | 2:70084af839d3 | 50 | |
| noutram | 8:b28defacd894 | 51 | //This thread has normal priority |
| noutram | 9:c46e831f8e4a | 52 | void thread2() |
| noutram | 0:f916cefba2f4 | 53 | { |
| noutram | 8:b28defacd894 | 54 | greenLED = 1; |
| noutram | 10:730250526509 | 55 | while (true) { |
| noutram | 10:730250526509 | 56 | Thread::wait(500); // WAIT FOR 0.5 SECONDS - spinning |
| noutram | 8:b28defacd894 | 57 | greenLED = !greenLED; |
| noutram | 6:2e463846b575 | 58 | } |
| noutram | 0:f916cefba2f4 | 59 | } |
| noutram | 0:f916cefba2f4 | 60 | |
| noutram | 4:dae8898e55fe | 61 | |
| noutram | 8:b28defacd894 | 62 | // Main thread |
| noutram | 0:f916cefba2f4 | 63 | int main() { |
| noutram | 10:730250526509 | 64 | |
| noutram | 10:730250526509 | 65 | //Power on self-test |
| noutram | 10:730250526509 | 66 | post(); |
| noutram | 10:730250526509 | 67 | |
| noutram | 10:730250526509 | 68 | //Start Threads |
| noutram | 10:730250526509 | 69 | t1.start(thread1); |
| noutram | 10:730250526509 | 70 | t2.start(thread2); |
| noutram | 7:cd015e83995a | 71 | |
| noutram | 8:b28defacd894 | 72 | //Hook up interrupt |
| noutram | 10:730250526509 | 73 | sw.fall(switchISR); |
| noutram | 8:b28defacd894 | 74 | |
| noutram | 10:730250526509 | 75 | printf("Main Thread\n"); |
| noutram | 2:70084af839d3 | 76 | while (true) { |
| noutram | 4:dae8898e55fe | 77 | Thread::wait(osWaitForever); |
| noutram | 0:f916cefba2f4 | 78 | } |
| noutram | 0:f916cefba2f4 | 79 | |
| noutram | 0:f916cefba2f4 | 80 | } |
| noutram | 2:70084af839d3 | 81 | |
| noutram | 2:70084af839d3 | 82 |
