2 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.
- Write a dummy value to a location in the page to set up the address
- Perform a
PAGEERASE
command - Fill the page buffer
- Perform a
PAGEWRITE
command
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.
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.