2025-10-17 2:47 AM - last edited on 2025-10-17 6:05 AM by mƎALLEm
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.
Solved! Go to Solution.
2025-10-21 4:19 AM
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.
2025-10-17 5:59 AM
If it's crashing here:
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?
2025-10-17 6:25 AM
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!!!
2025-10-17 7:07 AM
> flashErase2.Banks = 0;
Invalid parameter. Should be FLASH_BANK_1.
2025-10-17 7:09 AM
Doesn't make any difference. Tried with:
flashErase2.TypeErase = FLASH_TYPEERASE_PAGES;
flashErase2.Page = 96;
flashErase2.NbPages = 1;
flashErase2.Banks = FLASH_BANK_2;
2025-10-21 4:19 AM
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.