From a6b11dc738759863d3acb8a903e4f2262d0ec438 Mon Sep 17 00:00:00 2001 From: jflyper Date: Tue, 9 Oct 2018 16:20:08 +0900 Subject: [PATCH] config_streamer for H7 --- src/main/config/config_streamer.c | 136 +++++++++++++++++++++++++++--- src/main/config/config_streamer.h | 12 ++- 2 files changed, 135 insertions(+), 13 deletions(-) diff --git a/src/main/config/config_streamer.c b/src/main/config/config_streamer.c index a47f1d4f9b..8618b97997 100644 --- a/src/main/config/config_streamer.c +++ b/src/main/config/config_streamer.c @@ -61,6 +61,9 @@ extern uint8_t __config_end; # define FLASH_PAGE_SIZE ((uint32_t)0x8000) # elif defined(UNIT_TEST) # define FLASH_PAGE_SIZE (0x400) +// H7 +# elif defined(STM32H743xx) || defined(STM32H750xx) +# define FLASH_PAGE_SIZE ((uint32_t)0x20000) // 128K sectors // SIMULATOR # elif defined(SIMULATOR_BUILD) # define FLASH_PAGE_SIZE (0x400) @@ -80,7 +83,7 @@ void config_streamer_start(config_streamer_t *c, uintptr_t base, int size) c->address = base; c->size = size; if (!c->unlocked) { -#if defined(STM32F7) +#if defined(STM32F7) || defined(STM32H7) HAL_FLASH_Unlock(); #else FLASH_Unlock(); @@ -96,6 +99,8 @@ void config_streamer_start(config_streamer_t *c, uintptr_t base, int size) 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(UNIT_TEST) || defined(SIMULATOR_BUILD) // NOP #else @@ -242,14 +247,120 @@ static uint32_t getFLASHSectorForEEPROM(void) failureMode(FAILURE_FLASH_WRITE_FAILED); } } + +#elif defined(STM32H743xx) +/* +There are two banks of 8 of 128K sectors (up to 2MB flash) + +Bank 1 +Sector 0 0x08000000 - 0x0801FFFF 128 Kbytes +Sector 1 0x08020000 - 0x0803FFFF 128 Kbytes +Sector 2 0x08040000 - 0x0805FFFF 128 Kbytes +Sector 3 0x08060000 - 0x0807FFFF 128 Kbytes +Sector 4 0x08080000 - 0x0809FFFF 128 Kbytes +Sector 5 0x080A0000 - 0x080BFFFF 128 Kbytes +Sector 6 0x080C0000 - 0x080DFFFF 128 Kbytes +Sector 7 0x080E0000 - 0x080FFFFF 128 Kbytes + +Bank 2 +Sector 0 0x08100000 - 0x0811FFFF 128 Kbytes +Sector 1 0x08120000 - 0x0813FFFF 128 Kbytes +Sector 2 0x08140000 - 0x0815FFFF 128 Kbytes +Sector 3 0x08160000 - 0x0817FFFF 128 Kbytes +Sector 4 0x08180000 - 0x0819FFFF 128 Kbytes +Sector 5 0x081A0000 - 0x081BFFFF 128 Kbytes +Sector 6 0x081C0000 - 0x081DFFFF 128 Kbytes +Sector 7 0x081E0000 - 0x081FFFFF 128 Kbytes + +*/ + +static void getFLASHSectorForEEPROM(uint32_t *bank, uint32_t *sector) +{ + uint32_t start = (uint32_t)&__config_start; + + if (start >= FLASH_BANK1_BASE && start < FLASH_BANK2_BASE) { + *bank = FLASH_BANK_1; + } else if (start >= FLASH_BANK2_BASE && start < FLASH_BANK2_BASE + 0x100000) { + *bank = FLASH_BANK_2; + start -= 0x100000; + } else { + // Not good + while (1) { + failureMode(FAILURE_FLASH_WRITE_FAILED); + } + } + + if (start <= 0x0801FFFF) + *sector = FLASH_SECTOR_0; + else if (start <= 0x0803FFFF) + *sector = FLASH_SECTOR_1; + else if (start <= 0x0805FFFF) + *sector = FLASH_SECTOR_2; + else if (start <= 0x0807FFFF) + *sector = FLASH_SECTOR_3; + else if (start <= 0x0809FFFF) + *sector = FLASH_SECTOR_4; + else if (start <= 0x080BFFFF) + *sector = FLASH_SECTOR_5; + else if (start <= 0x080DFFFF) + *sector = FLASH_SECTOR_6; + else if (start <= 0x080FFFFF) + *sector = FLASH_SECTOR_7; +} +#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_FLASH_WRITE_FAILED); + } + } +} #endif -static int write_word(config_streamer_t *c, uint32_t value) +static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer) { if (c->err != 0) { return c->err; } -#if defined(STM32F7) +#if defined(STM32H7) + 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 + }; + getFLASHSectorForEEPROM(&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); + 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, @@ -263,27 +374,30 @@ static int write_word(config_streamer_t *c, uint32_t value) return -1; } } - const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, c->address, value); + + // 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; } -#else +#else // !STM32H7 && !STM32F7 if (c->address % FLASH_PAGE_SIZE == 0) { #if defined(STM32F4) const FLASH_Status status = FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3); //0x08080000 to 0x080A0000 -#else +#else // STM32F3 or STM32F1 const FLASH_Status status = FLASH_ErasePage(c->address); #endif if (status != FLASH_COMPLETE) { return -1; } } - const FLASH_Status status = FLASH_ProgramWord(c->address, value); + const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer); if (status != FLASH_COMPLETE) { return -2; } #endif - c->address += sizeof(value); + c->address += CONFIG_STREAMER_BUFFER_SIZE; return 0; } @@ -293,7 +407,7 @@ int config_streamer_write(config_streamer_t *c, const uint8_t *p, uint32_t size) 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; } } @@ -309,7 +423,7 @@ int 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; @@ -318,7 +432,7 @@ int config_streamer_flush(config_streamer_t *c) int config_streamer_finish(config_streamer_t *c) { if (c->unlocked) { -#if defined(STM32F7) +#if defined(STM32F7) || defined(STM32H7) HAL_FLASH_Lock(); #else FLASH_Lock(); diff --git a/src/main/config/config_streamer.h b/src/main/config/config_streamer.h index 96811b7245..0cbd884ea8 100644 --- a/src/main/config/config_streamer.h +++ b/src/main/config/config_streamer.h @@ -26,12 +26,20 @@ // Streams data out to the EEPROM, padding to the write size as // needed, and updating the checksum as it goes. +#ifdef STM32H7 +#define CONFIG_STREAMER_BUFFER_SIZE 32 // Flash word = 256-bits +typedef uint64_t config_streamer_buffer_align_type_t; +#else +#define CONFIG_STREAMER_BUFFER_SIZE 4 +typedef uint32_t config_streamer_buffer_align_type_t; +#endif + typedef struct config_streamer_s { uintptr_t address; int size; union { - uint8_t b[4]; - uint32_t w; + uint8_t b[CONFIG_STREAMER_BUFFER_SIZE]; + config_streamer_buffer_align_type_t w; } buffer; int at; int err;