cancel
Showing results for 
Search instead for 
Did you mean: 

How to allocate variable in Flash to desired address with CubeIDE?

FXuef.1
Associate II
  1. there is a same problem, but there is still no solution. I use STM32CubeIDE , meeting the same problem."https://community.st.com/s/question/0D50X00009XkZhJSAV/how-to-allocate-variable-in-flash-to-desired-address"
  2. I modify the project from Keil to CubeIDE, and need to store some data to Flash.
  3. I edit the .Id and .c according to: http://blog.atollic.com/using-gnu-gcc-on-arm-cortex-devices-placing-code-and-data-on-special-memory-addresses-using-the-gnu-ld-linker, but it doesn't work.
  4. my .Id file is shown as the attchment.
  5. and I add these in the main.c beform "int main(void)", but there is no data in the generated HEX file.

__attribute__((section(".myvars.VERSION_NUMBER"))) uint32_t VERSION_NUMBER = 0x12;

__attribute__((section(".myvars.CRC"))) uint32_t CRC16 = 0x12;

__attribute__((section(".myvars.BUILD_ID"))) uint16_t BUILD_ID = 0x12;

__attribute__((section(".myvars.OTHER_VAR"))) uint8_t OTHER_VAR = 0x12;

the mcu is stm32g474mb, please help me, thanks very much!

5 REPLIES 5

Does the .MAP file indicate the linker placed them somewhere, or discarded them?

Any other references to these variables stopping them from being discarded due to dead code removal?

Might want to work on the fact these sections overlap

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

AFAIK you cannot do it for gcc unless you spectic a secific section in the linker script and use the section attribute.

but, you can use a define like

#define FLASH_START         (*((const unsigned long *) 0x08000000))

Kraal
Senior III

Hello,

As @Community member​ suggested, it is likely related to dead code removal / unused data removal.

In order to force the linker to keep it I would add the line KEEP (*(.myvars*)) at the begining of .myvars

.myvars :
{
    KEEP (*(.myvars*))
    *(.myvars.VERSION_NUMBER)
    etc...
}

I'm not sure about the KEEP location, i.e. before or after the variables declaration.

Once the compilation is done, open the map file to check that it has been kept.

Best regards.

sorry I cant understand, and then how to save data to flash?

thanks

Flash is read-only memory unless you use dedicated registers+procedures for erasing and programming it. There is also a HAL_FLASH API for that.