10.3.5 Word Write
PFM can be written one word at a time to a pre-erased memory location. Refer to the “Word Modify” section for more information on writing to a prewritten memory location.
A single word is written by setting the NVMADR to the target address and loading
NVMDAT with the desired word. The word is then transferred to PFM by setting the NVMCMD
bits to ‘b011
then executing the unlock sequence and setting the GO
bit.
The sequence of events for programming single word to a pre-erased location of PFM is:
- Set the NVMADR registers to the target address.
- Load the NVMDAT with desired word.
- Set the NVMCMD
control bits to
‘b011
(Word Write). - Disable all interrupts.
- Perform the unlock sequence as described in the Unlock Sequence section.
- Set the GO bit to start the PFM word write.
- Monitor the GO bit or NVMIF interrupt flag to determine when the write has completed.
- Interrupts can be enabled after the GO bit is clear.
- Set the NVMCMD control
bits to
‘b000
.
Writing a Word of Program Flash Memory in C
// Code sequence to program one word to a pre-erased location in PFM // PFM target address is specified by WORD_ADDR // Target data are specified by WordValue // Save interrupt enable bit value uint8_t GIEBitValue = INTCON0bits.GIE; // Load NVMADR with the target address of the word NVMADR = WORD_ADDR; NVMDAT = WordValue; // Load NVMDAT with the desired value NVMCON1bits.CMD = 0x03; // Set the word write command INTCON0bits.GIE = 0; // Disable interrupts //––––––––– Required Unlock Sequence ––––––––– NVMLOCK = 0x55; NVMLOCK = 0xAA; NVMCON0bits.GO = 1; // Start word write //––––––––––––––––––––––––––––––––––––––––––––––– while (NVMCON0bits.GO); // Wait for the write operation to complete // Verify word write operation success and call the recovery function if needed if (NVMCON1bits.WRERR){ WRITE_FAULT_RECOVERY(); } INTCON0bits.GIE = GIEBitValue; // Restore interrupt enable bit value NVMCON1bits.CMD = 0x00; // Disable writes to memory