Latest
Dependencies: serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice
main.cpp@24:81981815ef20, 2018-12-19 (annotated)
- Committer:
- Swabey89
- Date:
- Wed Dec 19 13:09:42 2018 +0000
- Revision:
- 24:81981815ef20
- Parent:
- 23:f87fe0c55894
- Child:
- 25:831dc928ccde
SD card mount and unmount, locks required
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 | 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 | 23:f87fe0c55894 | 27 | bool sd_init; |
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 | 24:81981815ef20 | 76 | |
Swabey89 | 24:81981815ef20 | 77 | |
Swabey89 | 15:8af9672a6778 | 78 | int main() { |
Swabey89 | 17:ead43c1b729d | 79 | timeData = new tm; |
Swabey89 | 11:3ad0327e8c9f | 80 | pc = new RawSerial(USBTX, USBRX); |
Swabey89 | 8:c81b0ff8b822 | 81 | |
Swabey89 | 18:3edfb9152b05 | 82 | //Initialisations |
Swabey89 | 17:ead43c1b729d | 83 | SDcard(); |
Swabey89 | 9:fa8a35d9d6c0 | 84 | |
Swabey89 | 1:31c7b4ab552b | 85 | //Power on self test |
Swabey89 | 1:31c7b4ab552b | 86 | post(); |
Swabey89 | 18:3edfb9152b05 | 87 | |
Swabey89 | 18:3edfb9152b05 | 88 | //Start threads |
Swabey89 | 18:3edfb9152b05 | 89 | SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 18:3edfb9152b05 | 90 | LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 19:88d8359306a4 | 91 | serial_thread.start(callback(&serialqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 21:2c438eeaab14 | 92 | Network_thread.start(network); |
Swabey89 | 19:88d8359306a4 | 93 | producer_thread.start(sampleProducer); |
Swabey89 | 19:88d8359306a4 | 94 | consumer_thread.start(sampleConsumer); |
Swabey89 | 3:b1583f309b43 | 95 | |
Swabey89 | 24:81981815ef20 | 96 | //TEST FOR SD CARD |
Swabey89 | 24:81981815ef20 | 97 | SDmount_thread.start(SDmount); |
Swabey89 | 24:81981815ef20 | 98 | |
Swabey89 | 20:cdde0663c481 | 99 | |
Swabey89 | 18:3edfb9152b05 | 100 | //Attach ISRs |
Swabey89 | 18:3edfb9152b05 | 101 | sample.attach(&sampleISR, sample_rate); |
Swabey89 | 11:3ad0327e8c9f | 102 | pc->attach(serialISR, Serial::RxIrq); |
Swabey89 | 15:8af9672a6778 | 103 | |
Swabey89 | 24:81981815ef20 | 104 | //TEST FOR SD CARD MOUNT AND UNMOUNT |
Swabey89 | 24:81981815ef20 | 105 | usersw.rise(&userswRisingEdge); |
Swabey89 | 24:81981815ef20 | 106 | |
Swabey89 | 1:31c7b4ab552b | 107 | //Flash to indicate goodness |
Swabey89 | 1:31c7b4ab552b | 108 | while(true) { |
Swabey89 | 5:956984cbe447 | 109 | greenLED = !greenLED; |
Swabey89 | 5:956984cbe447 | 110 | Thread::wait(500); |
Swabey89 | 0:4afd4940a189 | 111 | } |
Swabey89 | 0:4afd4940a189 | 112 | } |
Swabey89 | 1:31c7b4ab552b | 113 | |
Swabey89 | 19:88d8359306a4 | 114 | /* |
Swabey89 | 19:88d8359306a4 | 115 | FUNCITONS BELOW NEED MOVING? |
Swabey89 | 19:88d8359306a4 | 116 | */ |
Swabey89 | 19:88d8359306a4 | 117 | |
Swabey89 | 8:c81b0ff8b822 | 118 | void sampleISR() |
Swabey89 | 8:c81b0ff8b822 | 119 | { |
Swabey89 | 19:88d8359306a4 | 120 | producer_thread.signal_set(TAKE_SAMPLE); |
Swabey89 | 8:c81b0ff8b822 | 121 | } |
Swabey89 | 1:31c7b4ab552b | 122 | |
Swabey89 | 11:3ad0327e8c9f | 123 | void serialISR() |
Swabey89 | 11:3ad0327e8c9f | 124 | { |
Swabey89 | 11:3ad0327e8c9f | 125 | pc->attach(NULL, Serial::RxIrq); |
Swabey89 | 19:88d8359306a4 | 126 | serialqueue.call(serialData); |
Swabey89 | 11:3ad0327e8c9f | 127 | } |
Swabey89 | 1:31c7b4ab552b | 128 | |
Swabey89 | 19:88d8359306a4 | 129 | void serialData() |
Swabey89 | 12:3a54cbaa714c | 130 | { |
Swabey89 | 17:ead43c1b729d | 131 | static int i = 0; |
Swabey89 | 11:3ad0327e8c9f | 132 | if (pc->readable()) |
Swabey89 | 11:3ad0327e8c9f | 133 | { |
Swabey89 | 18:3edfb9152b05 | 134 | cmdBuffer[i] = pc->getc(); |
Swabey89 | 18:3edfb9152b05 | 135 | if (cmdBuffer[i] == '\r') |
Swabey89 | 11:3ad0327e8c9f | 136 | { |
Swabey89 | 11:3ad0327e8c9f | 137 | i = 0; |
Swabey89 | 18:3edfb9152b05 | 138 | serialqueue.call(serialterm); |
Swabey89 | 11:3ad0327e8c9f | 139 | } |
Swabey89 | 11:3ad0327e8c9f | 140 | else i++; |
Swabey89 | 11:3ad0327e8c9f | 141 | } |
Swabey89 | 11:3ad0327e8c9f | 142 | pc->attach(serialISR, Serial::RxIrq); |
Swabey89 | 11:3ad0327e8c9f | 143 | } |
Swabey89 | 19:88d8359306a4 | 144 | |
Swabey89 | 19:88d8359306a4 | 145 | |
Swabey89 | 19:88d8359306a4 | 146 | void sampleProducer() |
Swabey89 | 19:88d8359306a4 | 147 | { |
Swabey89 | 19:88d8359306a4 | 148 | while(true) |
Swabey89 | 19:88d8359306a4 | 149 | { |
Swabey89 | 19:88d8359306a4 | 150 | //High priority thread |
Swabey89 | 19:88d8359306a4 | 151 | Thread::signal_wait(TAKE_SAMPLE); |
Swabey89 | 19:88d8359306a4 | 152 | |
Swabey89 | 19:88d8359306a4 | 153 | //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 | 154 | bufferLock.lock(); |
Swabey89 | 19:88d8359306a4 | 155 | //Update buffer |
Swabey89 | 19:88d8359306a4 | 156 | newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR |
Swabey89 | 19:88d8359306a4 | 157 | buffer[newestIndex].updatetemp(sensor.getTemperature()); |
Swabey89 | 19:88d8359306a4 | 158 | buffer[newestIndex].updatepress(sensor.getPressure()); |
Swabey89 | 19:88d8359306a4 | 159 | buffer[newestIndex].updatelight(adcIn.read()); |
Swabey89 | 21:2c438eeaab14 | 160 | buffer[newestIndex].updateTime(); |
Swabey89 | 21:2c438eeaab14 | 161 | |
Swabey89 | 19:88d8359306a4 | 162 | //bufferLock.unlock(); //normally here, moved due to updating queues. |
Swabey89 | 19:88d8359306a4 | 163 | samplesInBuffer.release(); |
Swabey89 | 19:88d8359306a4 | 164 | |
Swabey89 | 19:88d8359306a4 | 165 | //Pass onto queues |
Swabey89 | 19:88d8359306a4 | 166 | LCDqueue.call(LCD_display, buffer[newestIndex].gettemp(),buffer[newestIndex].getpress(),buffer[newestIndex].getlight()); |
Swabey89 | 19:88d8359306a4 | 167 | bufferLock.unlock(); |
Swabey89 | 19:88d8359306a4 | 168 | |
Swabey89 | 19:88d8359306a4 | 169 | //Write to the SD card when i have 120 samples in the buffer |
Swabey89 | 19:88d8359306a4 | 170 | if (newestIndex == 119) |
Swabey89 | 19:88d8359306a4 | 171 | { |
Swabey89 | 19:88d8359306a4 | 172 | //save to SD card |
Swabey89 | 19:88d8359306a4 | 173 | consumer_thread.signal_set(STORE_DATA); |
Swabey89 | 19:88d8359306a4 | 174 | |
Swabey89 | 19:88d8359306a4 | 175 | } |
Swabey89 | 19:88d8359306a4 | 176 | |
Swabey89 | 19:88d8359306a4 | 177 | //pc->printf("%d\r\n", newestIndex); |
Swabey89 | 19:88d8359306a4 | 178 | |
Swabey89 | 19:88d8359306a4 | 179 | } |
Swabey89 | 19:88d8359306a4 | 180 | } |
Swabey89 | 19:88d8359306a4 | 181 | |
Swabey89 | 19:88d8359306a4 | 182 | void sampleConsumer() |
Swabey89 | 19:88d8359306a4 | 183 | { |
Swabey89 | 19:88d8359306a4 | 184 | while(true) |
Swabey89 | 19:88d8359306a4 | 185 | { |
Swabey89 | 19:88d8359306a4 | 186 | |
Swabey89 | 19:88d8359306a4 | 187 | static time_t seconds; |
Swabey89 | 19:88d8359306a4 | 188 | |
Swabey89 | 19:88d8359306a4 | 189 | //write to the SD card from 0 up to newestIndex. |
Swabey89 | 19:88d8359306a4 | 190 | Thread::signal_wait(STORE_DATA); |
Swabey89 | 19:88d8359306a4 | 191 | bufferLock.lock(); |
Swabey89 | 19:88d8359306a4 | 192 | redLED = 1; |
Swabey89 | 19:88d8359306a4 | 193 | |
Swabey89 | 23:f87fe0c55894 | 194 | //DO THIS IF THE SD CARD IS INITIALISED |
Swabey89 | 23:f87fe0c55894 | 195 | |
Swabey89 | 19:88d8359306a4 | 196 | char fileDate[30]; |
Swabey89 | 19:88d8359306a4 | 197 | seconds = time(NULL); |
Swabey89 | 19:88d8359306a4 | 198 | timeData = localtime(&seconds); |
Swabey89 | 19:88d8359306a4 | 199 | set_time(mktime(timeData)); |
Swabey89 | 23:f87fe0c55894 | 200 | strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData); |
Swabey89 | 19:88d8359306a4 | 201 | |
Swabey89 | 19:88d8359306a4 | 202 | fp = fopen(fileDate,"a"); |
Swabey89 | 23:f87fe0c55894 | 203 | if (fp == NULL) |
Swabey89 | 23:f87fe0c55894 | 204 | { |
Swabey89 | 23:f87fe0c55894 | 205 | pc->printf("WARNING: SD card could not be updated\n\r"); |
Swabey89 | 19:88d8359306a4 | 206 | } |
Swabey89 | 23:f87fe0c55894 | 207 | else |
Swabey89 | 23:f87fe0c55894 | 208 | { |
Swabey89 | 23:f87fe0c55894 | 209 | for (int i = 0; i<BUFFERSIZE; i++) |
Swabey89 | 23:f87fe0c55894 | 210 | { |
Swabey89 | 23:f87fe0c55894 | 211 | 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 | 212 | } |
Swabey89 | 23:f87fe0c55894 | 213 | printf("SD card updated\r\n"); |
Swabey89 | 23:f87fe0c55894 | 214 | } |
Swabey89 | 19:88d8359306a4 | 215 | fclose(fp); |
Swabey89 | 19:88d8359306a4 | 216 | redLED = 0; |
Swabey89 | 23:f87fe0c55894 | 217 | bufferLock.unlock(); |
Swabey89 | 19:88d8359306a4 | 218 | } |
Swabey89 | 19:88d8359306a4 | 219 | } |
Swabey89 | 24:81981815ef20 | 220 | |
Swabey89 | 24:81981815ef20 | 221 | //TEST FOR MOUNTING AND UNMOUNTING SD CARD |
Swabey89 | 24:81981815ef20 | 222 | //Interrupt service routine for handling the timeout |
Swabey89 | 24:81981815ef20 | 223 | |
Swabey89 | 24:81981815ef20 | 224 | |
Swabey89 | 24:81981815ef20 | 225 | void userswTimeOutHandler() { |
Swabey89 | 24:81981815ef20 | 226 | userswTimeOut.detach(); //Stop the timeout counter firing |
Swabey89 | 24:81981815ef20 | 227 | |
Swabey89 | 24:81981815ef20 | 228 | //Which event does this follow? |
Swabey89 | 24:81981815ef20 | 229 | switch (userswState) { |
Swabey89 | 24:81981815ef20 | 230 | case EDGE_RISEN: |
Swabey89 | 24:81981815ef20 | 231 | usersw.fall(&userswFallingEdge); //Now wait for a falling edge |
Swabey89 | 24:81981815ef20 | 232 | break; |
Swabey89 | 24:81981815ef20 | 233 | case EDGE_FALLEN: |
Swabey89 | 24:81981815ef20 | 234 | usersw.rise(&userswRisingEdge); //Now wait for a rising edge |
Swabey89 | 24:81981815ef20 | 235 | break; |
Swabey89 | 24:81981815ef20 | 236 | } //end switch |
Swabey89 | 24:81981815ef20 | 237 | } |
Swabey89 | 24:81981815ef20 | 238 | |
Swabey89 | 24:81981815ef20 | 239 | //Interrupt service routine for a rising edge (press) |
Swabey89 | 24:81981815ef20 | 240 | void userswRisingEdge() { |
Swabey89 | 24:81981815ef20 | 241 | usersw.rise(NULL); //Disable detecting more rising edges |
Swabey89 | 24:81981815ef20 | 242 | userswState = EDGE_RISEN; //Flag state |
Swabey89 | 24:81981815ef20 | 243 | userswTimeOut.attach(&userswTimeOutHandler, 0.2); //Start timeout timer |
Swabey89 | 24:81981815ef20 | 244 | } |
Swabey89 | 24:81981815ef20 | 245 | |
Swabey89 | 24:81981815ef20 | 246 | //Interrupt service routive for SW1 falling edge (release) |
Swabey89 | 24:81981815ef20 | 247 | void userswFallingEdge() { |
Swabey89 | 24:81981815ef20 | 248 | usersw.fall(NULL); //Disable this interrupt |
Swabey89 | 24:81981815ef20 | 249 | SDmount_thread.signal_set(SIGNAL_SD); |
Swabey89 | 24:81981815ef20 | 250 | userswState = EDGE_FALLEN; //Flag state |
Swabey89 | 24:81981815ef20 | 251 | userswTimeOut.attach(&userswTimeOutHandler, 0.2); //Start timeout counter - may want to increase this |
Swabey89 | 24:81981815ef20 | 252 | } |