Atmel website | ARM Community | AVR freaks | Technical Support
Banner
 FAQ •  Search •  Register •  Login 

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Writing and Reading from embedded flash
PostPosted: Tue Aug 31, 2010 2:41 pm 
Offline

Joined: Fri Aug 13, 2010 11:38 am
Posts: 7
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!


Top
 Profile  
 
 Post subject: Re: Writing and Reading from embedded flash
PostPosted: Thu Sep 02, 2010 2:52 am 
Offline

Joined: Thu Feb 25, 2010 5:02 pm
Posts: 88
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,

_________________
Duane P. Fridley, IEEE CSDP
Viable Bytes, Inc.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: