10 Replies Latest reply on Nov 1, 2012 10:09 AM by RoNo_264271

    EEPROM to Float



      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;


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


      Can you give me an example?



        • 1. Re: EEPROM to Float

          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 -








          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.

          • 2. Re: EEPROM to Float

            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



            • 3. Re: EEPROM to Float

              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

              • 4. Re: EEPROM to Float

                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.

                • 5. Re: EEPROM to Float

                  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.

                  • 6. Re: EEPROM to Float





                    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.







                    • 7. Re: EEPROM to Float

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

                      • 8. Re: EEPROM to Float





                        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!







                        • 9. Re: EEPROM to Float

                          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;







                          • 10. Re: EEPROM to Float

                            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...
                                        Byte[i] = *((uint8*)&Fl+i);       
                                    //...and write them to EEPROM
                                        EEPROM_ByteWrite(Byte[i], 0,i);   


                            reading it out:


                                //read out the bytes...


                                    Byte[i] =CY_GET_REG8(CYDEV_EE_BASE + i);   


                               //...and assemble the floatvalue

                            • 11. Re: EEPROM to Float

                               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