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