Micro Boot Loader for AVR
This is a sample program of AVR boot loader project. If you have downloaded and installed AtmanAVR 7.0+,
you can find it in the Samples directory of the installation directory. Or download it from here
The target MCU of this project is ATmega64, and the peripheral expenses include Timer 0 and UART 1.
RS485 interface is adopted, and Xmodem protocol is used for downloading.
Moving the Interrupt Vectors
Timer 0 interrupt and UART 1 interrupt are used in the project, so the interrupt vector should be moved to
the Boot Loader Section at the beginning, and then moved to the Application Flash Section when the BootLoader exits.
Perform the function of moving the interrupt vector:
#define IVSEL_SECTION_APP 0
#define IVSEL_SECTION_BOOT 1
static void move_interrupts(unsigned char ivsel)
{
/* Enable change of Interrupt Vectors */
MCUCR = (1<<IVCE);
/* Move interrupts to application/boot Flash section */
MCUCR = (ivsel<<IVSEL);
}
Boot Loader programming flash page
The program memory is updated in a page by page fashion. Operation sequence:
- Perform a Page Erase
- Fill temporary page buffer
- Perform a Page Write
void boot_program_page (uint32_t page, uint8_t *buf)
{
uint16_t i; uint8_t sreg;
// Disable interrupts.
sreg = SREG;
cli();
eeprom_busy_wait ();
boot_page_erase (page);
boot_spm_busy_wait (); // Wait until the memory is erased.
for (i=0; i<SPM_PAGESIZE; i+=2)
{
// Set up little-endian word.
uint16_t w = *buf++;
w += (*buf++) << 8;
boot_page_fill (page + i, w);
}
boot_page_write (page); // Store buffer in flash page.
boot_spm_busy_wait(); // Wait until the memory is written.
// Reenable RWW-section again. We need this if we want to jump back
// to the application after bootloading.
boot_rww_enable ();
// Re-enable interrupts (if they were ever enabled).
SREG = sreg;
}
Exit the Boot Loader program
When exiting the Boot Loader, first turn off the interrupt, reset the IO registers of all peripherals used,
move the interrupt vector to the Application Section, and then jump directly to the first address of the
Application Section to start execution.
// Exit the Bootloader program and execute the application program from 0x0000.
void quit(void)
{
// Disable interrupt
cli();
#ifdef RAMPZ
RAMPZ = 0x00;
#endif
// Reset TC0
TCCR0 = 0x00;
OCR0 = 0x00;
TIMSK = 0x00;
// Reset UART1
UCSR1B = 0x00;
// Move interrupts to application section
move_interrupts(IVSEL_SECTION_APP);
// Execute the application program from 0x0000.
asm("jmp 0x0000\n");
}
Using the Micro Boot Loader
Download the compiled micro boot loader to the target MCU. When the user application is updated by using the
Micro Boot Loader:
- Connect the target MCU board with the upper computer by serial port
- Start any software supporting Xmodem protocol, such as mpsc.exe
- Load user application binary data and start sending
- Turn on the power of the target MCU board and wait for the completion of transmission