Demonstration of a message queue + memory pool

Committer:
noutram
Date:
Wed Mar 09 17:55:53 2016 +0000
Revision:
5:31707531f715
Parent:
4:dae8898e55fe
Child:
6:2e463846b575
Solution to 6.1.7 - by changing the sequence in which the locks are taken.

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 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 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 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 4:dae8898e55fe 100 Thread t1(thread1);
noutram 4:dae8898e55fe 101 Thread t2(thread2);
noutram 1:4fb27aea76b2 102
noutram 4:dae8898e55fe 103 pc.printf("Main Thread\n");
noutram 2:70084af839d3 104 while (true) {
noutram 4:dae8898e55fe 105 Thread::wait(osWaitForever);
noutram 0:f916cefba2f4 106 }
noutram 0:f916cefba2f4 107
noutram 0:f916cefba2f4 108 }
noutram 2:70084af839d3 109
noutram 2:70084af839d3 110