diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index d5ca72142..3d2885654 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -205,6 +205,63 @@ void rmDir(const char *dirname) #endif } +bool fsCheck() +{ +#if defined(ARCH_NRF52) + size_t write_size = 0; + size_t read_size = 0; + char buf[32] = {0}; + while (!Serial); + + Adafruit_LittleFS_Namespace::File file(FSCom); + const char *text = "meshtastic fs test"; + size_t text_length = strlen(text); + const char *filename = "/meshtastic.txt"; + + LOG_DEBUG("Try create file .\n"); + if (file.open(filename, FILE_O_WRITE)) { + write_size = file.write(text); + } else { + LOG_DEBUG("Open file failed .\n");; + goto FORMAT_FS; + } + + if (write_size != text_length) { + LOG_DEBUG("Text bytes do not match .\n"); + file.close(); + goto FORMAT_FS; + } + + file.close(); + + if (!file.open(filename, FILE_O_READ)) { + LOG_DEBUG("Open file failed .\n"); + goto FORMAT_FS; + } + + read_size = file.readBytes(buf, text_length); + if (read_size != text_length) { + LOG_DEBUG("Text bytes do not match .\n"); + file.close(); + goto FORMAT_FS; + } + + if (memcmp(buf, text, text_length) != 0) { + LOG_DEBUG("The written bytes do not match the read bytes .\n"); + file.close(); + goto FORMAT_FS; + } + return true; +FORMAT_FS: + LOG_DEBUG("Format FS ....\n"); + FSCom.format(); + FSCom.begin(); + return false; +#else + return true; +#endif +} + void fsInit() { #ifdef FSCom @@ -219,15 +276,28 @@ void fsInit() * nRF52840 has a certain chance of automatic formatting failure. * Try to create a file after initializing the file system. If the creation fails, * it means that the file system is not working properly. Please format it manually again. + * To check the normality of the file system, you need to disable the LFS_NO_ASSERT assertion. + * Otherwise, the assertion will be entered at the moment of reading or opening, and the FS will not be formatted. * */ - Adafruit_LittleFS_Namespace::File file(FSCom); - const char *filename = "/meshtastic.txt"; - if (!file.open(filename, FILE_O_WRITE)) { - LOG_DEBUG("Format ...."); - FSCom.format(); - FSCom.begin(); - } else { - file.close(); + bool ret = false; + uint8_t retry = 3; + + while (retry--) { + ret = fsCheck(); + if (ret) { + LOG_DEBUG("File system check is OK.\n"); + break; + } + delay(10); + } + + // It may not be possible to reach this step. + // Add a loop here to prevent unpredictable situations from happening. + // Can add a screen to display error status later. + if (!ret) { + while (1) { + Serial.println("The file system is damaged and cannot proceed to the next step.\n"); delay(1000); + } } #else LOG_DEBUG("Filesystem files:\n"); diff --git a/variants/t-echo/platformio.ini b/variants/t-echo/platformio.ini index 9ff60be3f..900c7ffd8 100644 --- a/variants/t-echo/platformio.ini +++ b/variants/t-echo/platformio.ini @@ -17,6 +17,7 @@ build_flags = ${nrf52840_base.build_flags} -Ivariants/t-echo -DEINK_LIMIT_RATE_RESPONSIVE_SEC=1 ; Minimum interval between RESPONSIVE updates -DEINK_LIMIT_GHOSTING_PX=2000 ; (Optional) How much image ghosting is tolerated -DEINK_BACKGROUND_USES_FAST ; (Optional) Use FAST refresh for both BACKGROUND and RESPONSIVE, until a limit is reached. + -DLFS_NO_ASSERT ; Disable LFS assertions build_src_filter = ${nrf52_base.build_src_filter} +<../variants/t-echo> lib_deps = ${nrf52840_base.lib_deps}