Demonstration of priority scheduling vs round robin
Dependencies: ELEC350-Practicals-FZ429
Fork of Task621-mbedos54 by
main.cpp@9:ac5a5458abe3, 2017-11-09 (annotated)
- Committer:
- noutram
- Date:
- Thu Nov 09 15:19:28 2017 +0000
- Revision:
- 9:ac5a5458abe3
- Parent:
- 8:2deeed44c6c3
updated for ELEC350
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
noutram | 9:ac5a5458abe3 | 2 | |
noutram | 2:70084af839d3 | 3 | #include "string.h" |
noutram | 2:70084af839d3 | 4 | #include <stdio.h> |
noutram | 2:70084af839d3 | 5 | #include <ctype.h> |
noutram | 9:ac5a5458abe3 | 6 | #include "sample_hardware.hpp" |
noutram | 0:f916cefba2f4 | 7 | |
noutram | 4:dae8898e55fe | 8 | #define DELAY 200 |
noutram | 0:f916cefba2f4 | 9 | |
noutram | 0:f916cefba2f4 | 10 | |
noutram | 0:f916cefba2f4 | 11 | //Thread ID for the Main function (CMSIS API) |
noutram | 0:f916cefba2f4 | 12 | osThreadId tidMain; |
noutram | 0:f916cefba2f4 | 13 | |
noutram | 8:2deeed44c6c3 | 14 | void thread1() |
noutram | 4:dae8898e55fe | 15 | { |
noutram | 9:ac5a5458abe3 | 16 | printf("Entering thread 1\n"); |
noutram | 4:dae8898e55fe | 17 | while (true) { |
noutram | 5:31707531f715 | 18 | yellowLED = 1; |
noutram | 6:2e463846b575 | 19 | Thread::wait(DELAY); |
noutram | 5:31707531f715 | 20 | yellowLED = 0; |
noutram | 6:2e463846b575 | 21 | Thread::wait(DELAY); |
noutram | 4:dae8898e55fe | 22 | } |
noutram | 2:70084af839d3 | 23 | } |
noutram | 2:70084af839d3 | 24 | |
noutram | 6:2e463846b575 | 25 | //This thread has higher priority |
noutram | 8:2deeed44c6c3 | 26 | void thread2() |
noutram | 0:f916cefba2f4 | 27 | { |
noutram | 9:ac5a5458abe3 | 28 | printf("Entering thread 2\n"); |
noutram | 2:70084af839d3 | 29 | while (true) { |
noutram | 5:31707531f715 | 30 | redLED = 1; |
noutram | 6:2e463846b575 | 31 | if (SW1 == 1) { |
noutram | 7:cd015e83995a | 32 | |
noutram | 7:cd015e83995a | 33 | // 1) Select the 'type' of wait |
noutram | 7:cd015e83995a | 34 | |
noutram | 6:2e463846b575 | 35 | //wait_ms(osWaitForever); |
noutram | 6:2e463846b575 | 36 | Thread::wait(osWaitForever); |
noutram | 6:2e463846b575 | 37 | } else { |
noutram | 6:2e463846b575 | 38 | Thread::wait(DELAY); |
noutram | 6:2e463846b575 | 39 | } |
noutram | 5:31707531f715 | 40 | redLED = 0; |
noutram | 6:2e463846b575 | 41 | Thread::wait(DELAY); |
noutram | 6:2e463846b575 | 42 | } |
noutram | 0:f916cefba2f4 | 43 | } |
noutram | 0:f916cefba2f4 | 44 | |
noutram | 4:dae8898e55fe | 45 | |
noutram | 0:f916cefba2f4 | 46 | //Main thread |
noutram | 0:f916cefba2f4 | 47 | int main() { |
noutram | 9:ac5a5458abe3 | 48 | post(); |
noutram | 9:ac5a5458abe3 | 49 | |
noutram | 0:f916cefba2f4 | 50 | //Main thread ID |
noutram | 0:f916cefba2f4 | 51 | tidMain = Thread::gettid(); |
noutram | 0:f916cefba2f4 | 52 | |
noutram | 9:ac5a5458abe3 | 53 | //Create a thread with normal priority |
noutram | 8:2deeed44c6c3 | 54 | Thread t1(osPriorityNormal); |
noutram | 8:2deeed44c6c3 | 55 | t1.start(thread1); |
noutram | 7:cd015e83995a | 56 | |
noutram | 7:cd015e83995a | 57 | // 2) Select the Thread Priority |
noutram | 7:cd015e83995a | 58 | |
noutram | 6:2e463846b575 | 59 | //Thread t2(thread2, NULL, osPriorityNormal); |
noutram | 8:2deeed44c6c3 | 60 | Thread t2(osPriorityAboveNormal); |
noutram | 8:2deeed44c6c3 | 61 | t2.start(thread2); |
noutram | 1:4fb27aea76b2 | 62 | |
noutram | 9:ac5a5458abe3 | 63 | printf("Main Thread\n"); |
noutram | 2:70084af839d3 | 64 | while (true) { |
noutram | 4:dae8898e55fe | 65 | Thread::wait(osWaitForever); |
noutram | 0:f916cefba2f4 | 66 | } |
noutram | 0:f916cefba2f4 | 67 | |
noutram | 0:f916cefba2f4 | 68 | } |
noutram | 2:70084af839d3 | 69 | |
noutram | 2:70084af839d3 | 70 |