8 16-BIT DATA EEPROM EMULATION INTERFACE FUNCTIONS

INITIALIZATION OPERATION(DEE_Init()):

The initialization routine, DEE_Init(), must be called before any other data EEPROM operation can occur. This initializes all of the EEPROM banks. If the routine determines that program memory has not been initialized for emulation, it will find the first allocated page of program memory and initialize its status information. Thereafter, the read and write functions may be called as needed

The routine may also determine whether data EEPROM emulation is already underway. If so, one of three scenarios may occur:

  • If only one active page is found, the routine assumes a Reset occurred. No action is taken and the routine exits normally. Any read/write operation that may have been active during the Reset should be repeated.
  • If two active pages are found, the routine assumes that an unexpected Reset occurred during a pack operation. The routine will erase the newest active page and call the pack routine to permit the refresh to complete.
  • If more than two active pages are found, the routine assumes program memory has been corrupted by the application code and sets the pageCorrupt flag.

It is important to monitor the page status bits, as well as the RCON and NVMCON (PIC24/dsPIC33F/dsPIC33E), the application can respond appropriately to Resets and supply voltage changes.

READ OPERATION(DEE_Read()):

The DEE_Read() function is used to retrieve data EEPROM information. It returns the data associated with the data EEPROM address. If the provided address is equal to or greater than the amount of defined data EEPROM, the illegalAddress flag is set and a value of all ‘1’s is returned. This return value mimics the response of dedicated data EEPROM, where an unwritten address returns an erased value.

The routine then searches for the active page in the EEPROM bank corresponding to the address. Once located, the active page is searched for an address match, starting from the last location in the page. For details on how data EEPROM information is stored in program memory, refer to Table 1.

If an active page is not found, the pageCorrupt flag is set. A flowchart of the read operation is illustrated in the following figure.

WRITE OPERATION(DEE_Write()):

To write emulated data EEPROM, the application uses the DEE_Write() function. Like the read function, it verifies that the data EEPROM address is between 0 and one less than the size of the emulated data EEPROM. If an unimplemented address is supplied, the illegalAddress flag is set and write operation is aborted.

The routine then searches for the active page of the EEPROM bank corresponding to the address. After the active page is located, a read operation is performed. To minimize the number of erase/write cycles, the value is programmed only if it has changed.

If an active page is not found, the pageCorrupt flag is set and a non-zero value is returned.

A forward search of the active page returns the offset for the next available address. If the next available address is equal to, or greater than, the last address in the page, the packSkipped flag is set and the write operation is aborted. Otherwise, the data EEPROM information is written to the next available address in the page.

If the information does not verify, the writeError flag is set and a non-zero value is returned. The user can attempt to rewrite the data or respond as needed.

The algorithm is designed to maintain at least one available location in the active page for the next write operation. After a successful verification of the write operation, the pack routine is called if no available locations remain.

After the routine completes successfully, a zero value is returned.

A flowchart of the write operation is illustrated in following Figure.