From 632e4611bf4d2f0fd1edfb5e971c43eb7cbe5fcb Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 27 Mar 2020 17:36:51 +0700 Subject: [PATCH] rename FLASH_SIZE to CFG_UF2_FLASH_SIZE, make it whole flash = 1MB also rename UF2_NUM_BLOCKS to CFG_UF2_NUM_BLOCKS --- src/usb/msc_uf2.c | 2 +- src/usb/uf2/ghostfat.c | 18 ++++++++++++++---- src/usb/uf2/uf2.h | 2 +- src/usb/uf2/uf2cfg.h | 4 ++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/usb/msc_uf2.c b/src/usb/msc_uf2.c index 07bedef..6bd0626 100644 --- a/src/usb/msc_uf2.c +++ b/src/usb/msc_uf2.c @@ -194,7 +194,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz { (void) lun; - *block_count = UF2_NUM_BLOCKS; + *block_count = CFG_UF2_NUM_BLOCKS; *block_size = 512; } diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index 8582bfc..97b4211 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -61,7 +61,7 @@ struct TextFile { // //--------------------------------------------------------------------+ -#define NUM_FAT_BLOCKS UF2_NUM_BLOCKS +#define NUM_FAT_BLOCKS CFG_UF2_NUM_BLOCKS #define STR0(x) #x #define STR(x) STR0(x) @@ -200,7 +200,7 @@ static uint32_t current_flash_size(void) // use maximum application size if ( (flash_sz == 0) || (flash_sz == 0xFFFFFFFFUL) ) { - flash_sz = FLASH_SIZE; + flash_sz = (USER_FLASH_END-USER_FLASH_START); } } @@ -299,7 +299,7 @@ void read_block(uint32_t block_no, uint8_t *data) { } else { // generate the UF2 file data on-the-fly sectionIdx -= NUM_FILES - 1; uint32_t addr = USER_FLASH_START + sectionIdx * 256; - if (addr < USER_FLASH_START+FLASH_SIZE) { + if (addr < CFG_UF2_FLASH_SIZE) { UF2_Block *bl = (void *)data; bl->magicStart0 = UF2_MAGIC_START0; bl->magicStart1 = UF2_MAGIC_START1; @@ -320,7 +320,14 @@ void read_block(uint32_t block_no, uint8_t *data) { /* Write UF2 *------------------------------------------------------------------*/ -/** Write an block +/** + * Write an uf2 block wrapped by 512 sector. Writing behavior is different when upgrading: + * - Application + * - current App is erased and flash with new firmware with same starting address + * + * - SoftDevice + Bootloader + * - Current App is erased, contents of SD + bootloader is written to App starting address + * - Trigger the SD + Bootloader migration * * @return number of bytes processed, only 3 following values * -1 : if not an uf2 block @@ -341,6 +348,9 @@ int write_block (uint32_t block_no, uint8_t *data, WriteState *state) return -1; } + +// if ( (bl->familyID == CFG_UF2_FAMILY_ID) && in_app_space(bl->targetAddr) ) + if ( (bl->targetAddr < USER_FLASH_START) || (bl->targetAddr + bl->payloadSize > USER_FLASH_END) ) { diff --git a/src/usb/uf2/uf2.h b/src/usb/uf2/uf2.h index 5be1c95..92e5ea3 100644 --- a/src/usb/uf2/uf2.h +++ b/src/usb/uf2/uf2.h @@ -51,7 +51,7 @@ SOFTWARE. #define UF2_FLAG_NOFLASH 0x00000001 #define UF2_FLAG_FAMILYID 0x00002000 -#define MAX_BLOCKS (FLASH_SIZE / 256 + 100) +#define MAX_BLOCKS (CFG_UF2_FLASH_SIZE / 256 + 100) typedef struct { uint32_t numBlocks; uint32_t numWritten; diff --git a/src/usb/uf2/uf2cfg.h b/src/usb/uf2/uf2cfg.h index 27ebbaf..689706a 100644 --- a/src/usb/uf2/uf2cfg.h +++ b/src/usb/uf2/uf2cfg.h @@ -1,7 +1,7 @@ #include "boards.h" -#define UF2_NUM_BLOCKS 8000 // at least 4,1 MB for FAT16 -#define FLASH_SIZE (USER_FLASH_END-USER_FLASH_START) // Max flash size +#define CFG_UF2_NUM_BLOCKS 8000 // at least 4,1 MB for FAT16 +#define CFG_UF2_FLASH_SIZE (1024*1024) // 1 MB // Only allow to write application TODO dynamic depending on SD size #define USER_FLASH_START (SD_FLASH_SIZE + MBR_SIZE)