diff --git a/libraries/AP_HAL_ChibiOS/Storage.cpp b/libraries/AP_HAL_ChibiOS/Storage.cpp index ca91c114cfa..297f07110b0 100644 --- a/libraries/AP_HAL_ChibiOS/Storage.cpp +++ b/libraries/AP_HAL_ChibiOS/Storage.cpp @@ -34,6 +34,11 @@ extern const AP_HAL::HAL& hal; #define HAL_STORAGE_FILE "/APM/" SKETCHNAME ".stg" #endif +#ifndef HAL_STORAGE_BACKUP_FILE +// location of backup file +#define HAL_STORAGE_BACKUP_FILE "/APM/" SKETCHNAME ".bak" +#endif + void Storage::_storage_open(void) { if (_initialised) { @@ -55,6 +60,7 @@ void Storage::_storage_open(void) if (!fram.read(0, _buffer, CH_STORAGE_SIZE)) { return; } + _save_backup(); _initialised = true; return; } @@ -64,9 +70,7 @@ void Storage::_storage_open(void) #ifdef STORAGE_FLASH_PAGE // load from storage backend _flash_load(); -#endif - -#ifdef USE_POSIX +#elif defined(USE_POSIX) // allow for fallback to microSD based storage sdcard_init(); @@ -93,9 +97,28 @@ void Storage::_storage_open(void) using_filesystem = true; #endif + _save_backup(); _initialised = true; } +/* + save a backup of storage file if we have microSD available. This is + very handy for diagnostics, and for moving a copy of storage into + SITL for testing + */ +void Storage::_save_backup(void) +{ +#ifdef USE_POSIX + // allow for fallback to microSD based storage + sdcard_init(); + int fd = open(HAL_STORAGE_BACKUP_FILE, O_WRONLY|O_CREAT|O_TRUNC); + if (fd != -1) { + write(fd, _buffer, CH_STORAGE_SIZE); + close(fd); + } +#endif +} + /* mark some lines as dirty. Note that there is no attempt to avoid the race condition between this code and the _timer_tick() code diff --git a/libraries/AP_HAL_ChibiOS/Storage.h b/libraries/AP_HAL_ChibiOS/Storage.h index 4c958a287ae..eb78a293252 100644 --- a/libraries/AP_HAL_ChibiOS/Storage.h +++ b/libraries/AP_HAL_ChibiOS/Storage.h @@ -47,6 +47,7 @@ private: volatile bool _initialised; void _storage_create(void); void _storage_open(void); + void _save_backup(void); void _mark_dirty(uint16_t loc, uint16_t length); uint8_t _buffer[CH_STORAGE_SIZE] __attribute__((aligned(4))); Bitmask _dirty_mask{CH_STORAGE_NUM_LINES};