Topic : STM32F0 runtime storing data in flash

Forum : ARM

Original Post
Post Information Post
February 25, 2013 - 8:28pm
Guest

I am trying to store some runtime data into flash and it is instead storing into RAM. I need it to persist so after power cycle I can read it back in. I have this in my header file:

// base address for flash storage
#define BASE_ADDRESS ((uint32_t)0x08006000)

I've also tried different values for the above but it seems the data is always stored at 0x20000054

Not sure what I have wrong but this sticks out, in my LD Linker setup, it calls out crt0_STM32.o and I see this file doesn't exist??

Replies
Post Information Post
+1
0
-1
February 25, 2013 - 8:49pm
Guest

Ride v7.42.12.0305
RKit v1.46.12.0305

+1
0
-1
February 26, 2013 - 1:55pm
Raisonance Support Team

Hi,

I'm afraid it's more complex than a #define. (much)
And changing this define, if as I guess it's the one from the ST lib (?), will make other things fail.

For doing this you need to use a custom linker script (usually copied from the default as starting point), then in this script create a new MEMORY region and a new SECTION for your Flash storage, and then in the source code use "_attribute_" to place your variables in your new section.

Also be careful that for writing Flash you cannot use simple C affectations. You need to use the Flash writing functions from the ST lib.

See the GettingStartedARM document for how to use a custom linker script and how to find the default script for your CPU.
See the GCC doc and example scripts for how to create MEMORY regions and SECTIONs in your linker script.
See the ST lib doc for how to use it for writing in Flash.

I hope it helps.

Best Regards,

Vincent

+1
0
-1
February 26, 2013 - 10:16pm
Guest

Thanks, got it!
Turns out first thing was the address I was trying to use was out of the range of the chip's flash size. I also did make a custom linker script, pretty easy with a couple posts I found.

+1
0
-1
February 26, 2013 - 10:40pm
Guest

OK, spoke too soon. I can only write once and the ST datasheet says the value should be 0xFFFF before writing, which proves the write once. I didn't seem to need to use the "_attribute_" declaration and if I try to use it, it says I've reached my debugging code size limit. What am I missing?

/* Memory Spaces Definitions */   /* STM32F051R4 */

MEMORY
{
  RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K
  FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K-1K
  STORAGE_REGION (rx) : ORIGIN = 0x08002000, LENGTH = 1K
  STARTFLASH (rx) : ORIGIN = 0x0, LENGTH = 0
  CRPPATCH (r) : ORIGIN = 0x0, LENGTH = 0
  FLASHPATCH (r) : ORIGIN = 0x00000000, LENGTH = 0
  ENDFLASH (rx)  : ORIGIN = 0x00000000, LENGTH = 0
  FLASHB1  (rx)  : ORIGIN = 0x00000000, LENGTH = 0x0
  EXTMEMB0 (rx)  : ORIGIN = 0x00000000, LENGTH = 0
  EXTMEMB1 (rx)  : ORIGIN = 0x00000000, LENGTH = 0
  EXTMEMB2 (rx)  : ORIGIN = 0x00000000, LENGTH = 0
  EXTMEMB3 (rx)  : ORIGIN = 0x00000000, LENGTH = 0
}

/* higher address of the user mode stack */
_estack = 0x20001000;

/* avoid link error if ___SK_limit___ is not defined at all */
PROVIDE ( ___SK_limit___ = 0 ) ;


/* Sections Definitions */

SECTIONS
{
    /* place data from storage_section section in STORAGE_REGION memory region (in the linker script)*/
    .storage_section :
    {
    . = ALIGN(4);
        *(.storage_section)
    } >STORAGE_REGION

// base address for flash storage
#define BASE_ADDRESS ((uint32_t)0x08002000)

+1
0
-1
February 28, 2013 - 3:19pm
Raisonance Support Team

Hi,

Well, I can already tell you that you'll have problems if you declare memory regions that overlap as FLASH and STORAGE_REGION do in your script. ;)

Where does the "BASE_ADDRESS" quote come from? Does it come from an example or library? (which one? from who?)

The message about debug limit comes because you used as starting point a default linker script generated for a project that had the "Starter Kit Limit" option activated. See the GettingStartedARM doc for what this option means. You should start again from a script generated for a project that does not have this option, which will remove this check and message from the script. Then you might have (or not) another message like 'memory full' which is what really happens for what the linker sees. Of course if you are in Lite with a standard RLink, then you must refrain from using the second half of the Flash, otherwise you'll not be able to debug the application. (you can compile and program it, no problem, but not debug it)

I hope it helps.

Best Regards,

Vincent

+1
0
-1
February 28, 2013 - 4:21pm
Guest

The BASE_ADDRRESS is defined in my header file. Turns out I didn't need a custom linker script at all. I just need to erase the page before writing to it again.

+1
0
-1
February 28, 2013 - 6:26pm
Raisonance Support Team

Hi,

Glad to hear that you are making progress.

I still think you'll need a custom linker script, if only to prevent the linker from placing data or code in the area that you want to use as data flash. But for this you don't need to create a new region and a new section. Just reduce the existing Flash region.

Best Regards,

Vincent