Mail Queue Task

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task633-mbedos54 by Nicholas Outram

Committer:
noutram
Date:
Tue Nov 21 11:53:44 2017 +0000
Revision:
14:03836b560d3d
Parent:
13:f05daa68720c
Updated for FZ429

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:f916cefba2f4 1 #include "mbed.h"
noutram 12:4605f1c51824 2 #include "sample_hardware.hpp"
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 11:ae623ddb2eff 9 void thread1();
noutram 11:ae623ddb2eff 10 void thread2();
noutram 8:c5663f5fa848 11 void switchISR();
noutram 0:f916cefba2f4 12
noutram 8:c5663f5fa848 13
noutram 8:c5663f5fa848 14 //Threads
noutram 13:f05daa68720c 15 Thread samplingThread(osPriorityRealtime);
noutram 12:4605f1c51824 16 Thread t1;
noutram 0:f916cefba2f4 17
noutram 10:3a3d2a571c8f 18 //Class type
noutram 10:3a3d2a571c8f 19 class message_t {
noutram 10:3a3d2a571c8f 20 public:
noutram 13:f05daa68720c 21 float adcValue;
noutram 10:3a3d2a571c8f 22 int sw1State;
noutram 10:3a3d2a571c8f 23 int sw2State;
noutram 10:3a3d2a571c8f 24
noutram 10:3a3d2a571c8f 25 //Constructor
noutram 10:3a3d2a571c8f 26 message_t(float f, int s1, int s2) {
noutram 10:3a3d2a571c8f 27 adcValue = f;
noutram 10:3a3d2a571c8f 28 sw1State = s1;
noutram 10:3a3d2a571c8f 29 sw2State = s2;
noutram 10:3a3d2a571c8f 30 }
noutram 10:3a3d2a571c8f 31 };
noutram 10:3a3d2a571c8f 32
noutram 10:3a3d2a571c8f 33 //Memory Pool - with capacity for 16 message_t types
noutram 11:ae623ddb2eff 34 //MemoryPool<message_t, 16> mpool;
noutram 0:f916cefba2f4 35
noutram 10:3a3d2a571c8f 36 //Message queue - matched to the memory pool
noutram 11:ae623ddb2eff 37 //Queue<message_t, 16> queue;
noutram 11:ae623ddb2eff 38
noutram 11:ae623ddb2eff 39 //Mail queue
noutram 11:ae623ddb2eff 40 Mail<message_t, 16> mail_box;
noutram 11:ae623ddb2eff 41
noutram 8:c5663f5fa848 42
noutram 9:31031bbb59c7 43 // Call this on precise intervals
noutram 9:31031bbb59c7 44 void adcISR() {
noutram 13:f05daa68720c 45 //This is going to be brief
noutram 13:f05daa68720c 46 samplingThread.signal_set(1);
noutram 13:f05daa68720c 47 }
noutram 13:f05daa68720c 48 void doSample() {
noutram 10:3a3d2a571c8f 49
noutram 13:f05daa68720c 50 while (true) {
noutram 13:f05daa68720c 51 //Waiting for ISR in WAITING state
noutram 13:f05daa68720c 52 Thread::signal_wait(1);
noutram 13:f05daa68720c 53
noutram 13:f05daa68720c 54 //Read sample - make a copy
noutram 13:f05daa68720c 55 float sample = adcIn;
noutram 13:f05daa68720c 56 //Grab switch state
noutram 13:f05daa68720c 57 uint32_t switch1State = SW1;
noutram 13:f05daa68720c 58 uint32_t switch2State = SW2;
noutram 13:f05daa68720c 59
noutram 13:f05daa68720c 60 //Allocate a block from the memory pool
noutram 13:f05daa68720c 61 message_t *message = mail_box.alloc();
noutram 13:f05daa68720c 62 if (message == NULL) {
noutram 13:f05daa68720c 63 //Out of memory
noutram 13:f05daa68720c 64 printf("Out of memory\n\r");
noutram 13:f05daa68720c 65 redLED = 1;
noutram 13:f05daa68720c 66 return;
noutram 13:f05daa68720c 67 }
noutram 13:f05daa68720c 68
noutram 13:f05daa68720c 69 //Fill in the data
noutram 13:f05daa68720c 70 message->adcValue = sample;
noutram 13:f05daa68720c 71 message->sw1State = switch1State;
noutram 13:f05daa68720c 72 message->sw2State = switch2State;
noutram 13:f05daa68720c 73
noutram 13:f05daa68720c 74 //Write to queue
noutram 13:f05daa68720c 75 osStatus stat = mail_box.put(message); //Note we are sending the "pointer"
noutram 13:f05daa68720c 76
noutram 13:f05daa68720c 77 //Check if succesful
noutram 13:f05daa68720c 78 if (stat == osErrorResource) {
noutram 13:f05daa68720c 79 redLED = 1;
noutram 13:f05daa68720c 80 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);
noutram 13:f05daa68720c 81 mail_box.free(message);
noutram 13:f05daa68720c 82 return;
noutram 13:f05daa68720c 83 }
noutram 13:f05daa68720c 84
noutram 13:f05daa68720c 85 } //end whie
noutram 8:c5663f5fa848 86 }
noutram 8:c5663f5fa848 87
noutram 9:31031bbb59c7 88 //Normal priority thread (consumer)
noutram 11:ae623ddb2eff 89 void thread1()
noutram 10:3a3d2a571c8f 90 {
noutram 14:03836b560d3d 91 static int counter = 0;
noutram 14:03836b560d3d 92
noutram 4:dae8898e55fe 93 while (true) {
noutram 10:3a3d2a571c8f 94 //Block on the queue
noutram 11:ae623ddb2eff 95 osEvent evt = mail_box.get();
noutram 9:31031bbb59c7 96
noutram 10:3a3d2a571c8f 97 //Check status
noutram 11:ae623ddb2eff 98 if (evt.status == osEventMail) {
noutram 10:3a3d2a571c8f 99 message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address)
noutram 10:3a3d2a571c8f 100 //Make a copy
noutram 10:3a3d2a571c8f 101 message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State);
noutram 10:3a3d2a571c8f 102 //We are done with this, so give back the memory to the pool
noutram 11:ae623ddb2eff 103 mail_box.free(pMessage);
noutram 10:3a3d2a571c8f 104
noutram 10:3a3d2a571c8f 105 //Echo to the terminal
noutram 10:3a3d2a571c8f 106 printf("ADC Value: %.2f\t", msg.adcValue);
noutram 10:3a3d2a571c8f 107 printf("SW1: %u\t", msg.sw1State);
noutram 10:3a3d2a571c8f 108 printf("SW2: %u\n\r", msg.sw2State);
noutram 14:03836b560d3d 109
noutram 14:03836b560d3d 110 //Toggle green
noutram 14:03836b560d3d 111 if ( (msg.sw1State == 1) && (msg.sw2State == 1)) {
noutram 14:03836b560d3d 112 counter++;
noutram 14:03836b560d3d 113 if (counter == 10) {
noutram 14:03836b560d3d 114 greenLED = !greenLED;
noutram 14:03836b560d3d 115 counter = 0;
noutram 14:03836b560d3d 116 }
noutram 14:03836b560d3d 117 } else {
noutram 14:03836b560d3d 118 counter = 0;
noutram 14:03836b560d3d 119 }
noutram 14:03836b560d3d 120
noutram 11:ae623ddb2eff 121 } else {
noutram 11:ae623ddb2eff 122 printf("ERROR: %x\n\r", evt.status);
noutram 11:ae623ddb2eff 123 }
noutram 14:03836b560d3d 124
noutram 9:31031bbb59c7 125 } //end while
noutram 0:f916cefba2f4 126 }
noutram 0:f916cefba2f4 127
noutram 4:dae8898e55fe 128
noutram 8:c5663f5fa848 129 // Main thread
noutram 0:f916cefba2f4 130 int main() {
noutram 12:4605f1c51824 131 //Power on self-test
noutram 12:4605f1c51824 132 post();
noutram 8:c5663f5fa848 133
noutram 9:31031bbb59c7 134 //Start message
noutram 10:3a3d2a571c8f 135 printf("Welcome\n");
noutram 10:3a3d2a571c8f 136
noutram 9:31031bbb59c7 137 //Hook up timer interrupt
noutram 9:31031bbb59c7 138 Ticker timer;
noutram 10:3a3d2a571c8f 139 timer.attach(&adcISR, 0.1);
noutram 9:31031bbb59c7 140
noutram 8:c5663f5fa848 141 //Threads
noutram 13:f05daa68720c 142 samplingThread.start(doSample); //High Priority
noutram 12:4605f1c51824 143 t1.start(thread1);
noutram 7:cd015e83995a 144
noutram 9:31031bbb59c7 145 printf("Main Thread\n");
noutram 2:70084af839d3 146 while (true) {
noutram 9:31031bbb59c7 147 Thread::wait(5000);
noutram 9:31031bbb59c7 148 puts("Main Thread Alive");
noutram 0:f916cefba2f4 149 }
noutram 0:f916cefba2f4 150 }
noutram 2:70084af839d3 151
noutram 2:70084af839d3 152