How to store variables in onchip flash

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

Posts: 2
Joined: Thu Mar 04, 2010 4:39 am

How to store variables in onchip flash

Thu Mar 25, 2010 9:37 am

Hi all,

I want to store an initialized variable in onchip flash of AT91SAM3U4E.
The variable should be modifiable and should retain the modified value after power off also.

I am trying to store a variable as shown below.

#define FLASH _Pragma("location=\"FLASH\"")
FLASH unsigned short F_Var=10;

2)__no_init unsigned short F_Var=10;

in both the cases the variable F_Var can not retain the modified value. After powerup it shows the same value 10 instead of modified value.

How I can achieve this?
Thank You
Posts: 90
Joined: Thu Feb 25, 2010 5:02 pm

Re: How to store variables in onchip flash

Fri Mar 26, 2010 12:29 am

If only it was that simple. :(

Unfortunately you will have to do some work to get where you want to be. Here is a list of very high level items you will have to complete (in no particular order) to update your persistent variable.

* Reserve a area of flash you want to use for persistent variables (i.e. data you would like to store over power cycles) typically this is done in the linker file by defining a new segment, you will also have to update your application segment definition so it doesn't overlap with your newly defined segment for persistent variables.
* Write initialization code that will configure the embedded flash controller (EFC) for use.
* Write code that can write data to flash using the EFC.
* (optionally) write code that can verify the write to flash and it's integrity and do something if the write fails or the data did not write properly.

For Example here is a high level view of what you might implement:

Code: Select all

void Flash_Init();
bool Flash_Write( void *source, void *destination, size_t numBytes );

// The "location" is the area you reserved in flash via the linker file
// Author Note: This definition and use may not be correct. I would take
// a different approach using my tools (Crossworks).
#define MyPersistantVars _Pragma("location=\"PERSISTENT_VARS\"")
MyPersistantVars unsigned short F_Var=10;

// A simple program that shows from a high level what is needed to 
// persist a variable into flash
void main( void )
    unsigned short temp;
    bool success;

    // Initialize the EFC for use

    // Get the current value from flash, change it and store it back to flash
    temp = F_Var;
    success = Flash_Write( &temp, &F_Var, sizeof( F_Var ) );

    if( success == false )
        // TODO: Handle error write to flash

    // End.
    while( 1 )

// You may or may not need this function as some tool sets generate 
// the code to initialize the EFC in their startup code.
// For example Crossworks compiler is one tool that will do this.
void Flash_Init( void )
    // TODO: Initialize the EFC for use

//           TO THE FLASH.
// Flash is normally written in pages (e.g. 256 bytes at a time)
// Inputs
//     source - address of the data to write
//     destination - address in flash to store the data
//     numBytes - the number of bytes to write
bool Flash_Write( void *source, void *destination, size_t numBytes )
    // TODO: Page writing code.

    // OPTIONAL: Verify the write was successful
    // If successful return true(1) otherwise return false(0)
This by no means is the only way to implement this. But this should give you an idea of what you will have to do.

For more information read the datasheet information for the Embedded Flash Controller (EFC) and download the sample code from Atmel. Take a look at the "basic-internalflash-project*" from the sample code. ...
Duane P. Fridley, IEEE CSDP
Viable Bytes, Inc.
Posts: 8
Joined: Sat Oct 17, 2009 3:13 pm

Re: How to store variables in onchip flash

Tue May 18, 2010 9:12 pm

Better to put an SPI memory chip on your board!

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 4 guests