Bootloader Code, Application Code, and Application Data Sections

The Flash memory can be divided into three sections: Bootloader Code (BOOT), Application Code (APPCODE), Application Data (APPDATA), each consisting in a variable number of Flash pages (512-bytes blocks):

Figure 1. AVR® DA Flash Memory Map

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 executing from the BOOT section, and code writing to the APPDATA section must be executing from either the BOOT section or the APPCODE section.

The fuses define the size of each of the respective sections. There are BOOTSIZE and CODESIZE fuses that control this. The following table shows how these fuses configure the sections.
Table 1. 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

A good way of making sure these fuses are set up as expected on a device is to use the FUSES macro in the bootloader code project. It can be found in fuse.h, which is included by io.h:

#include <avr/io.h> 
FUSES = { 
.OSCCFG = CLKSEL_OSCHF_gc,                       // High frequency oscillator selected
.SYSCFG0 = CRCSRC_NOCRC_gc | RSTPINCFG_GPIO_gc,  // No CRC enabled, RST pin in GPIO mode
.SYSCFG1 = SUT_64MS_gc,                          // Start-up time 64 ms
.BOOTSIZE = 0x02,                                // BOOT size = 0x02 * 512 bytes = 1024 bytes 
.CODESIZE = 0x00                                 // All remaining Flash used as App code 
};
Note: All fuse bytes in the struct must be configured, not only BOOTSIZE and CODESIZE. This is because an omitted fuse byte will be set to 0x00 and may cause an unwanted configuration.

By including this macro in the project, the fuse settings are included in compiled files. To write the fuse setting at the same time as the Flash, the *.elf file must be selected instead of the *.hex file during device programming.

In Atmel Studio 7.0, another method is to use the Production File tab from the Device Programming.

Figure 2. Writing Fuses using Production File Tab, Atmel Studio 7.0

In Atmel Studio 7.0, the fuses can also be configured using Device Programing (Ctrl +Shift + P) - Fuses, as shown in the figure below.

Figure 3. Configure BOOTSIZE and CODESIZE Fuses, Atmel Studio 7.0