From c989d9c4ba2fa711054d6ca7d61bf24ddd88691d Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 24 Jun 2018 16:43:13 +0700 Subject: [PATCH] improve uf2 get flash size --- src/usb/uf2/ghostfat.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index 94a07bf..cb5aab0 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -126,13 +126,25 @@ bool hadWrite = false; static uint32_t get_flash_size(void) { - // return 1 block of 256 bytes - if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) ) return 256; + static uint32_t flash_sz = 0; - const bootloader_settings_t * boot_setting; - bootloader_util_settings_get(&boot_setting); + // only need to compute once + if ( flash_sz == 0 ) + { + // return 1 block of 256 bytes + if ( !bootloader_app_is_valid(DFU_BANK_0_REGION_START) ) + { + flash_sz = 256; + }else + { + const bootloader_settings_t * boot_setting; + bootloader_util_settings_get(&boot_setting); - return boot_setting->bank_0_size; + flash_sz = boot_setting->bank_0_size; + } + } + + return flash_sz; } #if 0