Self-Programing

ATxmega128A1U flash program memory includes an application section of 120KB, an application table section of 8KB, and a boot loader section of 8KB. The flash map is shown in the figure below. For this application note, the bootloader implementation targets to re-program the application flash section.

Figure 1. Flash Section Map

Reading and writing (including erasing) the flash memory from the application code inside the device is referred to as self-programming for flash memory. A bootloader is such an application for this purpose. For ATxmega128A1U, to perform flash erase and write, the SPM instruction must be called and this instruction can only be performed from the boot loader flash section.

For this bootloader implementation, the code size is approximately 12KB, and the code is placed in the boot loader section and the application table section. When erasing or writing a page located inside the application section, only the boot loader section can be read while the application table section cannot be read. So the code relating to reading binary file and memory program must be placed at the boot loader section. To enable the code placement in the two sections, the linker options must be modified for an Atmel Studio project. Add the following linker options in the project property window, as shown in the figure below.

Wl,--section-start=.mysection=0x1E000 -Wl,--section-start=.text=0x20000

Figure 2. Add Linker Options in Atmel Studio
For the code to be located at application table section, add compilation attribute directives on functions definition.

#if defined(__GNUC__)
void PMIC_SetVectorLocationToBoot( void )__attribute__((section(".mysection")));
#endif