Latest

Dependencies:   serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice

Committer:
Swabey89
Date:
Sat Dec 22 21:15:49 2018 +0000
Revision:
28:7fccaef8fa72
Parent:
27:bb8d4c883e1b
Child:
29:806e9281af2f
producer-consumer now working and last of the commands finished

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