This is an automated email from the ASF dual-hosted git repository.

kasjer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f7306276 mcu/stm32h5: Clear flash errors before sector erase
0f7306276 is described below

commit 0f7306276c72a66a3e86d4e4eb00bb55cb9035d7
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Mon Oct 19 15:21:40 2026 +0200

    mcu/stm32h5: Clear flash errors before sector erase
    
    HAL_FLASHEx_Erase() fails when any error flag is already set in
    flash status register. Write function clears error flags before
    programming, erase did not, so a stale error (e.g. PGSERR) made
    every erase fail until some write was done.
    
    Clear error flags before erase and invalidate ICACHE after erase
    so erased content is not read from cache, same as write does.
    
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: Jerzy Kasenberg <[email protected]>
---
 hw/mcu/stm/stm32h5xx/src/hal_flash.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/hw/mcu/stm/stm32h5xx/src/hal_flash.c 
b/hw/mcu/stm/stm32h5xx/src/hal_flash.c
index 2e023d966..70e0a10fe 100644
--- a/hw/mcu/stm/stm32h5xx/src/hal_flash.c
+++ b/hw/mcu/stm/stm32h5xx/src/hal_flash.c
@@ -45,7 +45,13 @@ stm32_mcu_flash_erase_sector(const struct hal_flash *dev, 
uint32_t sector_addres
 #endif
         eraseinit.Sector = ((sector_address - dev->hf_base_addr) / 
FLASH_SECTOR_SIZE) % FLASH_SECTOR_NB;
         eraseinit.NbSectors = 1;
+        /* Stale error flags would make HAL_FLASHEx_Erase() fail */
+        STM32_HAL_FLASH_CLEAR_ERRORS();
         rc = HAL_FLASHEx_Erase(&eraseinit, &PageError);
+        /* ICACHE may still hold erased content */
+        if (LL_ICACHE_IsEnabled()) {
+            LL_ICACHE_Invalidate();
+        }
         if (rc == HAL_OK) {
             return 0;
         }

Reply via email to