From 4ea1156e7b1a64e5dc2956cf16ed98b8d7d6e150 Mon Sep 17 00:00:00 2001 From: Stacey Sheldon Date: Mon, 5 Mar 2012 22:33:04 -0500 Subject: [PATCH] f4 bl_helper: teach bl_helper about f4 flash sector map --- flight/PiOS/STM32F4xx/pios_bl_helper.c | 121 ++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 14 deletions(-) diff --git a/flight/PiOS/STM32F4xx/pios_bl_helper.c b/flight/PiOS/STM32F4xx/pios_bl_helper.c index 039a9791a..5473e8447 100644 --- a/flight/PiOS/STM32F4xx/pios_bl_helper.c +++ b/flight/PiOS/STM32F4xx/pios_bl_helper.c @@ -33,6 +33,7 @@ #if defined(PIOS_INCLUDE_BL_HELPER) #include #include "stm32f4xx_flash.h" +#include uint8_t *PIOS_BL_HELPER_FLASH_If_Read(uint32_t SectorAddress) { @@ -46,31 +47,123 @@ uint8_t PIOS_BL_HELPER_FLASH_Ini() return 1; } +struct device_flash_sector { + uint32_t start; + uint32_t size; + uint16_t st_sector; +}; + +static struct device_flash_sector flash_sectors[] = { + [0] = { + .start = 0x08000000, + .size = 16 * 1024, + .st_sector = FLASH_Sector_0, + }, + [1] = { + .start = 0x08004000, + .size = 16 * 1024, + .st_sector = FLASH_Sector_1, + }, + [2] = { + .start = 0x08008000, + .size = 16 * 1024, + .st_sector = FLASH_Sector_2, + }, + [3] = { + .start = 0x0800C000, + .size = 16 * 1024, + .st_sector = FLASH_Sector_3, + }, + [4] = { + .start = 0x08010000, + .size = 64 * 1024, + .st_sector = FLASH_Sector_4, + }, + [5] = { + .start = 0x08020000, + .size = 128 * 1024, + .st_sector = FLASH_Sector_5, + }, + [6] = { + .start = 0x08040000, + .size = 128 * 1024, + .st_sector = FLASH_Sector_6, + }, + [7] = { + .start = 0x08060000, + .size = 128 * 1024, + .st_sector = FLASH_Sector_7, + }, + [8] = { + .start = 0x08080000, + .size = 128 * 1024, + .st_sector = FLASH_Sector_8, + }, + [9] = { + .start = 0x080A0000, + .size = 128 * 1024, + .st_sector = FLASH_Sector_9, + }, + [10] = { + .start = 0x080C0000, + .size = 128 * 1024, + .st_sector = FLASH_Sector_10, + }, + [11] = { + .start = 0x080E0000, + .size = 128 * 1024, + .st_sector = FLASH_Sector_11, + }, +}; + +static bool PIOS_BL_HELPER_FLASH_GetSectorInfo(uint32_t address, uint8_t * sector_number, uint32_t *sector_start, uint32_t *sector_size) +{ + for (uint8_t i = 0; i < NELEMENTS(flash_sectors); i++) { + struct device_flash_sector * sector = &flash_sectors[i]; + if ((address >= sector->start) && + (address < (sector->start + sector->size))) { + /* address lies within this sector */ + *sector_number = sector->st_sector; + *sector_start = sector->start; + *sector_size = sector->size; + return (true); + } + } + + return (false); +} + uint8_t PIOS_BL_HELPER_FLASH_Start() { const struct pios_board_info * bdinfo = &pios_board_info_blob; - uint32_t pageAdress = bdinfo->fw_base; - uint8_t fail = FALSE; - while ((pageAdress < (bdinfo->fw_base + bdinfo->fw_size + bdinfo->desc_size)) - || (fail == TRUE)) { + uint32_t pageAddress = bdinfo->fw_base; + bool fail = false; + while ((pageAddress < (bdinfo->fw_base + bdinfo->fw_size + bdinfo->desc_size)) + && (fail == false)) { + uint8_t sector_number; + uint32_t sector_start; + uint32_t sector_size; + if (!PIOS_BL_HELPER_FLASH_GetSectorInfo(pageAddress, + §or_number, + §or_start, + §or_size)) { + /* We're asking for an invalid flash address */ + PIOS_Assert(0); + } for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) { - if (FLASH_ErasePage(pageAdress) == FLASH_COMPLETE) { - fail = FALSE; + if (FLASH_EraseSector(sector_number, VoltageRange_2) == FLASH_COMPLETE) { + fail = false; break; } else { - fail = TRUE; + fail = true; } } - -#ifdef STM32F10X_HD - pageAdress += 2048; -#elif defined (STM32F10X_MD) - pageAdress += 1024; -#endif + /* Move to the next sector */ + pageAddress += sector_size; } - return (fail == TRUE) ? 0 : 1; + return (fail == true) ? 0 : 1; } #endif