This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to prevent overwriting data in external EEPROM after msp430 restarts urgent plz

Other Parts Discussed in Thread: CC430F5137

Hi,

   I have two sensors, one measures the temperature and the other measures pressure and I am storing the values of both sensors on 64 kb external eeprom .Temperature sensor,pressure sensor and external eeprom all are connected cc430f5137 by I2c interface.But when the battery supply for cc430f5137 is turned on again after turning off .then the temperature and pressure sensor values are written in external eeprom from the first location (0x0000).How should I avoid the overwrrite in eeprom so that after turning on battery again my program stores the values in EEPROM from where it was left previously.I need urgent help plz 

Regards

Faisal 

  • Record in the EEPROM the last address you wrote to at a known location (like 0x0000 or 0xFFFF).  On power up, read that address from the EEPROM and that becomes the new starting address.  When you get ready to power down, write the current address to the EEPROM so you can resume it on the next power cycle.

    If your power state is unpredictable, then you might need to write the address to EEPROM more frequently.

  • Dear James,

                    How can I read the last accessed address from EEPROM after msp430 restarts ,because it wil continue to execute its code from the beginning 

  • Faisal khan2 said:
    How can I read the last accessed address from EEPROM after msp430 restarts ,because it wil continue to execute its code from the beginning 

    You change your code.

    At startup you check the magic location to see where you should start storing your data.

  •    Thanks for your replies,I m new to msp430 .I didnot get it properly .For example First time I start msp430 so it starts to write temp and  pressure values of sensors to eeprom from 0000h and so on and after some time the battery is off and then the battery is turned on again so it always starts to write it in eeprom from 0000 and then further.How can I figure out the last accessed location of eeprom upon restart of msp430.I did not get your point properly

    If you have some code or any reference link then please share with me  

  • How can I check the magic location upon  msp restart ,Please guide me 

  • As Brian stated, the magic location is a known place in the EEPROM. Pick an address.  0x0000 is a good address.  At that address, you constantly record the last address that you wrote to:

    InitializeBoard();
    InitializeI2C();
    currentWriteAddress = ReadEEPROMAddress(0x0000);
    if (currentWriteAddress  == 0x0000)
    {
       currentWriteAddress  = 0x0001;
    }
    while(true)
    {
       /* Read Sensor */
       writeEEPROMAddress(currentWriteAddress , newSensorValue);
       currentWriteAddress++;
       writeEEPROMAddress(0x0000, currentWriteAddress);
    }
    

    You still need to account for failed I2C operations, address rollover, etc.

    EDIT: edits for forgetting things...

  • Hi James,

                   My memory is 64kB ,it means I have 8192 locations in it so these are 13 bits, 2^13=8192 locations .So I have to assign two known locations in this memory ? lets say 0x0000 and 0x0001    ,and when my program is run first time in msp430 so I should read the first two locations of memory if it makes uint16==0 then I should add 2 in the address only once  so that it should start writing values from 0x0002 location and after writing each value in eeprom I should also store last accessed value in known locations . and when my eeprom values are filled upto 8192 locations then I should change the first two known locations from 8192 to again 0.        Is it right?
  • Sounds reasonable to me.

  • As I do for other posts with "urgent" in them, please see this link: http://www.catb.org/~esr/faqs/smart-questions.html#urgent

  • Dear Brian,

                   Thanks for sharing me this wonderful link and I got it :-)   . A lot of Thanks for your guidance and James too for my problem.

    Regards

    Faisal Khan

  • There’s another way to do it without the magic location.
    You can simply start reading the eeprom to the first ‘unwritten address’. When you start with an eeprom that is all 0xff or 0x00 (or any other value that can’t be valid), you simply start reading from 0x0000 until you get the first location with an invalid value. This is your first write position.
    The advantage is that you don’t need to write the address to the magic address on each write. Since EEPROM write is slow, this may speed-up things and also reduces the risk of spoiling the address if power fails during the address write.
    Of course it results in a slower startup (you may use an approximation algorithm to increase he search speed). Also, it means the eeprom needs to be initialized. Or you write not only the data, but also an invalid entry to the next address. This voids the speed advantage when writing but still reduces the risk of a spoiled pointer on power failure. It is a preferable approach when using flash, as flash can be erased (= initialized) block-wise. I use it for storing data in the MSP’s internal flash.

  • Dear Michael,

                       Thanks a lot for your reply.Yes you are right too in this sense .I do not have to write again and again to store my current address in Memory .

    Regards 

    Faisal Ali Khan 

**Attention** This is a public forum