Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 3 > EEPROM to Float

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



EEPROM to Float
Moderator:
RKRM

Post Reply
Follow this topic



EEPROM to Float

gogglygogol posted on 23 Oct 2012 5:03 AM PST
Member
4 Forum Posts

Hi,


I'm pretty new in programming and I have a Problem with reading out a Float Value from EEPROM.
 

To get the FloatValue in! the EEPROM i do this:

char *ptr = (char*) &FloatValue;

EEPROM_ByteWrite(*ptr++,0,0);
EEPROM_ByteWrite(*ptr++,1,0);
EEPROM_ByteWrite(*ptr++,2,0);
EEPROM_ByteWrite(*ptr++,3,0);

I think this works, but how can I get the value back?


Can you give me an example?


thanx




Re: EEPROM to Float

danaaknight posted on 23 Oct 2012 05:39 AM PST
Top Contributor
1773 Forum Posts

Often you have more than  just one varible to save/retrieve.

 

A good method is to create a structure for all the stuff you need to save,

then retrieve it. Here is a good exaple of this -

 

http://pastebin.com/X0N5NuF8

 

Note the example also handles an error return from the write f().

 

If you just want to retireve 1 float then use a pointer to float in addition to ptr to EE

and retireve pretty much like your write. Make sure byte order is same as write.

 

Regards, Dana.

 

 



Re: EEPROM to Float

Bob Marlowe posted on 23 Oct 2012 06:01 AM PST
Top Contributor
1768 Forum Posts

When writing byte-by-byte you should not increase the row number, but the byte number which is the last parameter of EEPROM_ByteWrite()

 

The pre-defined pointer CYDEV_EE_BASE points to the very begin of the EEPROM data, so you could use something like

char * Eptr = (char *) CYDEV_EE_BASE;

ptr = (char *)&FloatValue;

*Eptr++ = *ptr++;

*Eptr++ = *ptr++;

*Eptr++ = *ptr++;

*Eptr++ = *ptr++;

 

 

Happy coding

Bob



Re: EEPROM to Float

gogglygogol posted on 23 Oct 2012 06:49 AM PST
Member
4 Forum Posts

hmmm... i think i got the write in /read out procedure:

read 4 bytes:


FloatByte0 = CY_GET_REG8(0);
FloatByte1 = CY_GET_REG8(1);
FloatByte2 = CY_GET_REG8(2);
FloatByte3 = CY_GET_REG8(3);

but how can i get a float out of 4 Bytes???

thanks for your help



Re: EEPROM to Float

danaaknight posted on 23 Oct 2012 07:12 AM PST
Top Contributor
1773 Forum Posts

You have a declared float in RAM, which has an address.

 

When you wrote the 4 bytes, you obviously wrote them one

byte at a time, starting from the first byte.

 

So read those 4 EE bytes back using a ptr to the RAM float,

incing the ptr for both read EE and write RAM, and the float

is retrieved.

 

Regards, Dana.



Re: EEPROM to Float

danaaknight posted on 23 Oct 2012 07:18 AM PST
Top Contributor
1773 Forum Posts

At its most basic level -

 

*pTrram++ = *pTree++;

 

The right side has to have its pTree set to eebase + offset, eg. initialized to

where you wrote it to using the APIs.

 

Regards, Dana.



Re: EEPROM to Float

rolf posted on 23 Oct 2012 03:12 PM PST
Top Contributor
81 Forum Posts

Hi,

 

You could do something like this:

typedef union {
  unsigned char EEpromBytes[16];
  struct {
    float FloatVal;
    uint8 ByteVal;
    uint16 SomeMore;
  } Settings;
} EEpromStruct;

#define SettingsAddress CYDEV_EE_BASE
volatile EEpromStruct xdata EEpromData _at_ SettingsAddress; // The EEprom structure
EEpromStruct RAMcopy; // The copy in RAM

void main(void) {
  float FloatTest = 0x10203040;;

  RAMcopy = EEpromData; // Copy EEprom contents to RAM
  RAMcopy.Settings.FloatVal += FloatTest;
  EEPROM_Write((const uint8 *) &RAMcopy, 0); // Write RAMcopy to EEprom

  FloatTest = EEpromData.Settings.FloatVal; // Read FloatVal from EEprom to RAM

 

Remind that the EEPROM_Write always writes one full row (16 bytes). The structure reserves 16 bytes and you have to count your byte usage. If you don't like this, you may have to create your own EEPROM write routine.

I've not tested it though, so it may contains erors.

 

Regards,

Rolf



Re: EEPROM to Float

gogglygogol posted on 24 Oct 2012 09:23 AM PST
Member
4 Forum Posts

thank you, i try to understand what you are doing there...:-)



Re: EEPROM to Float

rolf posted on 25 Oct 2012 03:32 AM PST
Top Contributor
81 Forum Posts

 Okay...

 

First I create a union called EEpromStruct. In the union all the members are located at the same address. Inside this union there are two members: the EEpromBytes array and the Settings structure.

 

The members of a structure are sequentially placed in memory, thus first comes the FloatVal, next ByteVal etc.

 

Because the EEpromBytes array and Settings structure are within a union, their base address is the same. So FloatVal is located at EEpromBytes[0...3], ByteVal at EEpromBytes[4], SomeMore at EEpromBytes[5...6] etc.

 

The size of the union has always the size of the largest member, so it's for this example 16 bytes. That is intentionally matched with the size of one row. (the EEPROM_Write always writes one full row of 16 bytes).

 

Notice that the union has a typedef in it's declaration. This means it does not actually reserve memory but it tells the compiler about the memory layout of the union (or structure).

 

Next, I define a compiler directive called SettingsAddress and set it to the start of the PSoCs EEprom base location (CYDEV_EE_BASE).

The actual placement of the EEpromStruct union at the EEprom area is done at:  volatile EEpromStruct xdata EEpromData _at_ SettingsAddress;

 

Then I also place the same EEpromStruct to RAM by: EEpromStruct RAMcopy;

 

Now everything is set for the compiler to work with. Next we can use the struct and it's members to read and write to.

 

By RAMcopy = EEpromData; the contents of the EEpromData struct (resides at PSOCs EEprom location) is placed to the RAMcopy structure. The compiler just copies the complete structure for you to RAM.

 

By RAMcopy.Settings.FloatVal += FloatTest; you are just accessing the FloatVal member of the RAM structure and increasing it with FloatTest.

 

The EEPROM_Write next writes the 16 bytes RAM structure to Row 0. Row 0 is located at CYDEV_EE_BASE. You can use Row 1 also by using (CYDEV_EE_BASE + 16). The (const uint8 *) &RAMcopy is casting the base address (&RAMcopy) to a const uint8 pointer as this is what the library function would accept.

At least I showed that it's also possible to just use a member of the EEprom structure and copy it to RAM.

 

You can also directy use the EEprom location within your program like: if (EEpromData.Settings.ByteVal > 0x10) FloatTest += 0x10;

 

Hope this helps you!

 

Regards,

Rolf



Re: EEPROM to Float

rolf posted on 25 Oct 2012 04:22 AM PST
Top Contributor
81 Forum Posts

Oh and if you prefer the pointer way to read the value:

Assume your FloatVal is at the first 4 bytes of Row 0:

FloatVal = * (float *) CYDEV_EE_BASE;

 

Regards,

Rolf



Re: EEPROM to Float

gogglygogol posted on 30 Oct 2012 02:48 AM PST
Member
4 Forum Posts

thank you for your support, i solved the problem...for people who have a similar problem:

writing Float to EEPROM:

        // break down the Float to its 4 bytes...
        for(i=0;i<4;i++)
        {
            Byte[i] = *((uint8*)&Fl+i);       
        }
   
       
        //...and write them to EEPROM
        for(i=0;i<4;i++)
        {
            EEPROM_ByteWrite(Byte[i], 0,i);   
        }

reading it out:

    //read out the bytes...

    for(i=0;i<4;i++)
    {
        Byte[i] =CY_GET_REG8(CYDEV_EE_BASE + i);   
    }
 

   //...and assemble the floatvalue
    for(i=0;i<4;i++)
    {
        *((uint8*)&Fl+i)=Byte[i];   
    }



Re: EEPROM to Float

rolf posted on 01 Nov 2012 08:09 AM PST
Top Contributor
81 Forum Posts

 Okay, in that case (to save ram and have more readable code), I would rewrite your code:

 

Writing Float to EEPROM:

    // No need to break down Float

    //...write to EEPROM

    EEPROM_Write((const uint8 *) &Fl, 0); // Write complete row

Reading it out:

    // read out the float...

    Fl =CY_GET_REG32(CYDEV_EE_BASE);    // Read 4 bytes to Fl

Regards,

Rolf






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20