Skip to main content
Visitor II
October 17, 2025
Solved

STM32U385VGT6Q fails to write FLASH (EEPROM)

  • October 17, 2025
  • 4 replies
  • 459 views

Hello,

I've been using the Nucleo-U385RG-Q for my project and all features work fine, but when I switch the code to a STM32U385VGT6Q, there is a problem when attempting to write to FLASH. My Write function starts with:

 if (HAL_FLASHEx_Erase(&flashErase, &error) != HAL_OK)
 {
 break;
 }

and it precisely crashes at: FLASH_WaitForLastOperation in error = ((*reg_sr) & FLASH_FLAG_SR_ERRORS); with error code 128 (0x80). Searching for the error leads me to believe that the CPU is attempting to write protected/secure state areas but the Option Bytes says otherwise.I use Nima-LTD-I-CUBE-EE library.

    This topic has been closed for replies.
    Best answer by george-metasphere

    OK, I finally found the issue by accident. It was a Clock Configuration issue. Because I set my UART at 19200bps, the MSIS divider is downclocked and the entire system runs at 1.5Mhz instead of the stable 12Mhz. This apparently causes the EEPROM write to fail. Pushing the UART and clock speeds higher solves this issue.

    4 replies

    Super User
    October 17, 2025

    If it's crashing here:

    stm32u3xx-hal-driver/Src/stm32u3xx_hal_flash_ex.c at 192823dc5f75d31dae7723163b86bf11bb218be6 · STMicroelectronics/stm32u3xx-hal-driver

    there are error codes present prior to calling HAL_FLASHEx_Erase and you should address those first and find out the reason why. Why are the errors set? What does your program do?

     

    Visitor II
    October 17, 2025

    Actually the error occurs at line 225. I've now tried a small program to Erase a page as such:

    void attempt_erase()
    {
    	FLASH_EraseInitTypeDef flashErase2;
    	uint32_t error;
    HAL_FLASH_Unlock();
    HAL_ICACHE_Disable();
    
    flashErase2.TypeErase = 2;
    flashErase2.Page = 96;
    flashErase2.NbPages = 1;
    flashErase2.Banks = 0;
    
    /* erasing page/sector */
    if (HAL_FLASHEx_Erase(&flashErase2, &error) != HAL_OK)
    {
     printf("Failed to erase. Error code 0x%x!!!\r\n\r\n", error);
    }
    HAL_FLASH_Lock();
    HAL_ICACHE_Enable();
    }

     

    And what I'm getting is: 
    Failed to erase. Error code 0x60!!!

    Super User
    October 17, 2025

    > flashErase2.Banks = 0;

    Invalid parameter. Should be FLASH_BANK_1.

    Visitor II
    October 17, 2025

    Doesn't make any difference. Tried with:

    flashErase2.TypeErase = FLASH_TYPEERASE_PAGES;
    flashErase2.Page = 96;
    flashErase2.NbPages = 1;
    flashErase2.Banks = FLASH_BANK_2;

     

    george-metasphereAuthorAnswer
    Visitor II
    October 21, 2025

    OK, I finally found the issue by accident. It was a Clock Configuration issue. Because I set my UART at 19200bps, the MSIS divider is downclocked and the entire system runs at 1.5Mhz instead of the stable 12Mhz. This apparently causes the EEPROM write to fail. Pushing the UART and clock speeds higher solves this issue.

    Visitor II
    October 22, 2025

    I actually don't understand why STM32U385VGT6Q and STM32U385RGT6Q start at different MSIS clock dividers.