12.1.4 Writing to Program Flash Memory

Program memory can be written either one word at a time or a sector at a time.

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 Flash memory with the WR unlock and write sequence.

A sector is written by first loading a block of holding registers and then executing a sector write sequence. The programming write block size is specified as the holding registers, also referred to as sector RAM, in the Flash memory organization by device table. Table writes are used to write the holding register bytes that are then transferred to Flash memory with the SECWR unlock and write sequence. There are only as many holding registers as there are bytes in a write block.

Since the table latch (TABLAT12.5.6 TABLAT) is only a single byte, the TBLWT instruction needs to be executed multiple times for each sector programming operation. The write protection state is ignored for table writes. All of the table write operations will essentially be short writes because only the holding registers are written. NVMIF is not affected while writing to the holding registers.

After all the holding registers have been written, the programming operation of that sector of memory is started by setting NVMADR to an address within the target sector and executing a sector write unlock sequence followed immediately by setting the SECWR bit.

If the PFM address in the NVMADR is write-protected, or if NVMADR points to an invalid location, the SECWR bit is cleared without any effect and the NVMERR is set.

CPU operation is suspended during a long write cycle and resumes when the operation is complete. The long write operation completes in one extended instruction cycle. When complete, the SECWR or WR bit is cleared by hardware and NVMIF is set. An interrupt will occur if NVMIE is also set. The holding registers are unchanged. NVMEN is not changed.

The internal programming timer controls the write time. The write/erase voltages are generated by an on-chip charge pump, rated to operate over the voltage range of the device.

Important: The holding registers are undefined on device Resets and unchanged after write operations. Individual bytes of program memory may be modified, provided that the modification does not attempt to change any bit from a ‘0’ to a ‘1’. When modifying individual bytes with a sector write operation, it is necessary to load all holding registers with either FFh or the existing contents of memory before executing a long write operation. The fastest way to do this is by performing a sector read operation.
Figure 12-8. Table Writes to Program Flash Memory
Note: Refer to Flash memory organization by device table for number of holding registers (e.g. YY = FFh for 256 holding registers).