Latest

Dependencies:   serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice

Committer:
Swabey89
Date:
Wed Dec 19 15:30:44 2018 +0000
Revision:
25:831dc928ccde
Parent:
24:81981815ef20
Child:
26:94238a308ff9
Currently working, requires improvements to the producer and consumer threads. Read and delete commands need to coded next

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Swabey89 7:e2bf2d703867 1 #include "mbed.h"
Swabey89 1:31c7b4ab552b 2 #include "sample_hardware.hpp"
Swabey89 21:2c438eeaab14 3 #include "Networking.hpp"
Swabey89 5:956984cbe447 4 #include "serial_terminal.hpp"
Swabey89 6:a5394c9e5927 5 #include "SDCard.hpp"
Swabey89 5:956984cbe447 6 #include "rtos.h"
Swabey89 11:3ad0327e8c9f 7 #include "events/mbed_events.h"
Swabey89 8:c81b0ff8b822 8 #include "LCDdisplay.hpp"
Swabey89 5:956984cbe447 9
Swabey89 24:81981815ef20 10 //Defines TEST FOR SD CARD MOUNT AND UNMOUNT
Swabey89 24:81981815ef20 11 #define EDGE_FALLEN 0
Swabey89 24:81981815ef20 12 #define EDGE_RISEN 1
Swabey89 19:88d8359306a4 13
Swabey89 10:0fffc988d325 14 //Signals
Swabey89 8:c81b0ff8b822 15 #define TAKE_SAMPLE 1
Swabey89 19:88d8359306a4 16 #define STORE_DATA 2
Swabey89 10:0fffc988d325 17
Swabey89 24:81981815ef20 18
Swabey89 17:ead43c1b729d 19 //Global variables
Swabey89 19:88d8359306a4 20
Swabey89 19:88d8359306a4 21 unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0
Swabey89 19:88d8359306a4 22 unsigned int oldestIndex = BUFFERSIZE-1;
Swabey89 17:ead43c1b729d 23 FILE* fp;
Swabey89 17:ead43c1b729d 24 FATFileSystem* fs;
Swabey89 10:0fffc988d325 25
Swabey89 19:88d8359306a4 26 //Shared mutable variables VOLATILE
Swabey89 25:831dc928ccde 27 bool sd_init; //is it?
Swabey89 22:d9cbbbf3cf69 28 float sample_rate = 15; //is it?
Swabey89 17:ead43c1b729d 29 struct tm* timeData;
Swabey89 18:3edfb9152b05 30 char cmdBuffer[256];
Swabey89 19:88d8359306a4 31 sensorData buffer[BUFFERSIZE];
Swabey89 17:ead43c1b729d 32 RawSerial* pc;
Swabey89 8:c81b0ff8b822 33
Swabey89 19:88d8359306a4 34 //Thread synchronisation primatives
Swabey89 19:88d8359306a4 35 Semaphore spaceAvailable(BUFFERSIZE);
Swabey89 19:88d8359306a4 36 Semaphore samplesInBuffer(0);
Swabey89 19:88d8359306a4 37 Mutex bufferLock;
Swabey89 19:88d8359306a4 38
Swabey89 8:c81b0ff8b822 39 //Queues
Swabey89 11:3ad0327e8c9f 40 EventQueue SDqueue(32*EVENTS_EVENT_SIZE);
Swabey89 11:3ad0327e8c9f 41 EventQueue LCDqueue(32*EVENTS_EVENT_SIZE);
Swabey89 17:ead43c1b729d 42 EventQueue serialqueue(32*EVENTS_EVENT_SIZE);
Swabey89 1:31c7b4ab552b 43
Swabey89 1:31c7b4ab552b 44 //Threads
Swabey89 19:88d8359306a4 45 Thread producer_thread(osPriorityHigh);
Swabey89 19:88d8359306a4 46 Thread consumer_thread;
Swabey89 19:88d8359306a4 47 Thread serial_thread(osPriorityAboveNormal);
Swabey89 24:81981815ef20 48 Thread SDqueue_thread; //remove
Swabey89 11:3ad0327e8c9f 49 Thread LCDqueue_thread;
Swabey89 20:cdde0663c481 50 Thread Network_thread;
Swabey89 0:4afd4940a189 51
Swabey89 24:81981815ef20 52 //TEST FOR SD CARD
Swabey89 24:81981815ef20 53 Thread SDmount_thread;
Swabey89 24:81981815ef20 54
Swabey89 17:ead43c1b729d 55 //Timers
Swabey89 8:c81b0ff8b822 56 Ticker sample;
Swabey89 10:0fffc988d325 57
Swabey89 17:ead43c1b729d 58 //Function prototypes
Swabey89 8:c81b0ff8b822 59 void sampleISR(void);
Swabey89 19:88d8359306a4 60 void sampleProducer(void);
Swabey89 19:88d8359306a4 61 void sampleConsumer(void);
Swabey89 11:3ad0327e8c9f 62 void serialISR(void);
Swabey89 19:88d8359306a4 63 void serialData(void);
Swabey89 11:3ad0327e8c9f 64
Swabey89 24:81981815ef20 65
Swabey89 24:81981815ef20 66
Swabey89 24:81981815ef20 67 //TEST FOR SD CARD MOUNT AND UNMOUNT
Swabey89 24:81981815ef20 68 Timeout userswTimeOut;
Swabey89 24:81981815ef20 69 int userswState = EDGE_FALLEN;
Swabey89 24:81981815ef20 70 InterruptIn usersw(USER_BUTTON);
Swabey89 24:81981815ef20 71 void userswTimeOutHandler();
Swabey89 24:81981815ef20 72 void userswRisingEdge();
Swabey89 24:81981815ef20 73 void userswFallingEdge();
Swabey89 24:81981815ef20 74 void SDmount();
Swabey89 24:81981815ef20 75
Swabey89 25:831dc928ccde 76 Mutex printlock;
Swabey89 25:831dc928ccde 77 Mutex LCDlock;
Swabey89 25:831dc928ccde 78 Mutex timeLock;
Swabey89 25:831dc928ccde 79 Mutex SDlock;
Swabey89 24:81981815ef20 80
Swabey89 24:81981815ef20 81
Swabey89 15:8af9672a6778 82 int main() {
Swabey89 17:ead43c1b729d 83 timeData = new tm;
Swabey89 11:3ad0327e8c9f 84 pc = new RawSerial(USBTX, USBRX);
Swabey89 8:c81b0ff8b822 85
Swabey89 18:3edfb9152b05 86 //Initialisations
Swabey89 17:ead43c1b729d 87 SDcard();
Swabey89 9:fa8a35d9d6c0 88
Swabey89 1:31c7b4ab552b 89 //Power on self test
Swabey89 1:31c7b4ab552b 90 post();
Swabey89 18:3edfb9152b05 91
Swabey89 18:3edfb9152b05 92 //Start threads
Swabey89 18:3edfb9152b05 93 SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever));
Swabey89 18:3edfb9152b05 94 LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever));
Swabey89 19:88d8359306a4 95 serial_thread.start(callback(&serialqueue, &EventQueue::dispatch_forever));
Swabey89 21:2c438eeaab14 96 Network_thread.start(network);
Swabey89 19:88d8359306a4 97 producer_thread.start(sampleProducer);
Swabey89 19:88d8359306a4 98 consumer_thread.start(sampleConsumer);
Swabey89 3:b1583f309b43 99
Swabey89 24:81981815ef20 100 //TEST FOR SD CARD
Swabey89 24:81981815ef20 101 SDmount_thread.start(SDmount);
Swabey89 24:81981815ef20 102
Swabey89 20:cdde0663c481 103
Swabey89 18:3edfb9152b05 104 //Attach ISRs
Swabey89 18:3edfb9152b05 105 sample.attach(&sampleISR, sample_rate);
Swabey89 11:3ad0327e8c9f 106 pc->attach(serialISR, Serial::RxIrq);
Swabey89 15:8af9672a6778 107
Swabey89 24:81981815ef20 108 //TEST FOR SD CARD MOUNT AND UNMOUNT
Swabey89 24:81981815ef20 109 usersw.rise(&userswRisingEdge);
Swabey89 24:81981815ef20 110
Swabey89 1:31c7b4ab552b 111 //Flash to indicate goodness
Swabey89 1:31c7b4ab552b 112 while(true) {
Swabey89 5:956984cbe447 113 greenLED = !greenLED;
Swabey89 5:956984cbe447 114 Thread::wait(500);
Swabey89 0:4afd4940a189 115 }
Swabey89 0:4afd4940a189 116 }
Swabey89 1:31c7b4ab552b 117
Swabey89 19:88d8359306a4 118 /*
Swabey89 19:88d8359306a4 119 FUNCITONS BELOW NEED MOVING?
Swabey89 19:88d8359306a4 120 */
Swabey89 19:88d8359306a4 121
Swabey89 8:c81b0ff8b822 122 void sampleISR()
Swabey89 8:c81b0ff8b822 123 {
Swabey89 19:88d8359306a4 124 producer_thread.signal_set(TAKE_SAMPLE);
Swabey89 8:c81b0ff8b822 125 }
Swabey89 1:31c7b4ab552b 126
Swabey89 11:3ad0327e8c9f 127 void serialISR()
Swabey89 11:3ad0327e8c9f 128 {
Swabey89 11:3ad0327e8c9f 129 pc->attach(NULL, Serial::RxIrq);
Swabey89 19:88d8359306a4 130 serialqueue.call(serialData);
Swabey89 11:3ad0327e8c9f 131 }
Swabey89 1:31c7b4ab552b 132
Swabey89 19:88d8359306a4 133 void serialData()
Swabey89 12:3a54cbaa714c 134 {
Swabey89 17:ead43c1b729d 135 static int i = 0;
Swabey89 11:3ad0327e8c9f 136 if (pc->readable())
Swabey89 11:3ad0327e8c9f 137 {
Swabey89 18:3edfb9152b05 138 cmdBuffer[i] = pc->getc();
Swabey89 18:3edfb9152b05 139 if (cmdBuffer[i] == '\r')
Swabey89 11:3ad0327e8c9f 140 {
Swabey89 11:3ad0327e8c9f 141 i = 0;
Swabey89 18:3edfb9152b05 142 serialqueue.call(serialterm);
Swabey89 11:3ad0327e8c9f 143 }
Swabey89 11:3ad0327e8c9f 144 else i++;
Swabey89 11:3ad0327e8c9f 145 }
Swabey89 11:3ad0327e8c9f 146 pc->attach(serialISR, Serial::RxIrq);
Swabey89 11:3ad0327e8c9f 147 }
Swabey89 19:88d8359306a4 148
Swabey89 19:88d8359306a4 149
Swabey89 19:88d8359306a4 150 void sampleProducer()
Swabey89 19:88d8359306a4 151 {
Swabey89 19:88d8359306a4 152 while(true)
Swabey89 19:88d8359306a4 153 {
Swabey89 19:88d8359306a4 154 //High priority thread
Swabey89 19:88d8359306a4 155 Thread::signal_wait(TAKE_SAMPLE);
Swabey89 19:88d8359306a4 156
Swabey89 19:88d8359306a4 157 //int32_t Nspaces = spaceAvailable.wait(); //Blocking if space is not available //Dont check if there is space available becuase we are overwriting
Swabey89 19:88d8359306a4 158 bufferLock.lock();
Swabey89 19:88d8359306a4 159 //Update buffer
Swabey89 19:88d8359306a4 160 newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR
Swabey89 19:88d8359306a4 161 buffer[newestIndex].updatetemp(sensor.getTemperature());
Swabey89 19:88d8359306a4 162 buffer[newestIndex].updatepress(sensor.getPressure());
Swabey89 19:88d8359306a4 163 buffer[newestIndex].updatelight(adcIn.read());
Swabey89 21:2c438eeaab14 164 buffer[newestIndex].updateTime();
Swabey89 21:2c438eeaab14 165
Swabey89 19:88d8359306a4 166 //bufferLock.unlock(); //normally here, moved due to updating queues.
Swabey89 19:88d8359306a4 167 samplesInBuffer.release();
Swabey89 19:88d8359306a4 168
Swabey89 19:88d8359306a4 169 //Pass onto queues
Swabey89 19:88d8359306a4 170 LCDqueue.call(LCD_display, buffer[newestIndex].gettemp(),buffer[newestIndex].getpress(),buffer[newestIndex].getlight());
Swabey89 19:88d8359306a4 171 bufferLock.unlock();
Swabey89 19:88d8359306a4 172
Swabey89 19:88d8359306a4 173 //Write to the SD card when i have 120 samples in the buffer
Swabey89 19:88d8359306a4 174 if (newestIndex == 119)
Swabey89 19:88d8359306a4 175 {
Swabey89 19:88d8359306a4 176 //save to SD card
Swabey89 19:88d8359306a4 177 consumer_thread.signal_set(STORE_DATA);
Swabey89 19:88d8359306a4 178
Swabey89 19:88d8359306a4 179 }
Swabey89 19:88d8359306a4 180
Swabey89 19:88d8359306a4 181 //pc->printf("%d\r\n", newestIndex);
Swabey89 19:88d8359306a4 182
Swabey89 19:88d8359306a4 183 }
Swabey89 19:88d8359306a4 184 }
Swabey89 19:88d8359306a4 185
Swabey89 19:88d8359306a4 186 void sampleConsumer()
Swabey89 19:88d8359306a4 187 {
Swabey89 19:88d8359306a4 188 while(true)
Swabey89 19:88d8359306a4 189 {
Swabey89 19:88d8359306a4 190
Swabey89 19:88d8359306a4 191 static time_t seconds;
Swabey89 19:88d8359306a4 192
Swabey89 19:88d8359306a4 193 //write to the SD card from 0 up to newestIndex.
Swabey89 19:88d8359306a4 194 Thread::signal_wait(STORE_DATA);
Swabey89 19:88d8359306a4 195 redLED = 1;
Swabey89 19:88d8359306a4 196
Swabey89 23:f87fe0c55894 197 //DO THIS IF THE SD CARD IS INITIALISED
Swabey89 23:f87fe0c55894 198
Swabey89 19:88d8359306a4 199 char fileDate[30];
Swabey89 25:831dc928ccde 200 timeLock.lock();
Swabey89 19:88d8359306a4 201 seconds = time(NULL);
Swabey89 19:88d8359306a4 202 timeData = localtime(&seconds);
Swabey89 19:88d8359306a4 203 set_time(mktime(timeData));
Swabey89 23:f87fe0c55894 204 strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
Swabey89 25:831dc928ccde 205 timeLock.unlock();
Swabey89 19:88d8359306a4 206 fp = fopen(fileDate,"a");
Swabey89 23:f87fe0c55894 207 if (fp == NULL)
Swabey89 23:f87fe0c55894 208 {
Swabey89 25:831dc928ccde 209 printlock.lock();
Swabey89 25:831dc928ccde 210 pc->printf("WARNING: SD card could not be updated\n\r");
Swabey89 25:831dc928ccde 211 printlock.unlock();
Swabey89 19:88d8359306a4 212 }
Swabey89 23:f87fe0c55894 213 else
Swabey89 23:f87fe0c55894 214 {
Swabey89 25:831dc928ccde 215 //Nested locks probably a bad idea!
Swabey89 25:831dc928ccde 216 bufferLock.lock();
Swabey89 25:831dc928ccde 217 SDlock.lock();
Swabey89 23:f87fe0c55894 218 for (int i = 0; i<BUFFERSIZE; i++)
Swabey89 25:831dc928ccde 219 {
Swabey89 23:f87fe0c55894 220 fprintf(fp,"%s,%5.2f,%5.2f,%5.2f\n\r", buffer[i].getTime(), buffer[i].gettemp(), buffer[i].getpress(), buffer[i].getlight());
Swabey89 23:f87fe0c55894 221 }
Swabey89 25:831dc928ccde 222 SDlock.unlock();
Swabey89 25:831dc928ccde 223 bufferLock.unlock();
Swabey89 25:831dc928ccde 224
Swabey89 25:831dc928ccde 225 printlock.lock();
Swabey89 25:831dc928ccde 226 pc->printf("SD card updated\r\n");
Swabey89 25:831dc928ccde 227 printlock.unlock();
Swabey89 23:f87fe0c55894 228 }
Swabey89 19:88d8359306a4 229 fclose(fp);
Swabey89 25:831dc928ccde 230 redLED = 0;
Swabey89 19:88d8359306a4 231 }
Swabey89 19:88d8359306a4 232 }
Swabey89 24:81981815ef20 233
Swabey89 24:81981815ef20 234 //TEST FOR MOUNTING AND UNMOUNTING SD CARD
Swabey89 24:81981815ef20 235 //Interrupt service routine for handling the timeout
Swabey89 24:81981815ef20 236
Swabey89 24:81981815ef20 237
Swabey89 24:81981815ef20 238 void userswTimeOutHandler() {
Swabey89 24:81981815ef20 239 userswTimeOut.detach(); //Stop the timeout counter firing
Swabey89 24:81981815ef20 240
Swabey89 24:81981815ef20 241 //Which event does this follow?
Swabey89 24:81981815ef20 242 switch (userswState) {
Swabey89 24:81981815ef20 243 case EDGE_RISEN:
Swabey89 24:81981815ef20 244 usersw.fall(&userswFallingEdge); //Now wait for a falling edge
Swabey89 24:81981815ef20 245 break;
Swabey89 24:81981815ef20 246 case EDGE_FALLEN:
Swabey89 24:81981815ef20 247 usersw.rise(&userswRisingEdge); //Now wait for a rising edge
Swabey89 24:81981815ef20 248 break;
Swabey89 24:81981815ef20 249 } //end switch
Swabey89 24:81981815ef20 250 }
Swabey89 24:81981815ef20 251
Swabey89 24:81981815ef20 252 //Interrupt service routine for a rising edge (press)
Swabey89 24:81981815ef20 253 void userswRisingEdge() {
Swabey89 24:81981815ef20 254 usersw.rise(NULL); //Disable detecting more rising edges
Swabey89 24:81981815ef20 255 userswState = EDGE_RISEN; //Flag state
Swabey89 24:81981815ef20 256 userswTimeOut.attach(&userswTimeOutHandler, 0.2); //Start timeout timer
Swabey89 24:81981815ef20 257 }
Swabey89 24:81981815ef20 258
Swabey89 24:81981815ef20 259 //Interrupt service routive for SW1 falling edge (release)
Swabey89 24:81981815ef20 260 void userswFallingEdge() {
Swabey89 24:81981815ef20 261 usersw.fall(NULL); //Disable this interrupt
Swabey89 24:81981815ef20 262 SDmount_thread.signal_set(SIGNAL_SD);
Swabey89 24:81981815ef20 263 userswState = EDGE_FALLEN; //Flag state
Swabey89 24:81981815ef20 264 userswTimeOut.attach(&userswTimeOutHandler, 0.2); //Start timeout counter - may want to increase this
Swabey89 24:81981815ef20 265 }