Skip to content

Change semaphore name from xFATSemaphore to sdCardSemaphore #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Firmware/RTK_Surveyor/Begin.ino
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ void beginSD()
}

//Setup FAT file access semaphore
if (xFATSemaphore == NULL)
if (sdCardSemaphore == NULL)
{
xFATSemaphore = xSemaphoreCreateMutex();
if (xFATSemaphore != NULL)
xSemaphoreGive(xFATSemaphore); //Make the file system available for use
sdCardSemaphore = xSemaphoreCreateMutex();
if (sdCardSemaphore != NULL)
xSemaphoreGive(sdCardSemaphore); //Make the file system available for use
}

if (createTestFile() == false)
Expand Down
10 changes: 7 additions & 3 deletions Firmware/RTK_Surveyor/Buttons.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ void powerDown(bool displayInfo)
{
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile()
//Wait up to 1000ms
if (xSemaphoreTake(xFATSemaphore, 1000 / portTICK_PERIOD_MS) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, 1000 / portTICK_PERIOD_MS) == pdPASS)
{
//Close down file system
ubxFile.sync();
ubxFile.close();
//xSemaphoreGive(xFATSemaphore); //Do not release semaphore
} //End xFATSemaphore
//xSemaphoreGive(sdCardSemaphore); //Do not release semaphore
} //End sdCardSemaphore
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}

online.logging = false;
}
Expand Down
24 changes: 16 additions & 8 deletions Firmware/RTK_Surveyor/NVM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void recordSystemSettingsToFileSD(char *fileName)
if (online.microSD == true)
{
//Attempt to write to file system. This avoids collisions with file writing from other functions like updateLogs()
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
{
if (sd.exists(fileName))
sd.remove(fileName);
Expand All @@ -84,7 +84,11 @@ void recordSystemSettingsToFileSD(char *fileName)

log_d("Settings recorded to SD: %s", fileName);

xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
}
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
}
}
Expand Down Expand Up @@ -226,15 +230,15 @@ bool loadSystemSettingsFromFileSD(char* fileName, Settings *settings)
if (online.microSD == true)
{
//Attempt to access file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
{
if (sd.exists(fileName))
{
SdFile settingsFile; //FAT32
if (settingsFile.open(fileName, O_READ) == false)
{
Serial.println(F("Failed to open settings file"));
xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
return (false);
}

Expand All @@ -255,7 +259,7 @@ bool loadSystemSettingsFromFileSD(char* fileName, Settings *settings)
{
//If we can't read the first line of the settings file, give up
Serial.println(F("Giving up on settings file"));
xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
return (false);
}
}
Expand All @@ -265,7 +269,7 @@ bool loadSystemSettingsFromFileSD(char* fileName, Settings *settings)
{
//If we can't read the first line of the settings file, give up
Serial.println(F("Giving up on settings file"));
xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
return (false);
}
}
Expand All @@ -275,17 +279,21 @@ bool loadSystemSettingsFromFileSD(char* fileName, Settings *settings)

//Serial.println(F("Config file read complete"));
settingsFile.close();
xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
return (true);
}
else
{
log_d("File %s not found", fileName);
xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
return (false);
}

} //End Semaphore check
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
} //End SD online

log_d("Config file read failed: SD offline");
Expand Down
46 changes: 31 additions & 15 deletions Firmware/RTK_Surveyor/RTK_Surveyor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ SdFile newFirmwareFile; //File that is available if user uploads new firmware vi

//System crashes if two tasks access a file at the same time
//So we use a semaphore to see if file system is available
SemaphoreHandle_t xFATSemaphore;
SemaphoreHandle_t sdCardSemaphore;
const TickType_t fatSemaphore_shortWait_ms = 10 / portTICK_PERIOD_MS;
const TickType_t fatSemaphore_longWait_ms = 200 / portTICK_PERIOD_MS;

Expand All @@ -157,7 +157,7 @@ uint32_t sdUsedSpaceMB = 0;
#include <WiFi.h> //Built-in.
#include <HTTPClient.h> //Built-in. Needed for ThingStream API for ZTP
#include <ArduinoJson.h> //http://librarymanager/All#Arduino_JSON_messagepack v6.19.4
#include <WiFiClientSecure.h> //Built-in.
#include <WiFiClientSecure.h> //Built-in.
#include <PubSubClient.h> //Built-in. Used for MQTT obtaining of keys

#include "base64.h" //Built-in. Needed for NTRIP Client credential encoding.
Expand Down Expand Up @@ -340,7 +340,7 @@ unsigned long lastRockerSwitchChange = 0; //If quick toggle is detected (less th
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#ifdef COMPILE_WIFI
#ifdef COMPILE_AP
#include "ESPAsyncWebServer.h" //Get from: https://github.com/me-no-dev/ESPAsyncWebServer
#include "ESPAsyncWebServer.h" //Get from: https://github.com/me-no-dev/ESPAsyncWebServer
#include "form.h"

AsyncWebServer server(80);
Expand Down Expand Up @@ -563,23 +563,31 @@ void updateLogs()
else if (online.logging == true && settings.enableLogging == false)
{
//Close down file
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
{
ubxFile.sync();
ubxFile.close();
online.logging = false;
xSemaphoreGive(xFATSemaphore); //Release semaphore
xSemaphoreGive(sdCardSemaphore); //Release semaphore
}
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
}
else if (online.logging == true && settings.enableLogging == true && (systemTime_minutes - startCurrentLogTime_minutes) >= settings.maxLogLength_minutes)
{
//Close down file. A new one will be created at the next calling of updateLogs().
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
{
ubxFile.sync();
ubxFile.close();
online.logging = false;
xSemaphoreGive(xFATSemaphore); //Release semaphore
xSemaphoreGive(sdCardSemaphore); //Release semaphore
}
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
}

Expand All @@ -588,7 +596,7 @@ void updateLogs()
//Force file sync every 5000ms
if (millis() - lastUBXLogSyncTime > 5000)
{
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
{
if (productVariant == RTK_SURVEYOR)
digitalWrite(pin_baseStatusLED, !digitalRead(pin_baseStatusLED)); //Blink LED to indicate logging activity
Expand All @@ -604,11 +612,11 @@ void updateLogs()
updateDataFileAccess(&ubxFile); // Update the file access time & date

lastUBXLogSyncTime = millis();
xSemaphoreGive(xFATSemaphore);
} //End xFATSemaphore
xSemaphoreGive(sdCardSemaphore);
} //End sdCardSemaphore
else
{
log_d("Semaphore failed to yield");
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
}

Expand All @@ -624,13 +632,17 @@ void updateLogs()
char nmeaMessage[82]; //Max NMEA sentence length is 82
createNMEASentence(CUSTOM_NMEA_TYPE_EVENT, nmeaMessage, eventData); //textID, buffer, text

if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
{
ubxFile.println(nmeaMessage);

xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
newEventToRecord = false;
}
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
}

//Report file sizes to show recording is working
Expand All @@ -639,11 +651,15 @@ void updateLogs()
long fileSize = 0;

//Attempt to access file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
{
fileSize = ubxFile.fileSize();

xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
}
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}

if (fileSize > 0)
Expand Down
14 changes: 9 additions & 5 deletions Firmware/RTK_Surveyor/System.ino
Original file line number Diff line number Diff line change
Expand Up @@ -556,25 +556,29 @@ bool createTestFile()
SdFile testFile;
char testFileName[40] = "testfile.txt";

if (xFATSemaphore == NULL)
if (sdCardSemaphore == NULL)
{
log_d("xFATSemaphore is Null");
log_d("sdCardSemaphore is Null");
return (false);
}

//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
{
if (testFile.open(testFileName, O_CREAT | O_APPEND | O_WRITE) == true)
{
testFile.close();

if (sd.exists(testFileName))
sd.remove(testFileName);
xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
return (true);
}
xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
}
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}

return (false);
Expand Down
8 changes: 4 additions & 4 deletions Firmware/RTK_Surveyor/Tasks.ino
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ void F9PSerialReadTask(void *e)
if ((systemTime_minutes - startLogTime_minutes) < settings.maxLogTime_minutes)
{
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile()
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
{
ubxFile.write(rBuffer, s);

xSemaphoreGive(xFATSemaphore);
} //End xFATSemaphore
xSemaphoreGive(sdCardSemaphore);
} //End sdCardSemaphore
else
{
log_d("Semaphore failed to yield");
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
} //End maxLogTime
} //End logging
Expand Down
9 changes: 6 additions & 3 deletions Firmware/RTK_Surveyor/menuFirmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void scanForFirmware()
{
//Attempt to access file system. This avoids collisions with file writing in F9PSerialReadTask()
//Wait up to 5s, this is important
if (xSemaphoreTake(xFATSemaphore, 5000 / portTICK_PERIOD_MS) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, 5000 / portTICK_PERIOD_MS) == pdPASS)
{
//Count available binaries
SdFile tempFile;
Expand Down Expand Up @@ -92,9 +92,12 @@ void scanForFirmware()
tempFile.close();
}

xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
}
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}

}
}

Expand Down
10 changes: 7 additions & 3 deletions Firmware/RTK_Surveyor/menuMain.ino
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,16 @@ void factoryReset()
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
if (settings.enableSD && online.microSD)
{
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
{
//Remove this specific settings file. Don't remove the other profiles.
sd.remove(settingsFileName);
xSemaphoreGive(xFATSemaphore);
} //End xFATSemaphore
xSemaphoreGive(sdCardSemaphore);
} //End sdCardSemaphore
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
}

if (online.gnss == true)
Expand Down
14 changes: 9 additions & 5 deletions Firmware/RTK_Surveyor/menuMessages.ino
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void beginLogging()
}

//Attempt to write to file system. This avoids collisions with file writing in F9PSerialReadTask()
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
{
// O_CREAT - create the file if it does not exist
// O_APPEND - seek to the end of the file prior to each write
Expand All @@ -354,7 +354,7 @@ void beginLogging()
{
Serial.printf("Failed to create GNSS UBX data file: %s\n\r", fileName);
online.logging = false;
xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
return;
}

Expand Down Expand Up @@ -405,7 +405,7 @@ void beginLogging()
Serial.println(F("Appending last available log"));
}

xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
}
else
{
Expand Down Expand Up @@ -448,7 +448,7 @@ bool findLastLog(char *lastLogName)
{
//Attempt to access file system. This avoids collisions with file writing in F9PSerialReadTask()
//Wait up to 5s, this is important
if (xSemaphoreTake(xFATSemaphore, 5000 / portTICK_PERIOD_MS) == pdPASS)
if (xSemaphoreTake(sdCardSemaphore, 5000 / portTICK_PERIOD_MS) == pdPASS)
{
//Count available binaries
SdFile tempFile;
Expand Down Expand Up @@ -478,7 +478,11 @@ bool findLastLog(char *lastLogName)
tempFile.close();
}

xSemaphoreGive(xFATSemaphore);
xSemaphoreGive(sdCardSemaphore);
}
else
{
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
}
}

Expand Down
Loading