Device Self-Programming

In tinyAVR® 0- and 1-series, and megaAVR® 0-series MCUs, Flash programming is done one page at a time. The Flash page size is either 64 or 128 bytes, dependent on device Flash size, and the data must be loaded into a page buffer of the same size before it can be written to Flash.

Before writing the page buffer to Flash, the target page must be erased. Writing to an unerased Flash page will corrupt its content. Starting the page erase can be done at the same time as writing data to the page by loading the PAGEERASEWRITE command into the NVMCTRL.CTRLA register.

It is also possible to do the erase and write in two separate operations to enable shorter programming time for each command, using the following steps: The page buffer is automatically cleared after any of the commands in NVMCTRL.CTRLA are executed.

Flash word addressing uses little-endian byte order. If the Least Significant address bit (bit 0) is ‘0’, the low byte is accessed, and if it is ‘1’, the high byte is accessed.

NVMCTRL.CTRLA has Configuration Change Protection (CCP) to prevent accidental modification. Refer to the CPU chapter in the relevant device data sheet for details on CCP. To make sure the command has finished, it is advised to wait for the Flash Busy bit (FBUSY) in the NVMCTRL.STATUS register to clear.

Note: The CHIPERASE command in NVMCTRL.CTRLA will erase the entire Flash, so this may not be executed during self-programming unless the aim is to make the device useless.