26.5.4.3 NVM Write

The NVM Controller requires that an erase must be done before programming. The entire NVM main address space and the Data Flash address space can be erased by a debugger Chip Erase command. Alternatively, rows can be individually erased by the Erase Row command or the Data Flash Erase Row command to erase the NVM main address space or the Data Flash address space, respectively.

After programming the NVM main array, the region that the page resides in can be locked to prevent spurious write or erase sequences. Locking is performed on a per-region basis, and so, locking a region will lock all pages inside the region. Refer to Region Lock Bits for more information.

Data to be written to the NVM block are first written to and stored in an internal buffer called the page buffer. The page buffer contains the same number of bytes as an NVM page. Writes to the page buffer must be 32 bits. 16-bit and 8-bit writes to the page buffer are not allowed and will cause a system exception.

Internally, writes to the page buffer are on a 64-bit basis through the page buffer load data register (PBLDATA1 and PBLDATA0). The PBLDATA register is a holding register for writes to the same 64-bit page buffer section. Data within a 64-bit section can be written in any order. Crossing a 64-bit boundary will reset the PBLDATA register to all ones. The following example assumes startup from reset where the current address is 0 and PBLDATA is all ones. Only 64 bits of the page buffer are written at a time, but 128 bits are shown for reference.

Sequential 32-bit Write Example:
  • 32-bit 0x1 written to address 0
    • Page buffer[127:0] = {0xFFFFFFFF_FFFFFFFF, PBLDATA[63:32], 0x00000001}
    • PBLDATA[63:0] = {PBLDATA[63:32], 0x00000001}
  • 32-bit 0x2 written to address 1
    • Page buffer[127:0] = {0xFFFFFFFF_FFFFFFFF, 0x00000002, PBLDATA[31:0]}
    • PBLDATA[63:0] = {0x00000002, PBLDATA[31:0]}
  • 32-bit 0x3 written to address 2 (crosses 64-bit boundary)
    • Page buffer[127:0] = 0xFFFFFFFF_00000003_00000002_00000001
    • PBLDATA[63:0] = 0xFFFFFFFF_00000003

Random access writes to 32-bit words within the page buffer will overwrite the opposite word within the same 64-bit section with ones. In the following example, notice that 0x00000001 is overwritten with 0xFFFFFFFF from the third write due to the 64-bit boundary crossing. Only 64 bits of the page buffer are written at a time, but 128 bits are shown for reference.

Random Access 32-bit Write Example:
  • 32-bit 0x1 written to address 2
    • Page buffer[127:0] = 0xFFFFFFFF_00000001_FFFFFFFF_FFFFFFFF
    • PBLDATA[63:0] = 0xFFFFFFFF_00000001
  • 32-bit 0x2 written to address 1
    • Page buffer[127:0] = 0xFFFFFFFF_00000001_00000002_FFFFFFFF
    • PBLDATA[63:0] = 0x00000002_FFFFFFFF
  • 32-bit 0x3 written to address 3
    • Page buffer[127:0] = 0x00000003_FFFFFFFF_00000002_FFFFFFFF
    • PBLDATA[63:0] = 0x00000003_0xFFFFFFFF

Both the NVM main array and the Data Flash array share the same page buffer. Writing to the NVM block via the AHB bus is performed by a load operation to the page buffer. For each AHB bus write, the address is stored in the ADDR register. After the page buffer has been loaded with the required number of bytes, the page can be written to the NVM main array or the Data Flash by setting CTRLA.CMD to 'Write Page' or 'Data Flash Write Page', respectively, and setting the key value to CMDEX. The LOAD bit in the STATUS register indicates whether the page buffer has been loaded or not. Before writing the page to memory, the accessed row must be erased.

Automatic page writes are enabled by writing the manual write bit to zero (CTRLB.MANW=0). This will trigger a write operation to the page addressed by ADDR when the last location of the page is written.

Because the address is automatically stored in ADDR during the I/O bus write operation, the last given address will be present in the ADDR register. There is no need to load the ADDR register manually, unless a different page in memory is to be written.