From 652e7b5ff58c313f3bdd5a0506b282ea2d1c03fd Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 14 Feb 2023 17:28:46 +0000 Subject: [PATCH 1/3] First attempt at adding FileSdFatMMC --- Firmware/RTK_Surveyor/Begin.ino | 77 +++++--------------------- Firmware/RTK_Surveyor/FileSdFatMMC.h | 71 ++++++++++++++++++++++++ Firmware/RTK_Surveyor/Form.ino | 19 +------ Firmware/RTK_Surveyor/RTK_Surveyor.ino | 31 ++--------- 4 files changed, 92 insertions(+), 106 deletions(-) create mode 100644 Firmware/RTK_Surveyor/FileSdFatMMC.h diff --git a/Firmware/RTK_Surveyor/Begin.ino b/Firmware/RTK_Surveyor/Begin.ino index adcf2cfe1..d39b3c5fe 100644 --- a/Firmware/RTK_Surveyor/Begin.ino +++ b/Firmware/RTK_Surveyor/Begin.ino @@ -403,79 +403,28 @@ void beginSD() sdCardSize = 0; outOfSDSpace = true; - if (USE_SPI_MICROSD) + //Allocate the ubxFile + if (!ubxFile) { - //Allocate the ubxFile + ubxFile = new FileSdFatMMC(); if (!ubxFile) { - ubxFile = new SdFile(); - if (!ubxFile) - { - systemPrintln("Failed to allocate ubxFile!"); - break; - } - } - - //Allocate the managerTempFile - if (!managerTempFile) - { - managerTempFile = new SdFile(); - if (!managerTempFile) - { - systemPrintln("Failed to allocate managerTempFile!"); - break; - } - } - - //Allocate the marksFile - if (!marksFile) - { - marksFile = new SdFile(); - if (!marksFile) - { - systemPrintln("Failed to allocate marksFile!"); - break; - } + systemPrintln("Failed to allocate ubxFile!"); + break; } } -#ifdef COMPILE_SD_MMC - else + + //Allocate the managerTempFile + if (!managerTempFile) { - //Allocate the ubxFile - if (!ubxFile_SD_MMC) - { - ubxFile_SD_MMC = new File(); - if (!ubxFile_SD_MMC) - { - systemPrintln("Failed to allocate ubxFile!"); - break; - } - } - - //Allocate the managerTempFile - if (!managerTempFile_SD_MMC) - { - managerTempFile_SD_MMC = new File(); - if (!managerTempFile_SD_MMC) - { - systemPrintln("Failed to allocate managerTempFile!"); - break; - } - } - - //Allocate the marksFile - if (!marksFile_SD_MMC) + managerTempFile = new FileSdFatMMC(); + if (!managerTempFile) { - marksFile_SD_MMC = new File(); - if (!marksFile_SD_MMC) - { - systemPrintln("Failed to allocate marksFile!"); - break; - } + systemPrintln("Failed to allocate managerTempFile!"); + break; } } -#endif - + systemPrintln("microSD: Online"); online.microSD = true; break; diff --git a/Firmware/RTK_Surveyor/FileSdFatMMC.h b/Firmware/RTK_Surveyor/FileSdFatMMC.h new file mode 100644 index 000000000..ddd12f4cb --- /dev/null +++ b/Firmware/RTK_Surveyor/FileSdFatMMC.h @@ -0,0 +1,71 @@ +#ifdef COMPILE_SD_MMC + +#include "FS.h" +#include "SD_MMC.h" + +class FileSdFatMMC : public SdFile, public File + +#else + +class FileSdFatMMC : public SdFile + +#endif + +{ +public: + + FileSdFatMMC() + { + if (USE_SPI_MICROSD) + sdFile = new SdFile; +#ifdef COMPILE_SD_MMC + else + file = new File; +#endif + }; + + ~FileSdFatMMC() + { + if (USE_SPI_MICROSD) + if (sdFile) + delete sdFile; +#ifdef COMPILE_SD_MMC + else + if (file) + delete file; +#endif + }; + + size_t println(const char printMe[]) + { + if (USE_SPI_MICROSD) + return sdFile->println(printMe); +#ifdef COMPILE_SD_MMC + else + return file->println(printMe); +#endif + }; + + File open(const char *filepath, uint8_t mode) + { + if (USE_SPI_MICROSD) + return (File)sdFile->open(filepath, mode); +#ifdef COMPILE_SD_MMC + else + { + if (mode | O_APPEND) + return file->open(filepath, FILE_APPEND); + else if (mode | O_WRITE) + return file->open(filepath, FILE_WRITE); + else // if (mode | O_READ) + return file->open(filepath, FILE_READ); + } +#endif + }; + +protected: + SdFile * sdFile; +#ifdef COMPILE_SD_MMC + File * file; +#endif +}; diff --git a/Firmware/RTK_Surveyor/Form.ino b/Firmware/RTK_Surveyor/Form.ino index c6924de2f..230a04c35 100644 --- a/Firmware/RTK_Surveyor/Form.ino +++ b/Firmware/RTK_Surveyor/Form.ino @@ -229,23 +229,10 @@ static void handleFirmwareFileDownload(AsyncWebServerRequest *request) if (managerFileOpen == false) { - if (USE_SPI_MICROSD) - { - if (managerTempFile->open(fileName, O_READ) == true) - managerFileOpen = true; - else - systemPrintln("Error: File Manager failed to open file"); - } -#ifdef COMPILE_SD_MMC + if (managerTempFile->open(fileName, O_READ) == true) + managerFileOpen = true; else - { - *managerTempFile_SD_MMC = SD_MMC.open(fileName, FILE_READ); - if (managerTempFile_SD_MMC) - managerFileOpen = true; - else - systemPrintln("Error: File Manager failed to open file"); - } -#endif + systemPrintln("Error: File Manager failed to open file"); } else { diff --git a/Firmware/RTK_Surveyor/RTK_Surveyor.ino b/Firmware/RTK_Surveyor/RTK_Surveyor.ino index 9725d661e..35b247a63 100644 --- a/Firmware/RTK_Surveyor/RTK_Surveyor.ino +++ b/Firmware/RTK_Surveyor/RTK_Surveyor.ino @@ -125,9 +125,11 @@ ESP32Time rtc; #include "SdFat.h" //http://librarymanager/All#sdfat_exfat by Bill Greiman. Currently uses v2.1.1 SdFat *sd; +#include "FileSdFatMMC.h" // Hybrid SdFat and SD_MMC file access + char platformFilePrefix[40] = "SFE_Surveyor"; //Sets the prefix for logs and settings files -SdFile * ubxFile; //File that all GNSS ubx messages sentences are written to +FileSdFatMMC * ubxFile; //File that all GNSS ubx messages sentences are written to unsigned long lastUBXLogSyncTime = 0; //Used to record to SD every half second int startLogTime_minutes = 0; //Mark when we start any logging so we can stop logging after maxLogTime_minutes int startCurrentLogTime_minutes = 0; //Mark when we start this specific log file so we can close it after x minutes and start a new one @@ -154,32 +156,14 @@ typedef enum LoggingType { } LoggingType; LoggingType loggingType = LOGGING_UNKNOWN; -SdFile * managerTempFile; //File used for uploading or downloading in file manager section of AP config +FileSdFatMMC * managerTempFile; //File used for uploading or downloading in file manager section of AP config bool managerFileOpen = false; -SdFile * marksFile; // Marks file - used by States.ino -SdFile * firmwareFile; // Firmware file - used by menuFirmware - TaskHandle_t sdSizeCheckTaskHandle = NULL; //Store handles so that we can kill the task once size is found const uint8_t sdSizeCheckTaskPriority = 0; //3 being the highest, and 0 being the lowest const int sdSizeCheckStackSize = 2000; bool sdSizeCheckTaskComplete = false; -//microSD SDIO / MMC Interface - for the REFERENCE_STATION -//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- - -#ifdef COMPILE_SD_MMC - -#include "FS.h" -#include "SD_MMC.h" - -File * ubxFile_SD_MMC; -File * managerTempFile_SD_MMC; -File * marksFile_SD_MMC; -File * firmwareFile_SD_MMC; - -#endif - //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- //Connection settings to NTRIP Caster @@ -776,12 +760,7 @@ void updateLogs() { markSemaphore(FUNCTION_EVENT); - if (USE_SPI_MICROSD) - ubxFile->println(nmeaMessage); -#ifdef COMPILE_SD_MMC - else - ubxFile_SD_MMC->println(nmeaMessage); -#endif + ubxFile->println(nmeaMessage); xSemaphoreGive(sdCardSemaphore); newEventToRecord = false; From d3d8a2e19b4887e97c18932dd5acd4dd8c9978cf Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 14 Feb 2023 18:24:50 +0000 Subject: [PATCH 2/3] Fix FileSdFatMMC open - use operator bool correctly --- Firmware/RTK_Surveyor/FileSdFatMMC.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Firmware/RTK_Surveyor/FileSdFatMMC.h b/Firmware/RTK_Surveyor/FileSdFatMMC.h index ddd12f4cb..1a83140fc 100644 --- a/Firmware/RTK_Surveyor/FileSdFatMMC.h +++ b/Firmware/RTK_Surveyor/FileSdFatMMC.h @@ -27,11 +27,11 @@ class FileSdFatMMC : public SdFile ~FileSdFatMMC() { if (USE_SPI_MICROSD) - if (sdFile) + if (*sdFile) // operator bool delete sdFile; #ifdef COMPILE_SD_MMC else - if (file) + if (*file) // operator bool delete file; #endif }; @@ -46,19 +46,20 @@ class FileSdFatMMC : public SdFile #endif }; - File open(const char *filepath, uint8_t mode) + bool open(const char *filepath, uint8_t mode) { if (USE_SPI_MICROSD) - return (File)sdFile->open(filepath, mode); + return sdFile->open(filepath, mode); #ifdef COMPILE_SD_MMC else { if (mode | O_APPEND) - return file->open(filepath, FILE_APPEND); + *file = SD_MMC.open(filepath, FILE_APPEND); else if (mode | O_WRITE) - return file->open(filepath, FILE_WRITE); + *file = SD_MMC.open(filepath, FILE_WRITE); else // if (mode | O_READ) - return file->open(filepath, FILE_READ); + *file = SD_MMC.open(filepath, FILE_READ); + return (*file); // operator bool } #endif }; From d02ec3a9ffdf599b24d93b51179b094153b1b868 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 15 Feb 2023 11:27:13 +0000 Subject: [PATCH 3/3] Finish adding FileSdFatMMC --- Firmware/RTK_Surveyor/Begin.ino | 2 +- Firmware/RTK_Surveyor/FileSdFatMMC.h | 115 +++++++++++++++++++++++ Firmware/RTK_Surveyor/Form.ino | 72 ++++----------- Firmware/RTK_Surveyor/NVM.ino | 6 +- Firmware/RTK_Surveyor/SD.ino | 3 + Firmware/RTK_Surveyor/States.ino | 102 +++++++-------------- Firmware/RTK_Surveyor/System.ino | 33 +++---- Firmware/RTK_Surveyor/Tasks.ino | 22 +---- Firmware/RTK_Surveyor/menuBase.ino | 31 ++----- Firmware/RTK_Surveyor/menuFirmware.ino | 90 ++---------------- Firmware/RTK_Surveyor/menuMessages.ino | 121 +++++-------------------- 11 files changed, 227 insertions(+), 370 deletions(-) diff --git a/Firmware/RTK_Surveyor/Begin.ino b/Firmware/RTK_Surveyor/Begin.ino index d39b3c5fe..dc2c23c39 100644 --- a/Firmware/RTK_Surveyor/Begin.ino +++ b/Firmware/RTK_Surveyor/Begin.ino @@ -465,7 +465,7 @@ void endSD(bool alreadyHaveSemaphore, bool releaseSemaphore) xSemaphoreGive(sdCardSemaphore); } -//Attempt to de-init the SD card +//Attempt to de-init the SD card - SPI only //https://github.com/greiman/SdFat/issues/351 void resetSPI() { diff --git a/Firmware/RTK_Surveyor/FileSdFatMMC.h b/Firmware/RTK_Surveyor/FileSdFatMMC.h index 1a83140fc..0750ea704 100644 --- a/Firmware/RTK_Surveyor/FileSdFatMMC.h +++ b/Firmware/RTK_Surveyor/FileSdFatMMC.h @@ -1,3 +1,5 @@ +// Define a hybrid class which can support both SdFat SdFile and SD_MMC File + #ifdef COMPILE_SD_MMC #include "FS.h" @@ -36,6 +38,16 @@ class FileSdFatMMC : public SdFile #endif }; + operator bool() + { + if (USE_SPI_MICROSD) + return *sdFile; +#ifdef COMPILE_SD_MMC + else + return *file; +#endif + }; + size_t println(const char printMe[]) { if (USE_SPI_MICROSD) @@ -64,9 +76,112 @@ class FileSdFatMMC : public SdFile #endif }; + uint32_t size() + { + if (USE_SPI_MICROSD) + return sdFile->size(); +#ifdef COMPILE_SD_MMC + else + return file->size(); +#endif + }; + + uint32_t position() + { + if (USE_SPI_MICROSD) + return sdFile->position(); +#ifdef COMPILE_SD_MMC + else + return file->position(); +#endif + }; + + int available() + { + if (USE_SPI_MICROSD) + return sdFile->available(); +#ifdef COMPILE_SD_MMC + else + return file->available(); +#endif + }; + + int read(uint8_t *buf, uint16_t nbyte) + { + if (USE_SPI_MICROSD) + return sdFile->read(buf, nbyte); +#ifdef COMPILE_SD_MMC + else + return file->read(buf, nbyte); +#endif + }; + + size_t write(const uint8_t *buf, size_t size) + { + if (USE_SPI_MICROSD) + return sdFile->write(buf, size); +#ifdef COMPILE_SD_MMC + else + return file->write(buf, size); +#endif + }; + + void close() + { + if (USE_SPI_MICROSD) + sdFile->close(); +#ifdef COMPILE_SD_MMC + else + file->close(); +#endif + }; + + void updateFileAccessTimestamp() + { + if (USE_SPI_MICROSD) + if (online.rtc == true) + { + //ESP32Time returns month:0-11 + sdFile->timestamp(T_ACCESS, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); + sdFile->timestamp(T_WRITE, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); + } + }; + + void updateFileCreateTimestamp() + { + if (USE_SPI_MICROSD) + if (online.rtc == true) + sdFile->timestamp(T_CREATE, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); //ESP32Time returns month:0-11 + }; + + void sync() + { + if (USE_SPI_MICROSD) + sdFile->sync(); + }; + protected: SdFile * sdFile; #ifdef COMPILE_SD_MMC File * file; #endif }; + +//Update the file access and write time with date and time obtained from GNSS +// These are SdFile-specific. SD_MMC does this automatically +void updateDataFileAccess(SdFile *dataFile) +{ + if (online.rtc == true) + { + //ESP32Time returns month:0-11 + dataFile->timestamp(T_ACCESS, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); + dataFile->timestamp(T_WRITE, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); + } +} + +//Update the file create time with date and time obtained from GNSS +void updateDataFileCreate(SdFile *dataFile) +{ + if (online.rtc == true) + dataFile->timestamp(T_CREATE, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); //ESP32Time returns month:0-11 +} diff --git a/Firmware/RTK_Surveyor/Form.ino b/Firmware/RTK_Surveyor/Form.ino index 230a04c35..403b4052c 100644 --- a/Firmware/RTK_Surveyor/Form.ino +++ b/Firmware/RTK_Surveyor/Form.ino @@ -241,48 +241,24 @@ static void handleFirmwareFileDownload(AsyncWebServerRequest *request) } int dataAvailable; - if (USE_SPI_MICROSD) - dataAvailable = managerTempFile->size() - managerTempFile->position(); -#ifdef COMPILE_SD_MMC - else - dataAvailable = managerTempFile_SD_MMC->size() - managerTempFile_SD_MMC->position(); -#endif + dataAvailable = managerTempFile->size() - managerTempFile->position(); AsyncWebServerResponse *response = request->beginResponse("text/plain", dataAvailable, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { uint32_t bytes = 0; uint32_t availableBytes; - if (USE_SPI_MICROSD) - availableBytes = managerTempFile->available(); -#ifdef COMPILE_SD_MMC - else - availableBytes = managerTempFile_SD_MMC->available(); -#endif + availableBytes = managerTempFile->available(); if (availableBytes > maxLen) { - if (USE_SPI_MICROSD) bytes = managerTempFile->read(buffer, maxLen); -#ifdef COMPILE_SD_MMC - else - bytes = managerTempFile_SD_MMC->read(buffer, maxLen); -#endif } else - { - if (USE_SPI_MICROSD) { bytes = managerTempFile->read(buffer, availableBytes); managerTempFile->close(); - } -#ifdef COMPILE_SD_MMC - else - { - bytes = managerTempFile_SD_MMC->read(buffer, availableBytes); - managerTempFile_SD_MMC->close(); - } -#endif + managerFileOpen = false; websocket->textAll("fmNext,1,"); //Tell browser to send next file if needed @@ -1252,24 +1228,25 @@ String getFileList() if (USE_SPI_MICROSD) { - SdFile dir; - dir.open("/"); //Open root + SdFile root; + root.open("/"); //Open root + SdFile file; uint16_t fileCount = 0; - while (managerTempFile->openNext(&dir, O_READ)) + while (file.openNext(&root, O_READ)) { - if (managerTempFile->isFile()) + if (file.isFile()) { fileCount++; - managerTempFile->getName(fileName, sizeof(fileName)); + file.getName(fileName, sizeof(fileName)); - returnText += "fmName," + String(fileName) + ",fmSize," + stringHumanReadableSize(managerTempFile->fileSize()) + ","; + returnText += "fmName," + String(fileName) + ",fmSize," + stringHumanReadableSize(file.fileSize()) + ","; } } - dir.close(); - managerTempFile->close(); + root.close(); + file.close(); } #ifdef COMPILE_SD_MMC else @@ -1295,7 +1272,6 @@ String getFileList() } root.close(); - managerTempFile_SD_MMC->close(); } #endif @@ -1362,12 +1338,7 @@ void handleUpload(AsyncWebServerRequest * request, String filename, size_t index { markSemaphore(FUNCTION_FILEMANAGER_UPLOAD1); - if (USE_SPI_MICROSD) - managerTempFile->open(tempFileName, O_CREAT | O_APPEND | O_WRITE); -#ifdef COMPILE_SD_MMC - else - *managerTempFile_SD_MMC = SD_MMC.open(tempFileName, FILE_APPEND); -#endif + managerTempFile->open(tempFileName, O_CREAT | O_APPEND | O_WRITE); xSemaphoreGive(sdCardSemaphore); } @@ -1382,12 +1353,7 @@ void handleUpload(AsyncWebServerRequest * request, String filename, size_t index { markSemaphore(FUNCTION_FILEMANAGER_UPLOAD2); - if (USE_SPI_MICROSD) - managerTempFile->write(data, len); // stream the incoming chunk to the opened file -#ifdef COMPILE_SD_MMC - else - managerTempFile_SD_MMC->write(data, len); -#endif + managerTempFile->write(data, len); // stream the incoming chunk to the opened file xSemaphoreGive(sdCardSemaphore); } @@ -1401,15 +1367,9 @@ void handleUpload(AsyncWebServerRequest * request, String filename, size_t index { markSemaphore(FUNCTION_FILEMANAGER_UPLOAD3); - if (USE_SPI_MICROSD) - updateDataFileCreate(managerTempFile); // Update the file create time & date + managerTempFile->updateFileCreateTimestamp(); // Update the file create time & date - if (USE_SPI_MICROSD) - managerTempFile->close(); -#ifdef COMPILE_SD_MMC - else - managerTempFile_SD_MMC->close(); -#endif + managerTempFile->close(); xSemaphoreGive(sdCardSemaphore); } diff --git a/Firmware/RTK_Surveyor/NVM.ino b/Firmware/RTK_Surveyor/NVM.ino index 0c3211efd..f388cdd08 100644 --- a/Firmware/RTK_Surveyor/NVM.ino +++ b/Firmware/RTK_Surveyor/NVM.ino @@ -122,7 +122,7 @@ void recordSystemSettingsToFileSD(char *fileName) SD_MMC.remove(fileName); } - File settingsFile = SD_MMC.open(fileName, FILE_APPEND); + File settingsFile = SD_MMC.open(fileName, FILE_WRITE); if (!settingsFile) { @@ -130,12 +130,8 @@ void recordSystemSettingsToFileSD(char *fileName) break; } - //updateDataFileCreate(&settingsFile); // Update the file to create time & date - recordSystemSettingsToFile(&settingsFile); //Record all the settings via strings to file - //updateDataFileAccess(&settingsFile); // Update the file access time & date - settingsFile.close(); } #endif diff --git a/Firmware/RTK_Surveyor/SD.ino b/Firmware/RTK_Surveyor/SD.ino index 9e303c6cd..fa4bc69b1 100644 --- a/Firmware/RTK_Surveyor/SD.ino +++ b/Firmware/RTK_Surveyor/SD.ino @@ -36,6 +36,9 @@ //within a reasonable amount of time, there is no SD card on the bus. //Returns false if not card is detected //Returns true if a card responds +// +//This is SPI-specific. +//TODO: figure out how to incorporate the SD_MMC SD card detect pin into this. Maybe best done in beginSD? bool sdPresent(void) { if (USE_SPI_MICROSD) diff --git a/Firmware/RTK_Surveyor/States.ino b/Firmware/RTK_Surveyor/States.ino index c5dc7a84c..6512bb3b5 100644 --- a/Firmware/RTK_Surveyor/States.ino +++ b/Firmware/RTK_Surveyor/States.ino @@ -432,12 +432,7 @@ void updateSystemState() { char nmeaMessage[82]; //Max NMEA sentence length is 82 createNMEASentence(CUSTOM_NMEA_TYPE_WAYPOINT, nmeaMessage, (char*)"CustomEvent"); //textID, buffer, text - if (USE_SPI_MICROSD) - ubxFile->println(nmeaMessage); -#ifdef COMPILE_SD_MMC - else - ubxFile_SD_MMC->println(nmeaMessage); -#endif + ubxFile->println(nmeaMessage); logged = true; } @@ -467,47 +462,46 @@ void updateSystemState() if (online.microSD == true) { - //Open the marks file + //Check if the marks file already exists + bool marksFileExists = false; if (USE_SPI_MICROSD) { - if (marksFile && marksFile->open(fileName, O_APPEND | O_WRITE)) + marksFileExists = sd->exists(fileName); + } +#ifdef COMPILE_SD_MMC + else + { + marksFileExists = SD_MMC.exists(fileName); + } +#endif + + //Open the marks file + FileSdFatMMC marksFile; + + if (marksFileExists) + { + if (marksFile && marksFile.open(fileName, O_APPEND | O_WRITE)) { fileOpen = true; - marksFile->timestamp(T_CREATE, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), - rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); + marksFile.updateFileCreateTimestamp(); } - else if (marksFile && marksFile->open(fileName, O_CREAT | O_WRITE)) + } + else + { + if (marksFile && marksFile.open(fileName, O_CREAT | O_WRITE)) { fileOpen = true; - marksFile->timestamp(T_ACCESS, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), - rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); - marksFile->timestamp(T_WRITE, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), - rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); + marksFile.updateFileAccessTimestamp(); //Add the column headers //YYYYMMDDHHMMSS, Lat: xxxx, Long: xxxx, Alt: xxxx, SIV: xx, HPA: xxxx, Batt: xxx // 1 2 3 4 5 6 7 8 9 // 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901 strcpy(markBuffer, "Date, Time, Latitude, Longitude, Altitude Meters, SIV, HPA Meters, Battery Level, Voltage\n"); - marksFile->write(markBuffer, strlen(markBuffer)); - } - } -#ifdef COMPILE_SD_MMC - else - { - bool writeHeader = !SD_MMC.exists(fileName); // Check if the file already exists - *marksFile_SD_MMC = SD_MMC.open(fileName, FILE_APPEND); - if (marksFile_SD_MMC) - { - fileOpen = true; - if (writeHeader) - { - strcpy(markBuffer, "Date, Time, Latitude, Longitude, Altitude Meters, SIV, HPA Meters, Battery Level, Voltage\n"); - marksFile_SD_MMC->write((const uint8_t *)markBuffer, strlen(markBuffer)); - } + marksFile.write((const uint8_t *)markBuffer, strlen(markBuffer)); } } -#endif + if (fileOpen) { //Create the mark text @@ -535,43 +529,17 @@ void updateSystemState() latitude, longitude, altitude, numSV, horizontalAccuracy, battLevel, battVoltage); - if (USE_SPI_MICROSD) - { - //Write the mark to the file - marksFile->write(markBuffer, strlen(markBuffer)); - - // Update the file to create time & date - updateDataFileCreate(marksFile); - - //Close the mark file - marksFile->close(); - } -#ifdef COMPILE_SD_MMC - else - { - //Write the mark to the file - marksFile_SD_MMC->write((const uint8_t *)markBuffer, strlen(markBuffer)); - - //Close the mark file - marksFile_SD_MMC->close(); - } -#endif - marked = true; - } + //Write the mark to the file + marksFile.write((const uint8_t *)markBuffer, strlen(markBuffer)); - //Done with the file - if (USE_SPI_MICROSD) - { - if (marksFile) - delete (marksFile); - } -#ifdef COMPILE_SD_MMC - else - { - if (marksFile_SD_MMC) - delete (marksFile_SD_MMC); + // Update the file to create time & date + marksFile.updateFileCreateTimestamp(); + + //Close the mark file + marksFile.close(); + + marked = true; } -#endif //Dismount the SD card if (!sdCardWasOnline) diff --git a/Firmware/RTK_Surveyor/System.ino b/Firmware/RTK_Surveyor/System.ino index 65bcb45d6..28a114ea9 100644 --- a/Firmware/RTK_Surveyor/System.ino +++ b/Firmware/RTK_Surveyor/System.ino @@ -293,17 +293,18 @@ bool isConnected(uint8_t deviceAddress) //Create a test file in file structure to make sure we can bool createTestFile() { + FileSdFatMMC testFile; + char testFileName[40] = "testfile.txt"; + + //Attempt to write to the file system + if (testFile.open(testFileName, O_CREAT | O_APPEND | O_WRITE) != true) + return (false); + + //File successfully created + testFile.close(); + if (USE_SPI_MICROSD) { - SdFile testFile; - char testFileName[40] = "testfile.txt"; - - //Attempt to write to the file system - if (testFile.open(testFileName, O_CREAT | O_APPEND | O_WRITE) != true) - return (false); - - //File successfully created - testFile.close(); if (sd->exists(testFileName)) sd->remove(testFileName); return (!sd->exists(testFileName)); @@ -311,17 +312,9 @@ bool createTestFile() #ifdef COMPILE_SD_MMC else { - // SDIO MMC - char testFileName[40] = "/testfile.txt"; // Create it in the root directory - - File testFile = SD_MMC.open(testFileName, FILE_WRITE); - - if (!testFile) - return (false); - - //File successfully created - testFile.close(); - return (SD_MMC.remove(testFileName)); + if (SD_MMC.exists(testFileName)) + SD_MMC.remove(testFileName); + return (!SD_MMC.exists(testFileName)); } #endif } diff --git a/Firmware/RTK_Surveyor/Tasks.ino b/Firmware/RTK_Surveyor/Tasks.ino index 4d2da3e7c..ce1a9fe54 100644 --- a/Firmware/RTK_Surveyor/Tasks.ino +++ b/Firmware/RTK_Surveyor/Tasks.ino @@ -356,18 +356,8 @@ void handleGNSSDataTask(void *e) //Write the data to the file long startTime = millis(); - if (USE_SPI_MICROSD) - { - sdBytesToRecord = ubxFile->write(&ringBuffer[sdTail], sliceToRecord); - fileSize = ubxFile->fileSize(); //Update file size - } -#ifdef COMPILE_SD_MMC - else - { - sdBytesToRecord = ubxFile_SD_MMC->write(&ringBuffer[sdTail], sliceToRecord); - fileSize = ubxFile_SD_MMC->size(); //Update file size - } -#endif + sdBytesToRecord = ubxFile->write(&ringBuffer[sdTail], sliceToRecord); + fileSize = ubxFile->fileSize(); //Update file size sdFreeSpace -= sliceToRecord; //Update remaining space on SD @@ -377,11 +367,9 @@ void handleGNSSDataTask(void *e) if (productVariant == RTK_SURVEYOR) digitalWrite(pin_baseStatusLED, !digitalRead(pin_baseStatusLED)); //Blink LED to indicate logging activity - if (USE_SPI_MICROSD) - { - ubxFile->sync(); - updateDataFileAccess(ubxFile); // Update the file access time & date - } + ubxFile->sync(); + ubxFile->updateFileAccessTimestamp(); // Update the file access time & date + if (productVariant == RTK_SURVEYOR) digitalWrite(pin_baseStatusLED, !digitalRead(pin_baseStatusLED)); //Return LED to previous state diff --git a/Firmware/RTK_Surveyor/menuBase.ino b/Firmware/RTK_Surveyor/menuBase.ino index 3aa723902..52b379f69 100644 --- a/Firmware/RTK_Surveyor/menuBase.ino +++ b/Firmware/RTK_Surveyor/menuBase.ino @@ -607,32 +607,15 @@ void recordLineToSD(const char* fileName, const char* lineData) gotSemaphore = true; - if (USE_SPI_MICROSD) - { - SdFile file; //FAT32 - if (file.open(fileName, O_CREAT | O_APPEND | O_WRITE) == false) - { - log_d("File %s not found", fileName); - break; - } - - file.println(lineData); - file.close(); - } -#ifdef COMPILE_SD_MMC - else + FileSdFatMMC file; + if (file.open(fileName, O_CREAT | O_APPEND | O_WRITE) == false) { - File file = SD_MMC.open(fileName, FILE_APPEND); - if (!file) - { - log_d("File %s not found", fileName); - break; - } - - file.println(lineData); - file.close(); + log_d("File %s not found", fileName); + break; } -#endif + + file.println(lineData); + file.close(); break; } //End Semaphore check else diff --git a/Firmware/RTK_Surveyor/menuFirmware.ino b/Firmware/RTK_Surveyor/menuFirmware.ino index 49f5aaa18..5641c058a 100644 --- a/Firmware/RTK_Surveyor/menuFirmware.ino +++ b/Firmware/RTK_Surveyor/menuFirmware.ino @@ -339,62 +339,22 @@ void updateFromSD(const char *firmwareFileName) } #endif - if (USE_SPI_MICROSD) - { - //Allocate the firmwareFile - if (!firmwareFile) - { - firmwareFile = new SdFile(); - if (!firmwareFile) - { - systemPrintln("Failed to allocate firmwareFile!"); - return; - } - } - } -#ifdef COMPILE_SD_MMC - else - { - //Allocate the firmwareFile - if (!firmwareFile_SD_MMC) - { - firmwareFile_SD_MMC = new File(); - if (!firmwareFile_SD_MMC) - { - systemPrintln("Failed to allocate firmwareFile!"); - return; - } - } - } -#endif + FileSdFatMMC firmwareFile; + firmwareFile.open(firmwareFileName, O_READ); - if (USE_SPI_MICROSD) - firmwareFile->open(firmwareFileName, O_READ); -#ifdef COMPILE_SD_MMC - else - *firmwareFile_SD_MMC = SD_MMC.open(firmwareFileName, FILE_READ); -#endif - - size_t updateSize; - - if (USE_SPI_MICROSD) - updateSize = firmwareFile->fileSize(); -#ifdef COMPILE_SD_MMC - else - updateSize = firmwareFile_SD_MMC->size(); -#endif + size_t updateSize = firmwareFile.size(); if (updateSize == 0) { systemPrintln("Error: Binary is empty"); - firmwareFileClose(); + firmwareFile.close(); return; } if (Update.begin(updateSize) == false) { systemPrintln("Update begin failed. Not enough partition space available."); - firmwareFileClose(); + firmwareFile.close(); return; } @@ -412,15 +372,15 @@ void updateFromSD(const char *firmwareFileName) int barWidth = 0; //Bulk write from the SD file to flash - while (firmwareFileAvailable()) + while (firmwareFile.available()) { if (productVariant == RTK_SURVEYOR) digitalWrite(pin_baseStatusLED, !digitalRead(pin_baseStatusLED)); //Toggle LED to indcate activity int bytesToWrite = pageSize; //Max number of bytes to read - if (firmwareFileAvailable() < bytesToWrite) bytesToWrite = firmwareFileAvailable(); //Trim this read size as needed + if (firmwareFile.available() < bytesToWrite) bytesToWrite = firmwareFile.available(); //Trim this read size as needed - firmwareFileRead(dataArray, bytesToWrite); //Read the next set of bytes from file into our temp array + firmwareFile.read(dataArray, bytesToWrite); //Read the next set of bytes from file into our temp array if (Update.write(dataArray, bytesToWrite) != bytesToWrite) { @@ -463,7 +423,7 @@ void updateFromSD(const char *firmwareFileName) systemPrintln("Removing firmware file"); //Remove forced firmware file to prevent endless loading - firmwareFileClose(); + firmwareFile.close(); if (USE_SPI_MICROSD) sd->remove(firmwareFileName); @@ -487,43 +447,13 @@ void updateFromSD(const char *firmwareFileName) systemPrintln(String(Update.getError())); } - firmwareFileClose(); + firmwareFile.close(); displayMessage("Update Failed", 0); systemPrintln("Firmware update failed. Please try again."); } -void firmwareFileClose() -{ - if (USE_SPI_MICROSD) - firmwareFile->close(); -#ifdef COMPILE_SD_MMC - else - firmwareFile_SD_MMC->close(); -#endif -} - -int firmwareFileAvailable() -{ - if (USE_SPI_MICROSD) - return firmwareFile->available(); -#ifdef COMPILE_SD_MMC - else - return firmwareFile_SD_MMC->available(); -#endif -} - -size_t firmwareFileRead(uint8_t* buf, size_t size) -{ - if (USE_SPI_MICROSD) - return firmwareFile->read(buf, size); -#ifdef COMPILE_SD_MMC - else - return firmwareFile_SD_MMC->read(buf, size); -#endif -} - //Returns true if we successfully got the versionAvailable //Modifies versionAvailable with OTA getVersion response //Connects to WiFi as needed diff --git a/Firmware/RTK_Surveyor/menuMessages.ino b/Firmware/RTK_Surveyor/menuMessages.ino index 0780d210e..acbf727f5 100644 --- a/Firmware/RTK_Surveyor/menuMessages.ino +++ b/Firmware/RTK_Surveyor/menuMessages.ino @@ -361,40 +361,23 @@ void beginLogging(const char *customFileName) { markSemaphore(FUNCTION_CREATEFILE); - if (USE_SPI_MICROSD) + // O_CREAT - create the file if it does not exist + // O_APPEND - seek to the end of the file prior to each write + // O_WRITE - open for write + if (ubxFile->open(fileName, O_CREAT | O_APPEND | O_WRITE) == false) { - // O_CREAT - create the file if it does not exist - // O_APPEND - seek to the end of the file prior to each write - // O_WRITE - open for write - if (ubxFile->open(fileName, O_CREAT | O_APPEND | O_WRITE) == false) - { - systemPrintf("Failed to create GNSS UBX data file: %s\r\n", fileName); - online.logging = false; - xSemaphoreGive(sdCardSemaphore); - return; - } - } -#ifdef COMPILE_SD_MMC - else - { - *ubxFile_SD_MMC = SD_MMC.open(fileName, FILE_APPEND); - if (!ubxFile_SD_MMC) - { - systemPrintf("Failed to create GNSS UBX data file: %s\r\n", fileName); - online.logging = false; - xSemaphoreGive(sdCardSemaphore); - return; - } + systemPrintf("Failed to create GNSS UBX data file: %s\r\n", fileName); + online.logging = false; + xSemaphoreGive(sdCardSemaphore); + return; } -#endif fileSize = 0; lastLogSize = 0; //Reset counter - used for displaying active logging icon bufferOverruns = 0; //Reset counter - if (USE_SPI_MICROSD) - updateDataFileCreate(ubxFile); // Update the file to create time & date + ubxFile->updateFileCreateTimestamp(); // Update the file to create time & date startCurrentLogTime_minutes = millis() / 1000L / 60; //Mark now as start of logging @@ -421,12 +404,7 @@ void beginLogging(const char *customFileName) //Mark top of log with system information char nmeaMessage[82]; //Max NMEA sentence length is 82 createNMEASentence(CUSTOM_NMEA_TYPE_RESET_REASON, nmeaMessage, rstReason); //textID, buffer, text - if (USE_SPI_MICROSD) - ubxFile->println(nmeaMessage); -#ifdef COMPILE_SD_MMC - else - ubxFile_SD_MMC->println(nmeaMessage); -#endif + ubxFile->println(nmeaMessage); //Record system firmware versions and info to log @@ -434,33 +412,17 @@ void beginLogging(const char *customFileName) char firmwareVersion[30]; //v1.3 December 31 2021 sprintf(firmwareVersion, "v%d.%d-%s", FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR, __DATE__); createNMEASentence(CUSTOM_NMEA_TYPE_SYSTEM_VERSION, nmeaMessage, firmwareVersion); //textID, buffer, text - if (USE_SPI_MICROSD) - ubxFile->println(nmeaMessage); -#ifdef COMPILE_SD_MMC - else - ubxFile_SD_MMC->println(nmeaMessage); -#endif - + ubxFile->println(nmeaMessage); //ZED-F9P firmware: HPG 1.30 createNMEASentence(CUSTOM_NMEA_TYPE_ZED_VERSION, nmeaMessage, zedFirmwareVersion); //textID, buffer, text - if (USE_SPI_MICROSD) - ubxFile->println(nmeaMessage); -#ifdef COMPILE_SD_MMC - else - ubxFile_SD_MMC->println(nmeaMessage); -#endif + ubxFile->println(nmeaMessage); //Device BT MAC. See issue: https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/346 char macAddress[5]; sprintf(macAddress, "%02X%02X", btMACAddress[4], btMACAddress[5]); createNMEASentence(CUSTOM_NMEA_TYPE_DEVICE_BT_ID, nmeaMessage, macAddress); //textID, buffer, text - if (USE_SPI_MICROSD) - ubxFile->println(nmeaMessage); -#ifdef COMPILE_SD_MMC - else - ubxFile_SD_MMC->println(nmeaMessage); -#endif + ubxFile->println(nmeaMessage); if (reuseLastLog == true) { @@ -505,15 +467,8 @@ void endLogging(bool gotSemaphore, bool releaseSemaphore) char nmeaMessage[82]; //Max NMEA sentence length is 82 createNMEASentence(CUSTOM_NMEA_TYPE_PARSER_STATS, nmeaMessage, parserStats); //textID, buffer, text - if (USE_SPI_MICROSD) - { - ubxFile->println(nmeaMessage); - ubxFile->sync(); - } -#ifdef COMPILE_SD_MMC - else - ubxFile_SD_MMC->println(nmeaMessage); -#endif + ubxFile->println(nmeaMessage); + ubxFile->sync(); //Reset stats in case a new log is created failedParserMessages_NMEA = 0; @@ -521,22 +476,11 @@ void endLogging(bool gotSemaphore, bool releaseSemaphore) failedParserMessages_UBX = 0; //Close down file system - if (USE_SPI_MICROSD) - { - ubxFile->close(); - //Done with the log file - delete ubxFile; - ubxFile = NULL; - } -#ifdef COMPILE_SD_MMC - else - { - ubxFile_SD_MMC->close(); - //Done with the log file - delete ubxFile_SD_MMC; - ubxFile_SD_MMC = NULL; - } -#endif + ubxFile->close(); + //Done with the log file + delete ubxFile; + ubxFile = NULL; + systemPrintln("Log file closed"); //Release the semaphore if requested @@ -555,24 +499,6 @@ void endLogging(bool gotSemaphore, bool releaseSemaphore) } } -//Update the file access and write time with date and time obtained from GNSS -void updateDataFileAccess(SdFile *dataFile) -{ - if (online.rtc == true) - { - //ESP32Time returns month:0-11 - dataFile->timestamp(T_ACCESS, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); - dataFile->timestamp(T_WRITE, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); - } -} - -//Update the file create time with date and time obtained from GNSS -void updateDataFileCreate(SdFile *dataFile) -{ - if (online.rtc == true) - dataFile->timestamp(T_CREATE, rtc.getYear(), rtc.getMonth() + 1, rtc.getDay(), rtc.getHour(true), rtc.getMinute(), rtc.getSecond()); //ESP32Time returns month:0-11 -} - //Finds last log //Returns true if succesful bool findLastLog(char *lastLogName) @@ -921,12 +847,7 @@ void updateLogTest() { markSemaphore(FUNCTION_LOGTEST); - if (USE_SPI_MICROSD) - ubxFile->println(nmeaMessage); -#ifdef COMPILE_SD_MMC - else - ubxFile_SD_MMC->println(nmeaMessage); -#endif + ubxFile->println(nmeaMessage); xSemaphoreGive(sdCardSemaphore); }