SD card functionality

Dependents:   ELEC350_Project2 SDcard

Committer:
Swabey89
Date:
Wed Dec 19 15:29:27 2018 +0000
Revision:
10:f2b8e3b587d5
Parent:
9:113f4934b907
Child:
11:89960c1f2234
Added locks

Who changed what in which revision?

UserRevisionLine numberNew 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 9:113f4934b907 6 static time_t seconds; //static reqiured?
Swabey89 0:0fc2cf27ff9e 7
Swabey89 2:9a5eea2adbf8 8 //Initialise the SD card
Swabey89 8:ee8f65745141 9 if (sd.init() != 0) {
Swabey89 10:f2b8e3b587d5 10 printlock.lock();
Swabey89 8:ee8f65745141 11 pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r");
Swabey89 10:f2b8e3b587d5 12 printlock.unlock();
Swabey89 8:ee8f65745141 13 sd_init = false;
Swabey89 8:ee8f65745141 14 //lcd.cls();
Swabey89 8:ee8f65745141 15 //lcd.printf("CANNOT INIT SD");
Swabey89 8:ee8f65745141 16 //errorCode(FATAL);
Swabey89 2:9a5eea2adbf8 17 }
Swabey89 8:ee8f65745141 18 else
Swabey89 8:ee8f65745141 19 {
Swabey89 8:ee8f65745141 20 //Create a filing system for SD Card
Swabey89 8:ee8f65745141 21 fs = new FATFileSystem("sd", &sd);
Swabey89 10:f2b8e3b587d5 22 printlock.lock();
Swabey89 8:ee8f65745141 23 pc->printf("SD CARD INITIALISED\n\r");
Swabey89 10:f2b8e3b587d5 24 printlock.unlock();
Swabey89 7:393fa8184388 25
Swabey89 8:ee8f65745141 26 //Open to WRITE
Swabey89 8:ee8f65745141 27 char fileDate[30];
Swabey89 10:f2b8e3b587d5 28 timeLock.lock();
Swabey89 8:ee8f65745141 29 seconds = time(NULL);
Swabey89 8:ee8f65745141 30 timeData = localtime(&seconds);
Swabey89 8:ee8f65745141 31 set_time(mktime(timeData));
Swabey89 8:ee8f65745141 32 strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
Swabey89 10:f2b8e3b587d5 33 timeLock.unlock();
Swabey89 8:ee8f65745141 34 fp = fopen(fileDate,"a");
Swabey89 8:ee8f65745141 35
Swabey89 8:ee8f65745141 36 if (fp == NULL)
Swabey89 8:ee8f65745141 37 {
Swabey89 10:f2b8e3b587d5 38 printlock.lock();
Swabey89 8:ee8f65745141 39 pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r");
Swabey89 10:f2b8e3b587d5 40 printlock.unlock();
Swabey89 8:ee8f65745141 41 //lcd.cls();
Swabey89 8:ee8f65745141 42 //lcd.printf("CANNOT OPEN FILE\n\n");
Swabey89 8:ee8f65745141 43 //errorCode(FATAL);
Swabey89 8:ee8f65745141 44 }
Swabey89 8:ee8f65745141 45 else
Swabey89 8:ee8f65745141 46 {
Swabey89 10:f2b8e3b587d5 47 printlock.lock();
Swabey89 10:f2b8e3b587d5 48 pc->printf("FILE OPEN FOR WRITING\n\n\n\r");
Swabey89 10:f2b8e3b587d5 49 printlock.unlock();
Swabey89 8:ee8f65745141 50 sd_init = true;
Swabey89 8:ee8f65745141 51 }
Swabey89 8:ee8f65745141 52 fclose(fp);
Swabey89 8:ee8f65745141 53 }
Swabey89 1:c3af0c26ded2 54 }
Swabey89 1:c3af0c26ded2 55
Swabey89 1:c3af0c26ded2 56 void SDread(int n)
Swabey89 1:c3af0c26ded2 57 {
Swabey89 3:c6e5dd2faa22 58 //Read n samples from the SD card
Swabey89 10:f2b8e3b587d5 59 printlock.lock();
Swabey89 2:9a5eea2adbf8 60 if (n == -1) puts("Received command to read all");
Swabey89 2:9a5eea2adbf8 61 else printf("Received command to read %d\n", n);
Swabey89 10:f2b8e3b587d5 62 printlock.unlock();
Swabey89 2:9a5eea2adbf8 63 }
Swabey89 2:9a5eea2adbf8 64
Swabey89 2:9a5eea2adbf8 65 void SDdelete(int n)
Swabey89 2:9a5eea2adbf8 66 {
Swabey89 3:c6e5dd2faa22 67 //Delete n samples from the SD card
Swabey89 10:f2b8e3b587d5 68 printlock.lock();
Swabey89 2:9a5eea2adbf8 69 if (n == -1) puts("Received command to delete all");
Swabey89 10:f2b8e3b587d5 70 else printf("Received command to delete %d\n", n);
Swabey89 10:f2b8e3b587d5 71 printlock.unlock();
Swabey89 1:c3af0c26ded2 72 }
Swabey89 1:c3af0c26ded2 73
Swabey89 10:f2b8e3b587d5 74
Swabey89 10:f2b8e3b587d5 75
Swabey89 10:f2b8e3b587d5 76 //UNUSED
Swabey89 3:c6e5dd2faa22 77 void SDaddSample(double temp, double pressure)
Swabey89 3:c6e5dd2faa22 78 {
Swabey89 4:dc767b5f917b 79 //Add the sampled data to the SD card
Swabey89 6:5646450f583b 80 yellowLED = !yellowLED; //debugging
Swabey89 4:dc767b5f917b 81 fp = fopen("/sd/q.csv","a");
Swabey89 3:c6e5dd2faa22 82 fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
Swabey89 3:c6e5dd2faa22 83 fclose(fp);
Swabey89 3:c6e5dd2faa22 84 }
Swabey89 3:c6e5dd2faa22 85
Swabey89 1:c3af0c26ded2 86 void SDalive(void)
Swabey89 1:c3af0c26ded2 87 {
Swabey89 3:c6e5dd2faa22 88 //Signal that the SD thread is still alive
Swabey89 2:9a5eea2adbf8 89 //puts("SD THREAD ALIVE\n");
Swabey89 7:393fa8184388 90 }
Swabey89 7:393fa8184388 91
Swabey89 9:113f4934b907 92 void SDmount(void)
Swabey89 9:113f4934b907 93 {
Swabey89 9:113f4934b907 94 while(true)
Swabey89 9:113f4934b907 95 {
Swabey89 9:113f4934b907 96 Thread::signal_wait(SIGNAL_SD);
Swabey89 10:f2b8e3b587d5 97
Swabey89 9:113f4934b907 98 if (sd_init)
Swabey89 9:113f4934b907 99 {
Swabey89 9:113f4934b907 100 fclose(fp);
Swabey89 9:113f4934b907 101 sd.deinit();
Swabey89 10:f2b8e3b587d5 102 printlock.lock();
Swabey89 9:113f4934b907 103 pc->printf("SD CARD UNMOUNTED\n\r");
Swabey89 10:f2b8e3b587d5 104 printlock.unlock();
Swabey89 10:f2b8e3b587d5 105
Swabey89 10:f2b8e3b587d5 106 LCDlock.lock();
Swabey89 9:113f4934b907 107 lcd.cls();
Swabey89 10:f2b8e3b587d5 108 lcd.printf("Unmounted..");
Swabey89 10:f2b8e3b587d5 109 Thread::wait(5000);
Swabey89 10:f2b8e3b587d5 110 LCDlock.unlock();
Swabey89 10:f2b8e3b587d5 111
Swabey89 9:113f4934b907 112 sd_init = false;
Swabey89 9:113f4934b907 113
Swabey89 9:113f4934b907 114 }
Swabey89 9:113f4934b907 115 else
Swabey89 9:113f4934b907 116 {
Swabey89 9:113f4934b907 117 SDcard();
Swabey89 10:f2b8e3b587d5 118 LCDlock.lock();
Swabey89 10:f2b8e3b587d5 119 lcd.cls();
Swabey89 10:f2b8e3b587d5 120 if (sd_init)
Swabey89 10:f2b8e3b587d5 121 {
Swabey89 10:f2b8e3b587d5 122 lcd.printf("SD mounted..");
Swabey89 10:f2b8e3b587d5 123 }
Swabey89 10:f2b8e3b587d5 124 else
Swabey89 10:f2b8e3b587d5 125 {
Swabey89 10:f2b8e3b587d5 126 lcd.printf("SD FAILED..");
Swabey89 10:f2b8e3b587d5 127 }
Swabey89 10:f2b8e3b587d5 128 Thread::wait(5000);
Swabey89 10:f2b8e3b587d5 129 LCDlock.unlock();
Swabey89 9:113f4934b907 130 }
Swabey89 9:113f4934b907 131
Swabey89 9:113f4934b907 132 }
Swabey89 9:113f4934b907 133 }
Swabey89 9:113f4934b907 134