@@ -645,58 +645,55 @@ void loop() {
645645 Serial1.print (outputData); // Print to TX pin
646646
647647 // Record to SD
648- if (settings.logData == true )
648+ if (( settings.logData == true ) && (online. microSD ) )
649649 {
650- if (settings.enableSD && online.microSD )
650+ digitalWrite (PIN_STAT_LED, HIGH);
651+ uint32_t recordLength = sensorDataFile.write (outputData, strlen (outputData));
652+ if (recordLength != strlen (outputData)) // Record the buffer to the card
651653 {
652- digitalWrite (PIN_STAT_LED, HIGH);
653- uint32_t recordLength = sensorDataFile.write (outputData, strlen (outputData));
654- if (recordLength != strlen (outputData)) // Record the buffer to the card
654+ if (settings.printDebugMessages == true )
655655 {
656- if (settings.printDebugMessages == true )
657- {
658- SerialPrintf3 (" *** sensorDataFile.write data length mismatch! *** recordLength: %d, outputDataLength: %d\r\n " , recordLength, strlen (outputData));
659- }
656+ SerialPrintf3 (" *** sensorDataFile.write data length mismatch! *** recordLength: %d, outputDataLength: %d\r\n " , recordLength, strlen (outputData));
660657 }
658+ }
659+
660+ // Force sync every 500ms
661+ if (bestMillis () - lastDataLogSyncTime > 500 )
662+ {
663+ lastDataLogSyncTime = bestMillis ();
664+ sensorDataFile.sync ();
665+ if (settings.frequentFileAccessTimestamps == true )
666+ updateDataFileAccess (&sensorDataFile); // Update the file access time & date
667+ }
661668
662- // Force sync every 500ms
663- if (bestMillis () - lastDataLogSyncTime > 500 )
669+ // Check if it is time to open a new log file
670+ uint64_t secsSinceLastFileNameChange = rtcMillis () - lastSDFileNameChangeTime; // Calculate how long we have been logging for
671+ secsSinceLastFileNameChange /= 1000ULL ; // Convert to secs
672+ if ((settings.openNewLogFilesAfter > 0 ) && (((unsigned long )secsSinceLastFileNameChange) >= settings.openNewLogFilesAfter ))
673+ {
674+ // Close existings files
675+ if (online.dataLogging == true )
664676 {
665- lastDataLogSyncTime = bestMillis ();
666677 sensorDataFile.sync ();
667- if (settings.frequentFileAccessTimestamps == true )
668- updateDataFileAccess (&sensorDataFile); // Update the file access time & date
678+ updateDataFileAccess (&sensorDataFile); // Update the file access time & date
679+ sensorDataFile.close ();
680+ strcpy (sensorDataFileName, findNextAvailableLog (settings.nextDataLogNumber , " dataLog" ));
681+ beginDataLogging (); // 180ms
682+ if (settings.showHelperText == true ) printHelperText (false ); // printHelperText to terminal and sensor file
669683 }
670-
671- // Check if it is time to open a new log file
672- uint64_t secsSinceLastFileNameChange = rtcMillis () - lastSDFileNameChangeTime; // Calculate how long we have been logging for
673- secsSinceLastFileNameChange /= 1000ULL ; // Convert to secs
674- if ((settings.openNewLogFilesAfter > 0 ) && (((unsigned long )secsSinceLastFileNameChange) >= settings.openNewLogFilesAfter ))
684+ if (online.serialLogging == true )
675685 {
676- // Close existings files
677- if (online.dataLogging == true )
678- {
679- sensorDataFile.sync ();
680- updateDataFileAccess (&sensorDataFile); // Update the file access time & date
681- sensorDataFile.close ();
682- strcpy (sensorDataFileName, findNextAvailableLog (settings.nextDataLogNumber , " dataLog" ));
683- beginDataLogging (); // 180ms
684- if (settings.showHelperText == true ) printHelperText (false ); // printHelperText to terminal and sensor file
685- }
686- if (online.serialLogging == true )
687- {
688- serialDataFile.sync ();
689- updateDataFileAccess (&serialDataFile); // Update the file access time & date
690- serialDataFile.close ();
691- strcpy (serialDataFileName, findNextAvailableLog (settings.nextSerialLogNumber , " serialLog" ));
692- beginSerialLogging ();
693- }
694-
695- lastSDFileNameChangeTime = rtcMillis (); // Record the time of the file name change
686+ serialDataFile.sync ();
687+ updateDataFileAccess (&serialDataFile); // Update the file access time & date
688+ serialDataFile.close ();
689+ strcpy (serialDataFileName, findNextAvailableLog (settings.nextSerialLogNumber , " serialLog" ));
690+ beginSerialLogging ();
696691 }
697692
698- digitalWrite (PIN_STAT_LED, LOW);
693+ lastSDFileNameChangeTime = rtcMillis (); // Record the time of the file name change
699694 }
695+
696+ digitalWrite (PIN_STAT_LED, LOW);
700697 }
701698
702699 if ((settings.useGPIO32ForStopLogging == true ) && (stopLoggingSeen == true )) // Has the user pressed the stop logging button?
@@ -892,56 +889,51 @@ void beginSD()
892889 pin_config (PinName (PIN_MICROSD_CHIP_SELECT), g_AM_HAL_GPIO_OUTPUT); // Make sure the pin does actually get re-configured
893890 digitalWrite (PIN_MICROSD_CHIP_SELECT, HIGH); // Be sure SD is deselected
894891
895- if (settings.enableSD == true )
896- {
897- // For reasons I don't understand, we seem to have to wait for at least 1ms after SPI.begin before we call microSDPowerOn.
898- // If you comment the next line, the Artemis resets at microSDPowerOn when beginSD is called from wakeFromSleep...
899- // But only on one of my V10 red boards. The second one I have doesn't seem to need the delay!?
900- delay (5 );
892+ // If the microSD card is present, it needs to be powered on otherwise the IMU will fail to start
893+ // (The microSD card will pull the SPI pins low, preventing communication with the IMU)
894+
895+ // For reasons I don't understand, we seem to have to wait for at least 1ms after SPI.begin before we call microSDPowerOn.
896+ // If you comment the next line, the Artemis resets at microSDPowerOn when beginSD is called from wakeFromSleep...
897+ // But only on one of my V10 red boards. The second one I have doesn't seem to need the delay!?
898+ delay (5 );
901899
902- microSDPowerOn ();
900+ microSDPowerOn ();
903901
904- // Max power up time is 250ms: https://www.kingston.com/datasheets/SDCIT-specsheet-64gb_en.pdf
905- // Max current is 200mA average across 1s, peak 300mA
906- for (int i = 0 ; i < 10 ; i++) // Wait
902+ // Max power up time is 250ms: https://www.kingston.com/datasheets/SDCIT-specsheet-64gb_en.pdf
903+ // Max current is 200mA average across 1s, peak 300mA
904+ for (int i = 0 ; i < 10 ; i++) // Wait
905+ {
906+ checkBattery ();
907+ delay (1 );
908+ }
909+
910+ if (sd.begin (SD_CONFIG) == false ) // Try to begin the SD card using the correct chip select
911+ {
912+ printDebug (F (" SD init failed (first attempt). Trying again...\r\n " ));
913+ for (int i = 0 ; i < 250 ; i++) // Give SD more time to power up, then try again
907914 {
908915 checkBattery ();
909916 delay (1 );
910917 }
911-
912918 if (sd.begin (SD_CONFIG) == false ) // Try to begin the SD card using the correct chip select
913919 {
914- printDebug (F (" SD init failed (first attempt). Trying again...\r\n " ));
915- for (int i = 0 ; i < 250 ; i++) // Give SD more time to power up, then try again
916- {
917- checkBattery ();
918- delay (1 );
919- }
920- if (sd.begin (SD_CONFIG) == false ) // Try to begin the SD card using the correct chip select
921- {
922- SerialPrintln (F (" SD init failed (second attempt). Is card present? Formatted?" ));
923- SerialPrintln (F (" Please ensure the SD card is formatted correctly using https://www.sdcard.org/downloads/formatter/" ));
924- digitalWrite (PIN_MICROSD_CHIP_SELECT, HIGH); // Be sure SD is deselected
925- online.microSD = false ;
926- return ;
927- }
928- }
929-
930- // Change to root directory. All new file creation will be in root.
931- if (sd.chdir () == false )
932- {
933- SerialPrintln (F (" SD change directory failed" ));
920+ SerialPrintln (F (" SD init failed (second attempt). Is card present? Formatted?" ));
921+ SerialPrintln (F (" Please ensure the SD card is formatted correctly using https://www.sdcard.org/downloads/formatter/" ));
922+ digitalWrite (PIN_MICROSD_CHIP_SELECT, HIGH); // Be sure SD is deselected
934923 online.microSD = false ;
935924 return ;
936925 }
937-
938- online.microSD = true ;
939926 }
940- else
927+
928+ // Change to root directory. All new file creation will be in root.
929+ if (sd.chdir () == false )
941930 {
942- microSDPowerOff ( );
931+ SerialPrintln ( F ( " SD change directory failed " ) );
943932 online.microSD = false ;
933+ return ;
944934 }
935+
936+ online.microSD = true ;
945937}
946938
947939void enableCIPOpullUp () // updated for v2.1.0 of the Apollo3 core
0 commit comments