cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G0 - internal flash read operation returns always 0

ADel .3
Associate

Hello

I have a const variable located in the internal flash memory. When I directly access this variable in flash, the read operation returns always 0 (even when the content of that location in flash is not 0). When I change the code to first copy the variable from flash to ram, the correct values are read and next comparisons work.

Code which returns always 0:

volatile const t_Control mControl __attribute__((section(".saved_context")));

  if ( mControl.MemoryInitialised == MEMORY_INITIALISED)

  {

    ....

    if (wChecksum == mControl.Checksum)

    {

....

    }

  }

Code which returns the correct values:

  t_MemoryControl wControl;

  stooMemCopy((uint8_t*)&mControl, (uint8_t*)&wControl, sizeof(t_Control));

  if ( wControl.MemoryInitialised == MEMORY_INITIALISED)

  {

    ....

    if (wChecksum == wControl.Checksum)

    {

....

    }

  }

Do you have an idea why the direct to flash the variable in flash always return 0 and the comparisons always fail. But when I copy the content of the variable from flash to ram, the values are correct and the comparisons work.

I tried to set a wait state for the flash access but the problem remains. When I look at the assembly, the R3 register contains the correct value of the variable in the flash memory location but the instruction ldr   r3, [r3, #0] returns 0.

Did you already face this kind of issue?

Thanks for your answer

3 REPLIES 3

You can't write to Flash with random memcpy()'s

The pages need to be erased, and you get to write the minimum word lengths a single time.

Perhaps look at the CubeG0 directories, and examples for FLASH for your MCU / BOARD

STM32Cube_FW_G0_V1.4.1\Projects\NUCLEO-G031K8\Examples\FLASH\FLASH_EraseProgram

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

> When I look at the assembly, the R3 register

> contains the correct value

Value?​

> of the variable in the flash memory location

> but the instruction ldr   r3, [r3, #0] returns 0.

Post screenshots.

JW​

ADel .3
Associate

Hello

Thanks for your answer.

Of course, in my case, the problem is not in the write access to the flash. The write access works perfectly. The problem comes from the read that I perform at startup to check if the variable mControl in flash contains the correct value (typically a pattern like 0xAA55) which I know it is really present in the flash at that location (0x803FE58); I verified it by using an external Flasher interface to re-read the memory.

The strange behaviour happens when I read directly the memory location with the following code. The register r3 is initialized with the correct address (0x803FE58) of the variable I want to read from flash. But when the ldr r3, [f3, #0] is executed, the value 0 is loaded in r3 (should be 0xAA55).

438      if ( mControlContextAppli.MemoryInitialised == MEMORY_INITIALISED)

0802092a:  ldr   r3, [pc, #112] ; (0x802099c <pmmaGetContextAppli+140>)

0802092c:  ldr   r3, [r3, #0]

0802092e:  ldr   r2, [pc, #112] ; (0x80209a0 <pmmaGetContextAppli+144>)

08020930:  cmp   r3, r2

08020932:  bne.n  0x802097c <pmmaGetContextAppli+108>

I verified also that it's not a problem of memory alignment. It's also not a problem of wait states.

I use the GNU GCC compiler and the STM32CubeIDE tool for debugging.

It looks like the assembly instruction is not executed properly which I know is a little bit weird.

The stooMemCopy procedure is like the memcpy procedure used to copy the complete variable from flash to ram. In this case, the variable is correctly transferred from flash to ram and then the comparison works correctly.