STM32u535 Struggling with FLASH_WaitForLastOperation
I have flash update operations working fine on an STM32U535 with 512kB flash, but am getting an error on a 256kB device. Driving into the debugger, I find it coming out of the FLASH_WaitForLastOperation HAL routine, specifically after: reg_sr = IS_FLASH_SECURE_OPERATION() ? &(FLASH->SECSR) : &(FLASH_NS->NSSR) which returns 0b10001000, which winds up being the FLASH_NSSR_PROGERR, and FLASH_NSSR_PGSERR. I am not sure why. Code segment enclosed here:
const uint32_t NVRAM = 0x0803E000;
FLASH_EraseInitTypeDef EraseInitStruct = {0};
HAL_StatusTypeDef hfError;
uint32_t PAGEError = 0; // To hold any page error code
uint32_t pageNumber = (NVRAM - 0x08000000) / 0x2000;
uint32_t progAddress = NVRAM;
uint64_t *pSource = (uint64_t)&memory[0x1800]; // 1800
uint32_t quadWordCount = 0xFF / 16;
if (HAL_FLASH_Unlock() != HAL_OK) {
printf("Cannot erase FLASH memory.\n\r"); // Handle error, e.g., print error message
return;
}
// Clear OPTWERR bit set on virgin samples
__HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PROGERR);
// 2. Erase the data structures in the first bank of Flash memory
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Page = pageNumber; // Set the specific page to erase
EraseInitStruct.Banks = FLASH_BANK_1; // Or the correct bank for your device
EraseInitStruct.NbPages = 1; // Number of 8kByte pages to erase
if (hfError = HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_FLASH_ERROR_NONE) {
printf("FLASH erase error Bank 1.\n\r");
goto exit_function;
}
if (HAL_FLASH_Unlock() != HAL_OK) {
printf("Cannot erase FLASH memory.\n\r"); // Handle error, e.g., print error message
return;
}
for (uint32_t i = 0; i < quadWordCount; i++)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, progAddress, (uint32_t)pSource) != HAL_OK)
{
printf("Error Programming data.\n\r");
goto exit_function;
}
progAddress += 4;
pSource += 2;
}
and, of course the HAL_FLASH_Program call is the one failing. Erasing seems OK. What am I missing
