11.3.1.1 Flash

The Flash is divided into a set of pages. A page is the smallest addressable unit when erasing the Flash. It is only possible to erase an entire page or multiple pages at a time. Writes can be done per byte or word. One page consists of 512 bytes.

The Flash can be divided into three sections, each consisting of a variable number of pages. These sections are:

Boot Loader Code (BOOT) Section

The Flash section with full write access. Boot loader software must be placed in this section if used.

Application Code (APPCODE) Section

The Flash section with limited write access. An executable application code is usually placed in this section.

Application Data (APPDATA) Section

The Flash section without write access. Parameters are usually placed in this section.

Inter-Section Write Protection

For security reasons, it is not possible to write to the section of Flash the code is currently executing from. Code writing to the APPCODE section needs to be executed from the BOOT section, and code writing to the APPDATA section needs to be executed from either the BOOT section or the APPCODE section.

Table 11-1. Write Protection for Self-Programming
Program Execution Section Section Being Addressed Programming Allowed? CPU Halted?
BOOT BOOT No -
APPCODE Yes Yes
APPDATA Yes
EEPROM No
APPCODE BOOT No -
APPCODE
APPDATA Yes Yes
EEPROM No
APPDATA BOOT No -
APPCODE
APPDATA
EEPROM

Section Sizes

The sizes of these sections are set by the Boot Size (FUSE.BOOTSIZE) fuse and the Code Size (FUSE.CODESIZE) fuse. The fuses select the section sizes in blocks of 512 bytes. The BOOT section stretches from FLASHSTART to BOOTEND. The APPCODE section spreads from BOOTEND until APPEND. The remaining area is the APPDATA section.

Figure 11-2. Flash Sections Sizes and Locations

If FUSE.BOOTSIZE is written to ‘0’, the entire Flash is regarded as the BOOT section. If FUSE.CODESIZE is written to ‘0’ and FUSE.BOOTSIZE > 0, the APPCODE section runs from BOOTEND to the end of Flash (no APPDATA section).

When FUSE.CODESIZE ≤ FUSE.BOOTSIZE, the APPCODE section is removed, and the APPDATA runs from BOOTEND to the end of Flash.
Table 11-2. Setting Up Flash Sections
BOOTSIZE CODESIZE BOOT Section APPCODE Section APPDATA Section
0 - 0 to FLASHEND - -
> 0 0 0 to BOOTEND BOOTEND to FLASHEND -
> 0 ≤ BOOTSIZE 0 to BOOTEND - BOOTEND to FLASHEND
> 0 > BOOTSIZE 0 to BOOTEND BOOTEND to APPEND APPEND to FLASHEND

If there is no boot loader software, it is recommended to use the BOOT section for application code.

Note:
  1. After Reset, the default vector table location is at the start of the APPCODE section. The peripheral interrupts can be used in the code running in the BOOT section by relocating the interrupt vector table at the start of this section. That is done by setting the IVSEL bit in the CPUINT.CTRLA register. Refer to the CPUINT - CPU Interrupt Controller section for details.
  2. If BOOTEND/APPEND, as resulted from BOOTSIZE/CODESIZE fuse setting, exceed the device FLASHEND, the corresponding fuse setting is ignored, and the default value is used. Refer to FUSE - Configuration and User Fuses in the Memories section for default values.

Size of Flash Sections Example

If FUSE.BOOTSIZE is written to 0x04 and FUSE.CODESIZE is written to 0x08, the first 4*512 bytes will be BOOT, the next 4*512 bytes will be APPCODE, and the remaining Flash will be APPDATA.

Flash Protection

Additional to the inter-section write protection, the NVMCTRL provides a security mechanism to avoid unwanted access to the Flash memory sections. Even if the CPU can never write to the BOOT section, a Boot Section Read Protection (BOOTRP) bit in the Control B (NVMCTRL.CTRLB) register is provided to prevent the read and execution of code from the BOOT section. This bit can be set only from the code executed in the BOOT section and has effect only when leaving the BOOT section.

There are two other write protection (APPCODEWP and APPDATAWP) bits in the Control B (NVMCTRL.CTRLB) register that can be set to prevent further updates of the respective Application Code and Application Data sections.