Erasing the DFM

The DFM does not support the Page Erase operation. However, the DFM can be erased by writing 0xFF to all locations in the memory that need to be erased. The simple code example bellow shows how to erase ‘n’ number of bytes in DFM. Refer to the “Memory Organization” chapter for more details about the DFM size and valid address locations.

Erasing n Bytes of Data Flash Memory in C

// Code sequence to erase n bytes of DFM
// DFM target start address is specified by PAGE_ADDR
// Number of bytes to be eares is specified by n

// Save interrupt enable bit value
uint8_t GIEBitValue = INTCON0bits.GIE;

// Load NVMADR with the target address of the byte
NVMADR = DFM_ADDR;              
NVMDATL = 0xFF;                    // Load NVMDATL with 0xFF
NVMCON1bits.CMD = 0x04;            // Set the write and post increment command
INTCON0bits.GIE = 0;               // Disable interrupts

for (uint8_t i = 0; i < n; i++}(
        NVMLOCK = 0x55;
        NVMLOCK = 0xAA;
        NVMCON0bits.GO = 1;
}

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

INTCON0bits.GIE = GIEBitValue;     // Restore interrupt enable bit value
NVMCON1bits.CMD = 0;               // Disable writes to memory