Latest

Dependencies:   serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice

Committer:
Swabey89
Date:
Wed Dec 19 12:03:50 2018 +0000
Revision:
23:f87fe0c55894
Parent:
22:d9cbbbf3cf69
Child:
24:81981815ef20
Changes to SD card initialisation. Further work required to mount and unmount SD card using blue user button

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 19:88d8359306a4 10 //Defines
Swabey89 21:2c438eeaab14 11 //moved
Swabey89 19:88d8359306a4 12
Swabey89 10:0fffc988d325 13 //Signals
Swabey89 8:c81b0ff8b822 14 #define TAKE_SAMPLE 1
Swabey89 19:88d8359306a4 15 #define STORE_DATA 2
Swabey89 10:0fffc988d325 16
Swabey89 17:ead43c1b729d 17 //Global variables
Swabey89 19:88d8359306a4 18
Swabey89 19:88d8359306a4 19 unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0
Swabey89 19:88d8359306a4 20 unsigned int oldestIndex = BUFFERSIZE-1;
Swabey89 17:ead43c1b729d 21 FILE* fp;
Swabey89 17:ead43c1b729d 22 FATFileSystem* fs;
Swabey89 10:0fffc988d325 23
Swabey89 19:88d8359306a4 24 //Shared mutable variables VOLATILE
Swabey89 23:f87fe0c55894 25 bool sd_init;
Swabey89 22:d9cbbbf3cf69 26 float sample_rate = 15; //is it?
Swabey89 17:ead43c1b729d 27 struct tm* timeData;
Swabey89 18:3edfb9152b05 28 char cmdBuffer[256];
Swabey89 19:88d8359306a4 29 sensorData buffer[BUFFERSIZE];
Swabey89 17:ead43c1b729d 30 RawSerial* pc;
Swabey89 8:c81b0ff8b822 31
Swabey89 19:88d8359306a4 32 //Thread synchronisation primatives
Swabey89 19:88d8359306a4 33 Semaphore spaceAvailable(BUFFERSIZE);
Swabey89 19:88d8359306a4 34 Semaphore samplesInBuffer(0);
Swabey89 19:88d8359306a4 35 Mutex bufferLock;
Swabey89 19:88d8359306a4 36
Swabey89 8:c81b0ff8b822 37 //Queues
Swabey89 11:3ad0327e8c9f 38 EventQueue SDqueue(32*EVENTS_EVENT_SIZE);
Swabey89 11:3ad0327e8c9f 39 EventQueue LCDqueue(32*EVENTS_EVENT_SIZE);
Swabey89 17:ead43c1b729d 40 EventQueue serialqueue(32*EVENTS_EVENT_SIZE);
Swabey89 1:31c7b4ab552b 41
Swabey89 1:31c7b4ab552b 42 //Threads
Swabey89 19:88d8359306a4 43 Thread producer_thread(osPriorityHigh);
Swabey89 19:88d8359306a4 44 Thread consumer_thread;
Swabey89 19:88d8359306a4 45 Thread serial_thread(osPriorityAboveNormal);
Swabey89 11:3ad0327e8c9f 46 Thread SDqueue_thread;
Swabey89 11:3ad0327e8c9f 47 Thread LCDqueue_thread;
Swabey89 20:cdde0663c481 48 Thread Network_thread;
Swabey89 0:4afd4940a189 49
Swabey89 17:ead43c1b729d 50 //Timers
Swabey89 8:c81b0ff8b822 51 Ticker sample;
Swabey89 10:0fffc988d325 52
Swabey89 17:ead43c1b729d 53 //Function prototypes
Swabey89 8:c81b0ff8b822 54 void sampleISR(void);
Swabey89 19:88d8359306a4 55 void sampleProducer(void);
Swabey89 19:88d8359306a4 56 void sampleConsumer(void);
Swabey89 11:3ad0327e8c9f 57 void serialISR(void);
Swabey89 19:88d8359306a4 58 void serialData(void);
Swabey89 11:3ad0327e8c9f 59
Swabey89 15:8af9672a6778 60 int main() {
Swabey89 17:ead43c1b729d 61 timeData = new tm;
Swabey89 11:3ad0327e8c9f 62 pc = new RawSerial(USBTX, USBRX);
Swabey89 8:c81b0ff8b822 63
Swabey89 18:3edfb9152b05 64 //Initialisations
Swabey89 17:ead43c1b729d 65 SDcard();
Swabey89 9:fa8a35d9d6c0 66
Swabey89 1:31c7b4ab552b 67 //Power on self test
Swabey89 1:31c7b4ab552b 68 post();
Swabey89 18:3edfb9152b05 69
Swabey89 18:3edfb9152b05 70 //Start threads
Swabey89 18:3edfb9152b05 71 SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever));
Swabey89 18:3edfb9152b05 72 LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever));
Swabey89 19:88d8359306a4 73 serial_thread.start(callback(&serialqueue, &EventQueue::dispatch_forever));
Swabey89 21:2c438eeaab14 74 Network_thread.start(network);
Swabey89 19:88d8359306a4 75 producer_thread.start(sampleProducer);
Swabey89 19:88d8359306a4 76 consumer_thread.start(sampleConsumer);
Swabey89 3:b1583f309b43 77
Swabey89 20:cdde0663c481 78
Swabey89 18:3edfb9152b05 79 //Attach ISRs
Swabey89 18:3edfb9152b05 80 sample.attach(&sampleISR, sample_rate);
Swabey89 11:3ad0327e8c9f 81 pc->attach(serialISR, Serial::RxIrq);
Swabey89 15:8af9672a6778 82
Swabey89 1:31c7b4ab552b 83 //Flash to indicate goodness
Swabey89 1:31c7b4ab552b 84 while(true) {
Swabey89 5:956984cbe447 85 greenLED = !greenLED;
Swabey89 5:956984cbe447 86 Thread::wait(500);
Swabey89 0:4afd4940a189 87 }
Swabey89 0:4afd4940a189 88 }
Swabey89 1:31c7b4ab552b 89
Swabey89 19:88d8359306a4 90 /*
Swabey89 19:88d8359306a4 91 FUNCITONS BELOW NEED MOVING?
Swabey89 19:88d8359306a4 92 */
Swabey89 19:88d8359306a4 93
Swabey89 8:c81b0ff8b822 94 void sampleISR()
Swabey89 8:c81b0ff8b822 95 {
Swabey89 19:88d8359306a4 96 producer_thread.signal_set(TAKE_SAMPLE);
Swabey89 8:c81b0ff8b822 97 }
Swabey89 1:31c7b4ab552b 98
Swabey89 11:3ad0327e8c9f 99 void serialISR()
Swabey89 11:3ad0327e8c9f 100 {
Swabey89 11:3ad0327e8c9f 101 pc->attach(NULL, Serial::RxIrq);
Swabey89 19:88d8359306a4 102 serialqueue.call(serialData);
Swabey89 11:3ad0327e8c9f 103 }
Swabey89 1:31c7b4ab552b 104
Swabey89 19:88d8359306a4 105 void serialData()
Swabey89 12:3a54cbaa714c 106 {
Swabey89 17:ead43c1b729d 107 static int i = 0;
Swabey89 11:3ad0327e8c9f 108 if (pc->readable())
Swabey89 11:3ad0327e8c9f 109 {
Swabey89 18:3edfb9152b05 110 cmdBuffer[i] = pc->getc();
Swabey89 18:3edfb9152b05 111 if (cmdBuffer[i] == '\r')
Swabey89 11:3ad0327e8c9f 112 {
Swabey89 11:3ad0327e8c9f 113 i = 0;
Swabey89 18:3edfb9152b05 114 serialqueue.call(serialterm);
Swabey89 11:3ad0327e8c9f 115 }
Swabey89 11:3ad0327e8c9f 116 else i++;
Swabey89 11:3ad0327e8c9f 117 }
Swabey89 11:3ad0327e8c9f 118 pc->attach(serialISR, Serial::RxIrq);
Swabey89 11:3ad0327e8c9f 119 }
Swabey89 19:88d8359306a4 120
Swabey89 19:88d8359306a4 121
Swabey89 19:88d8359306a4 122 void sampleProducer()
Swabey89 19:88d8359306a4 123 {
Swabey89 19:88d8359306a4 124 while(true)
Swabey89 19:88d8359306a4 125 {
Swabey89 19:88d8359306a4 126 //High priority thread
Swabey89 19:88d8359306a4 127 Thread::signal_wait(TAKE_SAMPLE);
Swabey89 19:88d8359306a4 128
Swabey89 19:88d8359306a4 129 //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 130 bufferLock.lock();
Swabey89 19:88d8359306a4 131 //Update buffer
Swabey89 19:88d8359306a4 132 newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR
Swabey89 19:88d8359306a4 133 buffer[newestIndex].updatetemp(sensor.getTemperature());
Swabey89 19:88d8359306a4 134 buffer[newestIndex].updatepress(sensor.getPressure());
Swabey89 19:88d8359306a4 135 buffer[newestIndex].updatelight(adcIn.read());
Swabey89 21:2c438eeaab14 136 buffer[newestIndex].updateTime();
Swabey89 21:2c438eeaab14 137
Swabey89 19:88d8359306a4 138 //bufferLock.unlock(); //normally here, moved due to updating queues.
Swabey89 19:88d8359306a4 139 samplesInBuffer.release();
Swabey89 19:88d8359306a4 140
Swabey89 19:88d8359306a4 141 //Pass onto queues
Swabey89 19:88d8359306a4 142 LCDqueue.call(LCD_display, buffer[newestIndex].gettemp(),buffer[newestIndex].getpress(),buffer[newestIndex].getlight());
Swabey89 19:88d8359306a4 143 bufferLock.unlock();
Swabey89 19:88d8359306a4 144
Swabey89 19:88d8359306a4 145 //Write to the SD card when i have 120 samples in the buffer
Swabey89 19:88d8359306a4 146 if (newestIndex == 119)
Swabey89 19:88d8359306a4 147 {
Swabey89 19:88d8359306a4 148 //save to SD card
Swabey89 19:88d8359306a4 149 consumer_thread.signal_set(STORE_DATA);
Swabey89 19:88d8359306a4 150
Swabey89 19:88d8359306a4 151 }
Swabey89 19:88d8359306a4 152
Swabey89 19:88d8359306a4 153 //pc->printf("%d\r\n", newestIndex);
Swabey89 19:88d8359306a4 154
Swabey89 19:88d8359306a4 155 }
Swabey89 19:88d8359306a4 156 }
Swabey89 19:88d8359306a4 157
Swabey89 19:88d8359306a4 158 void sampleConsumer()
Swabey89 19:88d8359306a4 159 {
Swabey89 19:88d8359306a4 160 while(true)
Swabey89 19:88d8359306a4 161 {
Swabey89 19:88d8359306a4 162
Swabey89 19:88d8359306a4 163 static time_t seconds;
Swabey89 19:88d8359306a4 164
Swabey89 19:88d8359306a4 165 //write to the SD card from 0 up to newestIndex.
Swabey89 19:88d8359306a4 166 Thread::signal_wait(STORE_DATA);
Swabey89 19:88d8359306a4 167 bufferLock.lock();
Swabey89 19:88d8359306a4 168 redLED = 1;
Swabey89 19:88d8359306a4 169
Swabey89 23:f87fe0c55894 170 //DO THIS IF THE SD CARD IS INITIALISED
Swabey89 23:f87fe0c55894 171
Swabey89 19:88d8359306a4 172 char fileDate[30];
Swabey89 19:88d8359306a4 173 seconds = time(NULL);
Swabey89 19:88d8359306a4 174 timeData = localtime(&seconds);
Swabey89 19:88d8359306a4 175 set_time(mktime(timeData));
Swabey89 23:f87fe0c55894 176 strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
Swabey89 19:88d8359306a4 177
Swabey89 19:88d8359306a4 178 fp = fopen(fileDate,"a");
Swabey89 23:f87fe0c55894 179 if (fp == NULL)
Swabey89 23:f87fe0c55894 180 {
Swabey89 23:f87fe0c55894 181 pc->printf("WARNING: SD card could not be updated\n\r");
Swabey89 19:88d8359306a4 182 }
Swabey89 23:f87fe0c55894 183 else
Swabey89 23:f87fe0c55894 184 {
Swabey89 23:f87fe0c55894 185 for (int i = 0; i<BUFFERSIZE; i++)
Swabey89 23:f87fe0c55894 186 {
Swabey89 23:f87fe0c55894 187 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 188 }
Swabey89 23:f87fe0c55894 189 printf("SD card updated\r\n");
Swabey89 23:f87fe0c55894 190 }
Swabey89 19:88d8359306a4 191 fclose(fp);
Swabey89 19:88d8359306a4 192 redLED = 0;
Swabey89 23:f87fe0c55894 193 bufferLock.unlock();
Swabey89 19:88d8359306a4 194 }
Swabey89 19:88d8359306a4 195 }