updated to mbed os 5.4

Fork of Task633 by Stage-1 Students SoCEM

Committer:
noutram
Date:
Wed Mar 09 17:18:55 2016 +0000
Revision:
4:dae8898e55fe
Parent:
3:423191a375dc
Child:
5:31707531f715
Deadlock demonstration

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 4:dae8898e55fe 7 #define DELAY 200
noutram 0:f916cefba2f4 8
noutram 0:f916cefba2f4 9 //Digital outputs
noutram 0:f916cefba2f4 10 DigitalOut onBoardLED(LED1);
noutram 0:f916cefba2f4 11 DigitalOut redLED(D7);
noutram 0:f916cefba2f4 12 DigitalOut yellowLED(D6);
noutram 0:f916cefba2f4 13 DigitalOut greenLED(D5);
noutram 0:f916cefba2f4 14
noutram 2:70084af839d3 15 //Serial Interface
noutram 2:70084af839d3 16 Serial pc(USBTX, USBRX);
noutram 2:70084af839d3 17
noutram 0:f916cefba2f4 18 //Digital inputs
noutram 0:f916cefba2f4 19 DigitalIn onBoardSwitch(USER_BUTTON);
noutram 0:f916cefba2f4 20 DigitalIn SW1(D4);
noutram 0:f916cefba2f4 21 DigitalIn SW2(D3);
noutram 0:f916cefba2f4 22
noutram 0:f916cefba2f4 23 //Thread ID for the Main function (CMSIS API)
noutram 0:f916cefba2f4 24 osThreadId tidMain;
noutram 0:f916cefba2f4 25
noutram 2:70084af839d3 26 //Thread sychronisation primatives
noutram 4:dae8898e55fe 27 Mutex lock1;
noutram 4:dae8898e55fe 28 Mutex lock2;
noutram 4:dae8898e55fe 29 unsigned long sw1Count = 0;
noutram 4:dae8898e55fe 30 unsigned long sw2Count = 0;
noutram 2:70084af839d3 31
noutram 4:dae8898e55fe 32 void thread1( const void* arg )
noutram 4:dae8898e55fe 33 {
noutram 4:dae8898e55fe 34 pc.printf("Entering thread 1\n");
noutram 4:dae8898e55fe 35 while (true) {
noutram 4:dae8898e55fe 36
noutram 4:dae8898e55fe 37 //Start critical section
noutram 4:dae8898e55fe 38 lock1.lock();
noutram 4:dae8898e55fe 39
noutram 4:dae8898e55fe 40 sw1Count++;
noutram 4:dae8898e55fe 41 printf("\nCount1 = %lu", sw1Count);
noutram 4:dae8898e55fe 42
noutram 4:dae8898e55fe 43 //Thread::wait(1); //1ms
noutram 1:4fb27aea76b2 44
noutram 4:dae8898e55fe 45 if (SW1 == 1) {
noutram 4:dae8898e55fe 46 yellowLED = 1;
noutram 4:dae8898e55fe 47 lock2.lock();
noutram 4:dae8898e55fe 48 sw2Count--;
noutram 4:dae8898e55fe 49 lock2.unlock();
noutram 4:dae8898e55fe 50 yellowLED = 0;
noutram 4:dae8898e55fe 51 }
noutram 4:dae8898e55fe 52
noutram 4:dae8898e55fe 53 //End critical section
noutram 4:dae8898e55fe 54 lock1.unlock();
noutram 4:dae8898e55fe 55
noutram 4:dae8898e55fe 56 Thread::wait(DELAY);
noutram 4:dae8898e55fe 57 }
noutram 2:70084af839d3 58 }
noutram 2:70084af839d3 59
noutram 4:dae8898e55fe 60 void thread2( const void* arg )
noutram 0:f916cefba2f4 61 {
noutram 4:dae8898e55fe 62 pc.printf("Entering thread 2\n");
noutram 2:70084af839d3 63 while (true) {
noutram 2:70084af839d3 64
noutram 4:dae8898e55fe 65 //Start critical section
noutram 4:dae8898e55fe 66 lock2.lock();
noutram 2:70084af839d3 67
noutram 4:dae8898e55fe 68 sw2Count++;
noutram 4:dae8898e55fe 69 printf("\nCount2 = %lu", sw2Count);
noutram 4:dae8898e55fe 70
noutram 4:dae8898e55fe 71 //Thread::wait(1); //1ms
noutram 1:4fb27aea76b2 72
noutram 4:dae8898e55fe 73 if (SW2 == 1) {
noutram 4:dae8898e55fe 74 redLED = 1;
noutram 4:dae8898e55fe 75 lock1.lock();
noutram 4:dae8898e55fe 76 sw1Count--;
noutram 4:dae8898e55fe 77 lock1.unlock();
noutram 4:dae8898e55fe 78 redLED = 0;
noutram 4:dae8898e55fe 79 }
noutram 4:dae8898e55fe 80 //End critical section
noutram 4:dae8898e55fe 81 lock2.unlock();
noutram 1:4fb27aea76b2 82
noutram 4:dae8898e55fe 83 Thread::wait(DELAY);
noutram 4:dae8898e55fe 84 }
noutram 0:f916cefba2f4 85 }
noutram 0:f916cefba2f4 86
noutram 4:dae8898e55fe 87
noutram 0:f916cefba2f4 88 //Main thread
noutram 0:f916cefba2f4 89 int main() {
noutram 0:f916cefba2f4 90 redLED = 0;
noutram 0:f916cefba2f4 91 yellowLED = 0;
noutram 2:70084af839d3 92 greenLED = 0;
noutram 4:dae8898e55fe 93
noutram 0:f916cefba2f4 94 //Main thread ID
noutram 0:f916cefba2f4 95 tidMain = Thread::gettid();
noutram 0:f916cefba2f4 96
noutram 4:dae8898e55fe 97 //Threads
noutram 4:dae8898e55fe 98 Thread t1(thread1);
noutram 4:dae8898e55fe 99 Thread t2(thread2);
noutram 1:4fb27aea76b2 100
noutram 4:dae8898e55fe 101 pc.printf("Main Thread\n");
noutram 2:70084af839d3 102 while (true) {
noutram 4:dae8898e55fe 103 Thread::wait(osWaitForever);
noutram 0:f916cefba2f4 104 }
noutram 0:f916cefba2f4 105
noutram 0:f916cefba2f4 106 }
noutram 2:70084af839d3 107
noutram 2:70084af839d3 108