Demonstration of a message queue + memory pool

Committer:
noutram
Date:
Mon Mar 14 16:32:56 2016 +0000
Revision:
7:cd015e83995a
Parent:
6:2e463846b575
Child:
8:c5663f5fa848
Added comments

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 4:dae8898e55fe 26 void thread1( const void* arg )
noutram 4:dae8898e55fe 27 {
noutram 4:dae8898e55fe 28 pc.printf("Entering thread 1\n");
noutram 4:dae8898e55fe 29 while (true) {
noutram 5:31707531f715 30 yellowLED = 1;
noutram 6:2e463846b575 31 Thread::wait(DELAY);
noutram 5:31707531f715 32 yellowLED = 0;
noutram 6:2e463846b575 33 Thread::wait(DELAY);
noutram 4:dae8898e55fe 34 }
noutram 2:70084af839d3 35 }
noutram 2:70084af839d3 36
noutram 6:2e463846b575 37 //This thread has higher priority
noutram 4:dae8898e55fe 38 void thread2( const void* arg )
noutram 0:f916cefba2f4 39 {
noutram 4:dae8898e55fe 40 pc.printf("Entering thread 2\n");
noutram 2:70084af839d3 41 while (true) {
noutram 5:31707531f715 42 redLED = 1;
noutram 6:2e463846b575 43 if (SW1 == 1) {
noutram 7:cd015e83995a 44
noutram 7:cd015e83995a 45 // 1) Select the 'type' of wait
noutram 7:cd015e83995a 46
noutram 6:2e463846b575 47 //wait_ms(osWaitForever);
noutram 6:2e463846b575 48 Thread::wait(osWaitForever);
noutram 6:2e463846b575 49 } else {
noutram 6:2e463846b575 50 Thread::wait(DELAY);
noutram 6:2e463846b575 51 }
noutram 5:31707531f715 52 redLED = 0;
noutram 6:2e463846b575 53 Thread::wait(DELAY);
noutram 6:2e463846b575 54 }
noutram 0:f916cefba2f4 55 }
noutram 0:f916cefba2f4 56
noutram 4:dae8898e55fe 57
noutram 0:f916cefba2f4 58 //Main thread
noutram 0:f916cefba2f4 59 int main() {
noutram 0:f916cefba2f4 60 redLED = 0;
noutram 0:f916cefba2f4 61 yellowLED = 0;
noutram 2:70084af839d3 62 greenLED = 0;
noutram 4:dae8898e55fe 63
noutram 0:f916cefba2f4 64 //Main thread ID
noutram 0:f916cefba2f4 65 tidMain = Thread::gettid();
noutram 0:f916cefba2f4 66
noutram 6:2e463846b575 67 Thread t1(thread1, NULL, osPriorityNormal);
noutram 7:cd015e83995a 68
noutram 7:cd015e83995a 69 // 2) Select the Thread Priority
noutram 7:cd015e83995a 70
noutram 6:2e463846b575 71 //Thread t2(thread2, NULL, osPriorityNormal);
noutram 6:2e463846b575 72 Thread t2(thread2, NULL, osPriorityAboveNormal);
noutram 1:4fb27aea76b2 73
noutram 4:dae8898e55fe 74 pc.printf("Main Thread\n");
noutram 2:70084af839d3 75 while (true) {
noutram 4:dae8898e55fe 76 Thread::wait(osWaitForever);
noutram 0:f916cefba2f4 77 }
noutram 0:f916cefba2f4 78
noutram 0:f916cefba2f4 79 }
noutram 2:70084af839d3 80
noutram 2:70084af839d3 81