Solution to Task 6.1.7 Note the order in which the locks are taken. Updated for mbed os 5.4

Fork of Task617Solution-mbedos54 by Stage-1 Students SoCEM

Committer:
noutram
Date:
Mon Apr 03 11:38:04 2017 +0000
Revision:
6:d16ce38e9b8a
Parent:
5:31707531f715
Updated for mbed os 5.4

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 6:d16ce38e9b8a 32 void thread1()
noutram 4:dae8898e55fe 33 {
noutram 4:dae8898e55fe 34 pc.printf("Entering thread 1\n");
noutram 4:dae8898e55fe 35 while (true) {
noutram 5:31707531f715 36 yellowLED = 1;
noutram 4:dae8898e55fe 37
noutram 4:dae8898e55fe 38 //Start critical section
noutram 4:dae8898e55fe 39 lock1.lock();
noutram 4:dae8898e55fe 40
noutram 4:dae8898e55fe 41 sw1Count++;
noutram 4:dae8898e55fe 42 printf("\nCount1 = %lu", sw1Count);
noutram 4:dae8898e55fe 43
noutram 5:31707531f715 44 Thread::wait(1); //1ms
noutram 4:dae8898e55fe 45
noutram 4:dae8898e55fe 46 //End critical section
noutram 4:dae8898e55fe 47 lock1.unlock();
noutram 5:31707531f715 48
noutram 5:31707531f715 49 if (SW1 == 1) {
noutram 5:31707531f715 50 lock2.lock();
noutram 5:31707531f715 51 sw2Count--;
noutram 5:31707531f715 52 lock2.unlock();
noutram 5:31707531f715 53 }
noutram 4:dae8898e55fe 54
noutram 5:31707531f715 55 yellowLED = 0;
noutram 4:dae8898e55fe 56 Thread::wait(DELAY);
noutram 4:dae8898e55fe 57 }
noutram 2:70084af839d3 58 }
noutram 2:70084af839d3 59
noutram 6:d16ce38e9b8a 60 void thread2()
noutram 0:f916cefba2f4 61 {
noutram 4:dae8898e55fe 62 pc.printf("Entering thread 2\n");
noutram 2:70084af839d3 63 while (true) {
noutram 5:31707531f715 64 redLED = 1;
noutram 2:70084af839d3 65
noutram 4:dae8898e55fe 66 //Start critical section
noutram 4:dae8898e55fe 67 lock2.lock();
noutram 2:70084af839d3 68
noutram 4:dae8898e55fe 69 sw2Count++;
noutram 4:dae8898e55fe 70 printf("\nCount2 = %lu", sw2Count);
noutram 4:dae8898e55fe 71
noutram 5:31707531f715 72 Thread::wait(1); //1ms
noutram 1:4fb27aea76b2 73
noutram 5:31707531f715 74 //End critical section
noutram 5:31707531f715 75 lock2.unlock();
noutram 5:31707531f715 76
noutram 5:31707531f715 77 if (SW2 == 1) {
noutram 4:dae8898e55fe 78 lock1.lock();
noutram 4:dae8898e55fe 79 sw1Count--;
noutram 4:dae8898e55fe 80 lock1.unlock();
noutram 4:dae8898e55fe 81 }
noutram 5:31707531f715 82
noutram 5:31707531f715 83 redLED = 0;
noutram 5:31707531f715 84 Thread::wait(DELAY);
noutram 1:4fb27aea76b2 85
noutram 4:dae8898e55fe 86 }
noutram 0:f916cefba2f4 87 }
noutram 0:f916cefba2f4 88
noutram 4:dae8898e55fe 89
noutram 0:f916cefba2f4 90 //Main thread
noutram 0:f916cefba2f4 91 int main() {
noutram 0:f916cefba2f4 92 redLED = 0;
noutram 0:f916cefba2f4 93 yellowLED = 0;
noutram 2:70084af839d3 94 greenLED = 0;
noutram 4:dae8898e55fe 95
noutram 0:f916cefba2f4 96 //Main thread ID
noutram 0:f916cefba2f4 97 tidMain = Thread::gettid();
noutram 0:f916cefba2f4 98
noutram 4:dae8898e55fe 99 //Threads
noutram 6:d16ce38e9b8a 100 Thread t1, t2;
noutram 6:d16ce38e9b8a 101
noutram 6:d16ce38e9b8a 102 t1.start(thread1);
noutram 6:d16ce38e9b8a 103 t2.start(thread2);
noutram 1:4fb27aea76b2 104
noutram 4:dae8898e55fe 105 pc.printf("Main Thread\n");
noutram 2:70084af839d3 106 while (true) {
noutram 4:dae8898e55fe 107 Thread::wait(osWaitForever);
noutram 0:f916cefba2f4 108 }
noutram 0:f916cefba2f4 109
noutram 0:f916cefba2f4 110 }
noutram 2:70084af839d3 111
noutram 2:70084af839d3 112