Skip to content

Commit eded1ed

Browse files
authored
Merge pull request #136 from LeeLeahy2/semaphore
Change semaphore name from xFATSemaphore to sdCardSemaphore
2 parents 78bbfc1 + c02fc13 commit eded1ed

File tree

10 files changed

+100
-53
lines changed

10 files changed

+100
-53
lines changed

Firmware/RTK_Surveyor/Begin.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ void beginSD()
241241
}
242242

243243
//Setup FAT file access semaphore
244-
if (xFATSemaphore == NULL)
244+
if (sdCardSemaphore == NULL)
245245
{
246-
xFATSemaphore = xSemaphoreCreateMutex();
247-
if (xFATSemaphore != NULL)
248-
xSemaphoreGive(xFATSemaphore); //Make the file system available for use
246+
sdCardSemaphore = xSemaphoreCreateMutex();
247+
if (sdCardSemaphore != NULL)
248+
xSemaphoreGive(sdCardSemaphore); //Make the file system available for use
249249
}
250250

251251
if (createTestFile() == false)

Firmware/RTK_Surveyor/Buttons.ino

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ void powerDown(bool displayInfo)
2323
{
2424
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile()
2525
//Wait up to 1000ms
26-
if (xSemaphoreTake(xFATSemaphore, 1000 / portTICK_PERIOD_MS) == pdPASS)
26+
if (xSemaphoreTake(sdCardSemaphore, 1000 / portTICK_PERIOD_MS) == pdPASS)
2727
{
2828
//Close down file system
2929
ubxFile.sync();
3030
ubxFile.close();
31-
//xSemaphoreGive(xFATSemaphore); //Do not release semaphore
32-
} //End xFATSemaphore
31+
//xSemaphoreGive(sdCardSemaphore); //Do not release semaphore
32+
} //End sdCardSemaphore
33+
else
34+
{
35+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
36+
}
3337

3438
online.logging = false;
3539
}

Firmware/RTK_Surveyor/NVM.ino

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void recordSystemSettingsToFileSD(char *fileName)
6262
if (online.microSD == true)
6363
{
6464
//Attempt to write to file system. This avoids collisions with file writing from other functions like updateLogs()
65-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
65+
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
6666
{
6767
if (sd.exists(fileName))
6868
sd.remove(fileName);
@@ -84,7 +84,11 @@ void recordSystemSettingsToFileSD(char *fileName)
8484

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

87-
xSemaphoreGive(xFATSemaphore);
87+
xSemaphoreGive(sdCardSemaphore);
88+
}
89+
else
90+
{
91+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
8892
}
8993
}
9094
}
@@ -226,15 +230,15 @@ bool loadSystemSettingsFromFileSD(char* fileName, Settings *settings)
226230
if (online.microSD == true)
227231
{
228232
//Attempt to access file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
229-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
233+
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
230234
{
231235
if (sd.exists(fileName))
232236
{
233237
SdFile settingsFile; //FAT32
234238
if (settingsFile.open(fileName, O_READ) == false)
235239
{
236240
Serial.println(F("Failed to open settings file"));
237-
xSemaphoreGive(xFATSemaphore);
241+
xSemaphoreGive(sdCardSemaphore);
238242
return (false);
239243
}
240244

@@ -255,7 +259,7 @@ bool loadSystemSettingsFromFileSD(char* fileName, Settings *settings)
255259
{
256260
//If we can't read the first line of the settings file, give up
257261
Serial.println(F("Giving up on settings file"));
258-
xSemaphoreGive(xFATSemaphore);
262+
xSemaphoreGive(sdCardSemaphore);
259263
return (false);
260264
}
261265
}
@@ -265,7 +269,7 @@ bool loadSystemSettingsFromFileSD(char* fileName, Settings *settings)
265269
{
266270
//If we can't read the first line of the settings file, give up
267271
Serial.println(F("Giving up on settings file"));
268-
xSemaphoreGive(xFATSemaphore);
272+
xSemaphoreGive(sdCardSemaphore);
269273
return (false);
270274
}
271275
}
@@ -275,17 +279,21 @@ bool loadSystemSettingsFromFileSD(char* fileName, Settings *settings)
275279

276280
//Serial.println(F("Config file read complete"));
277281
settingsFile.close();
278-
xSemaphoreGive(xFATSemaphore);
282+
xSemaphoreGive(sdCardSemaphore);
279283
return (true);
280284
}
281285
else
282286
{
283287
log_d("File %s not found", fileName);
284-
xSemaphoreGive(xFATSemaphore);
288+
xSemaphoreGive(sdCardSemaphore);
285289
return (false);
286290
}
287291

288292
} //End Semaphore check
293+
else
294+
{
295+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
296+
}
289297
} //End SD online
290298

291299
log_d("Config file read failed: SD offline");

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ SdFile newFirmwareFile; //File that is available if user uploads new firmware vi
141141

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

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

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

346346
AsyncWebServer server(80);
@@ -563,23 +563,31 @@ void updateLogs()
563563
else if (online.logging == true && settings.enableLogging == false)
564564
{
565565
//Close down file
566-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
566+
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
567567
{
568568
ubxFile.sync();
569569
ubxFile.close();
570570
online.logging = false;
571-
xSemaphoreGive(xFATSemaphore); //Release semaphore
571+
xSemaphoreGive(sdCardSemaphore); //Release semaphore
572+
}
573+
else
574+
{
575+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
572576
}
573577
}
574578
else if (online.logging == true && settings.enableLogging == true && (systemTime_minutes - startCurrentLogTime_minutes) >= settings.maxLogLength_minutes)
575579
{
576580
//Close down file. A new one will be created at the next calling of updateLogs().
577-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
581+
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
578582
{
579583
ubxFile.sync();
580584
ubxFile.close();
581585
online.logging = false;
582-
xSemaphoreGive(xFATSemaphore); //Release semaphore
586+
xSemaphoreGive(sdCardSemaphore); //Release semaphore
587+
}
588+
else
589+
{
590+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
583591
}
584592
}
585593

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

606614
lastUBXLogSyncTime = millis();
607-
xSemaphoreGive(xFATSemaphore);
608-
} //End xFATSemaphore
615+
xSemaphoreGive(sdCardSemaphore);
616+
} //End sdCardSemaphore
609617
else
610618
{
611-
log_d("Semaphore failed to yield");
619+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
612620
}
613621
}
614622

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

627-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
635+
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
628636
{
629637
ubxFile.println(nmeaMessage);
630638

631-
xSemaphoreGive(xFATSemaphore);
639+
xSemaphoreGive(sdCardSemaphore);
632640
newEventToRecord = false;
633641
}
642+
else
643+
{
644+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
645+
}
634646
}
635647

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

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

646-
xSemaphoreGive(xFATSemaphore);
658+
xSemaphoreGive(sdCardSemaphore);
659+
}
660+
else
661+
{
662+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
647663
}
648664

649665
if (fileSize > 0)

Firmware/RTK_Surveyor/System.ino

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,25 +556,29 @@ bool createTestFile()
556556
SdFile testFile;
557557
char testFileName[40] = "testfile.txt";
558558

559-
if (xFATSemaphore == NULL)
559+
if (sdCardSemaphore == NULL)
560560
{
561-
log_d("xFATSemaphore is Null");
561+
log_d("sdCardSemaphore is Null");
562562
return (false);
563563
}
564564

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

572572
if (sd.exists(testFileName))
573573
sd.remove(testFileName);
574-
xSemaphoreGive(xFATSemaphore);
574+
xSemaphoreGive(sdCardSemaphore);
575575
return (true);
576576
}
577-
xSemaphoreGive(xFATSemaphore);
577+
xSemaphoreGive(sdCardSemaphore);
578+
}
579+
else
580+
{
581+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
578582
}
579583

580584
return (false);

Firmware/RTK_Surveyor/Tasks.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ void F9PSerialReadTask(void *e)
7575
if ((systemTime_minutes - startLogTime_minutes) < settings.maxLogTime_minutes)
7676
{
7777
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile()
78-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
78+
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
7979
{
8080
ubxFile.write(rBuffer, s);
8181

82-
xSemaphoreGive(xFATSemaphore);
83-
} //End xFATSemaphore
82+
xSemaphoreGive(sdCardSemaphore);
83+
} //End sdCardSemaphore
8484
else
8585
{
86-
log_d("Semaphore failed to yield");
86+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
8787
}
8888
} //End maxLogTime
8989
} //End logging

Firmware/RTK_Surveyor/menuFirmware.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void scanForFirmware()
5252
{
5353
//Attempt to access file system. This avoids collisions with file writing in F9PSerialReadTask()
5454
//Wait up to 5s, this is important
55-
if (xSemaphoreTake(xFATSemaphore, 5000 / portTICK_PERIOD_MS) == pdPASS)
55+
if (xSemaphoreTake(sdCardSemaphore, 5000 / portTICK_PERIOD_MS) == pdPASS)
5656
{
5757
//Count available binaries
5858
SdFile tempFile;
@@ -92,9 +92,12 @@ void scanForFirmware()
9292
tempFile.close();
9393
}
9494

95-
xSemaphoreGive(xFATSemaphore);
95+
xSemaphoreGive(sdCardSemaphore);
96+
}
97+
else
98+
{
99+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
96100
}
97-
98101
}
99102
}
100103

Firmware/RTK_Surveyor/menuMain.ino

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,16 @@ void factoryReset()
219219
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
220220
if (settings.enableSD && online.microSD)
221221
{
222-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
222+
if (xSemaphoreTake(sdCardSemaphore, fatSemaphore_longWait_ms) == pdPASS)
223223
{
224224
//Remove this specific settings file. Don't remove the other profiles.
225225
sd.remove(settingsFileName);
226-
xSemaphoreGive(xFATSemaphore);
227-
} //End xFATSemaphore
226+
xSemaphoreGive(sdCardSemaphore);
227+
} //End sdCardSemaphore
228+
else
229+
{
230+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
231+
}
228232
}
229233

230234
if (online.gnss == true)

Firmware/RTK_Surveyor/menuMessages.ino

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ void beginLogging()
345345
}
346346

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

@@ -405,7 +405,7 @@ void beginLogging()
405405
Serial.println(F("Appending last available log"));
406406
}
407407

408-
xSemaphoreGive(xFATSemaphore);
408+
xSemaphoreGive(sdCardSemaphore);
409409
}
410410
else
411411
{
@@ -448,7 +448,7 @@ bool findLastLog(char *lastLogName)
448448
{
449449
//Attempt to access file system. This avoids collisions with file writing in F9PSerialReadTask()
450450
//Wait up to 5s, this is important
451-
if (xSemaphoreTake(xFATSemaphore, 5000 / portTICK_PERIOD_MS) == pdPASS)
451+
if (xSemaphoreTake(sdCardSemaphore, 5000 / portTICK_PERIOD_MS) == pdPASS)
452452
{
453453
//Count available binaries
454454
SdFile tempFile;
@@ -478,7 +478,11 @@ bool findLastLog(char *lastLogName)
478478
tempFile.close();
479479
}
480480

481-
xSemaphoreGive(xFATSemaphore);
481+
xSemaphoreGive(sdCardSemaphore);
482+
}
483+
else
484+
{
485+
Serial.printf("sdCardSemaphore failed to yield, %s line %d\r\n", __FILE__, __LINE__);
482486
}
483487
}
484488

0 commit comments

Comments
 (0)