============================================
AVR-292 SD Card Bootloader on XmegaA1U
============================================

This application note describes a simple implementation of SD Card Bootloader on ATxmega128A1U.
The firmware runs on XMEGA A1U Xplained Pro, and an IO1 Xplained Pro is used to hold a MicroSD card.
The SD card is used as the SPI slave and ATXmega128A1U acts as SPI Master. The binary file (test.bin)
to be programmed is stored in the SD card, the boot loader reads content of the file
and flashes the binary image to application section of the MCU flash memory.

The IO1 Xplained Pro board should be connected to EXT1 connector of XMEGA A1U Xplained Pro.
Its application note document is available at [Atmel Website](http://www.atmel.com)

Setup procedure for Atmel Studio project,
1. Download the example firmware package for Atmel Studio from [Atmel|START](http://start.atmel.com)
2. Creat firmware project with the firmware package in Atmel Studio (7 or higher),
3. Set linker options and pre-defined symbles for the project in Atmel Studio 
On project proerty window, trace ToolChain--AVR/GNU Linker--Miscellaneous, 
and add the following line
-Wl,--relax -Wl,--section-start=.mysection=0x1E000 -Wl,--section-start=.text=0x20000
ToolChain--AVR/GNU Compiler--Optimization and select 
Optimize for size (-Os)
Trace ToolChain--AVR/GNU C Compiler--Symbols, add following lines:
BOARD=XMEGA_A1U_XPLAINED_PRO
IOPORT_XMEGA_COMPAT
SD_MMC_ENABLE
4. Build the firmware and generate bootloader image file (.elf, .hex, etc).
5. Connect IO1 Xplained Pro board to XMEGA A1U Xplained Pro board at EXT1 connector, 
and connect the latter to PC by USB port for EDBG on the board.
6. Program the fuse bit BOOTRST, which make the XmegaA1U run from bootloader section
In Atmel Studio, Tools--Device Programming, select correct device (ATxmega128A1U) and Tool (EDBG) and Interface and apply.
Then in Fuses tab page, select "BOOTLDR" for BOOTRST and program.
7. Program bootloader image into flash memory 
Tools--Device Programming--Memories
8. Store test.bin to the SD card, and mount the card on IO1 Xplained Pro board
9. If SW0 is pressed after reset or no application firmware in application flash section,
boot mode is triggerred and bootloader will try to find SD card.
And re-program application section with the test.bin file in SD card.

For IAR for AVR, 
1. Download firmware package for IAR from [Atmel|START](http://start.atmel.com)
2. Creat firmware project in IAR for AVR following the guide below
[http://start.atmel.com/static/help/index.html?GUID-AB6C30C2-90EB-4F75-843D-ED9EC099F76C]
3. Set options for the project in IAR,
General Options--Library Configuration: Select Normal DLib
General Options--System: set CSTACK to 0x300, RSTACK Depth to 20,
check "Enable bit definition in I/O-include files"
C/C++ Compiler--Optimization: Level High (Size)
C/C++ Compiler--Preprocessor
Add additional include directories:
$TOOLKIT_DIR$\inc
$TOOLKIT_DIR$\inc\dlib
$TOOLKIT_DIR$\inc\Atmel
Add pre-defined symbols:
__ATxmega128A1U__
SD_MMC_ENABLE
BOARD=XMEGA_A1U_XPLAINED_PRO
IOPORT_XMEGA_COMPAT
4.Override default linker configuration file
copy the following lines and save into a linker_bootloader.xcl file.
At option page Linker--Config,check "Override defaults" and select the 
just created .xcl file.

-ca90
-D_..X_INTVEC_SIZE=1FC   // 4 bytes * 125 vectors
-D_..X_APPLICATION_SECTION_START=20000
-D_..X_APPLICATION_SECTION_SIZE=2000
-D_..X_APPLICATION_START=(_..X_APPLICATION_SECTION_START+_..X_INTVEC_SIZE)
-D_..X_APPLICATION_END=(_..X_APPLICATION_SECTION_START+_..X_APPLICATION_SECTION_SIZE-1)
-D_..X_APPLICATION_TABLE_START=1E000
-D_..X_APPLICATION_TABLE_END=1FFFF
-D_..X_BOOT_START=(_..X_APPLICATION_TABLE_END+1)
-D_..X_BOOT_END=_..X_APPLICATION_END

-Z(CODE)INTVEC=_..X_APPLICATION_SECTION_START-(_..X_APPLICATION_START-1)
-Z(CODE)SWITCH,DIFUNCT=_..X_APPLICATION_START-_..X_APPLICATION_END
-Z(CODE)CODE=_..X_APPLICATION_SECTION_START-_..X_APPLICATION_END
-Z(CODE)APPLICATION_TABLE=_..X_APPLICATION_TABLE_START-_..X_APPLICATION_TABLE_END 
-Z(CODE)BOOT=_..X_BOOT_START-_..X_BOOT_END
-Z(FARCODE)FAR_ID=_..X_APPLICATION_SECTION_START-_..X_APPLICATION_END
-Z(CODE)FARCODE=_..X_APPLICATION_SECTION_START-_..X_APPLICATION_END
-Z(FARCODE)FAR_F=_..X_APPLICATION_SECTION_START-_..X_APPLICATION_END
-Z(CODE)HUGE_F,INITTAB=_..X_APPLICATION_SECTION_START-_..X_APPLICATION_END
-Z(CODE)TINY_ID,NEAR_ID,HUGE_ID=_..X_APPLICATION_SECTION_START-_..X_APPLICATION_END
-Z(CODE)CHECKSUM#_..X_APPLICATION_END

-D_..X_SRAM_BASE=2000    // Start of ram memory
-D_..X_SRAM_TBASE=0      // Start of tiny ram memory
-D_..X_SRAM_TSIZE=0      // Size of the tiny ram memory
-D_..X_SRAM_END=3FFF     // End of ram memory
-D_..X_EEPROM_END=7FF   // End of eeprom memory
-D_..X_EEPROM_START=0

-Z(DATA)TINY_I,TINY_Z,TINY_N=_..X_SRAM_TBASE:+_..X_SRAM_TSIZE
-Z(DATA)CSTACK+_..X_CSTACK_SIZE=_..X_CSTACK_BASE-_..X_CSTACK_END
-Z(DATA)HEAP+_..X_HEAP_SIZE=_..X_SRAM_BASE-_..X_SRAM_END
-Z(DATA)NEAR_HEAP+_..X_NEAR_HEAP_SIZE=_..X_SRAM_BASE-_..X_SRAM_END
-Z(DATA)FAR_HEAP+_..X_FAR_HEAP_SIZE=_..X_SRAM_BASE-_..X_SRAM_END
-Z(DATA)HUGE_HEAP+_..X_HUGE_HEAP_SIZE=_..X_SRAM_BASE-_..X_SRAM_END
-Z(DATA)RSTACK+_..X_RSTACK_SIZE=_..X_RSTACK_BASE-_..X_RSTACK_END
-Z(DATA)NEAR_I,NEAR_Z,NEAR_N=_..X_SRAM_BASE-_..X_SRAM_END
-Z(DATA)IOSTREAM_N#_..X_SRAM_BASE-_..X_SRAM_END
-Z(XDATA)EEPROM_I,EEPROM_N=_..X_EEPROM_START-_..X_EEPROM_END
-Z(FARDATA)FAR_I,FAR_Z,FAR_N=_..X_SRAM_BASE-_..X_SRAM_END
-Z(DATA)HUGE_I,HUGE_Z,HUGE_N=_..X_SRAM_BASE-_..X_SRAM_END

/* Suppress one warning which is not relevant for this processor */
-w29

5. Comment #include "avr/io.h" in atmel_start.h if needed, 
and comment all contents of include/port.h
6.follow Step 4-9 of setup procedure for Atmel Studio to run a demo on hardware kits.
