Writing/reading a float or mixed variable structure to EEPROM

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

 Hi,

   

I was wondering if anyone had any examples of how to write a floating point number to Em_EEPROM.  I remember reading something somewhere along these lines on these forums (though I cannot find it now) about accomplishing this, but do not recall how it was done.

   

Thank you in advance for any help!

   

Tom

0 Likes
14 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Declare a static const var or structure and initialize  it, without initialization it will reside in ram. Now you may

   

Result = EmEEPROM_Write((void *)(&Variable or structure), sizeof(Variable or structure));

   

Do not forget to check the result for successful operation.

   

 

   

Bob

0 Likes
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

 Hi,

   

Thank you for the information.  Is the below code a reasonable test?

   

// Sets up data structure

   

    struct Data

   

    {

   

        float pi;

   

        char title[10];

   

        int number;   

   

    }; 

   

    

   

    struct Data Data1;

   

    struct Data Data2;

   

    

   

    strcpy(Data1.title, "Testing");

   

    Data1.pi = 3.14159;

   

    Data1.number = 512;

   

    

   

    Em_EEPROM_Write((void *)(&Data1),eepromArray,sizeof(Data1));

   

    

   

    // Read flash array back

   

    memcpy((void *)(&Data2),eepromArray,ARRAY_SIZE);

   

    

   

    float pi = Data2.pi;

   

Thank you,
Tom

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

I think Bob meant checking the error code returned by the EEPROM_Write() method.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

No, that is not correct!

   

You must initialize your structure with ists declaration as

   

static const   struct Data

   

    {

   

        float pi = 3.14; // You may use 4 * atan(1.0)

   

        char title[10] = "Nothing";

   

        int number = 666;   

   

    }; 

   

   

   

 Bob

odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

@tvdesign

   

I don't know if this may help:

   

http://www.cypress.com/?app=forum&id=2492&rID=105370

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

I assume your ARRAY_SIZE is 16 ?

   

 

   

Regards, Dana.

0 Likes
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

 Hi,

   

Sorry for only posting part of my code.  I did initialize the structure, and it works great!

   

Thanks all,
Tom

0 Likes
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

 Hi Bob,

   

I tried to initialize the struct as you put it:

   

static const   struct Data

   

    {

   

        float pi = 3.14; // You may use 4 * atan(1.0)

   

        char title[10] = "Nothing";

   

        int number = 666;   

   

    }; 

   

 

   

The compiler gives me an error if I try to initialize it in the struct declaration.  Do you know what the issue is?

   

Thank you,
Tom

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Whats the actual error you get?

   

What you did in your code is to name the type of the struct ('Data'), but not the variable itself (that needs to come after the definition). The proper way for initializing a const struct is

   
static const struct StructName{  float pi;  char title[10];  int number;} varName={3.14, "Nothing",666};   
0 Likes
lock attach
Attachments are accessible only for community members.
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

 Attached is a stripped down version of what I am trying to do.  What I want to do is store the 'eepromData' struct in EEPROM, and recall it at start up, and write to EEPROM when a value changes.  

   

If I make it a const, I cannot write to it.  Do I use a different struct to write to the EEPROM?

   

What the program is doing is checking to see if the EEPROM is empty, and if so (like on initial startup) it writes initialization values into the struct and writes it to EEPROM.  Then it posts to the LCD if it was an inital run or not (i.e. if there were any values found in EEPROM).  It is not stroring into EEPROM, because after power down, it still prints 'Initial'.

   

Please let me know where I went wrong.
Thank you,
Tom

0 Likes
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

 I already spotted one error, my brackets are off in the data check...

0 Likes
ToVa_285016
Level 5
Level 5
100 replies posted 50 replies posted 50 questions asked

 The code seems to work fine with the following substituted:

   

    // Read flash array back and check to see if initial data write is needed

   

    memcpy((void *)(&dataLocal),(void *)&eepromArray,sizeof(dataLocal));

   

    if(dataLocal.averages == 0){dataLocal.averages = 10;dataFlag = TRUE;}

   

    if(dataLocal.baseline == 0){dataLocal.baseline = 150;dataFlag = TRUE;}

   

    if(dataLocal.gain == 0){dataLocal.gain = 1;dataFlag = TRUE;}

   

    if(dataLocal.genre == 0){dataLocal.genre = 1;dataFlag = TRUE;}    

   

    if(dataFlag){WriteEEPROM();}

   

    else{dataLocal.baseline++;WriteEEPROM();}

   

The baseline counts up after each power cycle.

   

Is this still incorrect?  What is the best practice for a situation like this.
Thank you,

   

Tom

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Did you look at the example project for the EM_EEPROM component? (Right-click on the component, "Find Example project"). It shows you how its supposed to work.

   

What you need is a 'const uint8' array thats placed in Flash, and a corresponding buffer in sRAM. YOu can read from Flash memory directly, but for writing you place the data into the buffer, and then write the buffer to the array (using Em_EEPROM_Write).

   

The compiler is right, a variable defined as const is non-modifiable and cannot be written to. Actually I think your sample project is quite OK.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You can take any format struc and store/retrieve from FLASH, EE.

   

 

   

Basically you know the size of the struc, you know the address of the first

   

byte element in RAM and FLASH, so you create a struc in RAM, then

   

when you read the FLASH version just use an incrementing pointer in a

   

for() loop to fill the RAM struc byte for byte.

   

 

   

So FLASH does not have a struc per say, just a byte for byte copy of the struc that you

   

defined in RAM.

   

 

   

The same is true of the writing, you give the API the source pointer, apointer to

   

the first byte of the struc, the dest pointer, and the size of struc, the rest is taken

   

care of. So you write the struc in RAM out to FLASH byte by byte.

   

 

   

Regards, Dana.