SD card functionality

Dependents:   ELEC350_Project2 SDcard

Committer:
Swabey89
Date:
Wed Dec 19 13:09:15 2018 +0000
Revision:
9:113f4934b907
Parent:
8:ee8f65745141
Child:
10:f2b8e3b587d5
Added ability to mount and unmount SD card

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 8:ee8f65745141 10 pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r");
Swabey89 8:ee8f65745141 11 sd_init = false;
Swabey89 8:ee8f65745141 12 //lcd.cls();
Swabey89 8:ee8f65745141 13 //lcd.printf("CANNOT INIT SD");
Swabey89 8:ee8f65745141 14 //errorCode(FATAL);
Swabey89 2:9a5eea2adbf8 15 }
Swabey89 8:ee8f65745141 16 else
Swabey89 8:ee8f65745141 17 {
Swabey89 8:ee8f65745141 18 //Create a filing system for SD Card
Swabey89 8:ee8f65745141 19 fs = new FATFileSystem("sd", &sd);
Swabey89 8:ee8f65745141 20 pc->printf("SD CARD INITIALISED\n\r");
Swabey89 7:393fa8184388 21
Swabey89 8:ee8f65745141 22 //Open to WRITE
Swabey89 8:ee8f65745141 23 char fileDate[30];
Swabey89 8:ee8f65745141 24 seconds = time(NULL);
Swabey89 8:ee8f65745141 25 timeData = localtime(&seconds);
Swabey89 8:ee8f65745141 26 set_time(mktime(timeData));
Swabey89 8:ee8f65745141 27 strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData);
Swabey89 8:ee8f65745141 28
Swabey89 8:ee8f65745141 29 fp = fopen(fileDate,"a");
Swabey89 8:ee8f65745141 30
Swabey89 8:ee8f65745141 31 if (fp == NULL)
Swabey89 8:ee8f65745141 32 {
Swabey89 8:ee8f65745141 33 pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r");
Swabey89 8:ee8f65745141 34 //lcd.cls();
Swabey89 8:ee8f65745141 35 //lcd.printf("CANNOT OPEN FILE\n\n");
Swabey89 8:ee8f65745141 36 //errorCode(FATAL);
Swabey89 8:ee8f65745141 37 }
Swabey89 8:ee8f65745141 38 else
Swabey89 8:ee8f65745141 39 {
Swabey89 8:ee8f65745141 40 pc->printf("FILE OPEN FOR WRITING\n\r");
Swabey89 8:ee8f65745141 41 sd_init = true;
Swabey89 8:ee8f65745141 42 }
Swabey89 8:ee8f65745141 43 fclose(fp);
Swabey89 8:ee8f65745141 44 }
Swabey89 7:393fa8184388 45
Swabey89 8:ee8f65745141 46 //Last message before sampling begins - probably remove
Swabey89 2:9a5eea2adbf8 47 lcd.cls();
Swabey89 9:113f4934b907 48 lcd.printf("SD CARD INIT...\n\n");
Swabey89 1:c3af0c26ded2 49 }
Swabey89 1:c3af0c26ded2 50
Swabey89 1:c3af0c26ded2 51 void SDread(int n)
Swabey89 1:c3af0c26ded2 52 {
Swabey89 3:c6e5dd2faa22 53 //Read n samples from the SD card
Swabey89 2:9a5eea2adbf8 54 if (n == -1) puts("Received command to read all");
Swabey89 2:9a5eea2adbf8 55 else printf("Received command to read %d\n", n);
Swabey89 2:9a5eea2adbf8 56 }
Swabey89 2:9a5eea2adbf8 57
Swabey89 2:9a5eea2adbf8 58 void SDdelete(int n)
Swabey89 2:9a5eea2adbf8 59 {
Swabey89 3:c6e5dd2faa22 60 //Delete n samples from the SD card
Swabey89 2:9a5eea2adbf8 61 if (n == -1) puts("Received command to delete all");
Swabey89 2:9a5eea2adbf8 62 else printf("Received command to delete %d\n", n);
Swabey89 1:c3af0c26ded2 63 }
Swabey89 1:c3af0c26ded2 64
Swabey89 3:c6e5dd2faa22 65 void SDaddSample(double temp, double pressure)
Swabey89 3:c6e5dd2faa22 66 {
Swabey89 4:dc767b5f917b 67 //Add the sampled data to the SD card
Swabey89 6:5646450f583b 68 yellowLED = !yellowLED; //debugging
Swabey89 4:dc767b5f917b 69 fp = fopen("/sd/q.csv","a");
Swabey89 3:c6e5dd2faa22 70 fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
Swabey89 3:c6e5dd2faa22 71 fclose(fp);
Swabey89 3:c6e5dd2faa22 72 }
Swabey89 3:c6e5dd2faa22 73
Swabey89 1:c3af0c26ded2 74 void SDalive(void)
Swabey89 1:c3af0c26ded2 75 {
Swabey89 3:c6e5dd2faa22 76 //Signal that the SD thread is still alive
Swabey89 2:9a5eea2adbf8 77 //puts("SD THREAD ALIVE\n");
Swabey89 7:393fa8184388 78 }
Swabey89 7:393fa8184388 79
Swabey89 9:113f4934b907 80 void SDmount(void)
Swabey89 9:113f4934b907 81 {
Swabey89 9:113f4934b907 82 while(true)
Swabey89 9:113f4934b907 83 {
Swabey89 9:113f4934b907 84 Thread::signal_wait(SIGNAL_SD);
Swabey89 9:113f4934b907 85
Swabey89 9:113f4934b907 86 //Change state of SD card
Swabey89 9:113f4934b907 87 if (sd_init)
Swabey89 9:113f4934b907 88 {
Swabey89 9:113f4934b907 89 fclose(fp);
Swabey89 9:113f4934b907 90 sd.deinit();
Swabey89 9:113f4934b907 91 pc->printf("SD CARD UNMOUNTED\n\r");
Swabey89 9:113f4934b907 92 lcd.cls();
Swabey89 9:113f4934b907 93 lcd.printf("Unmounted..\n\n");
Swabey89 9:113f4934b907 94 sd_init = false;
Swabey89 9:113f4934b907 95
Swabey89 9:113f4934b907 96 }
Swabey89 9:113f4934b907 97 else
Swabey89 9:113f4934b907 98 {
Swabey89 9:113f4934b907 99 //try to init, if failed say cannot init, if pass then say init passed and change state of sd_init
Swabey89 9:113f4934b907 100 SDcard();
Swabey89 9:113f4934b907 101 }
Swabey89 9:113f4934b907 102
Swabey89 9:113f4934b907 103 }
Swabey89 9:113f4934b907 104 }
Swabey89 9:113f4934b907 105