# Put saved data in flash of STM32 at the end of all other sections

I want to emulate EEPROM over flash of STM32F103. Is there a way to use all free flash instead of the only page? How can I initialize starting point of my EEPROM emulation so that it would be after all sections? I tried to add section after .text like

.text: ...

.myvars :
{
. = ALIGN(1024);
KEEP(*(.myvars));
KEEP(*(.myvars*));
} > rom


and define variable as

__attribute__((section(".myvars"))) static const flash_storage Flash_Storage = {
.magick = "1234567890absdef0987654321",
.all_stored = USERCONF_INITIALIZER
};


but it doesn't helps me: I see some more data after this structure in binary dump.

UPD: the solution of this problem quite simple. The section .myvars should be after last section in ram, i.e. after .data!

• Here's a link to ST's EEPROM emulation, in case you haven't seen it. There may be insights in their source code. – bitsmack Jul 10 at 18:58
• @bitsmack, sounds funny, but I can't find sources of ST's EEPROM emulation. In the RM there's nothing about memory structure. – Eddy_Em Jul 10 at 19:11
• Their website is strange, indeed :). Here's the software pack for STM32F103/105 microcontrollers. – bitsmack Jul 10 at 20:06
• @bitsmack, all OK! I just add section .myvars after section .data and now all fine! Also I have an error: STM32F103 page size is 1024 bytes, not 2048. – Eddy_Em Jul 10 at 20:19
• Great! Glad to know it worked :) Instead of updating your question, you should add your own answer! This will allow the database to work correctly, and we can also give you upvotes. :) – bitsmack Jul 10 at 20:24

The simplest solution of such problem is to put the section .myvars after latest section in ram. In my case it should be after section .data. The full linker file:

MEMORY
{
rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
}

ENTRY(reset_handler)

SECTIONS {
.vector_table 0x08000000 :
{
_sisrvectors = .;
KEEP(*(.vector_table))
/* ASSERT(. == _isrvectors_tend, "The vector table needs to be 84 elements long!"); */
_eisrvectors = .;
} >rom

.text :
{
. = ALIGN(4);
_stext = .;
*(.text*)
*(.rodata*)
. = ALIGN(4);
_etext = .;
} >rom

.ARM.extab :
{
} >rom

.ARM : {
*(.ARM.exidx*)
} >rom

.data :
{
. = ALIGN(4);
_sdata = .;
*(.data*)
. = ALIGN(4);
_edata = .;
} >ram AT >rom

.myvars :
{
. = ALIGN(1024);
KEEP(*(.myvars))
} > rom

.bss :
{
. = ALIGN(4);
_sbss = .;
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} >ram
}

PROVIDE(_stack = ORIGIN(ram) + LENGTH(ram));


Here is the code.