Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP280
SDCard.cpp@22:617bf92b481f, 2018-01-10 (annotated)
- Committer:
- mwthewsey
- Date:
- Wed Jan 10 00:32:08 2018 +0000
- Revision:
- 22:617bf92b481f
- Parent:
- 21:6e733076f49c
Added set date and time from LCD.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mwthewsey | 12:03589f1d5c30 | 1 | #include "mbed.h" |
| mwthewsey | 12:03589f1d5c30 | 2 | #include "Sampling.h" |
| mwthewsey | 12:03589f1d5c30 | 3 | #include "SDCard.h" |
| mwthewsey | 12:03589f1d5c30 | 4 | #include "SDBlockDevice.h" |
| mwthewsey | 12:03589f1d5c30 | 5 | #include "FATFileSystem.h" |
| mwthewsey | 12:03589f1d5c30 | 6 | #include "LCD.h" |
| mwthewsey | 12:03589f1d5c30 | 7 | |
| mwthewsey | 21:6e733076f49c | 8 | |
| mwthewsey | 21:6e733076f49c | 9 | //Hardware |
| mwthewsey | 12:03589f1d5c30 | 10 | DigitalOut SDCardStatusLED(LED3); |
| mwthewsey | 12:03589f1d5c30 | 11 | |
| mwthewsey | 21:6e733076f49c | 12 | //Variables |
| mwthewsey | 12:03589f1d5c30 | 13 | bool SDCardPresent; |
| mwthewsey | 12:03589f1d5c30 | 14 | bool SDCardMounted; |
| mwthewsey | 12:03589f1d5c30 | 15 | unsigned short SDinternalIndex; |
| mwthewsey | 12:03589f1d5c30 | 16 | |
| mwthewsey | 12:03589f1d5c30 | 17 | FILE* fp; //File pointer type |
| mwthewsey | 21:6e733076f49c | 18 | SDCardStates SDCurrentState; //Create state machine state |
| mwthewsey | 12:03589f1d5c30 | 19 | |
| mwthewsey | 21:6e733076f49c | 20 | Timeout antiBounce; |
| mwthewsey | 12:03589f1d5c30 | 21 | Thread SDCardThread; |
| mwthewsey | 12:03589f1d5c30 | 22 | FATFileSystem fs; |
| mwthewsey | 12:03589f1d5c30 | 23 | |
| mwthewsey | 12:03589f1d5c30 | 24 | |
| mwthewsey | 12:03589f1d5c30 | 25 | void SDCardInit(void) |
| mwthewsey | 12:03589f1d5c30 | 26 | { |
| mwthewsey | 12:03589f1d5c30 | 27 | SDCardThread.start(&SDCardCode); //Start thread running |
| mwthewsey | 12:03589f1d5c30 | 28 | SD_WP.rise(&SDCardRemovedISR); //SD removed |
| mwthewsey | 12:03589f1d5c30 | 29 | SD_WP.fall(&SDCardInsertedISR); //SD inserted |
| mwthewsey | 12:03589f1d5c30 | 30 | UserButton.fall(&SDCardButtonISR); //Button released |
| mwthewsey | 12:03589f1d5c30 | 31 | |
| mwthewsey | 21:6e733076f49c | 32 | if (SD_WP == 1) { //Checks if card is inserted on power up and sets states |
| mwthewsey | 12:03589f1d5c30 | 33 | SDCardPresent = false; |
| mwthewsey | 12:03589f1d5c30 | 34 | SDCurrentState = REMOVED; |
| mwthewsey | 12:03589f1d5c30 | 35 | SDCardMounted = false; |
| mwthewsey | 12:03589f1d5c30 | 36 | } else { |
| mwthewsey | 12:03589f1d5c30 | 37 | SDCardPresent = true; |
| mwthewsey | 12:03589f1d5c30 | 38 | SDCurrentState = INSERTED; |
| mwthewsey | 12:03589f1d5c30 | 39 | SDCardMounted = false; |
| mwthewsey | 12:03589f1d5c30 | 40 | } |
| mwthewsey | 12:03589f1d5c30 | 41 | } |
| mwthewsey | 12:03589f1d5c30 | 42 | |
| mwthewsey | 12:03589f1d5c30 | 43 | |
| mwthewsey | 12:03589f1d5c30 | 44 | //-----------------------------------------------------------------// |
| mwthewsey | 12:03589f1d5c30 | 45 | //ISRs |
| mwthewsey | 12:03589f1d5c30 | 46 | void SDCardRemovedISR(void) |
| mwthewsey | 12:03589f1d5c30 | 47 | { |
| mwthewsey | 12:03589f1d5c30 | 48 | SDCardPresent = false; |
| mwthewsey | 12:03589f1d5c30 | 49 | SDCurrentState = REMOVED; |
| mwthewsey | 21:6e733076f49c | 50 | antiBounce.attach(&antiBounceISR,1); |
| mwthewsey | 12:03589f1d5c30 | 51 | } |
| mwthewsey | 12:03589f1d5c30 | 52 | |
| mwthewsey | 12:03589f1d5c30 | 53 | void SDCardInsertedISR(void) |
| mwthewsey | 12:03589f1d5c30 | 54 | { |
| mwthewsey | 12:03589f1d5c30 | 55 | SDCardPresent = true; |
| mwthewsey | 12:03589f1d5c30 | 56 | SDCurrentState = INSERTED; |
| mwthewsey | 21:6e733076f49c | 57 | antiBounce.attach(&antiBounceISR,1); |
| mwthewsey | 12:03589f1d5c30 | 58 | |
| mwthewsey | 12:03589f1d5c30 | 59 | } |
| mwthewsey | 12:03589f1d5c30 | 60 | |
| mwthewsey | 12:03589f1d5c30 | 61 | void SDCardButtonISR(void) |
| mwthewsey | 12:03589f1d5c30 | 62 | { |
| mwthewsey | 12:03589f1d5c30 | 63 | SDCurrentState = DISMOUNTREQUEST; |
| mwthewsey | 21:6e733076f49c | 64 | antiBounce.attach(&antiBounceISR,1); |
| mwthewsey | 21:6e733076f49c | 65 | } |
| mwthewsey | 21:6e733076f49c | 66 | //-----------------------------------------------------------------// |
| mwthewsey | 21:6e733076f49c | 67 | |
| mwthewsey | 21:6e733076f49c | 68 | void antiBounceISR(void) |
| mwthewsey | 21:6e733076f49c | 69 | { |
| mwthewsey | 12:03589f1d5c30 | 70 | SDCardThread.signal_set(1); |
| mwthewsey | 12:03589f1d5c30 | 71 | } |
| mwthewsey | 12:03589f1d5c30 | 72 | |
| mwthewsey | 12:03589f1d5c30 | 73 | |
| mwthewsey | 12:03589f1d5c30 | 74 | void SDCardCode(void) |
| mwthewsey | 12:03589f1d5c30 | 75 | { |
| mwthewsey | 12:03589f1d5c30 | 76 | while(true) { |
| mwthewsey | 12:03589f1d5c30 | 77 | Thread::signal_wait(1); //Wait untill signal set |
| mwthewsey | 12:03589f1d5c30 | 78 | |
| mwthewsey | 12:03589f1d5c30 | 79 | if (SDCurrentState == REMOVED) { |
| mwthewsey | 12:03589f1d5c30 | 80 | |
| mwthewsey | 12:03589f1d5c30 | 81 | if (SDCardMounted) { |
| mwthewsey | 12:03589f1d5c30 | 82 | //print error message file currupt |
| mwthewsey | 12:03589f1d5c30 | 83 | lcd.SendMessage("SD CARD REMOVED! FILE CORRUPT",true); |
| mwthewsey | 22:617bf92b481f | 84 | SDCardMounted = 0; |
| mwthewsey | 12:03589f1d5c30 | 85 | } |
| mwthewsey | 21:6e733076f49c | 86 | LogEvent(Log_SDRemoved); |
| mwthewsey | 21:6e733076f49c | 87 | SDCardStatusLED = 0; //Set LEDs |
| mwthewsey | 12:03589f1d5c30 | 88 | GreenLED = 0; |
| mwthewsey | 12:03589f1d5c30 | 89 | |
| mwthewsey | 12:03589f1d5c30 | 90 | |
| mwthewsey | 12:03589f1d5c30 | 91 | |
| mwthewsey | 12:03589f1d5c30 | 92 | } else if (SDCurrentState == INSERTED) { |
| mwthewsey | 21:6e733076f49c | 93 | LogEvent(Log_SDInserted); |
| mwthewsey | 21:6e733076f49c | 94 | SDCardStatusLED = 0; //Set LEDs |
| mwthewsey | 21:6e733076f49c | 95 | GreenLED = 0; |
| mwthewsey | 12:03589f1d5c30 | 96 | |
| mwthewsey | 12:03589f1d5c30 | 97 | if ( sd.init() != 0) { //Initalises the SDCard and checks if it succeeded |
| mwthewsey | 12:03589f1d5c30 | 98 | //SDInit failed. Reinsert the SDCard |
| mwthewsey | 21:6e733076f49c | 99 | lcd.SendMessage("REINSERT SDCARD",true); |
| mwthewsey | 21:6e733076f49c | 100 | LogEvent(Log_SDInitFail); |
| mwthewsey | 12:03589f1d5c30 | 101 | |
| mwthewsey | 21:6e733076f49c | 102 | } else { |
| mwthewsey | 12:03589f1d5c30 | 103 | |
| mwthewsey | 21:6e733076f49c | 104 | //Create a filing system for SD Card |
| mwthewsey | 21:6e733076f49c | 105 | FATFileSystem fs("sd", &sd); |
| mwthewsey | 12:03589f1d5c30 | 106 | |
| mwthewsey | 21:6e733076f49c | 107 | //Open to WRITE |
| mwthewsey | 21:6e733076f49c | 108 | fp = fopen("/sd/samples.csv","w"); |
| mwthewsey | 12:03589f1d5c30 | 109 | |
| mwthewsey | 12:03589f1d5c30 | 110 | |
| mwthewsey | 21:6e733076f49c | 111 | if (fp == NULL) { |
| mwthewsey | 21:6e733076f49c | 112 | //error |
| mwthewsey | 21:6e733076f49c | 113 | lcd.SendMessage("CANNOT OPEN FILE/sd/samples.csv",true); |
| mwthewsey | 21:6e733076f49c | 114 | LogEvent(Log_SDFileOpenFail); |
| mwthewsey | 21:6e733076f49c | 115 | |
| mwthewsey | 21:6e733076f49c | 116 | } else { |
| mwthewsey | 21:6e733076f49c | 117 | SDCardMounted = true; //SD card is now mounted |
| mwthewsey | 21:6e733076f49c | 118 | |
| mwthewsey | 21:6e733076f49c | 119 | Sampling(false); //Stop sampling |
| mwthewsey | 21:6e733076f49c | 120 | SDinternalIndex = currentIndex; //SDinternalIndex for incrementing out data |
| mwthewsey | 21:6e733076f49c | 121 | TakeKeys(true); //Take keys |
| mwthewsey | 21:6e733076f49c | 122 | |
| mwthewsey | 21:6e733076f49c | 123 | fprintf(fp,"Date,Time,Temperature,Pressure,Light\n"); |
| mwthewsey | 21:6e733076f49c | 124 | for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize |
| mwthewsey | 21:6e733076f49c | 125 | tm T = ReturnDateTimeStruct(timeReadings[SDinternalIndex]); |
| mwthewsey | 21:6e733076f49c | 126 | //SDinternalIndex was set as newest. We will now decrement to display oldest to newest |
| mwthewsey | 21:6e733076f49c | 127 | fprintf(fp," %4d/%2d/%2d,%2d:%2d:%2d,%2.2f,%2.2f,%2.2f\n",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec,tempReadings[SDinternalIndex],presReadings[SDinternalIndex],LDRReadings[SDinternalIndex]); |
| mwthewsey | 21:6e733076f49c | 128 | SDinternalIndex = IndexDecrement(SDinternalIndex); //Decrement internal index |
| mwthewsey | 21:6e733076f49c | 129 | } |
| mwthewsey | 21:6e733076f49c | 130 | } |
| mwthewsey | 21:6e733076f49c | 131 | TakeKeys(false); //Return keys |
| mwthewsey | 21:6e733076f49c | 132 | Sampling(true); //Start sampling |
| mwthewsey | 21:6e733076f49c | 133 | |
| mwthewsey | 21:6e733076f49c | 134 | |
| mwthewsey | 21:6e733076f49c | 135 | fclose(fp); //Close file |
| mwthewsey | 21:6e733076f49c | 136 | SDCardStatusLED = 1;//Set light to indicate SD card file transfer complete. |
| mwthewsey | 21:6e733076f49c | 137 | } |
| mwthewsey | 12:03589f1d5c30 | 138 | |
| mwthewsey | 12:03589f1d5c30 | 139 | } else if (SDCurrentState == DISMOUNTREQUEST) { |
| mwthewsey | 12:03589f1d5c30 | 140 | |
| mwthewsey | 21:6e733076f49c | 141 | if (SDCardMounted) { |
| mwthewsey | 21:6e733076f49c | 142 | sd.deinit(); //De initalise SD card |
| mwthewsey | 21:6e733076f49c | 143 | SDCardMounted = false; //Card now not mounted |
| mwthewsey | 21:6e733076f49c | 144 | lcd.SendMessage("REMOVE SD CARD",true); //SD card can be removed |
| mwthewsey | 21:6e733076f49c | 145 | GreenLED = 1; //Indication LED |
| mwthewsey | 21:6e733076f49c | 146 | LogEvent(Log_SDDeInit); |
| mwthewsey | 21:6e733076f49c | 147 | } else { |
| mwthewsey | 21:6e733076f49c | 148 | lcd.SendMessage("NO SD CARD TO REMOVE",true); |
| mwthewsey | 21:6e733076f49c | 149 | LogEvent(Log_SDDismountFail); |
| mwthewsey | 21:6e733076f49c | 150 | } |
| mwthewsey | 12:03589f1d5c30 | 151 | |
| mwthewsey | 12:03589f1d5c30 | 152 | } |
| mwthewsey | 12:03589f1d5c30 | 153 | } |
| mwthewsey | 12:03589f1d5c30 | 154 | } |
| mwthewsey | 12:03589f1d5c30 | 155 | |
| mwthewsey | 12:03589f1d5c30 | 156 | |
| mwthewsey | 12:03589f1d5c30 | 157 |