From d9bda2800db90bab276002937e2bf80ae338f6a0 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Wed, 8 Nov 2017 09:02:21 +0100 Subject: [PATCH 1/2] STM32F4: Flash: fix 2MB GetSector computation The computation of the sector number in GetSector for 2MB flash sizes consists in adding an extra constant for 2MB dual bank (12 as in ADDR_FLASH_SECTOR_12), then going through the same address check as if it was an address of the first sector. That means that we need to modify tmp and address as if it was a first bank address, which we're doing here by sub_stracting 0x100000 delta. --- targets/TARGET_STM/TARGET_STM32F4/flash_api.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/flash_api.c b/targets/TARGET_STM/TARGET_STM32F4/flash_api.c index 1455f07aab..f3f1be95ac 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F4/flash_api.c @@ -180,8 +180,10 @@ static uint32_t GetSector(uint32_t address) /* This function supports 1Mb and 2Mb flash sizes */ #if defined(ADDR_FLASH_SECTOR_16) if (address & 0x100000) { // handle 2nd bank + /* Sector will be at least 12 */ sector = FLASH_SECTOR_12; - tmp = address - ADDR_FLASH_SECTOR_12; + tmp -= 0x100000; + address -= 0x100000; } #endif if (address < ADDR_FLASH_SECTOR_4) { // 16k sectorsize From 48a49b4a007f311c7746c609158cb0382576fd18 Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Wed, 8 Nov 2017 09:02:39 +0100 Subject: [PATCH 2/2] STM32: remove trailing white spaces --- targets/TARGET_STM/TARGET_STM32F4/flash_api.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/flash_api.c b/targets/TARGET_STM/TARGET_STM32F4/flash_api.c index f3f1be95ac..5ba7fb9e49 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F4/flash_api.c @@ -145,7 +145,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) { - if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { return MBED_FLASH_INVALID_SIZE; } @@ -165,7 +164,7 @@ uint32_t flash_get_start_address(const flash_t *obj) } uint32_t flash_get_size(const flash_t *obj) { - return FLASH_SIZE; + return FLASH_SIZE; } /** @@ -175,7 +174,7 @@ uint32_t flash_get_size(const flash_t *obj) */ static uint32_t GetSector(uint32_t address) { - uint32_t sector = 0; + uint32_t sector = 0; uint32_t tmp = address - ADDR_FLASH_SECTOR_0; /* This function supports 1Mb and 2Mb flash sizes */ #if defined(ADDR_FLASH_SECTOR_16) @@ -191,14 +190,14 @@ static uint32_t GetSector(uint32_t address) } #if defined(ADDR_FLASH_SECTOR_5) else if (address < ADDR_FLASH_SECTOR_5) { //64k sector size - sector += FLASH_SECTOR_4; + sector += FLASH_SECTOR_4; } else { sector += 4 + (tmp >>17); } #else // In case ADDR_FLASH_SECTOR_5 is not defined, sector 4 is the last one. else { //64k sector size - sector += FLASH_SECTOR_4; + sector += FLASH_SECTOR_4; } #endif return sector; @@ -227,7 +226,7 @@ if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_ sectorsize = 64 * 1024; } else { sectorsize = 128 * 1024; - } + } return sectorsize; }