Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/16/07 13:57
Read: times


 
Msg Score: +1
 +1 Informative
#143346 - Here's one method
Responding to: ???'s previous message
For projects like yours where I need to store a relatively small number of values in the EEPROM, I like to use two locations in the EEPROM for each value that I need to store. I call each such pair of locations a "cell". Each cell can be in one of three states:
  • Unused In unused cells, both of the cell's words contain all zeros.
  • Used In used cells, one of the cell's words contains the cell's value, and the other contains the one's complement of the cell's value.
  • Corrupted Any cells that don't qualify as Used or Unused are by definition corrupted.
When the firmware first comes alive, it checks the first cell for a known signature value. If the signature is absent, the firmware assumes that the EEPROM has never been written. In this case, it writes default values to all defined cells (i.e., cells where it expects to see useful data), and marks all undefined cells as unused.

If the signature is present in the first cell, the firmware assumes that the EEPROM has been written, but perhaps by an earlier version of the firmware. In this case, it checks all defined cells. It accepts those that appear "used" as is, writes default values to any that appear "unused", and rejects those remaining as corrupted.

This system is relatively simple, nearly guarantees that values read from the EEPROM will be correct, and helps to keep things under control when a particular version of firmware encounters an EEPROM that was written by a previous version.

-- Russ

/* ////////////////////////////////////////////////////////////////////////////
			      InitializeEeprom()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION:	This function is called once a boot time to initialize the
		EEPROM.

REVISIONS:	 6 Apr 07 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */

void InitializeEeprom() {

    unsigned value;
    unsigned cell;

    if ((ReadCell(0, &value) != USED) ||
	(value != SIGNATURE_VALUE)) {		/* Invalid signature */
	for (cell=0;				/* Write default values to */
	     cell < DEFINED_CELLS;		/*  all the defined cells */
	     cell++) {
	    WriteCell(cell, EEMirror[cell]);
	    }
	for (cell = DEFINED_CELLS;		/* Mark all the undefined */
	     cell < MAX_CELLS;			/*  cells as unused */
	     cell++) {
	    WriteEepromWord(cell * 2, 0);
	    WriteEepromWord((cell * 2) + OFFSET, 0);
	    }
	}					/* End 'invalid signature' */
    else {					/* Valid signature */
	for (cell=0;				/* For each defined cell ... */
	     cell < DEFINED_CELLS;
	     cell++) {
	    switch(ReadCell(cell, &value)) {
		case USED:			/* If it looks good, copy */
		    EEMirror[cell] = value;	/*  its value into the RAM */
		    break;			/*  mirror */
		case UNUSED:			/* If it's never been */
		    WriteCell(cell,		/*  written, write it with */
			EEMirror[cell]);	/*  a default value right */
		    break;			/*  now */
		case CORRUPTED:			/* If it's corrupted, punt */
		    PostError(EEPROM_CHECK_ERROR);
		    break;
		}				/* End switch */
	    }					/* End 'for each def'd cell' */
	}					/* End 'valid signature' */
    }						/* End InitializeEeprom() */



List of 19 messages in thread
TopicAuthorDate
Simple EEPROM checksumming            01/01/70 00:00      
   Pitfalls            01/01/70 00:00      
      a checksum out of 2 bytes?            01/01/70 00:00      
         It's an "either - or" ...            01/01/70 00:00      
            code sketch            01/01/70 00:00      
               a simple checksum            01/01/70 00:00      
                  CRC8 instead of CR32            01/01/70 00:00      
                     I am talking about CRC-16 not CRC-8            01/01/70 00:00      
                        oups            01/01/70 00:00      
   how much data?            01/01/70 00:00      
      approx 40-60 bytes            01/01/70 00:00      
      I use 2 copies and a counter for that            01/01/70 00:00      
         adding to Jan's post            01/01/70 00:00      
   Here's one method            01/01/70 00:00      
      another interesting scheme!            01/01/70 00:00      
   Try the Dallas 1-wire CRC            01/01/70 00:00      
      another good idea but...            01/01/70 00:00      
         I just meant the CRC            01/01/70 00:00      
            oups            01/01/70 00:00      

Back to Subject List