From 02432af6fa855ca39f27419fb8524ca21ea0a5ad Mon Sep 17 00:00:00 2001 From: Jay Blackman Date: Mon, 6 Jan 2025 19:36:24 +1100 Subject: [PATCH] Move STM (and clone) related config in flash implementation to platform (#14103) --- src/main/config/config_eeprom.c | 16 +- src/main/config/config_eeprom_impl.h | 25 + src/main/config/config_streamer.c | 486 ++------------------ src/main/config/config_streamer.h | 62 ++- src/main/config/config_streamer_impl.h | 29 ++ src/main/drivers/system.h | 2 + src/platform/APM32/mk/APM32F4.mk | 4 +- src/platform/APM32/platform_mcu.h | 4 + src/platform/AT32/mk/AT32F4.mk | 1 + src/platform/AT32/platform_mcu.h | 2 + src/platform/SIMULATOR/sitl.c | 43 +- src/platform/SIMULATOR/target/SITL/target.h | 14 - src/platform/STM32/mk/STM32F4.mk | 1 + src/platform/STM32/mk/STM32F7.mk | 2 - src/platform/STM32/mk/STM32G4.mk | 2 - src/platform/STM32/mk/STM32H5.mk | 2 - src/platform/STM32/mk/STM32H7.mk | 2 - src/platform/STM32/mk/STM32_COMMON.mk | 4 + src/platform/STM32/platform_mcu.h | 12 + src/platform/common/stm32/config_flash.c | 475 +++++++++++++++++++ 20 files changed, 653 insertions(+), 535 deletions(-) create mode 100644 src/main/config/config_eeprom_impl.h create mode 100644 src/main/config/config_streamer_impl.h create mode 100644 src/platform/common/stm32/config_flash.c diff --git a/src/main/config/config_eeprom.c b/src/main/config/config_eeprom.c index f77586d3d8..83e2d79fa2 100644 --- a/src/main/config/config_eeprom.c +++ b/src/main/config/config_eeprom.c @@ -30,7 +30,10 @@ #include "common/utils.h" #include "config/config_eeprom.h" +#include "config/config_eeprom_impl.h" #include "config/config_streamer.h" +#include "config/config_streamer_impl.h" + #include "pg/pg.h" #include "config/config.h" @@ -283,13 +286,6 @@ bool loadEEPROMFromSDCard(void) } #endif -#ifdef CONFIG_IN_FILE -void loadEEPROMFromFile(void) -{ - FLASH_Unlock(); // load existing config file into eepromData -} -#endif - void initEEPROM(void) { // Verify that this architecture packs as expected. @@ -301,7 +297,11 @@ void initEEPROM(void) STATIC_ASSERT(sizeof(configRecord_t) == 6, record_size_failed); #if defined(CONFIG_IN_FILE) - loadEEPROMFromFile(); + bool eepromLoaded = loadEEPROMFromFile(); + if (!eepromLoaded) { + // File read failed - just die now + failureMode(FAILURE_FILE_READ_FAILED); + } #elif defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_MEMORY_MAPPED_FLASH) bool eepromLoaded = loadEEPROMFromExternalFlash(); if (!eepromLoaded) { diff --git a/src/main/config/config_eeprom_impl.h b/src/main/config/config_eeprom_impl.h new file mode 100644 index 0000000000..40ec540bfe --- /dev/null +++ b/src/main/config/config_eeprom_impl.h @@ -0,0 +1,25 @@ +/* + * This file is part of Betaflight. + * + * Betaflight is free software. You can redistribute this software + * and/or modify this software under the terms of the GNU General + * Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later + * version. + * + * Betaflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this software. + * + * If not, see . + */ + +#pragma once + +// TODO: potentially move sdcard and external flash also +bool loadEEPROMFromFile(void); diff --git a/src/main/config/config_streamer.c b/src/main/config/config_streamer.c index fc5ab25696..d519d61b42 100644 --- a/src/main/config/config_streamer.c +++ b/src/main/config/config_streamer.c @@ -27,6 +27,7 @@ #include "config/config_eeprom.h" #include "config/config_streamer.h" +#include "config/config_streamer_impl.h" #if !defined(CONFIG_IN_FLASH) #if defined(CONFIG_IN_RAM) && defined(PERSISTENT) @@ -45,7 +46,7 @@ void config_streamer_init(config_streamer_t *c) memset(c, 0, sizeof(*c)); } -void config_streamer_start(config_streamer_t *c, uintptr_t base, int size) +void config_streamer_start(config_streamer_t *c, uintptr_t base, size_t size) { // base must start at FLASH_PAGE_SIZE boundary when using embedded flash. c->address = base; @@ -53,16 +54,8 @@ void config_streamer_start(config_streamer_t *c, uintptr_t base, int size) if (!c->unlocked) { #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD) // NOP -#elif defined(CONFIG_IN_FLASH) || defined(CONFIG_IN_FILE) -#if defined(STM32F7) || defined(STM32H7) || defined(STM32G4) - HAL_FLASH_Unlock(); -#elif defined(APM32F4) - DAL_FLASH_Unlock(); -#elif defined(AT32F4) - flash_unlock(); -#else - FLASH_Unlock(); -#endif +#elif defined(CONFIG_IN_FILE) || defined(CONFIG_IN_FLASH) + configUnlock(); #endif c->unlocked = true; } @@ -70,314 +63,20 @@ void config_streamer_start(config_streamer_t *c, uintptr_t base, int size) #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_FILE) || defined(CONFIG_IN_EXTERNAL_FLASH) // NOP #elif defined(CONFIG_IN_FLASH) -#if defined(STM32F4) - FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); -#elif defined(STM32F7) - // NOP -#elif defined(STM32H7) - // NOP -#elif defined(STM32G4) - // NOP -#elif defined(AT32F4) - flash_flag_clear(FLASH_ODF_FLAG | FLASH_PRGMERR_FLAG | FLASH_EPPERR_FLAG); -#elif defined(APM32F4) - __DAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); -#elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD) - // NOP -#else -# error "Unsupported CPU" + configClearFlags(); #endif -#endif - c->err = 0; + c->err = CONFIG_RESULT_SUCCESS; } #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD) // No flash sector method required. #elif defined(CONFIG_IN_FLASH) -#if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx) -/* -Sector 0 0x08000000 - 0x08007FFF 32 Kbytes -Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes -Sector 2 0x08010000 - 0x08017FFF 32 Kbytes -Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes -Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes -Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes -Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes -Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes - -F7X5XI device with 2M flash -Sector 8 0x08100000 - 0x0813FFFF 256 Kbytes -Sector 9 0x08140000 - 0x0817FFFF 256 Kbytes -Sector 10 0x08180000 - 0x081BFFFF 256 Kbytes -Sector 11 0x081C0000 - 0x081FFFFF 256 Kbytes -*/ - -static uint32_t getFLASHSectorForEEPROM(void) -{ - if ((uint32_t)&__config_start <= 0x08007FFF) - return FLASH_SECTOR_0; - if ((uint32_t)&__config_start <= 0x0800FFFF) - return FLASH_SECTOR_1; - if ((uint32_t)&__config_start <= 0x08017FFF) - return FLASH_SECTOR_2; - if ((uint32_t)&__config_start <= 0x0801FFFF) - return FLASH_SECTOR_3; - if ((uint32_t)&__config_start <= 0x0803FFFF) - return FLASH_SECTOR_4; - if ((uint32_t)&__config_start <= 0x0807FFFF) - return FLASH_SECTOR_5; - if ((uint32_t)&__config_start <= 0x080BFFFF) - return FLASH_SECTOR_6; - if ((uint32_t)&__config_start <= 0x080FFFFF) - return FLASH_SECTOR_7; -#if defined(STM32F765xx) - if ((uint32_t)&__config_start <= 0x0813FFFF) - return FLASH_SECTOR_8; - if ((uint32_t)&__config_start <= 0x0817FFFF) - return FLASH_SECTOR_9; - if ((uint32_t)&__config_start <= 0x081BFFFF) - return FLASH_SECTOR_10; - if ((uint32_t)&__config_start <= 0x081FFFFF) - return FLASH_SECTOR_11; +uint32_t getFLASHSectorForEEPROM(void); #endif - // Not good - while (1) { - failureMode(FAILURE_CONFIG_STORE_FAILURE); - } -} - -#elif defined(STM32F722xx) -/* -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 -*/ - -static uint32_t getFLASHSectorForEEPROM(void) +static int write_word(config_streamer_t *c, config_streamer_buffer_type_t *buffer) { - if ((uint32_t)&__config_start <= 0x08003FFF) - return FLASH_SECTOR_0; - if ((uint32_t)&__config_start <= 0x08007FFF) - return FLASH_SECTOR_1; - if ((uint32_t)&__config_start <= 0x0800BFFF) - return FLASH_SECTOR_2; - if ((uint32_t)&__config_start <= 0x0800FFFF) - return FLASH_SECTOR_3; - if ((uint32_t)&__config_start <= 0x0801FFFF) - return FLASH_SECTOR_4; - if ((uint32_t)&__config_start <= 0x0803FFFF) - return FLASH_SECTOR_5; - if ((uint32_t)&__config_start <= 0x0805FFFF) - return FLASH_SECTOR_6; - if ((uint32_t)&__config_start <= 0x0807FFFF) - return FLASH_SECTOR_7; - - // Not good - while (1) { - failureMode(FAILURE_CONFIG_STORE_FAILURE); - } -} - -#elif 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 ((uint32_t)&__config_start <= 0x08003FFF) - return FLASH_Sector_0; - if ((uint32_t)&__config_start <= 0x08007FFF) - return FLASH_Sector_1; - if ((uint32_t)&__config_start <= 0x0800BFFF) - return FLASH_Sector_2; - if ((uint32_t)&__config_start <= 0x0800FFFF) - return FLASH_Sector_3; - if ((uint32_t)&__config_start <= 0x0801FFFF) - return FLASH_Sector_4; - if ((uint32_t)&__config_start <= 0x0803FFFF) - return FLASH_Sector_5; - if ((uint32_t)&__config_start <= 0x0805FFFF) - return FLASH_Sector_6; - if ((uint32_t)&__config_start <= 0x0807FFFF) - return FLASH_Sector_7; - if ((uint32_t)&__config_start <= 0x0809FFFF) - return FLASH_Sector_8; - if ((uint32_t)&__config_start <= 0x080DFFFF) - return FLASH_Sector_9; - if ((uint32_t)&__config_start <= 0x080BFFFF) - return FLASH_Sector_10; - if ((uint32_t)&__config_start <= 0x080FFFFF) - return FLASH_Sector_11; - - // Not good - while (1) { - failureMode(FAILURE_CONFIG_STORE_FAILURE); - } -} - -#elif defined(STM32H743xx) || defined(STM32G4) || defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H723xx) || defined(STM32H725xx) -/* -MCUs with uniform array of equal size sectors, handled in two banks having contiguous address. -(Devices with non-contiguous flash layout is not currently useful anyways.) - -H743 -2 bank * 8 sector/bank * 128K/sector (2MB) -Bank 1 0x08000000 - 0x080FFFFF 128KB * 8 -Bank 2 0x08100000 - 0x081FFFFF 128KB * 8 - -H743 -1 bank * 8 sector/bank * 128K/sector (1MB) -Bank 1 0x08000000 - 0x080FFFFF 128KB * 8 - -H7A3 -2 bank * 128 sector/bank * 8KB/sector (2MB) -Bank 1 0x08000000 - 0x080FFFFF 8KB * 128 -Bank 2 0x08100000 - 0x081FFFFF 8KB * 128 - -G473/474 in dual bank mode -2 bank * 128 sector/bank * 2KB/sector (512KB) -Bank 1 0x08000000 - 0x0803FFFF 2KB * 128 -Bank 2 0x08040000 - 0x0807FFFF 2KB * 128 - -Note that FLASH_BANK_SIZE constant used in the following code changes depending on -bank operation mode. The code assumes dual bank operation, in which case the -FLASH_BANK_SIZE constant is set to one half of the available flash size in HAL. -*/ - -#if defined(STM32H743xx) || defined(STM32H723xx) || defined(STM32H725xx) -#define FLASH_PAGE_PER_BANK 8 -#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) -#define FLASH_PAGE_PER_BANK 128 -#elif defined(STM32G4) -#define FLASH_PAGE_PER_BANK 128 -// These are not defined in CMSIS like H7 -#define FLASH_BANK1_BASE FLASH_BASE -#define FLASH_BANK2_BASE (FLASH_BANK1_BASE + FLASH_BANK_SIZE) -#endif - -static void getFLASHSectorForEEPROM(uint32_t address, uint32_t *bank, uint32_t *sector) -{ -#if defined(FLASH_BANK2_BASE) - if (address >= FLASH_BANK1_BASE && address < FLASH_BANK2_BASE) { - *bank = FLASH_BANK_1; - } else if (address >= FLASH_BANK2_BASE && address < FLASH_BANK2_BASE + FLASH_BANK_SIZE) { - *bank = FLASH_BANK_2; - address -= FLASH_BANK_SIZE; - } -#else - if (address >= FLASH_BANK1_BASE && address < FLASH_BANK1_BASE + FLASH_BANK_SIZE) { - *bank = FLASH_BANK_1; - } -#endif - else { - // Not good - while (1) { - failureMode(FAILURE_CONFIG_STORE_FAILURE); - } - } - - address -= FLASH_BANK1_BASE; - *sector = address / FLASH_PAGE_SIZE; -} -#elif defined(STM32H750xx) -/* -The memory map supports 2 banks of 8 128k sectors like the H743xx, but there is only one 128K sector so we save some code -space by using a smaller function. - -Bank 1 -Sector 0 0x08000000 - 0x0801FFFF 128 Kbytes - -*/ - -static void getFLASHSectorForEEPROM(uint32_t *bank, uint32_t *sector) -{ - - uint32_t start = (uint32_t)&__config_start; - - if (start == FLASH_BANK1_BASE) { - *sector = FLASH_SECTOR_0; - *bank = FLASH_BANK_1; - } else { - // Not good - while (1) { - failureMode(FAILURE_CONFIG_STORE_FAILURE); - } - } -} -#elif defined(APM32F4) -/* -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 ((uint32_t)&__config_start <= 0x08003FFF) - return FLASH_SECTOR_0; - if ((uint32_t)&__config_start <= 0x08007FFF) - return FLASH_SECTOR_1; - if ((uint32_t)&__config_start <= 0x0800BFFF) - return FLASH_SECTOR_2; - if ((uint32_t)&__config_start <= 0x0800FFFF) - return FLASH_SECTOR_3; - if ((uint32_t)&__config_start <= 0x0801FFFF) - return FLASH_SECTOR_4; - if ((uint32_t)&__config_start <= 0x0803FFFF) - return FLASH_SECTOR_5; - if ((uint32_t)&__config_start <= 0x0805FFFF) - return FLASH_SECTOR_6; - if ((uint32_t)&__config_start <= 0x0807FFFF) - return FLASH_SECTOR_7; - if ((uint32_t)&__config_start <= 0x0809FFFF) - return FLASH_SECTOR_8; - if ((uint32_t)&__config_start <= 0x080DFFFF) - return FLASH_SECTOR_9; - if ((uint32_t)&__config_start <= 0x080BFFFF) - return FLASH_SECTOR_10; - if ((uint32_t)&__config_start <= 0x080FFFFF) - return FLASH_SECTOR_11; - - // Not good - while (1) { - failureMode(FAILURE_CONFIG_STORE_FAILURE); - } -} - -#endif -#endif // CONFIG_IN_FLASH - -// FIXME the return values are currently magic numbers -static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer) -{ - if (c->err != 0) { + if (c->err != CONFIG_RESULT_SUCCESS) { return c->err; } #if defined(CONFIG_IN_EXTERNAL_FLASH) @@ -392,7 +91,7 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t uint32_t flashAddress = flashStartAddress + dataOffset; if (flashAddress + CONFIG_STREAMER_BUFFER_SIZE > flashOverflowAddress) { - return -3; // address is past end of partition + return CONFIG_RESULT_ADDRESS_INVALID; // address is past end of partition } uint32_t flashSectorSize = flashGeometry->sectorSize; @@ -415,7 +114,7 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t flashPageProgramBegin(flashAddress, NULL); } - buffers[0] = (uint8_t *)buffer; + buffers[0] = (uint8_t*)buffer; bufferSizes[0] = CONFIG_STREAMER_BUFFER_SIZE; flashPageProgramContinue(buffers, bufferSizes, 1); @@ -425,179 +124,56 @@ static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t memset(eepromData, 0, sizeof(eepromData)); } - uint64_t *dest_addr = (uint64_t *)c->address; + uint64_t *dest_addr = (uint64_t*)c->address; uint64_t *src_addr = (uint64_t*)buffer; uint8_t row_index = CONFIG_STREAMER_BUFFER_SIZE / sizeof(uint64_t); STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE % sizeof(uint64_t) == 0, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); /* copy the 256 bits flash word */ - do - { + do { *dest_addr++ = *src_addr++; } while (--row_index != 0); -#elif defined(CONFIG_IN_FILE) +#elif defined(CONFIG_IN_FLASH) || defined(CONFIG_IN_FILE) - if (c->address % FLASH_PAGE_SIZE == 0) { - const FLASH_Status status = FLASH_ErasePage(c->address); - if (status != FLASH_COMPLETE) { - return -1; - } - } - STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); - const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer); - if (status != FLASH_COMPLETE) { - return -2; + configStreamerResult_e result = configWriteWord(c->address, buffer); + if (result != CONFIG_RESULT_SUCCESS) { + return result; } -#elif defined(CONFIG_IN_FLASH) - -#if defined(STM32H7) - if (c->address % FLASH_PAGE_SIZE == 0) { - FLASH_EraseInitTypeDef EraseInitStruct = { - .TypeErase = FLASH_TYPEERASE_SECTORS, -#if !(defined(STM32H7A3xx) || defined(STM32H7A3xxQ)) - .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V -#endif - .NbSectors = 1 - }; - getFLASHSectorForEEPROM(c->address, &EraseInitStruct.Banks, &EraseInitStruct.Sector); - uint32_t SECTORError; - const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError); - if (status != HAL_OK) { - return -1; - } - } - - // For H7 - // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t DataAddress); - STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * FLASH_NB_32BITWORD_IN_FLASHWORD, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); - const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, c->address, (uint64_t)(uint32_t)buffer); - if (status != HAL_OK) { - return -2; - } -#elif defined(STM32F7) - if (c->address % FLASH_PAGE_SIZE == 0) { - FLASH_EraseInitTypeDef EraseInitStruct = { - .TypeErase = FLASH_TYPEERASE_SECTORS, - .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V - .NbSectors = 1 - }; - EraseInitStruct.Sector = getFLASHSectorForEEPROM(); - uint32_t SECTORError; - const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError); - if (status != HAL_OK) { - return -1; - } - } - - STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); - // For F7 - // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data); - const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, c->address, (uint64_t)*buffer); - if (status != HAL_OK) { - return -2; - } -#elif defined(STM32G4) - if (c->address % FLASH_PAGE_SIZE == 0) { - - FLASH_EraseInitTypeDef EraseInitStruct = { - .TypeErase = FLASH_TYPEERASE_PAGES, - .NbPages = 1 - }; - getFLASHSectorForEEPROM(c->address, &EraseInitStruct.Banks, &EraseInitStruct.Page); - uint32_t SECTORError; - const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError); - if (status != HAL_OK) { - return -1; - } - } - - STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 2, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); - const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, c->address, (uint64_t)*buffer); - if (status != HAL_OK) { - return -2; - } -#elif defined(AT32F4) - if (c->address % FLASH_PAGE_SIZE == 0) { - const flash_status_type status = flash_sector_erase(c->address); - if (status != FLASH_OPERATE_DONE) { - return -1; - } - } - - STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); - const flash_status_type status = flash_word_program(c->address, (uint32_t)*buffer); - if (status != FLASH_OPERATE_DONE) { - return -2; - } -#elif defined(APM32F4) - if (c->address % FLASH_PAGE_SIZE == 0) { - FLASH_EraseInitTypeDef EraseInitStruct = { - .TypeErase = FLASH_TYPEERASE_SECTORS, - .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V - .NbSectors = 1 - }; - EraseInitStruct.Sector = getFLASHSectorForEEPROM(); - uint32_t SECTORError; - const DAL_StatusTypeDef status = DAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError); - if (status != DAL_OK) { - return -1; - } - } - - STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); - const DAL_StatusTypeDef status = DAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, c->address, (uint64_t)*buffer); - if (status != DAL_OK) { - return -2; - } -#else // !STM32H7 && !STM32F7 && !STM32G4 - if (c->address % FLASH_PAGE_SIZE == 0) { - const FLASH_Status status = FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3); //0x08080000 to 0x080A0000 - if (status != FLASH_COMPLETE) { - return -1; - } - } - - STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); - const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer); - if (status != FLASH_COMPLETE) { - return -2; - } -#endif #endif c->address += CONFIG_STREAMER_BUFFER_SIZE; - return 0; + return CONFIG_RESULT_SUCCESS; } -int config_streamer_write(config_streamer_t *c, const uint8_t *p, uint32_t size) +configStreamerResult_e config_streamer_write(config_streamer_t *c, const uint8_t *p, size_t size) { for (const uint8_t *pat = p; pat != (uint8_t*)p + size; pat++) { c->buffer.b[c->at++] = *pat; if (c->at == sizeof(c->buffer)) { - c->err = write_word(c, &c->buffer.w); + c->err = write_word(c, c->buffer.w); c->at = 0; } } return c->err; } -int config_streamer_status(config_streamer_t *c) +configStreamerResult_e config_streamer_status(config_streamer_t *c) { return c->err; } -int config_streamer_flush(config_streamer_t *c) +configStreamerResult_e config_streamer_flush(config_streamer_t *c) { if (c->at != 0) { memset(c->buffer.b + c->at, 0, sizeof(c->buffer) - c->at); - c->err = write_word(c, &c->buffer.w); + c->err = write_word(c, c->buffer.w); c->at = 0; } return c->err; } -int config_streamer_finish(config_streamer_t *c) +configStreamerResult_e config_streamer_finish(config_streamer_t *c) { if (c->unlocked) { #if defined(CONFIG_IN_SDCARD) @@ -608,18 +184,8 @@ int config_streamer_finish(config_streamer_t *c) saveEEPROMToMemoryMappedFlash(); #elif defined(CONFIG_IN_RAM) // NOP -#elif defined(CONFIG_IN_FILE) - FLASH_Lock(); -#elif defined(CONFIG_IN_FLASH) -#if defined(STM32F7) || defined(STM32H7) || defined(STM32G4) - HAL_FLASH_Lock(); -#elif defined(AT32F4) - flash_lock(); -#elif defined(APM32F4) - DAL_FLASH_Lock(); -#else - FLASH_Lock(); -#endif +#elif defined(CONFIG_IN_FILE) || defined(CONFIG_IN_FLASH) + configLock(); #endif c->unlocked = false; } diff --git a/src/main/config/config_streamer.h b/src/main/config/config_streamer.h index 930dcbe50b..df1ba4a03c 100644 --- a/src/main/config/config_streamer.h +++ b/src/main/config/config_streamer.h @@ -23,49 +23,63 @@ #include #include +#include "platform.h" + // Streams data out to the EEPROM, padding to the write size as // needed, and updating the checksum as it goes. #if defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_MEMORY_MAPPED_FLASH) #define CONFIG_STREAMER_BUFFER_SIZE 8 // Must not be greater than the smallest flash page size of all compiled-in flash devices. -typedef uint32_t config_streamer_buffer_align_type_t; +#define CONFIG_BUFFER_TYPE uint32_t #elif defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD) #define CONFIG_STREAMER_BUFFER_SIZE 32 -typedef uint64_t config_streamer_buffer_align_type_t; -#elif defined(STM32H743xx) || defined(STM32H750xx) || defined(STM32H723xx) || defined(STM32H725xx) -#define CONFIG_STREAMER_BUFFER_SIZE 32 // Flash word = 256-bits -typedef uint64_t config_streamer_buffer_align_type_t; -#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) -#define CONFIG_STREAMER_BUFFER_SIZE 16 // Flash word = 128-bits -typedef uint64_t config_streamer_buffer_align_type_t; -#elif defined(STM32G4) -#define CONFIG_STREAMER_BUFFER_SIZE 8 // Flash word = 64-bits -typedef uint64_t config_streamer_buffer_align_type_t; -#elif defined(APM32F4) -#define CONFIG_STREAMER_BUFFER_SIZE 4 // Flash word = 32-bits -typedef uint32_t config_streamer_buffer_align_type_t; -#else -#define CONFIG_STREAMER_BUFFER_SIZE 4 -typedef uint32_t config_streamer_buffer_align_type_t; +#define CONFIG_BUFFER_TYPE uint64_t +#elif defined(CONFIG_IN_FILE) +#define CONFIG_BUFFER_TYPE uint32_t +#elif defined(CONFIG_IN_FLASH) +#if defined(FLASH_CONFIG_STREAMER_BUFFER_SIZE) +#define CONFIG_STREAMER_BUFFER_SIZE FLASH_CONFIG_STREAMER_BUFFER_SIZE #endif +#define CONFIG_BUFFER_TYPE FLASH_CONFIG_BUFFER_TYPE +#endif + +#if !defined(CONFIG_BUFFER_TYPE) +#error "No config buffer alignment set" +#endif + +#if !defined(CONFIG_STREAMER_BUFFER_SIZE) +#define CONFIG_STREAMER_BUFFER_SIZE sizeof(CONFIG_BUFFER_TYPE) +#endif + +typedef enum { + CONFIG_RESULT_TIMEOUT = -4, + CONFIG_RESULT_ADDRESS_INVALID = -3, + CONFIG_RESULT_INCOMPLETE = -2, + CONFIG_RESULT_FAILURE = -1, + CONFIG_RESULT_SUCCESS = 0 +} configStreamerResult_e; + +typedef CONFIG_BUFFER_TYPE config_streamer_buffer_type_t; +STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE % sizeof(config_streamer_buffer_type_t) == 0, "CONFIG_STREAMER_BUFFER_SIZE does not fit to FLASH WORD size"); typedef struct config_streamer_s { uintptr_t address; int size; union { uint8_t b[CONFIG_STREAMER_BUFFER_SIZE]; - config_streamer_buffer_align_type_t w; + config_streamer_buffer_type_t w[CONFIG_STREAMER_BUFFER_SIZE / sizeof(config_streamer_buffer_type_t)]; } buffer; int at; - int err; + configStreamerResult_e err; bool unlocked; } config_streamer_t; void config_streamer_init(config_streamer_t *c); -void config_streamer_start(config_streamer_t *c, uintptr_t base, int size); -int config_streamer_write(config_streamer_t *c, const uint8_t *p, uint32_t size); -int config_streamer_flush(config_streamer_t *c); +void config_streamer_start(config_streamer_t *c, uintptr_t base, size_t size); +configStreamerResult_e config_streamer_write(config_streamer_t *c, const uint8_t *p, size_t size); +configStreamerResult_e config_streamer_flush(config_streamer_t *c); + +configStreamerResult_e config_streamer_finish(config_streamer_t *c); +configStreamerResult_e config_streamer_status(config_streamer_t *c); -int config_streamer_finish(config_streamer_t *c); -int config_streamer_status(config_streamer_t *c); diff --git a/src/main/config/config_streamer_impl.h b/src/main/config/config_streamer_impl.h new file mode 100644 index 0000000000..12fda926c6 --- /dev/null +++ b/src/main/config/config_streamer_impl.h @@ -0,0 +1,29 @@ +/* + * This file is part of Betaflight. + * + * Betaflight is free software. You can redistribute this software + * and/or modify this software under the terms of the GNU General + * Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later + * version. + * + * Betaflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this software. + * + * If not, see . + */ + +#pragma once + +#include "config_streamer.h" + +void configUnlock(void); +void configLock(void); +void configClearFlags(void); +configStreamerResult_e configWriteWord(uintptr_t address, config_streamer_buffer_type_t *buffer); diff --git a/src/main/drivers/system.h b/src/main/drivers/system.h index f76229d253..ff106bbc38 100644 --- a/src/main/drivers/system.h +++ b/src/main/drivers/system.h @@ -43,6 +43,8 @@ typedef enum { FAILURE_SDCARD_WRITE_FAILED, FAILURE_SDCARD_INITIALISATION_FAILED, FAILURE_SDCARD_REQUIRED, + FAILURE_FILE_READ_FAILED, + FAILURE_FILE_WRITE_FAILED, } failureMode_e; #define WARNING_FLASH_DURATION_MS 50 diff --git a/src/platform/APM32/mk/APM32F4.mk b/src/platform/APM32/mk/APM32F4.mk index 9ea1025baf..4b3a3cee21 100644 --- a/src/platform/APM32/mk/APM32F4.mk +++ b/src/platform/APM32/mk/APM32F4.mk @@ -148,6 +148,7 @@ endif MCU_COMMON_SRC = \ common/stm32/system.c \ common/stm32/io_impl.c \ + common/stm32/config_flash.c \ APM32/startup/system_apm32f4xx.c \ drivers/inverter.c \ drivers/dshot_bitbang_decode.c \ @@ -177,8 +178,8 @@ MCU_COMMON_SRC = \ drivers/adc.c \ drivers/bus_i2c_config.c \ drivers/bus_spi_config.c \ - common/stm32/bus_spi_pinconfig.c \ common/stm32/bus_spi_hw.c \ + common/stm32/bus_spi_pinconfig.c \ drivers/serial_escserial.c \ drivers/serial_pinconfig.c \ drivers/serial_uart_pinconfig.c \ @@ -204,7 +205,6 @@ MSC_SRC = \ msc/usbd_storage_sdio.c SPEED_OPTIMISED_SRC += \ - common/stm32/bus_spi_hw.c \ common/stm32/system.c SIZE_OPTIMISED_SRC += \ diff --git a/src/platform/APM32/platform_mcu.h b/src/platform/APM32/platform_mcu.h index 769d1cc3ed..8e0aa79749 100644 --- a/src/platform/APM32/platform_mcu.h +++ b/src/platform/APM32/platform_mcu.h @@ -188,3 +188,7 @@ #define USE_TX_IRQ_HANDLER #endif + +#if defined(APM32F4) +#define FLASH_CONFIG_BUFFER_TYPE uint32_t +#endif diff --git a/src/platform/AT32/mk/AT32F4.mk b/src/platform/AT32/mk/AT32F4.mk index cc5f9955f1..0c0ff8be39 100644 --- a/src/platform/AT32/mk/AT32F4.mk +++ b/src/platform/AT32/mk/AT32F4.mk @@ -83,6 +83,7 @@ DEVICE_FLAGS += -DUSE_ATBSP_DRIVER -DAT32F43x -DHSE_VALUE=$(HSE_VALUE) -DAT32 MCU_COMMON_SRC = \ common/stm32/system.c \ common/stm32/io_impl.c \ + common/stm32/config_flash.c \ AT32/startup/at32f435_437_clock.c \ AT32/startup/system_at32f435_437.c \ AT32/adc_at32f43x.c \ diff --git a/src/platform/AT32/platform_mcu.h b/src/platform/AT32/platform_mcu.h index 36baa0f7fc..9a42c16cfc 100644 --- a/src/platform/AT32/platform_mcu.h +++ b/src/platform/AT32/platform_mcu.h @@ -142,3 +142,5 @@ typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; #define MAX_SPI_PIN_SEL 4 #endif + +#define FLASH_CONFIG_BUFFER_TYPE uint32_t diff --git a/src/platform/SIMULATOR/sitl.c b/src/platform/SIMULATOR/sitl.c index c96c268fed..65752866c6 100644 --- a/src/platform/SIMULATOR/sitl.c +++ b/src/platform/SIMULATOR/sitl.c @@ -48,6 +48,8 @@ #include "config/feature.h" #include "config/config.h" +#include "config/config_streamer.h" + #include "scheduler/scheduler.h" #include "pg/rx.h" @@ -659,18 +661,18 @@ char _Min_Stack_Size; // virtual EEPROM static FILE *eepromFd = NULL; -void FLASH_Unlock(void) +bool loadEEPROMFromFile(void) { if (eepromFd != NULL) { fprintf(stderr, "[FLASH_Unlock] eepromFd != NULL\n"); - return; + return false; } // open or create - eepromFd = fopen(EEPROM_FILENAME,"r+"); + eepromFd = fopen(EEPROM_FILENAME, "r+"); if (eepromFd != NULL) { // obtain file size: - fseek(eepromFd , 0 , SEEK_END); + fseek(eepromFd, 0, SEEK_END); size_t lSize = ftell(eepromFd); rewind(eepromFd); @@ -679,21 +681,29 @@ void FLASH_Unlock(void) printf("[FLASH_Unlock] loaded '%s', size = %ld / %ld\n", EEPROM_FILENAME, lSize, sizeof(eepromData)); } else { fprintf(stderr, "[FLASH_Unlock] failed to load '%s'\n", EEPROM_FILENAME); - return; + return false; } } else { printf("[FLASH_Unlock] created '%s', size = %ld\n", EEPROM_FILENAME, sizeof(eepromData)); if ((eepromFd = fopen(EEPROM_FILENAME, "w+")) == NULL) { fprintf(stderr, "[FLASH_Unlock] failed to create '%s'\n", EEPROM_FILENAME); - return; + return false; } + if (fwrite(eepromData, sizeof(eepromData), 1, eepromFd) != 1) { fprintf(stderr, "[FLASH_Unlock] write failed: %s\n", strerror(errno)); + return false; } } + return true; } -void FLASH_Lock(void) +void configUnlock(void) +{ + loadEEPROMFromFile(); +} + +void configLock(void) { // flush & close if (eepromFd != NULL) { @@ -707,22 +717,17 @@ void FLASH_Lock(void) } } -FLASH_Status FLASH_ErasePage(uintptr_t Page_Address) +configStreamerResult_e configWriteWord(uintptr_t address, config_streamer_buffer_type_t *buffer) { - UNUSED(Page_Address); -// printf("[FLASH_ErasePage]%x\n", Page_Address); - return FLASH_COMPLETE; -} + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); -FLASH_Status FLASH_ProgramWord(uintptr_t addr, uint32_t value) -{ - if ((addr >= (uintptr_t)eepromData) && (addr < (uintptr_t)ARRAYEND(eepromData))) { - *((uint32_t*)addr) = value; - printf("[FLASH_ProgramWord]%p = %08x\n", (void*)addr, *((uint32_t*)addr)); + if ((address >= (uintptr_t)eepromData) && (address + sizeof(uint32_t) <= (uintptr_t)ARRAYEND(eepromData))) { + memcpy((void*)address, buffer, sizeof(config_streamer_buffer_type_t)); + printf("[FLASH_ProgramWord]%p = %08x\n", (void*)address, *((uint32_t*)address)); } else { - printf("[FLASH_ProgramWord]%p out of range!\n", (void*)addr); + printf("[FLASH_ProgramWord]%p out of range!\n", (void*)address); } - return FLASH_COMPLETE; + return CONFIG_RESULT_SUCCESS; } void IOConfigGPIO(IO_t io, ioConfig_t cfg) diff --git a/src/platform/SIMULATOR/target/SITL/target.h b/src/platform/SIMULATOR/target/SITL/target.h index e620fa9a40..5a75501c8f 100644 --- a/src/platform/SIMULATOR/target/SITL/target.h +++ b/src/platform/SIMULATOR/target/SITL/target.h @@ -236,15 +236,6 @@ typedef struct void* test; } I2C_TypeDef; -typedef enum -{ - FLASH_BUSY = 1, - FLASH_ERROR_PG, - FLASH_ERROR_WRP, - FLASH_COMPLETE, - FLASH_TIMEOUT -} FLASH_Status; - typedef struct { double timestamp; // in seconds double imu_angular_velocity_rpy[3]; // rad/s -> range: +/- 8192; +/- 2000 deg/se @@ -269,11 +260,6 @@ typedef struct { float pwm_output_raw[SIMULATOR_MAX_PWM_CHANNELS]; // Raw PWM from 1100 to 1900 } servo_packet_raw; -void FLASH_Unlock(void); -void FLASH_Lock(void); -FLASH_Status FLASH_ErasePage(uintptr_t Page_Address); -FLASH_Status FLASH_ProgramWord(uintptr_t addr, uint32_t Data); - uint64_t nanos64_real(void); uint64_t micros64_real(void); uint64_t millis64_real(void); diff --git a/src/platform/STM32/mk/STM32F4.mk b/src/platform/STM32/mk/STM32F4.mk index 43fc7b16b7..28bf0fecb6 100644 --- a/src/platform/STM32/mk/STM32F4.mk +++ b/src/platform/STM32/mk/STM32F4.mk @@ -171,6 +171,7 @@ DEVICE_FLAGS += -DHSE_VALUE=$(HSE_VALUE) -DSTM32 MCU_COMMON_SRC = \ common/stm32/system.c \ + common/stm32/config_flash.c \ drivers/accgyro/accgyro_mpu.c \ drivers/dshot_bitbang_decode.c \ drivers/inverter.c \ diff --git a/src/platform/STM32/mk/STM32F7.mk b/src/platform/STM32/mk/STM32F7.mk index b0341b7c75..b197c92774 100644 --- a/src/platform/STM32/mk/STM32F7.mk +++ b/src/platform/STM32/mk/STM32F7.mk @@ -140,7 +140,6 @@ VCP_SRC = \ drivers/usb_io.c MCU_COMMON_SRC = \ - common/stm32/system.c \ drivers/accgyro/accgyro_mpu.c \ drivers/bus_i2c_timing.c \ drivers/dshot_bitbang_decode.c \ @@ -189,7 +188,6 @@ MSC_SRC = \ msc/usbd_storage_sd_spi.c SPEED_OPTIMISED_SRC += \ - common/stm32/system.c \ STM32/bus_i2c_hal.c \ STM32/bus_spi_ll.c \ drivers/max7456.c \ diff --git a/src/platform/STM32/mk/STM32G4.mk b/src/platform/STM32/mk/STM32G4.mk index 8acdd13fca..41b1ba1a45 100644 --- a/src/platform/STM32/mk/STM32G4.mk +++ b/src/platform/STM32/mk/STM32G4.mk @@ -117,7 +117,6 @@ VCP_SRC = \ drivers/usb_io.c MCU_COMMON_SRC = \ - common/stm32/system.c \ drivers/accgyro/accgyro_mpu.c \ drivers/bus_i2c_timing.c \ drivers/dshot_bitbang_decode.c \ @@ -167,7 +166,6 @@ MSC_SRC = \ msc/usbd_storage_sd_spi.c SPEED_OPTIMISED_SRC += \ - common/stm32/system.c \ STM32/exti.c SIZE_OPTIMISED_SRC += \ diff --git a/src/platform/STM32/mk/STM32H5.mk b/src/platform/STM32/mk/STM32H5.mk index 3aade083b1..b8fb8511d2 100644 --- a/src/platform/STM32/mk/STM32H5.mk +++ b/src/platform/STM32/mk/STM32H5.mk @@ -148,7 +148,6 @@ VCP_SRC = drivers/usb_io.c MCU_COMMON_SRC = \ - common/stm32/system.c \ drivers/bus_i2c_timing.c \ drivers/bus_quadspi.c \ drivers/dshot_bitbang_decode.c \ @@ -201,7 +200,6 @@ MSC_SRC = msc/usbd_storage_sdio.c SPEED_OPTIMISED_SRC += \ - common/stm32/system.c \ STM32/exti.c SIZE_OPTIMISED_SRC += \ diff --git a/src/platform/STM32/mk/STM32H7.mk b/src/platform/STM32/mk/STM32H7.mk index c8ba1550d3..ffa2bf5b6f 100644 --- a/src/platform/STM32/mk/STM32H7.mk +++ b/src/platform/STM32/mk/STM32H7.mk @@ -263,7 +263,6 @@ VCP_SRC = \ drivers/usb_io.c MCU_COMMON_SRC = \ - common/stm32/system.c \ drivers/bus_i2c_timing.c \ drivers/bus_quadspi.c \ drivers/dshot_bitbang_decode.c \ @@ -316,7 +315,6 @@ MSC_SRC = \ msc/usbd_storage_sdio.c SPEED_OPTIMISED_SRC += \ - common/stm32/system.c \ STM32/exti.c SIZE_OPTIMISED_SRC += \ diff --git a/src/platform/STM32/mk/STM32_COMMON.mk b/src/platform/STM32/mk/STM32_COMMON.mk index 1e45a60ae2..e8f7914941 100644 --- a/src/platform/STM32/mk/STM32_COMMON.mk +++ b/src/platform/STM32/mk/STM32_COMMON.mk @@ -2,14 +2,18 @@ INCLUDE_DIRS += $(PLATFORM_DIR)/common/stm32 MCU_COMMON_SRC += \ + common/stm32/system.c \ + common/stm32/config_flash.c \ common/stm32/bus_spi_pinconfig.c \ common/stm32/bus_spi_hw.c \ common/stm32/io_impl.c SIZE_OPTIMISED_SRC += \ + common/stm32/config_flash.c \ common/stm32/bus_spi_pinconfig.c SPEED_OPTIMISED_SRC += \ + common/stm32/system.c \ common/stm32/bus_spi_hw.c \ common/stm32/io_impl.c diff --git a/src/platform/STM32/platform_mcu.h b/src/platform/STM32/platform_mcu.h index a620f27932..1f69a3cfc9 100644 --- a/src/platform/STM32/platform_mcu.h +++ b/src/platform/STM32/platform_mcu.h @@ -302,6 +302,18 @@ extern uint8_t _dmaram_end__; #endif +#if defined(STM32H743xx) || defined(STM32H750xx) || defined(STM32H723xx) || defined(STM32H725xx) +#define FLASH_CONFIG_STREAMER_BUFFER_SIZE 32 // Flash word = 256-bits (8 rows, uint32_t per row - 8 x 32) +#define FLASH_CONFIG_BUFFER_TYPE uint32_t +#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) +#define FLASH_CONFIG_STREAMER_BUFFER_SIZE 16 // Flash word = 128-bits (4 rows, uint32_t per row - 4 x 32) +#define FLASH_CONFIG_BUFFER_TYPE uint32_t +#elif defined(STM32G4) +#define FLASH_CONFIG_BUFFER_TYPE uint64_t +#else +#define FLASH_CONFIG_BUFFER_TYPE uint32_t +#endif + #if defined(STM32F4) #define SPI_IO_AF_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_NOPULL) #define SPI_IO_AF_SCK_CFG IO_CONFIG(GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_PP, GPIO_PuPd_DOWN) diff --git a/src/platform/common/stm32/config_flash.c b/src/platform/common/stm32/config_flash.c new file mode 100644 index 0000000000..e1f6b484ed --- /dev/null +++ b/src/platform/common/stm32/config_flash.c @@ -0,0 +1,475 @@ +/* + * This file is part of Betaflight. + * + * Betaflight is free software. You can redistribute this software + * and/or modify this software under the terms of the GNU General + * Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later + * version. + * + * Betaflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this software. + * + * If not, see . + */ + +#include + +#include "platform.h" +#include "drivers/system.h" +#include "config/config_streamer.h" + +#if defined(CONFIG_IN_FLASH) + +#if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx) +/* +Sector 0 0x08000000 - 0x08007FFF 32 Kbytes +Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes +Sector 2 0x08010000 - 0x08017FFF 32 Kbytes +Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes +Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes +Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes +Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes +Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes + +F7X5XI device with 2M flash +Sector 8 0x08100000 - 0x0813FFFF 256 Kbytes +Sector 9 0x08140000 - 0x0817FFFF 256 Kbytes +Sector 10 0x08180000 - 0x081BFFFF 256 Kbytes +Sector 11 0x081C0000 - 0x081FFFFF 256 Kbytes +*/ + +uint32_t getFLASHSectorForEEPROM(void) +{ + if ((uint32_t)&__config_start <= 0x08007FFF) + return FLASH_SECTOR_0; + if ((uint32_t)&__config_start <= 0x0800FFFF) + return FLASH_SECTOR_1; + if ((uint32_t)&__config_start <= 0x08017FFF) + return FLASH_SECTOR_2; + if ((uint32_t)&__config_start <= 0x0801FFFF) + return FLASH_SECTOR_3; + if ((uint32_t)&__config_start <= 0x0803FFFF) + return FLASH_SECTOR_4; + if ((uint32_t)&__config_start <= 0x0807FFFF) + return FLASH_SECTOR_5; + if ((uint32_t)&__config_start <= 0x080BFFFF) + return FLASH_SECTOR_6; + if ((uint32_t)&__config_start <= 0x080FFFFF) + return FLASH_SECTOR_7; +#if defined(STM32F765xx) + if ((uint32_t)&__config_start <= 0x0813FFFF) + return FLASH_SECTOR_8; + if ((uint32_t)&__config_start <= 0x0817FFFF) + return FLASH_SECTOR_9; + if ((uint32_t)&__config_start <= 0x081BFFFF) + return FLASH_SECTOR_10; + if ((uint32_t)&__config_start <= 0x081FFFFF) + return FLASH_SECTOR_11; +#endif + + // Not good + while (1) { + failureMode(FAILURE_CONFIG_STORE_FAILURE); + } +} + +#elif defined(STM32F722xx) +/* +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 +*/ + +uint32_t getFLASHSectorForEEPROM(void) +{ + if ((uint32_t)&__config_start <= 0x08003FFF) + return FLASH_SECTOR_0; + if ((uint32_t)&__config_start <= 0x08007FFF) + return FLASH_SECTOR_1; + if ((uint32_t)&__config_start <= 0x0800BFFF) + return FLASH_SECTOR_2; + if ((uint32_t)&__config_start <= 0x0800FFFF) + return FLASH_SECTOR_3; + if ((uint32_t)&__config_start <= 0x0801FFFF) + return FLASH_SECTOR_4; + if ((uint32_t)&__config_start <= 0x0803FFFF) + return FLASH_SECTOR_5; + if ((uint32_t)&__config_start <= 0x0805FFFF) + return FLASH_SECTOR_6; + if ((uint32_t)&__config_start <= 0x0807FFFF) + return FLASH_SECTOR_7; + + // Not good + while (1) { + failureMode(FAILURE_CONFIG_STORE_FAILURE); + } +} + +#elif 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 +*/ + +uint32_t getFLASHSectorForEEPROM(void) +{ + if ((uint32_t)&__config_start <= 0x08003FFF) + return FLASH_Sector_0; + if ((uint32_t)&__config_start <= 0x08007FFF) + return FLASH_Sector_1; + if ((uint32_t)&__config_start <= 0x0800BFFF) + return FLASH_Sector_2; + if ((uint32_t)&__config_start <= 0x0800FFFF) + return FLASH_Sector_3; + if ((uint32_t)&__config_start <= 0x0801FFFF) + return FLASH_Sector_4; + if ((uint32_t)&__config_start <= 0x0803FFFF) + return FLASH_Sector_5; + if ((uint32_t)&__config_start <= 0x0805FFFF) + return FLASH_Sector_6; + if ((uint32_t)&__config_start <= 0x0807FFFF) + return FLASH_Sector_7; + if ((uint32_t)&__config_start <= 0x0809FFFF) + return FLASH_Sector_8; + if ((uint32_t)&__config_start <= 0x080DFFFF) + return FLASH_Sector_9; + if ((uint32_t)&__config_start <= 0x080BFFFF) + return FLASH_Sector_10; + if ((uint32_t)&__config_start <= 0x080FFFFF) + return FLASH_Sector_11; + + // Not good + while (1) { + failureMode(FAILURE_CONFIG_STORE_FAILURE); + } +} + +#elif defined(STM32H743xx) || defined(STM32G4) || defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H723xx) || defined(STM32H725xx) +/* +MCUs with uniform array of equal size sectors, handled in two banks having contiguous address. +(Devices with non-contiguous flash layout is not currently useful anyways.) + +H743 +2 bank * 8 sector/bank * 128K/sector (2MB) +Bank 1 0x08000000 - 0x080FFFFF 128KB * 8 +Bank 2 0x08100000 - 0x081FFFFF 128KB * 8 + +H743 +1 bank * 8 sector/bank * 128K/sector (1MB) +Bank 1 0x08000000 - 0x080FFFFF 128KB * 8 + +H7A3 +2 bank * 128 sector/bank * 8KB/sector (2MB) +Bank 1 0x08000000 - 0x080FFFFF 8KB * 128 +Bank 2 0x08100000 - 0x081FFFFF 8KB * 128 + +G473/474 in dual bank mode +2 bank * 128 sector/bank * 2KB/sector (512KB) +Bank 1 0x08000000 - 0x0803FFFF 2KB * 128 +Bank 2 0x08040000 - 0x0807FFFF 2KB * 128 + +Note that FLASH_BANK_SIZE constant used in the following code changes depending on +bank operation mode. The code assumes dual bank operation, in which case the +FLASH_BANK_SIZE constant is set to one half of the available flash size in HAL. +*/ + +#if defined(STM32H743xx) || defined(STM32H723xx) || defined(STM32H725xx) +#define FLASH_PAGE_PER_BANK 8 +#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ) +#define FLASH_PAGE_PER_BANK 128 +#elif defined(STM32G4) +#define FLASH_PAGE_PER_BANK 128 +// These are not defined in CMSIS like H7 +#define FLASH_BANK1_BASE FLASH_BASE +#define FLASH_BANK2_BASE (FLASH_BANK1_BASE + FLASH_BANK_SIZE) +#endif + +void getFLASHSectorForEEPROM(uint32_t address, uint32_t *bank, uint32_t *sector) +{ +#if defined(FLASH_BANK2_BASE) + if (address >= FLASH_BANK1_BASE && address < FLASH_BANK2_BASE) { + *bank = FLASH_BANK_1; + } else if (address >= FLASH_BANK2_BASE && address < FLASH_BANK2_BASE + FLASH_BANK_SIZE) { + *bank = FLASH_BANK_2; + address -= FLASH_BANK_SIZE; + } +#else + if (address >= FLASH_BANK1_BASE && address < FLASH_BANK1_BASE + FLASH_BANK_SIZE) { + *bank = FLASH_BANK_1; + } +#endif + else { + // Not good + while (1) { + failureMode(FAILURE_CONFIG_STORE_FAILURE); + } + } + + address -= FLASH_BANK1_BASE; + *sector = address / FLASH_PAGE_SIZE; +} +#elif defined(STM32H750xx) +/* +The memory map supports 2 banks of 8 128k sectors like the H743xx, but there is only one 128K sector so we save some code +space by using a smaller function. + +Bank 1 +Sector 0 0x08000000 - 0x0801FFFF 128 Kbytes + +*/ + +void getFLASHSectorForEEPROM(uint32_t *bank, uint32_t *sector) +{ + + uint32_t start = (uint32_t)&__config_start; + + if (start == FLASH_BANK1_BASE) { + *sector = FLASH_SECTOR_0; + *bank = FLASH_BANK_1; + } else { + // Not good + while (1) { + failureMode(FAILURE_CONFIG_STORE_FAILURE); + } + } +} +#elif defined(APM32F4) +/* +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 +*/ + +uint32_t getFLASHSectorForEEPROM(void) +{ + if ((uint32_t)&__config_start <= 0x08003FFF) + return FLASH_SECTOR_0; + if ((uint32_t)&__config_start <= 0x08007FFF) + return FLASH_SECTOR_1; + if ((uint32_t)&__config_start <= 0x0800BFFF) + return FLASH_SECTOR_2; + if ((uint32_t)&__config_start <= 0x0800FFFF) + return FLASH_SECTOR_3; + if ((uint32_t)&__config_start <= 0x0801FFFF) + return FLASH_SECTOR_4; + if ((uint32_t)&__config_start <= 0x0803FFFF) + return FLASH_SECTOR_5; + if ((uint32_t)&__config_start <= 0x0805FFFF) + return FLASH_SECTOR_6; + if ((uint32_t)&__config_start <= 0x0807FFFF) + return FLASH_SECTOR_7; + if ((uint32_t)&__config_start <= 0x0809FFFF) + return FLASH_SECTOR_8; + if ((uint32_t)&__config_start <= 0x080DFFFF) + return FLASH_SECTOR_9; + if ((uint32_t)&__config_start <= 0x080BFFFF) + return FLASH_SECTOR_10; + if ((uint32_t)&__config_start <= 0x080FFFFF) + return FLASH_SECTOR_11; + + // Not good + while (1) { + failureMode(FAILURE_CONFIG_STORE_FAILURE); + } +} +#endif + +void configUnlock(void) +{ +#if defined(STM32F7) || defined(STM32H7) || defined(STM32G4) + HAL_FLASH_Unlock(); +#elif defined(APM32F4) + DAL_FLASH_Unlock(); +#elif defined(AT32F4) + flash_unlock(); +#else + FLASH_Unlock(); +#endif +} + +void configLock(void) +{ +#if defined(STM32F7) || defined(STM32H7) || defined(STM32G4) + HAL_FLASH_Lock(); +#elif defined(AT32F4) + flash_lock(); +#elif defined(APM32F4) + DAL_FLASH_Lock(); +#else + FLASH_Lock(); +#endif +} + +void configClearFlags(void) +{ +#if defined(STM32F4) + FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); +#elif defined(STM32F7) + // NOP +#elif defined(STM32H7) + // NOP +#elif defined(STM32G4) + // NOP +#elif defined(AT32F4) + flash_flag_clear(FLASH_ODF_FLAG | FLASH_PRGMERR_FLAG | FLASH_EPPERR_FLAG); +#elif defined(APM32F4) + __DAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); +#elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD) + // NOP +#else +# error "Unsupported CPU" +#endif +} + +configStreamerResult_e configWriteWord(uintptr_t address, config_streamer_buffer_type_t *buffer) +{ +#if defined(STM32H7) + if (address % FLASH_PAGE_SIZE == 0) { + FLASH_EraseInitTypeDef EraseInitStruct = { + .TypeErase = FLASH_TYPEERASE_SECTORS, +#if !(defined(STM32H7A3xx) || defined(STM32H7A3xxQ)) + .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V +#endif + .NbSectors = 1 + }; + getFLASHSectorForEEPROM(address, &EraseInitStruct.Banks, &EraseInitStruct.Sector); + uint32_t SECTORError; + const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError); + if (status != HAL_OK) { + return CONFIG_RESULT_FAILURE; + } + } + + // For H7 + // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t DataAddress); + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * FLASH_NB_32BITWORD_IN_FLASHWORD, "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); + const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, address, (uint32_t)buffer); + if (status != HAL_OK) { + return CONFIG_RESULT_ADDRESS_INVALID; + } +#elif defined(STM32F7) + if (address % FLASH_PAGE_SIZE == 0) { + FLASH_EraseInitTypeDef EraseInitStruct = { + .TypeErase = FLASH_TYPEERASE_SECTORS, + .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V + .NbSectors = 1 + }; + EraseInitStruct.Sector = getFLASHSectorForEEPROM(); + uint32_t SECTORError; + const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError); + if (status != HAL_OK) { + return CONFIG_RESULT_FAILURE; + } + } + + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); + // For F7 + // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data); + const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *buffer); + if (status != HAL_OK) { + return CONFIG_RESULT_INCOMPLETE; + } +#elif defined(STM32G4) + if (address % FLASH_PAGE_SIZE == 0) { + + FLASH_EraseInitTypeDef EraseInitStruct = { + .TypeErase = FLASH_TYPEERASE_PAGES, + .NbPages = 1 + }; + getFLASHSectorForEEPROM(address, &EraseInitStruct.Banks, &EraseInitStruct.Page); + uint32_t SECTORError; + const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError); + if (status != HAL_OK) { + return CONFIG_RESULT_FAILURE; + } + } + + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint64_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); + const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, *buffer); + if (status != HAL_OK) { + return CONFIG_RESULT_ADDRESS_INVALID; + } +#elif defined(AT32F4) + if (address % FLASH_PAGE_SIZE == 0) { + const flash_status_type status = flash_sector_erase(address); + if (status != FLASH_OPERATE_DONE) { + return CONFIG_RESULT_FAILURE; + } + } + + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); + const flash_status_type status = flash_word_program(address, *buffer); + if (status != FLASH_OPERATE_DONE) { + return CONFIG_RESULT_ADDRESS_INVALID; + } +#elif defined(APM32F4) + if (address % FLASH_PAGE_SIZE == 0) { + FLASH_EraseInitTypeDef EraseInitStruct = { + .TypeErase = FLASH_TYPEERASE_SECTORS, + .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V + .NbSectors = 1 + }; + EraseInitStruct.Sector = getFLASHSectorForEEPROM(); + uint32_t SECTORError; + const DAL_StatusTypeDef status = DAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError); + if (status != DAL_OK) { + return CONFIG_RESULT_FAILURE; + } + } + + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); + const DAL_StatusTypeDef status = DAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *buffer); + if (status != DAL_OK) { + return CONFIG_RESULT_ADDRESS_INVALID; + } +#elif defined(STM32F4) + if (address % FLASH_PAGE_SIZE == 0) { + const FLASH_Status status = FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3); //0x08080000 to 0x080A0000 + if (status != FLASH_COMPLETE) { + return CONFIG_RESULT_FAILURE; + } + } + + STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size"); + const FLASH_Status status = FLASH_ProgramWord(address, *buffer); + if (status != FLASH_COMPLETE) { + return CONFIG_RESULT_ADDRESS_INVALID; + } +#else +#error "MCU not catered for in configWriteWord for config_streamer" +#endif + return CONFIG_RESULT_SUCCESS; +} + +#endif // CONFIG_IN_FLASH