Skip to content

Commit 50a7fc7

Browse files
committed
Adding semaphore blocks for anything that does FS access. Increase semaphore wait to 200ms.
1 parent cead023 commit 50a7fc7

File tree

9 files changed

+100
-73
lines changed

9 files changed

+100
-73
lines changed

Firmware/RTK_Surveyor/Begin.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ void beginSD()
156156
return;
157157
}
158158

159+
//Setup FAT file access semaphore
160+
if (xFATSemaphore == NULL)
161+
{
162+
xFATSemaphore = xSemaphoreCreateMutex();
163+
if (xFATSemaphore != NULL)
164+
xSemaphoreGive(xFATSemaphore); //Make the file system available for use
165+
}
166+
159167
if (createTestFile() == false)
160168
{
161169
Serial.println(F("Failed to create test file. Format SD card with 'SD Card Formatter'."));
@@ -165,14 +173,6 @@ void beginSD()
165173
}
166174

167175
online.microSD = true;
168-
169-
//Setup FAT file access semaphore
170-
if (xFATSemaphore == NULL)
171-
{
172-
xFATSemaphore = xSemaphoreCreateMutex();
173-
if (xFATSemaphore != NULL)
174-
xSemaphoreGive(xFATSemaphore); //Make the file system available for use
175-
}
176176
}
177177
else
178178
{

Firmware/RTK_Surveyor/NVM.ino

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void recordSystemSettingsToFile()
6767
if (online.microSD == true)
6868
{
6969
//Attempt to write to file system. This avoids collisions with file writing from other functions like updateLogs()
70-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait) == pdPASS)
70+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
7171
{
7272
//Assemble settings file name
7373
char settingsFileName[40]; //SFE_Surveyor_Settings.txt
@@ -234,64 +234,74 @@ bool loadSystemSettingsFromFile()
234234
{
235235
if (online.microSD == true)
236236
{
237-
//Assemble settings file name
238-
char settingsFileName[40]; //SFE_Surveyor_Settings.txt
239-
strcpy(settingsFileName, platformFilePrefix);
240-
strcat(settingsFileName, "_Settings.txt");
241-
242-
if (sd.exists(settingsFileName))
237+
//Attempt to access file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
238+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
243239
{
244-
SdFile settingsFile; //FAT32
245-
if (settingsFile.open(settingsFileName, O_READ) == false)
240+
//Assemble settings file name
241+
char settingsFileName[40]; //SFE_Surveyor_Settings.txt
242+
strcpy(settingsFileName, platformFilePrefix);
243+
strcat(settingsFileName, "_Settings.txt");
244+
245+
if (sd.exists(settingsFileName))
246246
{
247-
Serial.println(F("Failed to open settings file"));
248-
return (false);
249-
}
247+
SdFile settingsFile; //FAT32
248+
if (settingsFile.open(settingsFileName, O_READ) == false)
249+
{
250+
Serial.println(F("Failed to open settings file"));
251+
xSemaphoreGive(xFATSemaphore);
252+
return (false);
253+
}
250254

251-
char line[60];
252-
int lineNumber = 0;
255+
char line[60];
256+
int lineNumber = 0;
253257

254-
while (settingsFile.available()) {
258+
while (settingsFile.available()) {
255259

256-
//Get the next line from the file
257-
//int n = getLine(&settingsFile, line, sizeof(line)); //Use with SD library
258-
int n = settingsFile.fgets(line, sizeof(line)); //Use with SdFat library
259-
if (n <= 0) {
260-
Serial.printf("Failed to read line %d from settings file\r\n", lineNumber);
261-
}
262-
else if (line[n - 1] != '\n' && n == (sizeof(line) - 1)) {
263-
Serial.printf("Settings line %d too long\r\n", lineNumber);
264-
if (lineNumber == 0)
265-
{
266-
//If we can't read the first line of the settings file, give up
267-
Serial.println(F("Giving up on settings file"));
268-
return (false);
260+
//Get the next line from the file
261+
//int n = getLine(&settingsFile, line, sizeof(line)); //Use with SD library
262+
int n = settingsFile.fgets(line, sizeof(line)); //Use with SdFat library
263+
if (n <= 0) {
264+
Serial.printf("Failed to read line %d from settings file\r\n", lineNumber);
269265
}
270-
}
271-
else if (parseLine(line) == false) {
272-
Serial.printf("Failed to parse line %d: %s\r\n", lineNumber, line);
273-
if (lineNumber == 0)
274-
{
275-
//If we can't read the first line of the settings file, give up
276-
Serial.println(F("Giving up on settings file"));
277-
return (false);
266+
else if (line[n - 1] != '\n' && n == (sizeof(line) - 1)) {
267+
Serial.printf("Settings line %d too long\r\n", lineNumber);
268+
if (lineNumber == 0)
269+
{
270+
//If we can't read the first line of the settings file, give up
271+
Serial.println(F("Giving up on settings file"));
272+
xSemaphoreGive(xFATSemaphore);
273+
return (false);
274+
}
275+
}
276+
else if (parseLine(line) == false) {
277+
Serial.printf("Failed to parse line %d: %s\r\n", lineNumber, line);
278+
if (lineNumber == 0)
279+
{
280+
//If we can't read the first line of the settings file, give up
281+
Serial.println(F("Giving up on settings file"));
282+
xSemaphoreGive(xFATSemaphore);
283+
return (false);
284+
}
278285
}
286+
287+
lineNumber++;
279288
}
280289

281-
lineNumber++;
290+
//Serial.println(F("Config file read complete"));
291+
settingsFile.close();
292+
xSemaphoreGive(xFATSemaphore);
293+
return (true);
294+
}
295+
else
296+
{
297+
Serial.println(F("No config file found. Using settings from EEPROM."));
298+
//The defaults of the struct will be recorded to a file later on.
299+
xSemaphoreGive(xFATSemaphore);
300+
return (false);
282301
}
283302

284-
//Serial.println(F("Config file read complete"));
285-
settingsFile.close();
286-
return (true);
287-
}
288-
else
289-
{
290-
Serial.println(F("No config file found. Using settings from EEPROM."));
291-
//The defaults of the struct will be recorded to a file later on.
292-
return (false);
293-
}
294-
}
303+
} //End Semaphore check
304+
} //End SD online
295305

296306
Serial.println(F("Config file read failed: SD offline"));
297307
return (false); //SD offline
@@ -358,7 +368,7 @@ bool parseLine(char* str) {
358368
strcat(settingsFileName, "_Settings.txt");
359369
sd.remove(settingsFileName);
360370

361-
Serial.println(F("RTK Surveyor has been factory reset. Freezing. Please restart and open terminal at 115200bps."));
371+
Serial.printf("RTK %s has been factory reset via settings file. Freezing. Please restart and open terminal at 115200bps.\n\r", platformBluetoothPrefix);
362372

363373
while (1)
364374
delay(1); //Prevent CPU freakout

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ int startLogTime_minutes = 0; //Mark when we start logging so we can stop loggin
113113
//System crashes if two tasks access a file at the same time
114114
//So we use a semaphore to see if file system is available
115115
SemaphoreHandle_t xFATSemaphore;
116-
const int fatSemaphore_maxWait = 5; //TickType_t
116+
const TickType_t fatSemaphore_maxWait_ms = 200 / portTICK_PERIOD_MS;
117117

118118
#define sdWriteSize 512 // Write data to the SD card in blocks of 512 bytes
119119
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Firmware/RTK_Surveyor/System.ino

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,19 @@ bool createTestFile()
623623
SdFile testFile;
624624
char testFileName[40] = "testfile.txt";
625625

626-
if (testFile.open(testFileName, O_CREAT | O_APPEND | O_WRITE) == true)
626+
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
627+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
627628
{
628-
testFile.close();
629+
if (testFile.open(testFileName, O_CREAT | O_APPEND | O_WRITE) == true)
630+
{
631+
testFile.close();
629632

630-
if (sd.exists(testFileName))
631-
sd.remove(testFileName);
632-
return (true);
633+
if (sd.exists(testFileName))
634+
sd.remove(testFileName);
635+
xSemaphoreGive(xFATSemaphore);
636+
return (true);
637+
}
638+
xSemaphoreGive(xFATSemaphore);
633639
}
634640

635641
return (false);

Firmware/RTK_Surveyor/Tasks.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void F9PSerialReadTask(void *e)
6767
if ((systemTime_minutes - startLogTime_minutes) < settings.maxLogTime_minutes)
6868
{
6969
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile()
70-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait) == pdPASS)
70+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
7171
{
7272
ubxFile.write(rBuffer, s);
7373

Firmware/RTK_Surveyor/menuFirmware.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void updateFromSD(char *firmwareFileName)
103103
//Turn off any tasks so that we are not disrupted
104104
stopWiFi();
105105
endBluetooth();
106-
106+
107107
Serial.printf("Loading %s\n\r", firmwareFileName);
108108
if (sd.exists(firmwareFileName))
109109
{
@@ -141,8 +141,8 @@ void updateFromSD(char *firmwareFileName)
141141
//Bulk write from the SD file to the EEPROM
142142
while (firmwareFile.available())
143143
{
144-
if (productVariant == RTK_SURVEYOR)
145-
digitalWrite(pin_baseStatusLED, !digitalRead(pin_baseStatusLED)); //Toggle LED to indcate activity
144+
if (productVariant == RTK_SURVEYOR)
145+
digitalWrite(pin_baseStatusLED, !digitalRead(pin_baseStatusLED)); //Toggle LED to indcate activity
146146

147147
int bytesToWrite = pageSize; //Max number of bytes to read
148148
if (firmwareFile.available() < bytesToWrite) bytesToWrite = firmwareFile.available(); //Trim this read size as needed

Firmware/RTK_Surveyor/menuMain.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void menuMain()
5959
else if (incoming == '6' && settings.enableSD == true && online.microSD == true)
6060
{
6161
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
62-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait) == pdPASS)
62+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
6363
{
6464
Serial.println(F("Files found (date time size name):\n\r"));
6565
sd.ls(LS_R | LS_DATE | LS_SIZE);
@@ -82,8 +82,13 @@ void menuMain()
8282
strcpy(settingsFileName, platformFilePrefix);
8383
strcat(settingsFileName, "_Settings.txt");
8484

85-
if (sd.exists(settingsFileName))
86-
sd.remove(settingsFileName);
85+
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
86+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
87+
{
88+
if (sd.exists(settingsFileName))
89+
sd.remove(settingsFileName);
90+
xSemaphoreGive(xFATSemaphore);
91+
} //End xFATSemaphore
8792

8893
i2cGNSS.factoryReset(); //Reset everything: baud rate, I2C address, update rate, everything.
8994

Firmware/RTK_Surveyor/menuMessages.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ void beginLogging()
823823
}
824824

825825
//Attempt to write to file system. This avoids collisions with file writing in F9PSerialReadTask()
826-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait) == pdPASS)
826+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
827827
{
828828
// O_CREAT - create the file if it does not exist
829829
// O_APPEND - seek to the end of the file prior to each write

Firmware/RTK_Surveyor/menuTest.ino

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ void menuTest()
4949
{
5050
if (settings.enableSD && online.microSD)
5151
{
52-
Serial.println(F("Files found (date time size name):\n\r"));
53-
sd.ls(LS_R | LS_DATE | LS_SIZE);
52+
//Attempt to access file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
53+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
54+
{
55+
Serial.println(F("Files found (date time size name):\n\r"));
56+
sd.ls(LS_R | LS_DATE | LS_SIZE);
57+
58+
xSemaphoreGive(xFATSemaphore);
59+
}
5460
}
5561
}
5662
else if (incoming == 'x')

0 commit comments

Comments
 (0)