Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task622-mbedos54 by
main.cpp@9:f9031b045b9a, 2017-04-03 (annotated)
- Committer:
- noutram
- Date:
- Mon Apr 03 11:53:02 2017 +0000
- Revision:
- 9:f9031b045b9a
- Parent:
- 8:c5663f5fa848
updated for mbed os 5.4
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
| noutram | 0:f916cefba2f4 | 2 | #include "rtos.h" |
| noutram | 2:70084af839d3 | 3 | #include "string.h" |
| noutram | 2:70084af839d3 | 4 | #include <stdio.h> |
| noutram | 2:70084af839d3 | 5 | #include <ctype.h> |
| noutram | 0:f916cefba2f4 | 6 | |
| noutram | 8:c5663f5fa848 | 7 | #define SWITCH1_RELEASE 1 |
| noutram | 8:c5663f5fa848 | 8 | |
| noutram | 9:f9031b045b9a | 9 | void thread1(); |
| noutram | 9:f9031b045b9a | 10 | void thread2(); |
| noutram | 8:c5663f5fa848 | 11 | void switchISR(); |
| noutram | 0:f916cefba2f4 | 12 | |
| noutram | 0:f916cefba2f4 | 13 | //Digital outputs |
| noutram | 0:f916cefba2f4 | 14 | DigitalOut onBoardLED(LED1); |
| noutram | 0:f916cefba2f4 | 15 | DigitalOut redLED(D7); |
| noutram | 0:f916cefba2f4 | 16 | DigitalOut yellowLED(D6); |
| noutram | 0:f916cefba2f4 | 17 | DigitalOut greenLED(D5); |
| noutram | 0:f916cefba2f4 | 18 | |
| noutram | 2:70084af839d3 | 19 | //Serial Interface |
| noutram | 2:70084af839d3 | 20 | Serial pc(USBTX, USBRX); |
| noutram | 2:70084af839d3 | 21 | |
| noutram | 0:f916cefba2f4 | 22 | //Digital inputs |
| noutram | 0:f916cefba2f4 | 23 | DigitalIn onBoardSwitch(USER_BUTTON); |
| noutram | 8:c5663f5fa848 | 24 | DigitalIn sw1(D4); //CONSIDER CHANGING THIS TO AN INTERRUPT |
| noutram | 8:c5663f5fa848 | 25 | DigitalIn sw2(D3); |
| noutram | 8:c5663f5fa848 | 26 | |
| noutram | 8:c5663f5fa848 | 27 | //Threads |
| noutram | 8:c5663f5fa848 | 28 | Thread *t1; |
| noutram | 8:c5663f5fa848 | 29 | Thread *t2; |
| noutram | 0:f916cefba2f4 | 30 | |
| noutram | 0:f916cefba2f4 | 31 | //Thread ID for the Main function (CMSIS API) |
| noutram | 0:f916cefba2f4 | 32 | osThreadId tidMain; |
| noutram | 8:c5663f5fa848 | 33 | osThreadId tid1; |
| noutram | 8:c5663f5fa848 | 34 | osThreadId tid2; |
| noutram | 0:f916cefba2f4 | 35 | |
| noutram | 8:c5663f5fa848 | 36 | |
| noutram | 8:c5663f5fa848 | 37 | //TBD: Call this on the falling edge of SW1 |
| noutram | 8:c5663f5fa848 | 38 | void switchISR() { |
| noutram | 8:c5663f5fa848 | 39 | //TBD |
| noutram | 8:c5663f5fa848 | 40 | } |
| noutram | 8:c5663f5fa848 | 41 | |
| noutram | 8:c5663f5fa848 | 42 | //High priority thread |
| noutram | 9:f9031b045b9a | 43 | void thread1() |
| noutram | 4:dae8898e55fe | 44 | { |
| noutram | 8:c5663f5fa848 | 45 | redLED = 1; |
| noutram | 4:dae8898e55fe | 46 | while (true) { |
| noutram | 8:c5663f5fa848 | 47 | |
| noutram | 8:c5663f5fa848 | 48 | // THIS IS BAD BAD BAD BAD BAD BAD |
| noutram | 8:c5663f5fa848 | 49 | // BLOCK ON FALLING EDGE OF SW1 BY RAPID POLLING (SPINNING) |
| noutram | 8:c5663f5fa848 | 50 | // Thread is blocked in the RUNNING state |
| noutram | 8:c5663f5fa848 | 51 | while (sw1 == 0); |
| noutram | 8:c5663f5fa848 | 52 | wait_ms(200); //Wait for debounce |
| noutram | 8:c5663f5fa848 | 53 | while (sw1 == 1); |
| noutram | 8:c5663f5fa848 | 54 | // TODO: FIX THIS! GET THE INTERRUPT TO SIGNAL THIS THREAD |
| noutram | 8:c5663f5fa848 | 55 | |
| noutram | 8:c5663f5fa848 | 56 | redLED = !redLED; |
| noutram | 8:c5663f5fa848 | 57 | Thread::wait(1000); //Thread in WAITING state |
| noutram | 8:c5663f5fa848 | 58 | redLED = !redLED; |
| noutram | 8:c5663f5fa848 | 59 | Thread::wait(1000); //Thread in WAITING state |
| noutram | 4:dae8898e55fe | 60 | } |
| noutram | 2:70084af839d3 | 61 | } |
| noutram | 2:70084af839d3 | 62 | |
| noutram | 8:c5663f5fa848 | 63 | // This thread has normal priority |
| noutram | 8:c5663f5fa848 | 64 | // It is supposed to flash the green LED every second. |
| noutram | 8:c5663f5fa848 | 65 | // THIS IS NOT WORKING because it is currently being starved by thread1 (while polling the switch) |
| noutram | 9:f9031b045b9a | 66 | void thread2() |
| noutram | 0:f916cefba2f4 | 67 | { |
| noutram | 8:c5663f5fa848 | 68 | greenLED = 1; |
| noutram | 8:c5663f5fa848 | 69 | while (true) { |
| noutram | 8:c5663f5fa848 | 70 | Thread::wait(500); |
| noutram | 8:c5663f5fa848 | 71 | greenLED = !greenLED; |
| noutram | 6:2e463846b575 | 72 | } |
| noutram | 0:f916cefba2f4 | 73 | } |
| noutram | 0:f916cefba2f4 | 74 | |
| noutram | 4:dae8898e55fe | 75 | |
| noutram | 8:c5663f5fa848 | 76 | // Main thread |
| noutram | 0:f916cefba2f4 | 77 | int main() { |
| noutram | 0:f916cefba2f4 | 78 | redLED = 0; |
| noutram | 0:f916cefba2f4 | 79 | yellowLED = 0; |
| noutram | 2:70084af839d3 | 80 | greenLED = 0; |
| noutram | 8:c5663f5fa848 | 81 | |
| noutram | 9:f9031b045b9a | 82 | //Threads - created dynamically with new |
| noutram | 9:f9031b045b9a | 83 | t1 = new Thread(osPriorityRealtime); //HIGH PRIORITY |
| noutram | 9:f9031b045b9a | 84 | t2 = new Thread(osPriorityNormal); |
| noutram | 9:f9031b045b9a | 85 | |
| noutram | 9:f9031b045b9a | 86 | //Note t1 and t2 are pointers (32 bit addresses), so the arrow -> is used |
| noutram | 9:f9031b045b9a | 87 | t1->start(thread1); |
| noutram | 9:f9031b045b9a | 88 | t2->start(thread2); |
| noutram | 9:f9031b045b9a | 89 | |
| noutram | 8:c5663f5fa848 | 90 | // Thread IDs |
| noutram | 0:f916cefba2f4 | 91 | tidMain = Thread::gettid(); |
| noutram | 8:c5663f5fa848 | 92 | tid1 = t1->gettid(); |
| noutram | 8:c5663f5fa848 | 93 | tid2 = t2->gettid(); |
| noutram | 7:cd015e83995a | 94 | |
| noutram | 8:c5663f5fa848 | 95 | //TBD: Hook up interrupt |
| noutram | 8:c5663f5fa848 | 96 | |
| noutram | 4:dae8898e55fe | 97 | pc.printf("Main Thread\n"); |
| noutram | 2:70084af839d3 | 98 | while (true) { |
| noutram | 4:dae8898e55fe | 99 | Thread::wait(osWaitForever); |
| noutram | 0:f916cefba2f4 | 100 | } |
| noutram | 0:f916cefba2f4 | 101 | } |
| noutram | 2:70084af839d3 | 102 | |
| noutram | 2:70084af839d3 | 103 |