15.2.2.3 NVMREG Erase of DFM

DFM can be erased by writing ‘0xFF’ to all locations that need to be erased. To erase a DFM word:
  1. Set the NVMREGS bit.
  2. Write the desired address into the NVMADRH:NVMADRL register pair.
  3. Set the WREN bit.
  4. Perform the unlock sequence as described in the NVM Unlock Sequence section.
  5. Set the WR bit.

A single byte (0xFF) is written into the DFM location. The DFM circuitry automatically erases the memory location before performing the write operation. If the DFM address is write-protected, the WR bit will be cleared and the write operation will not take place. Upon completion, hardware clears the WR bit, the NVMIF bit is set, and an interrupt will occur if the NVMIE bit is also set.

Figure 15-10. DFM Erase Sequence

Erasing One Byte of Data Flash Memory

// Code sequence to erase one byte of DFM

NVMCON1bits.NVMREGS = 1;       // Point to DFM
NVMADR = DFM_ADDR;             // NVMADRH = 0x70, NVMADRL = DFM address
NVMDATL = 0xFF;                // Load NVMDATL with 0xFF
NVMCON1bits.WREN = 1;          // Allows program/erase cycles
INTCONbits.GIE = 0;            // Disable interrupts

// Perform required unlock sequence
NVMCON2 = 0x55;
NVMCON2 = 0xAA;
NVMCON1bits.WR = 1;            // Begin program/erase cycle

INTCONbits.GIE = 1;            // Restore interrupt enable bit value
NVMCON1bits.WREN = 0;          // Disable program/erase

// Verify byte erase operation success and call the recovery function if needed
if (NVMCON1bits.WRERR)
{
    ERASE_FAULT_RECOVERY();
}