SD card functionality
Dependents: ELEC350_Project2 SDcard
SDCard.cpp@13:5f786448e883, 2018-12-28 (annotated)
- Committer:
- Swabey89
- Date:
- Fri Dec 28 10:03:52 2018 +0000
- Revision:
- 13:5f786448e883
- Parent:
- 12:3198f0b7b36f
- Child:
- 14:8e92eb9fc088
Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Swabey89 | 0:0fc2cf27ff9e | 1 | #include "SDCard.hpp" |
Swabey89 | 7:393fa8184388 | 2 | #include <string> |
Swabey89 | 3:c6e5dd2faa22 | 3 | |
Swabey89 | 1:c3af0c26ded2 | 4 | void SDcard(void) |
Swabey89 | 0:0fc2cf27ff9e | 5 | { |
Swabey89 | 13:5f786448e883 | 6 | SD_tout.attach(SD_toutISR,TOUT_TIME); |
Swabey89 | 13:5f786448e883 | 7 | |
Swabey89 | 9:113f4934b907 | 8 | static time_t seconds; //static reqiured? |
Swabey89 | 0:0fc2cf27ff9e | 9 | |
Swabey89 | 2:9a5eea2adbf8 | 10 | //Initialise the SD card |
Swabey89 | 8:ee8f65745141 | 11 | if (sd.init() != 0) { |
Swabey89 | 10:f2b8e3b587d5 | 12 | printlock.lock(); |
Swabey89 | 8:ee8f65745141 | 13 | pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 14 | printlock.unlock(); |
Swabey89 | 8:ee8f65745141 | 15 | sd_init = false; |
Swabey89 | 8:ee8f65745141 | 16 | //lcd.cls(); |
Swabey89 | 8:ee8f65745141 | 17 | //lcd.printf("CANNOT INIT SD"); |
Swabey89 | 8:ee8f65745141 | 18 | //errorCode(FATAL); |
Swabey89 | 2:9a5eea2adbf8 | 19 | } |
Swabey89 | 8:ee8f65745141 | 20 | else |
Swabey89 | 8:ee8f65745141 | 21 | { |
Swabey89 | 8:ee8f65745141 | 22 | //Create a filing system for SD Card |
Swabey89 | 8:ee8f65745141 | 23 | fs = new FATFileSystem("sd", &sd); |
Swabey89 | 10:f2b8e3b587d5 | 24 | printlock.lock(); |
Swabey89 | 13:5f786448e883 | 25 | pc->printf("\nSD CARD INITIALISED\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 26 | printlock.unlock(); |
Swabey89 | 7:393fa8184388 | 27 | |
Swabey89 | 8:ee8f65745141 | 28 | //Open to WRITE |
Swabey89 | 8:ee8f65745141 | 29 | char fileDate[30]; |
Swabey89 | 10:f2b8e3b587d5 | 30 | timeLock.lock(); |
Swabey89 | 8:ee8f65745141 | 31 | seconds = time(NULL); |
Swabey89 | 8:ee8f65745141 | 32 | timeData = localtime(&seconds); |
Swabey89 | 8:ee8f65745141 | 33 | set_time(mktime(timeData)); |
Swabey89 | 8:ee8f65745141 | 34 | strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData); |
Swabey89 | 10:f2b8e3b587d5 | 35 | timeLock.unlock(); |
Swabey89 | 8:ee8f65745141 | 36 | fp = fopen(fileDate,"a"); |
Swabey89 | 8:ee8f65745141 | 37 | |
Swabey89 | 8:ee8f65745141 | 38 | if (fp == NULL) |
Swabey89 | 8:ee8f65745141 | 39 | { |
Swabey89 | 10:f2b8e3b587d5 | 40 | printlock.lock(); |
Swabey89 | 8:ee8f65745141 | 41 | pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 42 | printlock.unlock(); |
Swabey89 | 8:ee8f65745141 | 43 | //lcd.cls(); |
Swabey89 | 8:ee8f65745141 | 44 | //lcd.printf("CANNOT OPEN FILE\n\n"); |
Swabey89 | 8:ee8f65745141 | 45 | //errorCode(FATAL); |
Swabey89 | 8:ee8f65745141 | 46 | } |
Swabey89 | 8:ee8f65745141 | 47 | else |
Swabey89 | 8:ee8f65745141 | 48 | { |
Swabey89 | 10:f2b8e3b587d5 | 49 | printlock.lock(); |
Swabey89 | 10:f2b8e3b587d5 | 50 | pc->printf("FILE OPEN FOR WRITING\n\n\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 51 | printlock.unlock(); |
Swabey89 | 8:ee8f65745141 | 52 | sd_init = true; |
Swabey89 | 8:ee8f65745141 | 53 | } |
Swabey89 | 8:ee8f65745141 | 54 | fclose(fp); |
Swabey89 | 8:ee8f65745141 | 55 | } |
Swabey89 | 13:5f786448e883 | 56 | SD_tout.detach(); |
Swabey89 | 1:c3af0c26ded2 | 57 | } |
Swabey89 | 1:c3af0c26ded2 | 58 | |
Swabey89 | 1:c3af0c26ded2 | 59 | void SDread(int n) |
Swabey89 | 1:c3af0c26ded2 | 60 | { |
Swabey89 | 13:5f786448e883 | 61 | SD_tout.attach(SD_toutISR,TOUT_TIME); |
Swabey89 | 12:3198f0b7b36f | 62 | //Read n samples from the SD card |
Swabey89 | 12:3198f0b7b36f | 63 | unsigned int i=0; |
Swabey89 | 12:3198f0b7b36f | 64 | unsigned int j = newestIndex; |
Swabey89 | 12:3198f0b7b36f | 65 | if (n==-1) {n = (BUFFERSIZE-Nspaces);} |
Swabey89 | 12:3198f0b7b36f | 66 | |
Swabey89 | 10:f2b8e3b587d5 | 67 | printlock.lock(); |
Swabey89 | 12:3198f0b7b36f | 68 | bufferLock.lock(); |
Swabey89 | 12:3198f0b7b36f | 69 | while (i < n) //ONLY USE SPACE AVAILABLE - KEEP IN MIND LAST READ WAS BEFORE IT WAS DECREMENTED |
Swabey89 | 12:3198f0b7b36f | 70 | { |
Swabey89 | 12:3198f0b7b36f | 71 | pc->printf("Date/Time: %s\tTemperature: %5.2f\tPressure: %5.2f\tLight: %5.2f\n\r", buffer[j].getTime(), buffer[j].gettemp(), buffer[j].getpress(), buffer[j].getlight()); |
Swabey89 | 12:3198f0b7b36f | 72 | j = (j?j:BUFFERSIZE)-1; |
Swabey89 | 12:3198f0b7b36f | 73 | i++; |
Swabey89 | 12:3198f0b7b36f | 74 | } |
Swabey89 | 12:3198f0b7b36f | 75 | bufferLock.unlock(); |
Swabey89 | 12:3198f0b7b36f | 76 | printf("%d records read\r\n\n", i); |
Swabey89 | 12:3198f0b7b36f | 77 | printlock.unlock(); |
Swabey89 | 13:5f786448e883 | 78 | SD_tout.detach(); |
Swabey89 | 2:9a5eea2adbf8 | 79 | } |
Swabey89 | 2:9a5eea2adbf8 | 80 | |
Swabey89 | 12:3198f0b7b36f | 81 | void SDdelete(int n) //MUST RELEASE SPACE AVAILABLE |
Swabey89 | 2:9a5eea2adbf8 | 82 | { |
Swabey89 | 13:5f786448e883 | 83 | SD_tout.attach(SD_toutISR,TOUT_TIME); |
Swabey89 | 3:c6e5dd2faa22 | 84 | //Delete n samples from the SD card |
Swabey89 | 12:3198f0b7b36f | 85 | unsigned int i = 0; |
Swabey89 | 12:3198f0b7b36f | 86 | if (n==-1) {n = (BUFFERSIZE-Nspaces);} |
Swabey89 | 12:3198f0b7b36f | 87 | bufferLock.lock(); |
Swabey89 | 12:3198f0b7b36f | 88 | while (i < n) |
Swabey89 | 11:89960c1f2234 | 89 | { |
Swabey89 | 12:3198f0b7b36f | 90 | spaceAvailable.release(); |
Swabey89 | 12:3198f0b7b36f | 91 | i++; |
Swabey89 | 11:89960c1f2234 | 92 | } |
Swabey89 | 12:3198f0b7b36f | 93 | bufferLock.unlock(); |
Swabey89 | 12:3198f0b7b36f | 94 | Nspaces += i; |
Swabey89 | 12:3198f0b7b36f | 95 | printlock.lock(); |
Swabey89 | 12:3198f0b7b36f | 96 | pc->printf("Deleted %d records\r\n\n\n", i); |
Swabey89 | 12:3198f0b7b36f | 97 | printlock.unlock(); |
Swabey89 | 13:5f786448e883 | 98 | SD_tout.detach(); |
Swabey89 | 1:c3af0c26ded2 | 99 | } |
Swabey89 | 1:c3af0c26ded2 | 100 | |
Swabey89 | 10:f2b8e3b587d5 | 101 | |
Swabey89 | 10:f2b8e3b587d5 | 102 | |
Swabey89 | 10:f2b8e3b587d5 | 103 | //UNUSED |
Swabey89 | 3:c6e5dd2faa22 | 104 | void SDaddSample(double temp, double pressure) |
Swabey89 | 3:c6e5dd2faa22 | 105 | { |
Swabey89 | 4:dc767b5f917b | 106 | //Add the sampled data to the SD card |
Swabey89 | 6:5646450f583b | 107 | yellowLED = !yellowLED; //debugging |
Swabey89 | 4:dc767b5f917b | 108 | fp = fopen("/sd/q.csv","a"); |
Swabey89 | 3:c6e5dd2faa22 | 109 | fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure); |
Swabey89 | 3:c6e5dd2faa22 | 110 | fclose(fp); |
Swabey89 | 3:c6e5dd2faa22 | 111 | } |
Swabey89 | 3:c6e5dd2faa22 | 112 | |
Swabey89 | 1:c3af0c26ded2 | 113 | void SDalive(void) |
Swabey89 | 1:c3af0c26ded2 | 114 | { |
Swabey89 | 3:c6e5dd2faa22 | 115 | //Signal that the SD thread is still alive |
Swabey89 | 2:9a5eea2adbf8 | 116 | //puts("SD THREAD ALIVE\n"); |
Swabey89 | 7:393fa8184388 | 117 | } |
Swabey89 | 7:393fa8184388 | 118 | |
Swabey89 | 9:113f4934b907 | 119 | void SDmount(void) |
Swabey89 | 9:113f4934b907 | 120 | { |
Swabey89 | 9:113f4934b907 | 121 | while(true) |
Swabey89 | 9:113f4934b907 | 122 | { |
Swabey89 | 9:113f4934b907 | 123 | Thread::signal_wait(SIGNAL_SD); |
Swabey89 | 13:5f786448e883 | 124 | |
Swabey89 | 13:5f786448e883 | 125 | SD_tout.attach(SD_toutISR,TOUT_TIME); |
Swabey89 | 13:5f786448e883 | 126 | |
Swabey89 | 9:113f4934b907 | 127 | if (sd_init) |
Swabey89 | 9:113f4934b907 | 128 | { |
Swabey89 | 9:113f4934b907 | 129 | fclose(fp); |
Swabey89 | 9:113f4934b907 | 130 | sd.deinit(); |
Swabey89 | 10:f2b8e3b587d5 | 131 | printlock.lock(); |
Swabey89 | 9:113f4934b907 | 132 | pc->printf("SD CARD UNMOUNTED\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 133 | printlock.unlock(); |
Swabey89 | 10:f2b8e3b587d5 | 134 | |
Swabey89 | 10:f2b8e3b587d5 | 135 | LCDlock.lock(); |
Swabey89 | 9:113f4934b907 | 136 | lcd.cls(); |
Swabey89 | 10:f2b8e3b587d5 | 137 | lcd.printf("Unmounted.."); |
Swabey89 | 10:f2b8e3b587d5 | 138 | Thread::wait(5000); |
Swabey89 | 10:f2b8e3b587d5 | 139 | LCDlock.unlock(); |
Swabey89 | 10:f2b8e3b587d5 | 140 | |
Swabey89 | 9:113f4934b907 | 141 | sd_init = false; |
Swabey89 | 9:113f4934b907 | 142 | |
Swabey89 | 9:113f4934b907 | 143 | } |
Swabey89 | 9:113f4934b907 | 144 | else |
Swabey89 | 9:113f4934b907 | 145 | { |
Swabey89 | 9:113f4934b907 | 146 | SDcard(); |
Swabey89 | 10:f2b8e3b587d5 | 147 | LCDlock.lock(); |
Swabey89 | 10:f2b8e3b587d5 | 148 | lcd.cls(); |
Swabey89 | 10:f2b8e3b587d5 | 149 | if (sd_init) |
Swabey89 | 10:f2b8e3b587d5 | 150 | { |
Swabey89 | 10:f2b8e3b587d5 | 151 | lcd.printf("SD mounted.."); |
Swabey89 | 10:f2b8e3b587d5 | 152 | } |
Swabey89 | 10:f2b8e3b587d5 | 153 | else |
Swabey89 | 10:f2b8e3b587d5 | 154 | { |
Swabey89 | 10:f2b8e3b587d5 | 155 | lcd.printf("SD FAILED.."); |
Swabey89 | 10:f2b8e3b587d5 | 156 | } |
Swabey89 | 10:f2b8e3b587d5 | 157 | Thread::wait(5000); |
Swabey89 | 10:f2b8e3b587d5 | 158 | LCDlock.unlock(); |
Swabey89 | 9:113f4934b907 | 159 | } |
Swabey89 | 13:5f786448e883 | 160 | SD_tout.detach(); |
Swabey89 | 9:113f4934b907 | 161 | } |
Swabey89 | 9:113f4934b907 | 162 | } |
Swabey89 | 9:113f4934b907 | 163 |