From 613bdcac5824146305e9324582ab2698c0eb6c84 Mon Sep 17 00:00:00 2001 From: Andy Piper Date: Fri, 13 Dec 2024 10:13:52 +0000 Subject: [PATCH] AP_Logger: nuance minspace for W25NXX and W25QXX --- libraries/AP_Logger/AP_Logger.cpp | 8 ++++++-- libraries/AP_Logger/AP_Logger_File.cpp | 12 ++++++++++-- libraries/AP_Logger/AP_Logger_File.h | 6 +++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/libraries/AP_Logger/AP_Logger.cpp b/libraries/AP_Logger/AP_Logger.cpp index 3d834946edf5c..f19a3636f0cd5 100644 --- a/libraries/AP_Logger/AP_Logger.cpp +++ b/libraries/AP_Logger/AP_Logger.cpp @@ -58,7 +58,11 @@ extern const AP_HAL::HAL& hal; #ifndef HAL_LOGGER_MIN_MB_FREE #if AP_FILESYSTEM_LITTLEFS_ENABLED -#define HAL_LOGGER_MIN_MB_FREE 1 +#if AP_FILESYSTEM_LITTLEFS_FLASH_TYPE == AP_FILESYSTEM_FLASH_W25NXX +#define HAL_LOGGER_MIN_MB_FREE 10 +#else +#define HAL_LOGGER_MIN_MB_FREE 2 +#endif #else #define HAL_LOGGER_MIN_MB_FREE 500 #endif @@ -143,7 +147,7 @@ const AP_Param::GroupInfo AP_Logger::var_info[] = { // @DisplayName: Old logs on the SD card will be deleted to maintain this amount of free space // @Description: Set this such that the free space is larger than your largest typical flight log // @Units: MB - // @Range: 10 1000 + // @Range: 2 1000 // @User: Standard AP_GROUPINFO("_FILE_MB_FREE", 7, AP_Logger, _params.min_MB_free, HAL_LOGGER_MIN_MB_FREE), diff --git a/libraries/AP_Logger/AP_Logger_File.cpp b/libraries/AP_Logger/AP_Logger_File.cpp index aab5a15585295..30a53b7a8fad6 100644 --- a/libraries/AP_Logger/AP_Logger_File.cpp +++ b/libraries/AP_Logger/AP_Logger_File.cpp @@ -1011,14 +1011,22 @@ void AP_Logger_File::io_timer(void) _writebuf.advance(nwritten); /* - fsync on littlefs is extremely expensive (20% CPU on an H7) and not - required since the whole point of the filesystem is to avoid corruption + fsync on littlefs is extremely expensive (20% CPU on an H7) particularly because the + COW architecture can mean you end up copying a load of blocks. instead try and only sync + at the end of a block to avoid copying and minimise CPU. fsync is needed for the file + metadata (including size) to be updated */ #if CONFIG_HAL_BOARD != HAL_BOARD_SITL && CONFIG_HAL_BOARD_SUBTYPE != HAL_BOARD_SUBTYPE_LINUX_NONE #if AP_FILESYSTEM_LITTLEFS_ENABLED if (sync_block) #endif // AP_FILESYSTEM_LITTLEFS_ENABLED { + /* + the best strategy for minimizing corruption on microSD cards + seems to be to write in 4k chunks and fsync the file on each + chunk, ensuring the directory entry is updated after each + write. + */ last_io_operation = "fsync"; AP::FS().fsync(_write_fd); last_io_operation = ""; diff --git a/libraries/AP_Logger/AP_Logger_File.h b/libraries/AP_Logger/AP_Logger_File.h index c465b96b3457a..77732b1860008 100644 --- a/libraries/AP_Logger/AP_Logger_File.h +++ b/libraries/AP_Logger/AP_Logger_File.h @@ -130,7 +130,11 @@ class AP_Logger_File : public AP_Logger_Backend uint32_t _free_space_last_check_time; // milliseconds const uint32_t _free_space_check_interval = 1000UL; // milliseconds #if AP_FILESYSTEM_LITTLEFS_ENABLED - const uint32_t _free_space_min_avail = 4096; // bytes +#if AP_FILESYSTEM_LITTLEFS_FLASH_TYPE == AP_FILESYSTEM_FLASH_W25NXX + const uint32_t _free_space_min_avail = 1024 * 1024; // bytes +#else + const uint32_t _free_space_min_avail = 1024 * 256; // bytes +#endif #else const uint32_t _free_space_min_avail = 8388608; // bytes #endif