24.15.1 Page Erase Retry
Page Erase Retry is a method to improve the life of a Flash by attempting to erase again if the Page Erase was not successful. Page Erase Retry can only be used for a Page Erase.
11
. If Page Erase is not successful after 7 trials, this means that
the Flash for that page, or the Words that did not erase, must be considered
“non-functional”.Together with the normal Page Erase controls, Page Erase Retry also uses the WS[4:0], CREAD1, VREAD1 and RETRY[1:0] bits in the NVMCON2 register. The ERS[3:0] bits (NVMCON2[31:28]) are for the benefit of software performing the programming sequence in the event that a drop in power causes a BOR event but not a POR event.
- Set the NVMADDR register with the address of the page to be erased.
- Execute the write unlock sequence.
- Save the value of the NVMCON2 register.
- Do the following in the NVMCON2 register:
- Set the ERS[3:0] bits as desired.
- Set the WS[4:0] bits per the description.
- Set the VREAD1 bit to ‘
1
’. - Set the CREAD1 bit to ‘
1
’. - Set the RETRY[1:0] bits to ‘
00
’.
- Run the unlock sequence using the Page Erase command to start the sequence.
- Wait for the WR bit (NVMCON[15]) to be cleared by hardware.
- Clear the WREN bit (NVMCON[14]).
- Verify the erase using the CPU. To shorten the
verify time, use CREAD1 =
1
to perform a hardware compare to logic ‘1
’ of each bit in the Flash Word including ECC. A successful compare yields a read of 0x00000001 in the lowest addressed word in a Flash Word (128 bits). This is the Compare Word. All other Words are 0x00010000. If any bit is logic ‘0
’, all Words in the Flash Word read 0x00000000. Remember to increment the address by the number of bytes in a Flash Word between reads. - If all Compare Words verify correctly, the Page Erase Retry process is complete. Go to step 11.
- If a Compare Word yields a read of 0x00000000, perform steps 4 through 9 up to six more times with the following change to step 4:
- Increment the RETRY[1:0] bits by one if the bit
has not already reached the ‘
11
’ setting. - Maintain all other fields.
- Increment the RETRY[1:0] bits by one if the bit
has not already reached the ‘
- Restore the value of the NVMCON2 register, which was saved in step 3.
- When the VREAD1 =
1
, the Flash uses the WS[3:0] bits for Flash access wait state generation to the panel selected by NVMADDR. Software is responsible for writing the VREAD1 bit back to ‘0
’ when both erase and verify is complete. - The device configuration boot page (the page containing the DEVCFGx values) does not support Page Erase Retry.
The following code provides code for a single page erase operation at address 0x1008000, where Page Erase Retry is used.
uint32_tsaveNVMCON2;
uint32_t*cmpPtr;
uint8_terased;
uint8_ttryCount;
// set destination page address
NVMADDR = 0x1008000; // Page physical address
// define flash operation
NVMCONbits.NVMOP = 0x4; // NVMOP for Page Erase
// Unlock sequence
NVMKEY = 0x0;
NVMKEY = 0xAA996655;
NVMKEY = 0x556699AA;
// save NVMCON2
saveNVMCON2 = NVMCON2;
// set up Page Erase Retry
NVMCON2bits.ERS = 0; // Stage 0 - SW use only
NVMCON2bits.VREAD1 = 1;
NVMCON2bits.CREAD1 = 1;
NVMCON2bits.RETRY = 0b00;
tryCount = 0; // Up to 4 attempts
do {
tryCount++;
// commence programming
NVMInitiateOperation();
// Wait for WR bit to clear
while(NVMCONbits.WR);
// Turn off WREN
NVMCONbits.WREN = 0;
// Check that the page was erased
erased = 1;
cmpPtr = (uint32_t *)NVMADDR;
erased &= (*cmpPtr == 0x00000001);
cmpPtr++;
erased &= (*cmpPtr == 0x00010000);
cmpPtr++;
erased &= (*cmpPtr == 0x00010000);
cmpPtr++;
erased &= (*cmpPtr == 0x00010000);
if (!erased) {
// Erase failed. Try with different settings.
NVMCON2bits.RETRY++;
NVMCONbits.NVMOP = 0x4;
NVMCONbits.WREN = 1;
}
} while (!erased && (tryCount < 4));
// Restore settings
NVMCON2 = saveNVMCON2;