gdfgdh

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task633-mbedos-FZ429 by University of Plymouth - Stages 1, 2 and 3

Committer:
noutram
Date:
Tue Nov 21 11:36:41 2017 +0000
Revision:
13:f05daa68720c
Parent:
12:4605f1c51824
Child:
14:03836b560d3d
Fixed

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 4:dae8898e55fe 91 while (true) {
noutram 10:3a3d2a571c8f 92 //Block on the queue
noutram 11:ae623ddb2eff 93 osEvent evt = mail_box.get();
noutram 9:31031bbb59c7 94
noutram 10:3a3d2a571c8f 95 //Check status
noutram 11:ae623ddb2eff 96 if (evt.status == osEventMail) {
noutram 10:3a3d2a571c8f 97 message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address)
noutram 10:3a3d2a571c8f 98 //Make a copy
noutram 10:3a3d2a571c8f 99 message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State);
noutram 10:3a3d2a571c8f 100 //We are done with this, so give back the memory to the pool
noutram 11:ae623ddb2eff 101 mail_box.free(pMessage);
noutram 10:3a3d2a571c8f 102
noutram 10:3a3d2a571c8f 103 //Echo to the terminal
noutram 10:3a3d2a571c8f 104 printf("ADC Value: %.2f\t", msg.adcValue);
noutram 10:3a3d2a571c8f 105 printf("SW1: %u\t", msg.sw1State);
noutram 10:3a3d2a571c8f 106 printf("SW2: %u\n\r", msg.sw2State);
noutram 11:ae623ddb2eff 107 } else {
noutram 11:ae623ddb2eff 108 printf("ERROR: %x\n\r", evt.status);
noutram 11:ae623ddb2eff 109 }
noutram 11:ae623ddb2eff 110
noutram 9:31031bbb59c7 111 } //end while
noutram 0:f916cefba2f4 112 }
noutram 0:f916cefba2f4 113
noutram 4:dae8898e55fe 114
noutram 8:c5663f5fa848 115 // Main thread
noutram 0:f916cefba2f4 116 int main() {
noutram 12:4605f1c51824 117 //Power on self-test
noutram 12:4605f1c51824 118 post();
noutram 8:c5663f5fa848 119
noutram 9:31031bbb59c7 120 //Start message
noutram 10:3a3d2a571c8f 121 printf("Welcome\n");
noutram 10:3a3d2a571c8f 122
noutram 9:31031bbb59c7 123 //Hook up timer interrupt
noutram 9:31031bbb59c7 124 Ticker timer;
noutram 10:3a3d2a571c8f 125 timer.attach(&adcISR, 0.1);
noutram 9:31031bbb59c7 126
noutram 8:c5663f5fa848 127 //Threads
noutram 13:f05daa68720c 128 samplingThread.start(doSample); //High Priority
noutram 12:4605f1c51824 129 t1.start(thread1);
noutram 7:cd015e83995a 130
noutram 9:31031bbb59c7 131 printf("Main Thread\n");
noutram 2:70084af839d3 132 while (true) {
noutram 9:31031bbb59c7 133 Thread::wait(5000);
noutram 9:31031bbb59c7 134 puts("Main Thread Alive");
noutram 0:f916cefba2f4 135 }
noutram 0:f916cefba2f4 136 }
noutram 2:70084af839d3 137
noutram 2:70084af839d3 138