gdfgdh

Dependencies:   ELEC350-Practicals-FZ429

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

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