diff --git a/targets/TARGET_STM/TARGET_STM32H7/flash_api.c b/targets/TARGET_STM/TARGET_STM32H7/flash_api.c index 1186f8c28a..6052d6aa47 100644 --- a/targets/TARGET_STM/TARGET_STM32H7/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32H7/flash_api.c @@ -23,6 +23,7 @@ static uint32_t GetSector(uint32_t Address); static uint32_t GetSectorSize(uint32_t Sector); +static uint32_t GetSectorBase(uint32_t SectorId); int32_t flash_init(flash_t *obj) { @@ -91,6 +92,9 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) } } + SCB_CleanInvalidateDCache_by_Addr((uint32_t *)GetSectorBase(SectorId), GetSectorSize(SectorId)); + SCB_InvalidateICache(); + HAL_FLASH_Lock(); #if defined(DUAL_CORE) LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, HSEM_CR_COREID_CURRENT); @@ -103,6 +107,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, { uint32_t StartAddress = 0; int32_t status = 0; + uint32_t FullSize = size; if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) { return -1; @@ -135,6 +140,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } + SCB_CleanInvalidateDCache_by_Addr((uint32_t *)StartAddress, FullSize); + SCB_InvalidateICache(); + HAL_FLASH_Lock(); #if defined(DUAL_CORE) LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, HSEM_CR_COREID_CURRENT); @@ -213,6 +221,22 @@ static uint32_t GetSectorSize(uint32_t Sector) return (uint32_t)(128 * 1024); // 128 KB } +/** + * @brief Gets sector base address + * @param SectorId + * @retval base address of a given sector + */ +static uint32_t GetSectorBase(uint32_t SectorId) +{ + uint32_t i = 0; + uint32_t address_sector = FLASH_BASE; + + for (i = 0; i < SectorId; i++) { + address_sector += GetSectorSize(i); + } + return address_sector; +} + uint8_t flash_get_erase_value(const flash_t *obj) { (void)obj;