Latest

Dependencies:   serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice

Committer:
Swabey89
Date:
Fri Dec 28 10:04:33 2018 +0000
Revision:
29:806e9281af2f
Parent:
28:7fccaef8fa72
Child:
30:7873beb54744
Updated

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 29:806e9281af2f 42 EventQueue SDqueue(32*EVENTS_EVENT_SIZE); //changed from 32
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 29:806e9281af2f 46
Swabey89 29:806e9281af2f 47
Swabey89 1:31c7b4ab552b 48 //Threads
Swabey89 19:88d8359306a4 49 Thread producer_thread(osPriorityHigh);
Swabey89 19:88d8359306a4 50 Thread consumer_thread;
Swabey89 19:88d8359306a4 51 Thread serial_thread(osPriorityAboveNormal);
Swabey89 29:806e9281af2f 52 Thread SDqueue_thread; //take out queue in name?
Swabey89 11:3ad0327e8c9f 53 Thread LCDqueue_thread;
Swabey89 29:806e9281af2f 54 Thread network_thread;
Swabey89 0:4afd4940a189 55
Swabey89 24:81981815ef20 56 //TEST FOR SD CARD
Swabey89 24:81981815ef20 57 Thread SDmount_thread;
Swabey89 24:81981815ef20 58
Swabey89 17:ead43c1b729d 59 //Timers
Swabey89 8:c81b0ff8b822 60 Ticker sample;
Swabey89 10:0fffc988d325 61
Swabey89 17:ead43c1b729d 62 //Function prototypes
Swabey89 8:c81b0ff8b822 63 void sampleISR(void);
Swabey89 19:88d8359306a4 64 void sampleProducer(void);
Swabey89 19:88d8359306a4 65 void sampleConsumer(void);
Swabey89 11:3ad0327e8c9f 66 void serialISR(void);
Swabey89 19:88d8359306a4 67 void serialData(void);
Swabey89 11:3ad0327e8c9f 68
Swabey89 24:81981815ef20 69
Swabey89 24:81981815ef20 70
Swabey89 24:81981815ef20 71 //TEST FOR SD CARD MOUNT AND UNMOUNT
Swabey89 24:81981815ef20 72 Timeout userswTimeOut;
Swabey89 24:81981815ef20 73 int userswState = EDGE_FALLEN;
Swabey89 24:81981815ef20 74 InterruptIn usersw(USER_BUTTON);
Swabey89 24:81981815ef20 75 void userswTimeOutHandler();
Swabey89 24:81981815ef20 76 void userswRisingEdge();
Swabey89 24:81981815ef20 77 void userswFallingEdge();
Swabey89 24:81981815ef20 78 void SDmount();
Swabey89 24:81981815ef20 79
Swabey89 25:831dc928ccde 80 Mutex printlock;
Swabey89 25:831dc928ccde 81 Mutex LCDlock;
Swabey89 25:831dc928ccde 82 Mutex timeLock;
Swabey89 25:831dc928ccde 83 Mutex SDlock;
Swabey89 24:81981815ef20 84
Swabey89 24:81981815ef20 85
Swabey89 27:bb8d4c883e1b 86 //TEST FOR BUFFER CHANGES
Swabey89 28:7fccaef8fa72 87 unsigned int saveIndex = BUFFERSIZE-1;
Swabey89 28:7fccaef8fa72 88
Swabey89 28:7fccaef8fa72 89 int32_t Nspaces = BUFFERSIZE;
Swabey89 28:7fccaef8fa72 90 int32_t Nsamples;
Swabey89 27:bb8d4c883e1b 91
Swabey89 27:bb8d4c883e1b 92
Swabey89 29:806e9281af2f 93 //TEST FOR WATCHDOG
Swabey89 29:806e9281af2f 94 char threadstates = 0;
Swabey89 29:806e9281af2f 95
Swabey89 29:806e9281af2f 96 Timeout producer_tout;
Swabey89 29:806e9281af2f 97 Timeout consumer_tout;
Swabey89 29:806e9281af2f 98 Timeout serial_tout;
Swabey89 29:806e9281af2f 99 Timeout SD_tout;
Swabey89 29:806e9281af2f 100 Timeout LCD_tout;
Swabey89 29:806e9281af2f 101 Timeout network_tout;
Swabey89 29:806e9281af2f 102
Swabey89 29:806e9281af2f 103 Thread watchdog_thread(osPriorityHigh);
Swabey89 29:806e9281af2f 104
Swabey89 29:806e9281af2f 105 void producer_toutISR(void);
Swabey89 29:806e9281af2f 106 void consumer_toutISR(void);
Swabey89 29:806e9281af2f 107 void serial_toutISR(void);
Swabey89 29:806e9281af2f 108 void SD_toutISR(void);
Swabey89 29:806e9281af2f 109 void LCD_toutISR(void);
Swabey89 29:806e9281af2f 110 void network_toutISR(void);
Swabey89 29:806e9281af2f 111 void watchdog(void);
Swabey89 29:806e9281af2f 112
Swabey89 29:806e9281af2f 113 osThreadId main_thread;
Swabey89 29:806e9281af2f 114
Swabey89 29:806e9281af2f 115 #define PRODUCER 1<<0
Swabey89 29:806e9281af2f 116 #define CONSUMER 1<<1
Swabey89 29:806e9281af2f 117 #define SERIAL 1<<2
Swabey89 29:806e9281af2f 118 #define SD 1<<3
Swabey89 29:806e9281af2f 119 #define LCD 1<<4
Swabey89 29:806e9281af2f 120 #define NETWORK 1<<5
Swabey89 29:806e9281af2f 121
Swabey89 15:8af9672a6778 122 int main() {
Swabey89 29:806e9281af2f 123
Swabey89 29:806e9281af2f 124 main_thread = Thread::gettid();
Swabey89 17:ead43c1b729d 125 timeData = new tm;
Swabey89 11:3ad0327e8c9f 126 pc = new RawSerial(USBTX, USBRX);
Swabey89 8:c81b0ff8b822 127
Swabey89 18:3edfb9152b05 128 //Initialisations
Swabey89 17:ead43c1b729d 129 SDcard();
Swabey89 9:fa8a35d9d6c0 130
Swabey89 1:31c7b4ab552b 131 //Power on self test
Swabey89 1:31c7b4ab552b 132 post();
Swabey89 18:3edfb9152b05 133
Swabey89 18:3edfb9152b05 134 //Start threads
Swabey89 18:3edfb9152b05 135 SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever));
Swabey89 18:3edfb9152b05 136 LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever));
Swabey89 19:88d8359306a4 137 serial_thread.start(callback(&serialqueue, &EventQueue::dispatch_forever));
Swabey89 29:806e9281af2f 138 network_thread.start(network);
Swabey89 19:88d8359306a4 139 producer_thread.start(sampleProducer);
Swabey89 19:88d8359306a4 140 consumer_thread.start(sampleConsumer);
Swabey89 3:b1583f309b43 141
Swabey89 24:81981815ef20 142 //TEST FOR SD CARD
Swabey89 24:81981815ef20 143 SDmount_thread.start(SDmount);
Swabey89 24:81981815ef20 144
Swabey89 29:806e9281af2f 145 //TEST FOR WATCGDOG
Swabey89 29:806e9281af2f 146 watchdog_thread.start(watchdog);
Swabey89 29:806e9281af2f 147
Swabey89 28:7fccaef8fa72 148
Swabey89 18:3edfb9152b05 149 //Attach ISRs
Swabey89 28:7fccaef8fa72 150 sample.attach(&sampleISR, sample_rate); //Allow sampling to start
Swabey89 11:3ad0327e8c9f 151 pc->attach(serialISR, Serial::RxIrq);
Swabey89 15:8af9672a6778 152
Swabey89 24:81981815ef20 153 //TEST FOR SD CARD MOUNT AND UNMOUNT
Swabey89 29:806e9281af2f 154 usersw.rise(&userswRisingEdge);
Swabey89 24:81981815ef20 155
Swabey89 1:31c7b4ab552b 156 //Flash to indicate goodness
Swabey89 1:31c7b4ab552b 157 while(true) {
Swabey89 5:956984cbe447 158 greenLED = !greenLED;
Swabey89 5:956984cbe447 159 Thread::wait(500);
Swabey89 0:4afd4940a189 160 }
Swabey89 0:4afd4940a189 161 }
Swabey89 1:31c7b4ab552b 162
Swabey89 19:88d8359306a4 163 /*
Swabey89 19:88d8359306a4 164 FUNCITONS BELOW NEED MOVING?
Swabey89 19:88d8359306a4 165 */
Swabey89 19:88d8359306a4 166
Swabey89 8:c81b0ff8b822 167 void sampleISR()
Swabey89 8:c81b0ff8b822 168 {
Swabey89 19:88d8359306a4 169 producer_thread.signal_set(TAKE_SAMPLE);
Swabey89 8:c81b0ff8b822 170 }
Swabey89 1:31c7b4ab552b 171
Swabey89 11:3ad0327e8c9f 172 void serialISR()
Swabey89 11:3ad0327e8c9f 173 {
Swabey89 11:3ad0327e8c9f 174 pc->attach(NULL, Serial::RxIrq);
Swabey89 19:88d8359306a4 175 serialqueue.call(serialData);
Swabey89 11:3ad0327e8c9f 176 }
Swabey89 1:31c7b4ab552b 177
Swabey89 19:88d8359306a4 178 void serialData()
Swabey89 12:3a54cbaa714c 179 {
Swabey89 17:ead43c1b729d 180 static int i = 0;
Swabey89 29:806e9281af2f 181
Swabey89 11:3ad0327e8c9f 182 if (pc->readable())
Swabey89 11:3ad0327e8c9f 183 {
Swabey89 18:3edfb9152b05 184 cmdBuffer[i] = pc->getc();
Swabey89 29:806e9281af2f 185 if (i != 29)
Swabey89 11:3ad0327e8c9f 186 {
Swabey89 29:806e9281af2f 187
Swabey89 29:806e9281af2f 188 if (cmdBuffer[i] == '\b')
Swabey89 29:806e9281af2f 189 {
Swabey89 29:806e9281af2f 190 i = (i ? i-1 : 0);
Swabey89 29:806e9281af2f 191 }
Swabey89 29:806e9281af2f 192 else if (cmdBuffer[i] == '\r')
Swabey89 29:806e9281af2f 193 {
Swabey89 29:806e9281af2f 194 cmdBuffer[i+1]==NULL;
Swabey89 29:806e9281af2f 195 serialqueue.call(serialterm);
Swabey89 29:806e9281af2f 196 i = 0;
Swabey89 29:806e9281af2f 197 }
Swabey89 29:806e9281af2f 198 else i++;
Swabey89 29:806e9281af2f 199 }
Swabey89 29:806e9281af2f 200 else
Swabey89 29:806e9281af2f 201 {
Swabey89 28:7fccaef8fa72 202 serialqueue.call(serialterm);
Swabey89 29:806e9281af2f 203 i = 0;
Swabey89 11:3ad0327e8c9f 204 }
Swabey89 11:3ad0327e8c9f 205 }
Swabey89 11:3ad0327e8c9f 206 pc->attach(serialISR, Serial::RxIrq);
Swabey89 11:3ad0327e8c9f 207 }
Swabey89 19:88d8359306a4 208
Swabey89 19:88d8359306a4 209
Swabey89 19:88d8359306a4 210 void sampleProducer()
Swabey89 19:88d8359306a4 211 {
Swabey89 19:88d8359306a4 212 while(true)
Swabey89 19:88d8359306a4 213 {
Swabey89 19:88d8359306a4 214 //High priority thread
Swabey89 29:806e9281af2f 215 Thread::signal_wait(TAKE_SAMPLE);
Swabey89 29:806e9281af2f 216 //wd_thread.signal_set(PROD_SIGNAL);
Swabey89 29:806e9281af2f 217 //prod_stat = 0;
Swabey89 29:806e9281af2f 218 producer_tout.attach(producer_toutISR, TOUT_TIME);
Swabey89 28:7fccaef8fa72 219 Nspaces = spaceAvailable.wait(0); //Non-blocking
Swabey89 29:806e9281af2f 220 bufferLock.lock();
Swabey89 29:806e9281af2f 221
Swabey89 28:7fccaef8fa72 222 //Update buffer
Swabey89 28:7fccaef8fa72 223 newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR
Swabey89 27:bb8d4c883e1b 224
Swabey89 28:7fccaef8fa72 225 if (newestIndex == oldestIndex)
Swabey89 28:7fccaef8fa72 226 {
Swabey89 28:7fccaef8fa72 227 oldestIndex = (oldestIndex+1) % BUFFERSIZE;
Swabey89 28:7fccaef8fa72 228 }
Swabey89 28:7fccaef8fa72 229
Swabey89 19:88d8359306a4 230 buffer[newestIndex].updatetemp(sensor.getTemperature());
Swabey89 19:88d8359306a4 231 buffer[newestIndex].updatepress(sensor.getPressure());
Swabey89 19:88d8359306a4 232 buffer[newestIndex].updatelight(adcIn.read());
Swabey89 27:bb8d4c883e1b 233 buffer[newestIndex].updateTime();
Swabey89 28:7fccaef8fa72 234
Swabey89 28:7fccaef8fa72 235 if (Nspaces != 0)
Swabey89 28:7fccaef8fa72 236 {
Swabey89 28:7fccaef8fa72 237 Nspaces--;
Swabey89 28:7fccaef8fa72 238 }
Swabey89 28:7fccaef8fa72 239
Swabey89 27:bb8d4c883e1b 240 samplesInBuffer.release();
Swabey89 27:bb8d4c883e1b 241
Swabey89 19:88d8359306a4 242 //Pass onto queues
Swabey89 28:7fccaef8fa72 243 LCDqueue.call(LCD_display, buffer[newestIndex].gettemp(),buffer[newestIndex].getpress(),buffer[newestIndex].getlight());
Swabey89 28:7fccaef8fa72 244
Swabey89 28:7fccaef8fa72 245 if(logging)
Swabey89 28:7fccaef8fa72 246 {
Swabey89 28:7fccaef8fa72 247 printlock.lock();
Swabey89 29:806e9281af2f 248 pc->printf("Sample placed in buffer at position %d\r\n", newestIndex);
Swabey89 28:7fccaef8fa72 249 pc->printf("Number of spaces available in buffer:%d\r\n\n",Nspaces);
Swabey89 28:7fccaef8fa72 250 printlock.unlock();
Swabey89 28:7fccaef8fa72 251 }
Swabey89 28:7fccaef8fa72 252
Swabey89 19:88d8359306a4 253 bufferLock.unlock();
Swabey89 29:806e9281af2f 254 producer_tout.detach();
Swabey89 19:88d8359306a4 255 }
Swabey89 19:88d8359306a4 256 }
Swabey89 19:88d8359306a4 257
Swabey89 19:88d8359306a4 258 void sampleConsumer()
Swabey89 19:88d8359306a4 259 {
Swabey89 19:88d8359306a4 260 while(true)
Swabey89 19:88d8359306a4 261 {
Swabey89 28:7fccaef8fa72 262 static time_t seconds; //possibly move into if(sd_init)
Swabey89 28:7fccaef8fa72 263 //write to the SD card from oldestindex up to newestIndex.
Swabey89 19:88d8359306a4 264
Swabey89 28:7fccaef8fa72 265 Nsamples = samplesInBuffer.wait(); //Block if no samples to take - acquires
Swabey89 29:806e9281af2f 266 consumer_tout.attach(consumer_toutISR,TOUT_TIME);
Swabey89 19:88d8359306a4 267
Swabey89 28:7fccaef8fa72 268 if (sd_init)
Swabey89 28:7fccaef8fa72 269 {
Swabey89 28:7fccaef8fa72 270 char fileDate[30];
Swabey89 28:7fccaef8fa72 271 timeLock.lock();
Swabey89 28:7fccaef8fa72 272 seconds = time(NULL);
Swabey89 28:7fccaef8fa72 273 timeData = localtime(&seconds);
Swabey89 28:7fccaef8fa72 274
Swabey89 28:7fccaef8fa72 275 //set_time(mktime(timeData));
Swabey89 28:7fccaef8fa72 276
Swabey89 28:7fccaef8fa72 277 strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
Swabey89 28:7fccaef8fa72 278 timeLock.unlock();
Swabey89 28:7fccaef8fa72 279
Swabey89 28:7fccaef8fa72 280 fp = fopen(fileDate,"a");
Swabey89 28:7fccaef8fa72 281 if (fp == NULL)
Swabey89 28:7fccaef8fa72 282 {
Swabey89 28:7fccaef8fa72 283 printlock.lock();
Swabey89 28:7fccaef8fa72 284 pc->printf("WARNING: SD card could not be updated\r\n\n");
Swabey89 28:7fccaef8fa72 285 sd_init = false;
Swabey89 28:7fccaef8fa72 286 printlock.unlock();
Swabey89 28:7fccaef8fa72 287 samplesInBuffer.release();
Swabey89 28:7fccaef8fa72 288 }
Swabey89 28:7fccaef8fa72 289 else
Swabey89 28:7fccaef8fa72 290 {
Swabey89 28:7fccaef8fa72 291 //Nested locks probably a bad idea!
Swabey89 28:7fccaef8fa72 292 bufferLock.lock();
Swabey89 28:7fccaef8fa72 293 SDlock.lock();
Swabey89 28:7fccaef8fa72 294 oldestIndex = (oldestIndex+1) % BUFFERSIZE;
Swabey89 28:7fccaef8fa72 295 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 296 SDlock.unlock();
Swabey89 28:7fccaef8fa72 297
Swabey89 28:7fccaef8fa72 298 if(logging)
Swabey89 28:7fccaef8fa72 299 {
Swabey89 28:7fccaef8fa72 300 printlock.lock();
Swabey89 28:7fccaef8fa72 301 pc->printf("Log file %s updated with sample from position %d in buffer\r\n",fileDate,oldestIndex);
Swabey89 28:7fccaef8fa72 302 pc->printf("newestIndex position %d\r\n",newestIndex);
Swabey89 28:7fccaef8fa72 303 pc->printf("oldestIndex position %d\r\n",oldestIndex);
Swabey89 28:7fccaef8fa72 304 printlock.unlock();
Swabey89 28:7fccaef8fa72 305 }
Swabey89 28:7fccaef8fa72 306
Swabey89 28:7fccaef8fa72 307 bufferLock.unlock();
Swabey89 28:7fccaef8fa72 308 fclose(fp);
Swabey89 28:7fccaef8fa72 309 }
Swabey89 28:7fccaef8fa72 310 redLED = 0;
Swabey89 28:7fccaef8fa72 311 }
Swabey89 23:f87fe0c55894 312 else
Swabey89 23:f87fe0c55894 313 {
Swabey89 28:7fccaef8fa72 314 samplesInBuffer.release();
Swabey89 28:7fccaef8fa72 315 }
Swabey89 28:7fccaef8fa72 316
Swabey89 29:806e9281af2f 317 consumer_tout.detach();
Swabey89 19:88d8359306a4 318 }
Swabey89 19:88d8359306a4 319 }
Swabey89 24:81981815ef20 320
Swabey89 24:81981815ef20 321 //TEST FOR MOUNTING AND UNMOUNTING SD CARD
Swabey89 24:81981815ef20 322 //Interrupt service routine for handling the timeout
Swabey89 24:81981815ef20 323
Swabey89 24:81981815ef20 324
Swabey89 24:81981815ef20 325 void userswTimeOutHandler() {
Swabey89 24:81981815ef20 326 userswTimeOut.detach(); //Stop the timeout counter firing
Swabey89 24:81981815ef20 327
Swabey89 24:81981815ef20 328 //Which event does this follow?
Swabey89 24:81981815ef20 329 switch (userswState) {
Swabey89 24:81981815ef20 330 case EDGE_RISEN:
Swabey89 24:81981815ef20 331 usersw.fall(&userswFallingEdge); //Now wait for a falling edge
Swabey89 24:81981815ef20 332 break;
Swabey89 24:81981815ef20 333 case EDGE_FALLEN:
Swabey89 24:81981815ef20 334 usersw.rise(&userswRisingEdge); //Now wait for a rising edge
Swabey89 24:81981815ef20 335 break;
Swabey89 24:81981815ef20 336 } //end switch
Swabey89 24:81981815ef20 337 }
Swabey89 24:81981815ef20 338
Swabey89 24:81981815ef20 339 //Interrupt service routine for a rising edge (press)
Swabey89 24:81981815ef20 340 void userswRisingEdge() {
Swabey89 24:81981815ef20 341 usersw.rise(NULL); //Disable detecting more rising edges
Swabey89 24:81981815ef20 342 userswState = EDGE_RISEN; //Flag state
Swabey89 24:81981815ef20 343 userswTimeOut.attach(&userswTimeOutHandler, 0.2); //Start timeout timer
Swabey89 24:81981815ef20 344 }
Swabey89 24:81981815ef20 345
Swabey89 24:81981815ef20 346 //Interrupt service routive for SW1 falling edge (release)
Swabey89 24:81981815ef20 347 void userswFallingEdge() {
Swabey89 24:81981815ef20 348 usersw.fall(NULL); //Disable this interrupt
Swabey89 24:81981815ef20 349 SDmount_thread.signal_set(SIGNAL_SD);
Swabey89 24:81981815ef20 350 userswState = EDGE_FALLEN; //Flag state
Swabey89 24:81981815ef20 351 userswTimeOut.attach(&userswTimeOutHandler, 0.2); //Start timeout counter - may want to increase this
Swabey89 24:81981815ef20 352 }
Swabey89 29:806e9281af2f 353
Swabey89 29:806e9281af2f 354 //TEST FOR WATCHDOG
Swabey89 29:806e9281af2f 355
Swabey89 29:806e9281af2f 356 //ISR
Swabey89 29:806e9281af2f 357 void producer_toutISR(void)
Swabey89 29:806e9281af2f 358 {
Swabey89 29:806e9281af2f 359 threadstates |= PRODUCER;
Swabey89 29:806e9281af2f 360 }
Swabey89 29:806e9281af2f 361
Swabey89 29:806e9281af2f 362 void consumer_toutISR(void)
Swabey89 29:806e9281af2f 363 {
Swabey89 29:806e9281af2f 364 threadstates |= CONSUMER;
Swabey89 29:806e9281af2f 365 }
Swabey89 29:806e9281af2f 366
Swabey89 29:806e9281af2f 367 void serial_toutISR(void)
Swabey89 29:806e9281af2f 368 {
Swabey89 29:806e9281af2f 369 threadstates |= SERIAL;
Swabey89 29:806e9281af2f 370 }
Swabey89 29:806e9281af2f 371
Swabey89 29:806e9281af2f 372 void SD_toutISR(void)
Swabey89 29:806e9281af2f 373 {
Swabey89 29:806e9281af2f 374 threadstates |= SD;
Swabey89 29:806e9281af2f 375 }
Swabey89 29:806e9281af2f 376
Swabey89 29:806e9281af2f 377 void LCD_toutISR(void)
Swabey89 29:806e9281af2f 378 {
Swabey89 29:806e9281af2f 379 threadstates |= LCD;
Swabey89 29:806e9281af2f 380 }
Swabey89 29:806e9281af2f 381
Swabey89 29:806e9281af2f 382 void network_toutISR(void)
Swabey89 29:806e9281af2f 383 {
Swabey89 29:806e9281af2f 384 threadstates |= NETWORK;
Swabey89 29:806e9281af2f 385 }
Swabey89 29:806e9281af2f 386
Swabey89 29:806e9281af2f 387 void watchdog(void)
Swabey89 29:806e9281af2f 388 {
Swabey89 29:806e9281af2f 389 while(true)
Swabey89 29:806e9281af2f 390 {
Swabey89 29:806e9281af2f 391 Thread::wait(10000);
Swabey89 29:806e9281af2f 392
Swabey89 29:806e9281af2f 393 if(threadstates)
Swabey89 29:806e9281af2f 394 {
Swabey89 29:806e9281af2f 395 producer_thread.terminate();
Swabey89 29:806e9281af2f 396 consumer_thread.terminate();
Swabey89 29:806e9281af2f 397 serial_thread.terminate();
Swabey89 29:806e9281af2f 398 SDqueue_thread.terminate();
Swabey89 29:806e9281af2f 399 LCDqueue_thread.terminate();
Swabey89 29:806e9281af2f 400 network_thread.terminate();
Swabey89 29:806e9281af2f 401
Swabey89 29:806e9281af2f 402 pc->printf("THREAD PSW: 0x%x\n\r", threadstates);
Swabey89 29:806e9281af2f 403
Swabey89 29:806e9281af2f 404 switch (threadstates)
Swabey89 29:806e9281af2f 405 {
Swabey89 29:806e9281af2f 406 case (PRODUCER) :
Swabey89 29:806e9281af2f 407 pc->printf("PRODUCER THREAD DEADLOCK\r\n\n");
Swabey89 29:806e9281af2f 408 lcd.cls();
Swabey89 29:806e9281af2f 409 lcd.printf("PRODUCER\nDEADLOCK");
Swabey89 29:806e9281af2f 410 break;
Swabey89 29:806e9281af2f 411
Swabey89 29:806e9281af2f 412 case (CONSUMER) :
Swabey89 29:806e9281af2f 413 pc->printf("CONSUMER THREAD DEADLOCK\r\n\n");
Swabey89 29:806e9281af2f 414 lcd.cls();
Swabey89 29:806e9281af2f 415 lcd.printf("CONSUMER\nDEADLOCK");
Swabey89 29:806e9281af2f 416 break;
Swabey89 29:806e9281af2f 417
Swabey89 29:806e9281af2f 418 case (SERIAL) :
Swabey89 29:806e9281af2f 419 pc->printf("SERIAL THREAD DEADLOCK\r\n\n");
Swabey89 29:806e9281af2f 420 lcd.cls();
Swabey89 29:806e9281af2f 421 lcd.printf("SERIAL\nDEADLOCK");
Swabey89 29:806e9281af2f 422 break;
Swabey89 29:806e9281af2f 423
Swabey89 29:806e9281af2f 424 case (SD) :
Swabey89 29:806e9281af2f 425 pc->printf("SD CARD THREAD DEADLOCK\r\n\n");
Swabey89 29:806e9281af2f 426 lcd.cls();
Swabey89 29:806e9281af2f 427 lcd.printf("SD CARD\nDEADLOCK");
Swabey89 29:806e9281af2f 428 break;
Swabey89 29:806e9281af2f 429
Swabey89 29:806e9281af2f 430 case (LCD) :
Swabey89 29:806e9281af2f 431 pc->printf("LCD THREAD DEADLOCK\r\n\n");
Swabey89 29:806e9281af2f 432 lcd.cls();
Swabey89 29:806e9281af2f 433 lcd.printf("LCD\nDEADLOCK");
Swabey89 29:806e9281af2f 434 break;
Swabey89 29:806e9281af2f 435
Swabey89 29:806e9281af2f 436 case (NETWORK) :
Swabey89 29:806e9281af2f 437 pc->printf("NETWORK THREAD DEADLOCK\r\n\n");
Swabey89 29:806e9281af2f 438 lcd.cls();
Swabey89 29:806e9281af2f 439 lcd.printf("NETWORK\nDEADLOCK");
Swabey89 29:806e9281af2f 440 break;
Swabey89 29:806e9281af2f 441
Swabey89 29:806e9281af2f 442 default:
Swabey89 29:806e9281af2f 443 pc->printf("MULTIPLE THREAD DEADLOCK\r\n\n");
Swabey89 29:806e9281af2f 444 lcd.cls();
Swabey89 29:806e9281af2f 445 lcd.printf("DEADLOCK");
Swabey89 29:806e9281af2f 446 break;
Swabey89 29:806e9281af2f 447 }
Swabey89 29:806e9281af2f 448
Swabey89 29:806e9281af2f 449 for (int i = 0;i<50;i++)
Swabey89 29:806e9281af2f 450 {
Swabey89 29:806e9281af2f 451 redLED = 1;
Swabey89 29:806e9281af2f 452 wait(0.05);
Swabey89 29:806e9281af2f 453 redLED = 0;
Swabey89 29:806e9281af2f 454 wait(0.05);
Swabey89 29:806e9281af2f 455 }
Swabey89 29:806e9281af2f 456 NVIC_SystemReset();
Swabey89 29:806e9281af2f 457 }
Swabey89 29:806e9281af2f 458 else if (logging) {printlock.lock(); pc->printf("WATCHDOG RAN WITH NO DEADLOCKED THREADS\r\n"); printlock.unlock();}
Swabey89 29:806e9281af2f 459 }
Swabey89 29:806e9281af2f 460 }