gdfgdh

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task633-mbedos-FZ429 by University of Plymouth - Stages 1, 2 and 3

Committer:
noutram
Date:
Mon Mar 14 19:48:24 2016 +0000
Revision:
8:c5663f5fa848
Parent:
7:cd015e83995a
Child:
9:31031bbb59c7
Task 622 - mixing interrupts and threads

Who changed what in which revision?

UserRevisionLine numberNew 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 8:c5663f5fa848 9 void thread1( const void* );
noutram 8:c5663f5fa848 10 void thread2( const void* );
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 4:dae8898e55fe 43 void thread1( const void* arg )
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 4:dae8898e55fe 66 void thread2( const void* arg )
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 8:c5663f5fa848 82 //Threads
noutram 8:c5663f5fa848 83 t1 = new Thread(&thread1, NULL, osPriorityRealtime); //HIGH PRIORITY
noutram 8:c5663f5fa848 84 t2 = new Thread(&thread2, NULL, osPriorityNormal);
noutram 8:c5663f5fa848 85
noutram 8:c5663f5fa848 86 // Thread IDs
noutram 0:f916cefba2f4 87 tidMain = Thread::gettid();
noutram 8:c5663f5fa848 88 tid1 = t1->gettid();
noutram 8:c5663f5fa848 89 tid2 = t2->gettid();
noutram 7:cd015e83995a 90
noutram 8:c5663f5fa848 91 //TBD: Hook up interrupt
noutram 8:c5663f5fa848 92
noutram 4:dae8898e55fe 93 pc.printf("Main Thread\n");
noutram 2:70084af839d3 94 while (true) {
noutram 4:dae8898e55fe 95 Thread::wait(osWaitForever);
noutram 0:f916cefba2f4 96 }
noutram 0:f916cefba2f4 97 }
noutram 2:70084af839d3 98
noutram 2:70084af839d3 99