Skip to main content
Graduate
October 10, 2025
Question

STM32U5 + HAL_FLASH_Program() + DCACHE?

  • October 10, 2025
  • 2 replies
  • 259 views

Hi,

I am currently developing a small application with a STM32U5 where I write a few bytes into flash using HAL_FLASH_Program(). All seems to work fine. Except I can only read the data back after I do a reset. I suspect that DCACHE is playing a trick on me? But I am not sure how to invalidate the DCache (preferably only for a small section)

SCB_InvalidateDCache();

Seems to have some problems on the STM32U5? At least, I was unable to include the prober h-file w/o getting tons of errors.

 

What is the new strategy for cache invalidation? 

Thanks,

Juergen 

    This topic has been closed for replies.

    2 replies

    Super User
    October 10, 2025

    SCB_InvalidateDCache or SCB_InvalidateDCache_by_Addr are the correct ways to invalidate a section of memory. What problems are you running into? Should be able to call those with code generated from an IOC file by STM32CubeMX.

    JEAuthor
    Graduate
    October 10, 2025

    Thanks for your VERY quick reply.

    What file do i need to include?

    I tried directly in main.c (code generated from IOC file):

    ../Core/Src/main.c:385:3: error: implicit declaration of function 'SCB_InvalidateDCache' [-Werror=implicit-function-declaration]
     385 | SCB_InvalidateDCache();
     | ^~~~~~~~~~~~~~~~~~~~

    I once used a STM32H7, there everything worked fine. But the STM32U5 and the departure from CMSIS are making things a bit harder :)

     

    Thanks,

    Juergen

    Super User
    October 10, 2025

    What chip do you have? Are you starting from an IOC file?

     

    In a random STM32U5 chip, the compiler has no problem with "SCB_InvalidateDCache" for me.

    JEAuthor
    Graduate
    October 10, 2025

    Hi,

     

    for completion, I figured it out.

     

    HAL_StatusTypeDef HAL_DCACHE_InvalidateByAddr(DCACHE_HandleTypeDef *hdcache, const uint32_t *const pAddr, uint32_t dSize)

    is the new Api to use. But apparently, if you write to flash Dcache is not the correct cache for that? What works for me is:

    HAL_ICACHE_Invalidate();

    Apperently Icache is responsible for that (at least AI tells me?).

    I had success by removing all Dcache but not for specific memory areas which caught my suspicias/unsatisfied. I expect that beeing not correct.

    HAL_DCACHE_Invalidate(&hdcache1);

     Can anybody shed some light here?

    Thanks,

    Juergen

     

    Super User
    October 10, 2025

    ICACHE isn't relevant unless you are changing instruction code, which is not likely.

    Didn't see answers to any of my questions.