Constant tables in flash memory

Discussions concerning TrueSTUDIO for STM32 9.0.0 and later versions.

Moderators: Markus Girdland, Mattias Norlander

Posts: 115
Joined: Wed Apr 05, 2017 5:08 pm
Location: USA

Constant tables in flash memory

Postby frank_ee » Sun Feb 10, 2019 9:50 pm


How to place a constant array in the flash memory? I can place const data in the flash using the "__attribute__" keyword along with updating the linker script, but how about arrays? An application of this will be for a table of values in the flash memory that are predetermined.

For example,

const int8_t codes[MAX_CODE_SIZE] = { 0x00, 0x01, 0x02 };

Posts: 6
Joined: Mon Feb 11, 2019 2:35 am

Re: Constant tables in flash memory

Postby chriskeeser » Mon Feb 11, 2019 4:29 pm

The values are stored in flash (how else will they be initialized on program start?) but I think what you are requesting is to not have those values loaded into RAM. Using your __attribute__, place them in a special linker section of memory marked as belonging in FLASH only.

Posts: 115
Joined: Wed Apr 05, 2017 5:08 pm
Location: USA

Re: Constant tables in flash memory

Postby frank_ee » Mon Feb 11, 2019 8:12 pm


The reason behind is to place constant data into a dedicated page in the flash. Let's say the last page of flash for the STM32F411VE for constant data. Also, the last page can be used to write new data to the flash using the HAL flash library.

I read the instructions to modify the linker script file, but the constant data was not seen in the debugger.

The issue is resolved by:

Added CONST_DATA section to the last page of flash:
/* Specify the memory areas */
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 384K
CONST_DATA (rx) : ORIGIN = 0x08060000, LENGTH = 128K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K

Added to linker script:

/* Constant data section at CONST_DATA */
.const_data :
. = ALIGN(4);

Added to source code: (note, do use the const keyword before "unint32_t". The variables will be allocated to flash starting at address 0x08060000.

__attribute__((section(".const_data.VERSION_NUMBER"))) uint32_t VERSION_NUMBER[2] = { 0x55555555, 0x22222222 };
__attribute__((section(".const_data.VERSION_NUMBER2"))) uint32_t VERSION_NUMBER2 = 0xAAAAAAAA;

Added to source code: (constant data allocated to flash must be read or used by the application)
static uint32_t vn[2];
static uint32_t vn2;

vn[0] = VERSION_NUMBER[0];
vn[1] = VERSION_NUMBER[1];

The debugger shows 0x55555555, 0x22222222, 0xAAAAAAAA in the memory browser starting at address 0x08060000.

Return to “TrueSTUDIO for STM32 discussions”

Who is online

Users browsing this forum: No registered users and 1 guest