From e5015a6b81bb10fde695f210d1aa0ff37b74b973 Mon Sep 17 00:00:00 2001 From: Michael Keller Date: Tue, 3 Dec 2019 13:53:52 +1300 Subject: [PATCH] Restructure flashfs MSC to prevent device timeout on MacOS (#9252) Restructure flashfs MSC to prevent device timeout on MacOS --- src/main/drivers/usb_msc_f4xx.c | 2 -- src/main/drivers/usb_msc_f7xx.c | 2 -- src/main/fc/init.c | 11 +++++++ src/main/msc/emfat_file.c | 51 +++++++++++++++++++++++-------- src/main/msc/usbd_storage_emfat.c | 23 ++------------ 5 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/main/drivers/usb_msc_f4xx.c b/src/main/drivers/usb_msc_f4xx.c index 19234ea6c0..b7608b87a9 100644 --- a/src/main/drivers/usb_msc_f4xx.c +++ b/src/main/drivers/usb_msc_f4xx.c @@ -76,8 +76,6 @@ void mscInit(void) uint8_t mscStart(void) { - ledInit(statusLedConfig()); - //Start USB usbGenerateDisconnectPulse(); diff --git a/src/main/drivers/usb_msc_f7xx.c b/src/main/drivers/usb_msc_f7xx.c index 980eea4f9c..d6b4c1887e 100644 --- a/src/main/drivers/usb_msc_f7xx.c +++ b/src/main/drivers/usb_msc_f7xx.c @@ -74,8 +74,6 @@ void mscInit(void) uint8_t mscStart(void) { - ledInit(statusLedConfig()); - //Start USB usbGenerateDisconnectPulse(); diff --git a/src/main/fc/init.c b/src/main/fc/init.c index 246faa4c01..aeccfb4aa0 100644 --- a/src/main/fc/init.c +++ b/src/main/fc/init.c @@ -121,6 +121,7 @@ #include "io/vtx_smartaudio.h" #include "io/vtx_tramp.h" +#include "msc/emfat_file.h" #ifdef USE_PERSISTENT_MSC_RTC #include "msc/usbd_storage.h" #endif @@ -611,6 +612,16 @@ void init(void) * so there is no bottleneck in reading and writing data */ mscInit(); if (mscCheckBoot() || mscCheckButton()) { + ledInit(statusLedConfig()); + +#if defined(USE_FLASHFS) && defined(USE_FLASH_CHIP) + // If the blackbox device is onboard flash, then initialize and scan + // it to identify the log files *before* starting the USB device to + // prevent timeouts of the mass storage device. + if (blackboxConfig()->device == BLACKBOX_DEVICE_FLASH) { + emfat_init_files(); + } +#endif if (mscStart() == 0) { mscWaitForButton(); } else { diff --git a/src/main/msc/emfat_file.c b/src/main/msc/emfat_file.c index e52975ed52..25d551e627 100644 --- a/src/main/msc/emfat_file.c +++ b/src/main/msc/emfat_file.c @@ -31,8 +31,14 @@ #include "common/time.h" #include "common/utils.h" +#include "drivers/flash.h" +#include "drivers/light_led.h" +#include "drivers/time.h" + #include "io/flashfs.h" +#include "pg/flash.h" + #include "msc/usbd_storage.h" #define FILESYSTEM_SIZE_MB 256 @@ -300,9 +306,18 @@ static void emfat_add_log(emfat_entry_t *entry, int number, uint32_t offset, uin entry->cma_time[2] = entry->cma_time[0]; } -static int emfat_find_log(emfat_entry_t *entry, int maxCount) +static void blinkStatusLed(void) +{ + static timeMs_t nextToggleMs = 0; + const timeMs_t nowMs = millis(); + if (nowMs > nextToggleMs) { + LED0_TOGGLE; + nextToggleMs = nowMs + 50; // toggle the status LED every 50ms + } +} + +static int emfat_find_log(emfat_entry_t *entry, int maxCount, int flashfsUsedSpace) { - int limit = flashfsIdentifyStartOfFreeSpace(); int lastOffset = 0; int currOffset = 0; int buffOffset; @@ -316,8 +331,9 @@ static int emfat_find_log(emfat_entry_t *entry, int maxCount) int lenTimeHeader = strlen(timeHeader); int timeHeaderMatched = 0; - for ( ; currOffset < limit ; currOffset += 2048) { // XXX 2048 = FREE_BLOCK_SIZE in io/flashfs.c + for ( ; currOffset < flashfsUsedSpace ; currOffset += 2048) { // XXX 2048 = FREE_BLOCK_SIZE in io/flashfs.c + blinkStatusLed(); flashfsReadAbs(currOffset, buffer, HDR_BUF_SIZE); if (strncmp((char *)buffer, logHeader, lenLogHeader)) { @@ -373,7 +389,7 @@ static int emfat_find_log(emfat_entry_t *entry, int maxCount) hdrOffset += HDR_BUF_SIZE; // Check for flash overflow - if (hdrOffset > limit) { + if (hdrOffset > flashfsUsedSpace) { break; } @@ -411,6 +427,12 @@ void emfat_init_files(void) } #endif + flashInit(flashConfig()); + flashfsInit(); + LED0_OFF; + + const int flashfsUsedSpace = flashfsIdentifyStartOfFreeSpace(); + for (size_t i = 0 ; i < PREDEFINED_ENTRY_COUNT ; i++) { entries[i] = entriesPredefined[i]; // These entries have timestamps corresponding to when the filesystem is mounted @@ -418,7 +440,7 @@ void emfat_init_files(void) } // Detect and create entries for each individual log - const int logCount = emfat_find_log(&entries[PREDEFINED_ENTRY_COUNT], EMFAT_MAX_LOG_ENTRY); + const int logCount = emfat_find_log(&entries[PREDEFINED_ENTRY_COUNT], EMFAT_MAX_LOG_ENTRY, flashfsUsedSpace); int entryIndex = PREDEFINED_ENTRY_COUNT + logCount; @@ -427,7 +449,7 @@ void emfat_init_files(void) // allow downloading the entire log in one file entries[entryIndex] = entriesPredefined[PREDEFINED_ENTRY_COUNT]; entry = &entries[entryIndex]; - entry->curr_size = flashfsIdentifyStartOfFreeSpace(); + entry->curr_size = flashfsUsedSpace; entry->max_size = entry->curr_size; // This entry has timestamps corresponding to when the filesystem is mounted emfat_set_entry_cma(entry); @@ -435,13 +457,16 @@ void emfat_init_files(void) } // Padding file to fill out the filesystem size to FILESYSTEM_SIZE_MB - entries[entryIndex] = entriesPredefined[PREDEFINED_ENTRY_COUNT + 1]; - entry = &entries[entryIndex]; - // used space is doubled because of the individual files plus the single complete file - entry->curr_size = (FILESYSTEM_SIZE_MB * 1024 * 1024) - (flashfsIdentifyStartOfFreeSpace() * 2); - entry->max_size = entry->curr_size; - // This entry has timestamps corresponding to when the filesystem is mounted - emfat_set_entry_cma(entry); + if (flashfsUsedSpace * 2 < FILESYSTEM_SIZE_MB * 1024 * 1024) { + entries[entryIndex] = entriesPredefined[PREDEFINED_ENTRY_COUNT + 1]; + entry = &entries[entryIndex]; + // used space is doubled because of the individual files plus the single complete file + entry->curr_size = (FILESYSTEM_SIZE_MB * 1024 * 1024) - (flashfsUsedSpace * 2); + entry->max_size = entry->curr_size; + // This entry has timestamps corresponding to when the filesystem is mounted + emfat_set_entry_cma(entry); + } emfat_init(&emfat, "BETAFLT", entries); + LED0_OFF; } diff --git a/src/main/msc/usbd_storage_emfat.c b/src/main/msc/usbd_storage_emfat.c index d7cebc5f63..66656c0f17 100644 --- a/src/main/msc/usbd_storage_emfat.c +++ b/src/main/msc/usbd_storage_emfat.c @@ -33,16 +33,9 @@ #include "common/utils.h" #include "drivers/light_led.h" -#include "drivers/time.h" -#include "drivers/flash.h" -#include "io/flashfs.h" - -#include "pg/flash.h" - -#include "usbd_storage.h" -#include "usbd_storage_emfat.h" -#include "emfat_file.h" +#include "msc/usbd_storage.h" +#include "msc/usbd_storage_emfat.h" #define STORAGE_LUN_NBR 1 @@ -66,18 +59,6 @@ static int8_t STORAGE_Init(uint8_t lun) { UNUSED(lun); - LED0_ON; - -#ifdef USE_FLASHFS -#ifdef USE_FLASH_CHIP - flashInit(flashConfig()); -#endif - flashfsInit(); -#endif - emfat_init_files(); - - delay(1000); - LED0_OFF; return 0;