Atmel website | ARM Community | AVR freaks | Technical Support
Banner
Welcome to AT91SAM Community Forum
http://www.at91.com/samphpbb/

Writing and Reading from embedded flash
http://www.at91.com/samphpbb/viewtopic.php?f=15&t=19521
Page 1 of 1

Author:  rdamoise [ Tue Aug 31, 2010 2:41 pm ]
Post subject:  Writing and Reading from embedded flash

Hi all,

I am using the AT91SAM7S512 for quite a while now in one of my projects.

In order to store parameters, i would like to save them in the embedded flash. I pretty much figured out how to write, wich seems to work fine now, but i am not able to read the saved data back.

I realy don't have a clue of how to tackle this situation.

I would be delighted if someone could provide me with some sort of example!

Thanks in advance!

Author:  dfridley [ Thu Sep 02, 2010 2:52 am ]
Post subject:  Re: Writing and Reading from embedded flash

Hello -

If you have the address where you wrote the data you can dereference a pointer to the data.

eg.

myInteger = *((int *)0x00104000);

Here's some contrived examples...

Example 1
Code:
// Assumes the following prototype
void WriteFlash( void *src_, void *dst_, unsigned int len_ );

void foo( void )
{
    int myInteger = 10000;

    // Store the integer into flash at address 0x00104000
    WriteFlash( &myInteger,
                (void *)0x00104000,
                sizeof( int ) );

    // Verify that the ram copy matches the flash copy
    if( myInteger == *((int *)0x00104000) )
    {
        // ...
    }
}


Example 2 (struct's)
Code:
// Assumes the following prototype
void WriteFlash( void *src_, void *dst_, unsigned int len_ );

typedef struct
{
    bool isConfigured;
    float setPoint;
    unsigned int counter;
}ParameterStruct;

const ParameterStruct *pFlashParamsAddr = (ParameterStruct *)0x00104000;

static ParameterStruct ramcopyParams;

void foo( void )
{
    // Initialize ram struct
    ramcopyParams.isConfigured = true;
    ramcopyParams.setPoint = 10.25f;
    ramcopyParams.counter = 10000;

    // Write all params to flash
    WriteFlash( &ramcopyParams,
                pFlashParamsAddr,
                sizeof( ParameterStruct ) );

    // Verify that the ram copy matches the flash copy
    if( ramcopyParams.isConfigured == pFlashParamsAddr->isConfigured )
    {
        // ...
    }
    if( ramcopyParams.setPoint == pFlashParamsAddr->setPoint )
    {
        // ...
    }
    if( ramcopyParams.counter == pFlashParamsAddr->counter )
    {
        // ...
    }
}


Example 3
Code:
// Assumes the following prototype
void WriteFlash( void *src_, void *dst_, sizeof len_ );

// Flash addresses for parameters
const bool *pFlashIsConfigured    = (bool *)0x00104000;
const float *pFlashSetPoint       = (float *)(0x00104000 + 1);
const unsigned int *pFlashCounter = (unsigned int *)(0x00104000 + 1 + 4);

static bool isConfigured;
static float setPoint;
static unsigned int counter;

void foo( void )
{
    // Initialize parameters
    isConfigured = true;
    setPoint = 10.25f;
    counter = 10000;

    // Write parameters to flash
    WriteFlash( &isConfigured,
                pFlashIsConfigured,
                sizeof( bool ) );
    WriteFlash( &setPoint,
                pFlashSetPoint,
                sizeof( float ) );
    WriteFlash( &counter,
                pFlashCounter,
                sizeof( unsigned int ) );

    // Compare the ram copies to the stored flash copies
    if( isConfigured == *pFlashIsConfigured )
    {
        // ...
    }
    if( setPoint == *pFlashSetPoint )
    {
        // ...
    }
    if( counter == *pFlashCounter )
    {
        // ...
    }
}


Note: You will want to reserve a block of flash for your parameters via your linker definition file.

My pref is the struct with a data integrity value, I also like to get the address from the linker instead of hard coding it into the source...

Regards,

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/