Skip to main content
JR2963
Senior II
April 11, 2025
Question

Problem writing to Flash - Function returns success but memory remains 0xFF

  • April 11, 2025
  • 1 reply
  • 1100 views

Hi,

I'm working with STM32WB (with BLE) and trying to write to Flash in a custom section (contains version info, flags, etc.). The target flash memory initially contains 0xFFFFFFFFFFFFFFFF (confirmed in debugger), and I’m trying to write a uint64_t value like 0x0000000000000000. I'm using a function FD_WriteData(...), which internally calls HAL_FLASH_Program(...) via FD_WriteSingleData(...).

Here is the FD_WriteData() function:

uint32_t FD_WriteData(uint32_t DestAddress, uint64_t * pSrcBuffer, uint32_t NbrOfData)
{
 uint32_t loop_flash;
 uint32_t return_value;
 SingleFlashOperationStatus_t single_flash_operation_status = SINGLE_FLASH_OPERATION_DONE;

 // Take semaphore (dual-core STM32WB)
 while (LL_HSEM_1StepLock(HSEM, CFG_HW_FLASH_SEMID));

 HAL_FLASH_Unlock();

 for (loop_flash = 0; (loop_flash < NbrOfData) &&
 (single_flash_operation_status == SINGLE_FLASH_OPERATION_DONE); loop_flash++)
 {
 single_flash_operation_status = FD_WriteSingleData(DestAddress + (8 * loop_flash),
 *(pSrcBuffer + loop_flash));
 }

 if (single_flash_operation_status != SINGLE_FLASH_OPERATION_DONE)
 {
 return_value = NbrOfData - loop_flash + 1;
 }
 else
 {
 HAL_FLASH_Lock();
 LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, 0);
 return_value = 0;
 }

 return return_value;
}

I call this function with a valid flash address (aligned to 8 bytes), and the function returns 0 (success), but when I inspect flash memory afterward (in debugger or memory view), the values are still 0xFFFFFFFFFFFFFFFF.

I'm trying to write into a "metadata" area that looks like this in the .ld file:

My app is in region  FLASH_APP1, and the area I want to write is FLASH_METADATA_APP1.

/* Specify the memory areas */
MEMORY
{
 /* Bootloader region */
 FLASH_BOOTLOADER(rx) : ORIGIN = 0x08000000, LENGTH = 32K

 /* METADATA region for App1 */
 FLASH_METADATA_APP1 (rx) : ORIGIN = 0x08008000, LENGTH = 1K 

 /* Application 1 region */
 FLASH_APP1 (rx) : ORIGIN = 0x08008400, LENGTH = 363K
 

This area holds a group of const variables, like this:

SECTIONS
{

 .metadata :
 {
 . = ALIGN(4);
 KEEP(*(.metadata.FLASH_CONTENT))
 KEEP(*(.metadata.TARGET_PROJECT))
 KEEP(*(.metadata.SW_VERSION_MAJOR))
 KEEP(*(.metadata.SW_VERSION_MINOR))
 KEEP(*(.metadata.SW_VERSION_PATCH))
 KEEP(*(.metadata.FW_FIRST_BOOT_FLAG))
 KEEP(*(.metadata.RUNABLE_FLAGS))
 . = ALIGN(4);
 } >FLASH_METADATA_APP1
...
 .metadata1 :
 {
 _start_of_metadata_1 = ORIGIN(FLASH_METADATA_APP1); 
 KEEP(*(.metadata1))
 _end_of_metadata_1 = ORIGIN(FLASH_METADATA_APP1) + LENGTH(FLASH_METADATA_APP1); 
 . = ALIGN(4);
 } >FLASH_METADATA_APP1

and in the app_data.c:

const uint32_t SW_VERSION_PATCH __attribute__((section(".metadata.SW_VERSION_PATCH"))) = ...;
const uint64_t FW_FIRST_BOOT_FLAG __attribute__((section(".metadata.FW_FIRST_BOOT_FLAG"))) __attribute__((aligned(8))) = 0xFFFFFFFFFFFFFFFF;

Flash content is unchanged — it still reads as 0xFF FF FF FF FF FF FF FF after the write 0x0

 

IMPORTANT: I can write to address which is in FLASH_APP1 region without problem! What does it mean?

1 reply

TDK
Super User
April 11, 2025

Are you erasing the flash before writing?

Perhaps simplify the example to write only a single double word.

"If you feel a post has answered your question, please click ""Accept as Solution""."
JR2963
JR2963Author
Senior II
April 11, 2025

I can not erase it, in the section are another important data. I just want to write to address 0x8008018  one double word (0x0). This place is filled with 0xFF from default.

TDK
Super User
April 11, 2025

If 0xFF (or anything else) has been written to it after erasing, you cannot change the value again. Doing so will yield an ECC error which may or may not be getting detected by the functions you're using.

"If you feel a post has answered your question, please click ""Accept as Solution""."