Page 1 of 3

Howto saving Data in Flash in Cortex M3/M4

Posted: Mon Apr 23, 2012 4:46 pm
by hoppel
Hello,

today, i have a question about the flash. We had a project, where we can save data into flash, using it like an eeprom.
Therefor we had a linkerscript.
Here is the beginning code:

Code: Select all

ENTRY(Reset_Handler)

MEMORY {
   RAM      (RWX) : ORIGIN = 0x20000000     , LENGTH = 20K
   EXTSRAM  (RWX) : ORIGIN = 0x68000000     , LENGTH = 0
   FLASH    (RX)  : ORIGIN = 0x08000000     , LENGTH = 126K /* adjust to Device -- Flash Size -2k*/
   EEMUL    (RWX) : ORIGIN = 0x08000000+510K, LENGTH = 2K
}

_estack         = ORIGIN(RAM)+LENGTH(RAM);      /* end of the stack */
_seemul         = ORIGIN(EEMUL);                /* start of the eeprom emulation area */
_min_stack      = 0x100;                        /* minimum stack space to reserve for the user app */


We found that script in www, it works fine. But i have no experience about that script language.
Now we switched to use Chibios, because we likes the idea behind a RT.
So my Question is, how i have to change the Chibios Linker scripts to use the flash for storage.

Regards,
Andre

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Mon Apr 23, 2012 6:38 pm
by Giovanni
Hi Andre,

The linker script in ChibiOS is very similar, you have to reserve some flash space for the EEPROM emulator, something like:

Code: Select all

MEMORY
{
    flash : org = 0x08000000, len = 112K
    EEMUL: org = 0x08000000 + 112K, len = 16K
    ram : org = 0x20000000, len = 20k
}


I think it would work like in your example.

Giovanni

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Mon Apr 23, 2012 9:14 pm
by hoppel
Ok, thank you. I will test it.

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Tue Jul 17, 2012 10:13 pm
by alex31
On 1024k flash device, the pages at beginning of address space are 16Ko long and can be erased in relatively short time.

the pages at the end of address space are 128ko long and can take 1 second to be erased, so if i want to use a 16ko page, is it possible
to reserve page 0 for EEPROM emulator, then put the program on page 1 and subsequent ?

Thanks
Alexandre

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Wed Jul 18, 2012 6:53 am
by barthess
Yes, it is possible. But in 0 sector you must place bootloader loading your program from proper sector.
EEMUL must reside in 1st (or any not 0th) sector.
Here you can find some discussion viewtopic.php?f=2&t=229

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Wed Jul 18, 2012 9:57 am
by alex31
Thanks for pointing this thread.

Did you resolve all the problem mentioned in the thread to make your program began at an offseted address ?

Alexandre

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Wed Jul 18, 2012 11:14 am
by barthess
Sure.

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Fri Nov 09, 2012 5:34 pm
by jeremie.delaitre
Hi,

I'm trying to save data (app settings) in flash too on the STM32P407 board.
I've changed my linker script with:

Code: Select all

MEMORY
{
    flash : org = 0x08000000, len = 500k
    persistentdata : org = 0x08000000 + 500k, len = 128k
    ram : org = 0x20000000, len = 112k
    ethram : org = 0x2001C000, len = 16k
    ccmram : org = 0x10000000, len = 64k
}

...

SECTIONS
{
...
    .persistentdata :
    {
        *(.persistentdata)
        KEEP(*(.persistentdata))
    } > persistentdata
...


Then, I've declared some variable with:

Code: Select all

__attribute__ ((section (".persistentdata")))
__IO uint32_t persistentValue;
__attribute__ ((section (".persistentdata")))
__IO uint32_t persistentValue2;


Everything compile as expected but when print persistentValue, then change its value and reprint it, I always got 0.

Do you have any hint?

I've read on the Internet that I should maybe erase the NAND page/sector before writing the new value (like when I flash my program with openocd) but how can I do that?

Saving app settings in flash seems to be a quiet common requirement so I'm wondering if it makes sense to have some helper in ChibiOS to do that?

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Fri Nov 09, 2012 6:06 pm
by mabl
I've not much time now. But for writing flash pages, I've once hacked this together:
https://github.com/mabl/ARMCM3-STM32F10 ... ADER/flash

Re: Howto saving Data in Flash in Cortex M3/M4

Posted: Mon Nov 12, 2012 10:20 am
by jeremie.delaitre
Awesome! I'll give it a try when time permits (probably next week).