Skip to main content
Associate III
September 2, 2025
Question

STM32C071RBT6 Internal Flash memory to write configuration data failed first write operation

  • September 2, 2025
  • 1 reply
  • 293 views

Hi,

I have the following code inside a function that writes data into flash memory. I noticed that on the first iteration of the for loop, when HAL_FLASH_Program is called, it fails to write at the flash starting address 0x0801F080. However, from the second iteration onward, it successfully writes data into flash. Because of this, the initial data is missing.

While debugging, I found that the failure occurs due to this line:

error = (FLASH->SR & FLASH_FLAG_SR_ERROR);

The error value I get is 128 (decimal).

is there anything I am doing wrong?

1.JYI, I have erased the memory before write to confirm this I am observing FF bytes 

2.To confirm again I have changed the address from 0x0801F080 to 0x0801F088 still same scenario occours 

bool write_function(void)

{

HAL_StatusTypeDef status.
uint64_t temp;
uint32_t i, j.
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_SIZERR | FLASH_FLAG_OPERR | FLASH_FLAG_PROGERR);

for (i = 0; i < 64; i += 4)
{
temp = 0;
for (j = 0; j < 4; j++)
{
if (i + j < 64)
{
temp |= ((uint64_t)data[i + j]) << (16 * j);
}
}
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,address + (i * 2), temp);
if (status != HAL_OK ||*(volatile U64 *)(address + (i * 2)) != temp)
{
HAL_FLASH_Lock();
return false;
}
}HAL_FLASH_Lock();
return TRUE;

}

 Post edited by ST moderator to follow the community rules especially for sharing the code. Please read this post.

1 reply

ST Employee
September 4, 2025

Hello @curiocity,

This post has been escalated to the ST Online Support Team for additional assistance. We'll contact you directly.

Best regards,
Maxime

​In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.