UART Command Parser Time Manager Data Store for SD Card for stm32l476 [it's not Licensed as BSD/GPLx]

Dependencies:   mbed SDFileSystem

Committer:
Inscape_ao
Date:
Wed Apr 24 00:40:42 2019 +0000
Revision:
4:bec3f80dc49c
Parent:
2:a694440145e9
Child:
5:a37e3a15444b
marge SDFileSystem and add sampleCode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Inscape_ao 0:c347f602596d 1 /** --- Includes --- */
Inscape_ao 0:c347f602596d 2 #include "mbed.h"
Inscape_ao 2:a694440145e9 3 #include "SDFileSystem.h"
Inscape_ao 2:a694440145e9 4 #include "TimeManager.h"
Inscape_ao 0:c347f602596d 5 #include "UartReceiver.h"
Inscape_ao 0:c347f602596d 6 #include "CommandParser.h"
Inscape_ao 0:c347f602596d 7 #include "global.h"
Inscape_ao 0:c347f602596d 8 #include "string.h"
Inscape_ao 0:c347f602596d 9
Inscape_ao 0:c347f602596d 10 /** --- Global Variables --- */
Inscape_ao 0:c347f602596d 11 Serial pc(SERIAL_TX, SERIAL_RX);
Inscape_ao 0:c347f602596d 12 DigitalOut myled(LED1);
Inscape_ao 4:bec3f80dc49c 13 SDFileSystem sd(D11, D12, D13, D4, "sd"); // MOSI, MISO, SCK, CS
Inscape_ao 4:bec3f80dc49c 14 FILE *fp;
Inscape_ao 4:bec3f80dc49c 15
Inscape_ao 4:bec3f80dc49c 16 class SDDataStore
Inscape_ao 4:bec3f80dc49c 17 {
Inscape_ao 4:bec3f80dc49c 18 public:
Inscape_ao 4:bec3f80dc49c 19 const static int MaxPathLength = 256;
Inscape_ao 4:bec3f80dc49c 20 private:
Inscape_ao 4:bec3f80dc49c 21 SDFileSystem *pS;
Inscape_ao 4:bec3f80dc49c 22 TimeManager *pT;
Inscape_ao 4:bec3f80dc49c 23 FILE *fpCurrent;
Inscape_ao 4:bec3f80dc49c 24 char fnameCurrent[MaxPathLength];
Inscape_ao 4:bec3f80dc49c 25 private:
Inscape_ao 4:bec3f80dc49c 26 /* (INVALIDED) constructor */
Inscape_ao 4:bec3f80dc49c 27 SDDataStore(void);
Inscape_ao 4:bec3f80dc49c 28 public:
Inscape_ao 4:bec3f80dc49c 29 /* constructor, pSD, TimeManager */
Inscape_ao 4:bec3f80dc49c 30 SDDataStore(SDFileSystem *pSetSD, TimeManager *pSetTM)
Inscape_ao 4:bec3f80dc49c 31 {
Inscape_ao 4:bec3f80dc49c 32 pS = pSetSD;
Inscape_ao 4:bec3f80dc49c 33 pT = pSetTM;
Inscape_ao 4:bec3f80dc49c 34 fpCurrent = NULL;
Inscape_ao 4:bec3f80dc49c 35 fnameCurrent[0] = '\0';
Inscape_ao 4:bec3f80dc49c 36 }
Inscape_ao 4:bec3f80dc49c 37
Inscape_ao 4:bec3f80dc49c 38 /* destructor */
Inscape_ao 4:bec3f80dc49c 39 ~SDDataStore()
Inscape_ao 4:bec3f80dc49c 40 {
Inscape_ao 4:bec3f80dc49c 41 closeFile();
Inscape_ao 4:bec3f80dc49c 42 }
Inscape_ao 4:bec3f80dc49c 43
Inscape_ao 4:bec3f80dc49c 44 bool startFileWithTimeStamp(void)
Inscape_ao 4:bec3f80dc49c 45 {
Inscape_ao 4:bec3f80dc49c 46 char timeStamp[TimeManager::TimeStampLength + 1] = {0};
Inscape_ao 4:bec3f80dc49c 47 char fname[MaxPathLength] = {0};
Inscape_ao 4:bec3f80dc49c 48 pT->getTimeStamp(timeStamp);
Inscape_ao 4:bec3f80dc49c 49 sprintf(fname, "%s.txt", timeStamp);
Inscape_ao 4:bec3f80dc49c 50 if (this->checkFileExist(fname) == true) {
Inscape_ao 4:bec3f80dc49c 51 this->removeFile(fname);
Inscape_ao 4:bec3f80dc49c 52 }
Inscape_ao 4:bec3f80dc49c 53 if (this->openFile(fname) != true) {
Inscape_ao 4:bec3f80dc49c 54 return false;
Inscape_ao 4:bec3f80dc49c 55 }
Inscape_ao 4:bec3f80dc49c 56 return true;
Inscape_ao 4:bec3f80dc49c 57 }
Inscape_ao 4:bec3f80dc49c 58
Inscape_ao 4:bec3f80dc49c 59 /* checkFileExist, false=NOT-EXIST, true=EXIST */
Inscape_ao 4:bec3f80dc49c 60 bool checkFileExist(char *fname)
Inscape_ao 4:bec3f80dc49c 61 {
Inscape_ao 4:bec3f80dc49c 62 FILE *fp;
Inscape_ao 4:bec3f80dc49c 63 char filePath[MaxPathLength];
Inscape_ao 4:bec3f80dc49c 64 sprintf(filePath, "/sd/%s", fname);
Inscape_ao 4:bec3f80dc49c 65 pathTermination(filePath);
Inscape_ao 4:bec3f80dc49c 66 fp = fopen(filePath, "r");
Inscape_ao 4:bec3f80dc49c 67 if (fp == NULL) {
Inscape_ao 4:bec3f80dc49c 68 return false;
Inscape_ao 4:bec3f80dc49c 69 }
Inscape_ao 4:bec3f80dc49c 70 fclose(fp);
Inscape_ao 4:bec3f80dc49c 71 return true;
Inscape_ao 4:bec3f80dc49c 72 }
Inscape_ao 4:bec3f80dc49c 73
Inscape_ao 4:bec3f80dc49c 74 /* removeFile, false=NOT-EXIST, true=EXIST */
Inscape_ao 4:bec3f80dc49c 75 void removeFile(char *fname)
Inscape_ao 4:bec3f80dc49c 76 {
Inscape_ao 4:bec3f80dc49c 77 char filePath[MaxPathLength];
Inscape_ao 4:bec3f80dc49c 78 sprintf(filePath, "/sd/%s", fname);
Inscape_ao 4:bec3f80dc49c 79 pathTermination(filePath);
Inscape_ao 4:bec3f80dc49c 80 remove(filePath);
Inscape_ao 4:bec3f80dc49c 81 }
Inscape_ao 4:bec3f80dc49c 82
Inscape_ao 4:bec3f80dc49c 83 /* open file at WriteMode FALSE=ERR, TRUE=OK */
Inscape_ao 4:bec3f80dc49c 84 bool openFile(char *fname)
Inscape_ao 4:bec3f80dc49c 85 {
Inscape_ao 4:bec3f80dc49c 86 FILE *fp;
Inscape_ao 4:bec3f80dc49c 87 char filePath[MaxPathLength];
Inscape_ao 4:bec3f80dc49c 88 sprintf(filePath, "/sd/%s", fname);
Inscape_ao 4:bec3f80dc49c 89 pathTermination(filePath);
Inscape_ao 4:bec3f80dc49c 90
Inscape_ao 4:bec3f80dc49c 91 fp = fopen(filePath, "w");
Inscape_ao 4:bec3f80dc49c 92 if (fp == NULL) {
Inscape_ao 4:bec3f80dc49c 93 return false;
Inscape_ao 4:bec3f80dc49c 94 }
Inscape_ao 4:bec3f80dc49c 95 fpCurrent = fp;
Inscape_ao 4:bec3f80dc49c 96 sprintf(fnameCurrent, "%s", fname);
Inscape_ao 4:bec3f80dc49c 97 return true;
Inscape_ao 4:bec3f80dc49c 98 }
Inscape_ao 4:bec3f80dc49c 99
Inscape_ao 4:bec3f80dc49c 100 /* get FileName of open file */
Inscape_ao 4:bec3f80dc49c 101 char *getFileName(void)
Inscape_ao 4:bec3f80dc49c 102 {
Inscape_ao 4:bec3f80dc49c 103 return fnameCurrent;
Inscape_ao 4:bec3f80dc49c 104 }
Inscape_ao 4:bec3f80dc49c 105
Inscape_ao 4:bec3f80dc49c 106 /* get current file pointer */
Inscape_ao 4:bec3f80dc49c 107 FILE *getFilePointer(void) {
Inscape_ao 4:bec3f80dc49c 108 return fpCurrent;
Inscape_ao 4:bec3f80dc49c 109 }
Inscape_ao 4:bec3f80dc49c 110
Inscape_ao 4:bec3f80dc49c 111 /* close file with this Class */
Inscape_ao 4:bec3f80dc49c 112 void syncFile(void)
Inscape_ao 4:bec3f80dc49c 113 {
Inscape_ao 4:bec3f80dc49c 114 if (fpCurrent != NULL) {
Inscape_ao 4:bec3f80dc49c 115 fflush(fpCurrent);
Inscape_ao 4:bec3f80dc49c 116 }
Inscape_ao 4:bec3f80dc49c 117 }
Inscape_ao 4:bec3f80dc49c 118
Inscape_ao 4:bec3f80dc49c 119 /* close file with this Class */
Inscape_ao 4:bec3f80dc49c 120 void closeFile(void)
Inscape_ao 4:bec3f80dc49c 121 {
Inscape_ao 4:bec3f80dc49c 122 if (fpCurrent != NULL) {
Inscape_ao 4:bec3f80dc49c 123 fclose(fpCurrent);
Inscape_ao 4:bec3f80dc49c 124 fpCurrent = NULL;
Inscape_ao 4:bec3f80dc49c 125 fnameCurrent[0] = '\0';
Inscape_ao 4:bec3f80dc49c 126 }
Inscape_ao 4:bec3f80dc49c 127 }
Inscape_ao 4:bec3f80dc49c 128
Inscape_ao 4:bec3f80dc49c 129 private:
Inscape_ao 4:bec3f80dc49c 130 void pathTermination(char *pathArray)
Inscape_ao 4:bec3f80dc49c 131 {
Inscape_ao 4:bec3f80dc49c 132 pathArray[MaxPathLength-1] = '\0';
Inscape_ao 4:bec3f80dc49c 133 }
Inscape_ao 4:bec3f80dc49c 134 };
Inscape_ao 4:bec3f80dc49c 135
Inscape_ao 4:bec3f80dc49c 136
Inscape_ao 4:bec3f80dc49c 137
Inscape_ao 4:bec3f80dc49c 138 /** --- static --- */
Inscape_ao 4:bec3f80dc49c 139 static void sdcard_main()
Inscape_ao 4:bec3f80dc49c 140 {
Inscape_ao 4:bec3f80dc49c 141 FILE *fp;
Inscape_ao 4:bec3f80dc49c 142 SDDataStore *pSds;
Inscape_ao 4:bec3f80dc49c 143 pc.printf("Initializing...\r\n");
Inscape_ao 4:bec3f80dc49c 144 pSds = new SDDataStore(&sd, pTM);
Inscape_ao 4:bec3f80dc49c 145
Inscape_ao 4:bec3f80dc49c 146 if (pSds->startFileWithTimeStamp() != true) {
Inscape_ao 4:bec3f80dc49c 147 pc.printf("Unable to write the file\n");
Inscape_ao 4:bec3f80dc49c 148 return;
Inscape_ao 4:bec3f80dc49c 149 }
Inscape_ao 4:bec3f80dc49c 150 if ((fp = pSds->getFilePointer()) == NULL) {
Inscape_ao 4:bec3f80dc49c 151 pc.printf("Unable to write the file\r\n");
Inscape_ao 4:bec3f80dc49c 152 return;
Inscape_ao 4:bec3f80dc49c 153 }
Inscape_ao 4:bec3f80dc49c 154 fprintf(fp, "mbed SDCard application!(in %s)", pSds->getFileName());
Inscape_ao 4:bec3f80dc49c 155 pSds->syncFile();
Inscape_ao 4:bec3f80dc49c 156 pSds->closeFile();
Inscape_ao 4:bec3f80dc49c 157 pc.printf("File successfully written!\r\n");
Inscape_ao 4:bec3f80dc49c 158 }
Inscape_ao 1:71c9c97c9f3d 159
Inscape_ao 0:c347f602596d 160 /** --- main --- */
Inscape_ao 0:c347f602596d 161 int main()
Inscape_ao 0:c347f602596d 162 {
Inscape_ao 0:c347f602596d 163 /** UART Initalizer */
Inscape_ao 0:c347f602596d 164 /* setup UART 115200, 8bit, Parity=None, stopbit:1bit */
Inscape_ao 0:c347f602596d 165 /* https://os.mbed.com/users/okini3939/notebook/Serial_jp/ */
Inscape_ao 0:c347f602596d 166 pc.baud(115200);
Inscape_ao 0:c347f602596d 167 pc.format(8, Serial::None, 1);
Inscape_ao 0:c347f602596d 168
Inscape_ao 2:a694440145e9 169 /* new timer manager */
Inscape_ao 2:a694440145e9 170 pTM = new TimeManager();
Inscape_ao 0:c347f602596d 171 /* Receive Buffer Control */
Inscape_ao 0:c347f602596d 172 pUR = new UartReceiver(&pc);
Inscape_ao 0:c347f602596d 173 /* Generate Command parser as DeviceID = 0, ParsingRule = rules */
Inscape_ao 2:a694440145e9 174 pCP = new CommandParser(pUR, 0, rules, getNumOfRules);
Inscape_ao 0:c347f602596d 175 pCP->run();
Inscape_ao 0:c347f602596d 176
Inscape_ao 0:c347f602596d 177 pc.printf("Hello World !\n");
Inscape_ao 4:bec3f80dc49c 178 sdcard_main();
Inscape_ao 4:bec3f80dc49c 179
Inscape_ao 0:c347f602596d 180 while(1) {
Inscape_ao 0:c347f602596d 181 wait(1);
Inscape_ao 0:c347f602596d 182 }
Inscape_ao 0:c347f602596d 183 }