Skip to content

Commit 44d929e

Browse files
committed
Add semaphore check to file size. Add short time for semaphore.
1 parent 2af5d05 commit 44d929e

File tree

7 files changed

+50
-32
lines changed

7 files changed

+50
-32
lines changed

Firmware/RTK_Surveyor/NVM.ino

Lines changed: 2 additions & 2 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_ms) == pdPASS)
70+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
7171
{
7272
//Assemble settings file name
7373
char settingsFileName[40]; //SFE_Surveyor_Settings.txt
@@ -240,7 +240,7 @@ bool loadSystemSettingsFromFile()
240240
if (online.microSD == true)
241241
{
242242
//Attempt to access file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
243-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_maxWait_ms) == pdPASS)
243+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
244244
{
245245
//Assemble settings file name
246246
char settingsFileName[40]; //SFE_Surveyor_Settings.txt

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ 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 TickType_t fatSemaphore_maxWait_ms = 200 / portTICK_PERIOD_MS;
116+
const TickType_t fatSemaphore_shortWait_ms = 10 / portTICK_PERIOD_MS;
117+
const TickType_t fatSemaphore_longWait_ms = 200 / portTICK_PERIOD_MS;
117118

118119
#define sdWriteSize 512 // Write data to the SD card in blocks of 512 bytes
119120
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
@@ -375,42 +376,59 @@ void updateLogs()
375376
else if (online.logging == true && settings.enableLogging == false)
376377
{
377378
//Close down file
378-
ubxFile.sync();
379-
ubxFile.close();
380-
online.logging = false;
379+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
380+
{
381+
ubxFile.sync();
382+
ubxFile.close();
383+
online.logging = false;
384+
//xSemaphoreGive(xFATSemaphore); //Do not release semaphore
385+
}
381386
}
382387

383388
//Report file sizes to show recording is working
384389
if (online.logging == true)
385390
{
386391
if (millis() - lastFileReport > 5000)
387392
{
388-
lastFileReport = millis();
389-
Serial.printf("UBX file size: %d", ubxFile.fileSize());
393+
long fileSize = 0;
390394

391-
if ((systemTime_minutes - startLogTime_minutes) < settings.maxLogTime_minutes)
392-
{
393-
//Calculate generation and write speeds every 5 seconds
394-
uint32_t delta = ubxFile.fileSize() - lastLogSize;
395-
Serial.printf(" - Generation rate: %0.1fkB/s", delta / 5.0 / 1000.0);
396-
Serial.printf(" - Write speed: %0.1fkB/s", delta / (totalWriteTime / 1000000.0) / 1000.0);
397-
}
398-
else
395+
//Attempt to access file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
396+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
399397
{
400-
Serial.printf(" reached max log time %d", settings.maxLogTime_minutes);
401-
}
402-
403-
Serial.println();
398+
fileSize = ubxFile.fileSize();
404399

405-
totalWriteTime = 0; //Reset write time every 5s
400+
xSemaphoreGive(xFATSemaphore);
401+
}
406402

407-
if (ubxFile.fileSize() > lastLogSize)
403+
if (fileSize > 0)
408404
{
409-
lastLogSize = ubxFile.fileSize();
410-
logIncreasing = true;
405+
lastFileReport = millis();
406+
Serial.printf("UBX file size: %ld", fileSize);
407+
408+
if ((systemTime_minutes - startLogTime_minutes) < settings.maxLogTime_minutes)
409+
{
410+
//Calculate generation and write speeds every 5 seconds
411+
uint32_t delta = fileSize - lastLogSize;
412+
Serial.printf(" - Generation rate: %0.1fkB/s", delta / 5.0 / 1000.0);
413+
Serial.printf(" - Write speed: %0.1fkB/s", delta / (totalWriteTime / 1000000.0) / 1000.0);
414+
}
415+
else
416+
{
417+
Serial.printf(" reached max log time %d", settings.maxLogTime_minutes);
418+
}
419+
420+
Serial.println();
421+
422+
totalWriteTime = 0; //Reset write time every 5s
423+
424+
if (fileSize > lastLogSize)
425+
{
426+
lastLogSize = fileSize;
427+
logIncreasing = true;
428+
}
429+
else
430+
logIncreasing = false;
411431
}
412-
else
413-
logIncreasing = false;
414432
}
415433
}
416434
}

Firmware/RTK_Surveyor/System.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ bool createTestFile()
624624
char testFileName[40] = "testfile.txt";
625625

626626
//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)
627+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
628628
{
629629
if (testFile.open(testFileName, O_CREAT | O_APPEND | O_WRITE) == true)
630630
{

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_ms) == pdPASS)
70+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
7171
{
7272
ubxFile.write(rBuffer, s);
7373

Firmware/RTK_Surveyor/menuMain.ino

Lines changed: 2 additions & 2 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_ms) == pdPASS)
62+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
6363
{
6464
Serial.println(F("Files found (date time size name):\n\r"));
6565
sd.ls(LS_R | LS_DATE | LS_SIZE);
@@ -83,7 +83,7 @@ void menuMain()
8383
strcat(settingsFileName, "_Settings.txt");
8484

8585
//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)
86+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
8787
{
8888
if (sd.exists(settingsFileName))
8989
sd.remove(settingsFileName);

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_ms) == pdPASS)
826+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void menuTest()
5050
if (settings.enableSD && online.microSD)
5151
{
5252
//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)
53+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
5454
{
5555
Serial.println(F("Files found (date time size name):\n\r"));
5656
sd.ls(LS_R | LS_DATE | LS_SIZE);

0 commit comments

Comments
 (0)