From 837f24f098c5216bde9d4e881f3b3639a319a303 Mon Sep 17 00:00:00 2001 From: Hydra Date: Sun, 2 Apr 2017 10:08:25 +0100 Subject: [PATCH] fix wrong flash sector on F4 targets. --- src/main/config/config_eeprom.c | 58 ++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/main/config/config_eeprom.c b/src/main/config/config_eeprom.c index 5c32f18183..21047bdf19 100644 --- a/src/main/config/config_eeprom.c +++ b/src/main/config/config_eeprom.c @@ -224,6 +224,58 @@ void writeEEPROM(void) resumeRxSignal(); } #else + + +#if defined(STM32F4) +/* +Sector 0 0x08000000 - 0x08003FFF 16 Kbytes +Sector 1 0x08004000 - 0x08007FFF 16 Kbytes +Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes +Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes +Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes +Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes +Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes +Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes +Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes +Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes +Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes +Sector 11 0x080E0000 - 0x080FFFFF 128 Kbytes +*/ + +static uint32_t getFLASHSectorForEEPROM(void) +{ + if (CONFIG_START_FLASH_ADDRESS <= 0x08003FFF) + return FLASH_Sector_0; + if (CONFIG_START_FLASH_ADDRESS <= 0x08007FFF) + return FLASH_Sector_1; + if (CONFIG_START_FLASH_ADDRESS <= 0x0800BFFF) + return FLASH_Sector_2; + if (CONFIG_START_FLASH_ADDRESS <= 0x0800FFFF) + return FLASH_Sector_3; + if (CONFIG_START_FLASH_ADDRESS <= 0x0801FFFF) + return FLASH_Sector_4; + if (CONFIG_START_FLASH_ADDRESS <= 0x0803FFFF) + return FLASH_Sector_5; + if (CONFIG_START_FLASH_ADDRESS <= 0x0805FFFF) + return FLASH_Sector_6; + if (CONFIG_START_FLASH_ADDRESS <= 0x0807FFFF) + return FLASH_Sector_7; + if (CONFIG_START_FLASH_ADDRESS <= 0x0809FFFF) + return FLASH_Sector_8; + if (CONFIG_START_FLASH_ADDRESS <= 0x080DFFFF) + return FLASH_Sector_9; + if (CONFIG_START_FLASH_ADDRESS <= 0x080BFFFF) + return FLASH_Sector_10; + if (CONFIG_START_FLASH_ADDRESS <= 0x080FFFFF) + return FLASH_Sector_11; + + // Not good + while (1) { + failureMode(FAILURE_FLASH_WRITE_FAILED); + } +} +#endif + void writeEEPROM(void) { // Generate compile time error if the config does not fit in the reserved area of flash. @@ -255,10 +307,8 @@ void writeEEPROM(void) #endif for (wordOffset = 0; wordOffset < sizeof(master_t); wordOffset += 4) { if (wordOffset % FLASH_PAGE_SIZE == 0) { -#if defined(STM32F40_41xxx) - status = FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3); //0x08080000 to 0x080A0000 -#elif defined (STM32F411xE) - status = FLASH_EraseSector(FLASH_Sector_7, VoltageRange_3); //0x08060000 to 0x08080000 +#if defined(STM32F4) + status = FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3); //0x08060000 to 0x08080000 #else status = FLASH_ErasePage(CONFIG_START_FLASH_ADDRESS + wordOffset); #endif