From 52c710bb6242b02a472e35359f2f5d8efb9cb65b Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 4 Feb 2020 18:36:38 -0800 Subject: [PATCH] move flash addr in to mpu_util (#1123) * move flash addr in to mpu_util * this _may_ work * don't use offsetof * proper header + defines --- firmware/config/boards/kinetis/efifeatures.h | 12 +----------- .../controllers/algo/engine_configuration.cpp | 3 ++- firmware/controllers/flash_main.cpp | 19 ++++++------------- firmware/hw_layer/flash.h | 10 +++------- firmware/hw_layer/ports/kinetis/mpu_util.cpp | 16 ++++++++++++++++ .../hw_layer/ports/stm32/stm32f4/mpu_util.cpp | 8 ++++++++ .../hw_layer/ports/stm32/stm32f7/mpu_util.cpp | 7 +++++++ 7 files changed, 43 insertions(+), 32 deletions(-) diff --git a/firmware/config/boards/kinetis/efifeatures.h b/firmware/config/boards/kinetis/efifeatures.h index 3fbebb0e8c..ab140bd599 100644 --- a/firmware/config/boards/kinetis/efifeatures.h +++ b/firmware/config/boards/kinetis/efifeatures.h @@ -105,16 +105,6 @@ #define EFI_INTERNAL_FLASH TRUE -/** - * Flex Non Volatile Memory is faster than flash - * It also has smaller pages so it takes less time to erase - * - * There is no remote access to FlexNVM meaning that we cannot erase settings externally - */ - -#define FLASH_ADDR 0x10000000 // FlexNVM -#define FLASH_ADDR_SECOND_COPY 0x10008000 - /** * Usually you need shaft position input, but maybe you do not need it? */ @@ -406,7 +396,7 @@ #define EFI_PRINT_ERRORS_AS_WARNINGS TRUE #define EFI_PRINT_MESSAGES_TO_TERMINAL TRUE -#define EFI_ACTIVE_CONFIGURATION_IN_FLASH (FLASH_ADDR + offsetof(persistent_config_container_s, persistentConfiguration.engineConfiguration)) +#define EFI_ACTIVE_CONFIGURATION_IN_FLASH //#define PWM_PHASE_MAX_COUNT 122 diff --git a/firmware/controllers/algo/engine_configuration.cpp b/firmware/controllers/algo/engine_configuration.cpp index 498816ced9..8d5c64acf3 100644 --- a/firmware/controllers/algo/engine_configuration.cpp +++ b/firmware/controllers/algo/engine_configuration.cpp @@ -142,7 +142,8 @@ static fuel_table_t alphaNfuel = { * todo: place this field next to 'engineConfiguration'? */ #ifdef EFI_ACTIVE_CONFIGURATION_IN_FLASH -engine_configuration_s & activeConfiguration = *(engine_configuration_s *)EFI_ACTIVE_CONFIGURATION_IN_FLASH; +#include "flash.h" +engine_configuration_s & activeConfiguration = reinterpret_cast(getFlashAddrFirstCopy())->persistentConfiguration.engineConfiguration; // we cannot use this activeConfiguration until we call rememberCurrentConfiguration() bool isActiveConfigurationVoid = true; #else diff --git a/firmware/controllers/flash_main.cpp b/firmware/controllers/flash_main.cpp index af2f34567d..772808599e 100644 --- a/firmware/controllers/flash_main.cpp +++ b/firmware/controllers/flash_main.cpp @@ -40,10 +40,6 @@ extern engine_configuration_s *engineConfiguration; * todo: an ideal solution would be to define this address in the .ld / .icf mapping file */ -#ifndef FLASH_ADDR -#define FLASH_ADDR 0x080E0000 -#endif - #define PERSISTENT_SIZE sizeof(persistent_config_container_s) /** @@ -52,9 +48,6 @@ extern engine_configuration_s *engineConfiguration; * In order to preserve at least one copy of the tune in case of electrical issues address of second configuration copy * should be in a different sector of flash since complete flash sectors are erased on write. */ -#ifndef FLASH_ADDR_SECOND_COPY -#define FLASH_ADDR_SECOND_COPY 0x080C0000 -#endif crc_t flashStateCrc(persistent_config_container_s *state) { return calc_crc((const crc_t*) &state->persistentConfiguration, sizeof(persistent_config_s)); @@ -87,12 +80,12 @@ void writeToFlashNow(void) { crc_t crcResult = flashStateCrc(&persistentState); persistentState.value = crcResult; scheduleMsg(logger, "Reseting flash: size=%d", PERSISTENT_SIZE); - flashErase(FLASH_ADDR, PERSISTENT_SIZE); + flashErase(getFlashAddrFirstCopy(), PERSISTENT_SIZE); scheduleMsg(logger, "Flashing with CRC=%d", crcResult); efitimems_t nowMs = currentTimeMillis(); - int result = flashWrite(FLASH_ADDR, (const char *) &persistentState, PERSISTENT_SIZE); - flashErase(FLASH_ADDR_SECOND_COPY, PERSISTENT_SIZE); - flashWrite(FLASH_ADDR_SECOND_COPY, (const char *) &persistentState, PERSISTENT_SIZE); + int result = flashWrite(getFlashAddrFirstCopy(), (const char *) &persistentState, PERSISTENT_SIZE); + flashErase(getFlashAddrSecondCopy(), PERSISTENT_SIZE); + flashWrite(getFlashAddrSecondCopy(), (const char *) &persistentState, PERSISTENT_SIZE); scheduleMsg(logger, "Flash programmed in %dms", currentTimeMillis() - nowMs); bool isSuccess = result == FLASH_RETURN_SUCCESS; if (isSuccess) { @@ -135,10 +128,10 @@ static persisted_configuration_state_e doReadConfiguration(flashaddr_t address, */ persisted_configuration_state_e readConfiguration(Logging * logger) { efiAssert(CUSTOM_ERR_ASSERT, getCurrentRemainingStack() > EXPECTED_REMAINING_STACK, "read f", PC_ERROR); - persisted_configuration_state_e result = doReadConfiguration(FLASH_ADDR, logger); + persisted_configuration_state_e result = doReadConfiguration(getFlashAddrFirstCopy(), logger); if (result != PC_OK) { printMsg(logger, "Reading second configuration copy"); - result = doReadConfiguration(FLASH_ADDR_SECOND_COPY, logger); + result = doReadConfiguration(getFlashAddrSecondCopy(), logger); } if (result == CRC_FAILED) { diff --git a/firmware/hw_layer/flash.h b/firmware/hw_layer/flash.h index 94f913889f..f5a15dd87c 100644 --- a/firmware/hw_layer/flash.h +++ b/firmware/hw_layer/flash.h @@ -7,13 +7,6 @@ #include "global.h" -/** - * @brief Number of sectors in the flash memory. - */ -#if !defined(FLASH_SECTOR_COUNT) -#define FLASH_SECTOR_COUNT 12 -#endif - /* Error codes */ /** @brief Flash operation successful */ @@ -73,6 +66,9 @@ typedef uint8_t flashsector_t; */ size_t flashSectorSize(flashsector_t sector); +uintptr_t getFlashAddrFirstCopy(void); +uintptr_t getFlashAddrSecondCopy(void); + /** * @brief Get the beginning address of @p sector. * @param sector Sector to retrieve the beginning address of. diff --git a/firmware/hw_layer/ports/kinetis/mpu_util.cpp b/firmware/hw_layer/ports/kinetis/mpu_util.cpp index a3506b411c..185404e4c0 100644 --- a/firmware/hw_layer/ports/kinetis/mpu_util.cpp +++ b/firmware/hw_layer/ports/kinetis/mpu_util.cpp @@ -241,5 +241,21 @@ size_t flashSectorSize(flashsector_t sector) { return 0; } + +/** + * Flex Non Volatile Memory is faster than flash + * It also has smaller pages so it takes less time to erase + * + * There is no remote access to FlexNVM meaning that we cannot erase settings externally + */ + +uintptr_t getFlashAddrFirstCopy() { + return 0x10000000; +} + +uintptr_t getFlashAddrSecondCopy() { + return 0x10008000; +} + #endif /* EFI_PROD_CODE */ diff --git a/firmware/hw_layer/ports/stm32/stm32f4/mpu_util.cpp b/firmware/hw_layer/ports/stm32/stm32f4/mpu_util.cpp index 91c0ba0413..88983eb2e7 100644 --- a/firmware/hw_layer/ports/stm32/stm32f4/mpu_util.cpp +++ b/firmware/hw_layer/ports/stm32/stm32f4/mpu_util.cpp @@ -439,5 +439,13 @@ size_t flashSectorSize(flashsector_t sector) { return 0; } +uintptr_t getFlashAddrFirstCopy() { + return 0x080E0000; +} + +uintptr_t getFlashAddrSecondCopy() { + return 0x080C0000; +} + #endif /* EFI_PROD_CODE */ diff --git a/firmware/hw_layer/ports/stm32/stm32f7/mpu_util.cpp b/firmware/hw_layer/ports/stm32/stm32f7/mpu_util.cpp index 647181b6e1..f6f412bc50 100644 --- a/firmware/hw_layer/ports/stm32/stm32f7/mpu_util.cpp +++ b/firmware/hw_layer/ports/stm32/stm32f7/mpu_util.cpp @@ -428,3 +428,10 @@ size_t flashSectorSize(flashsector_t sector) { return 0; } +uintptr_t getFlashAddrFirstCopy() { + return 0x08100000; +} + +uintptr_t getFlashAddrSecondCopy() { + return 0x08140000; +}