I am writing a bootloader application but can't seem to get it to consistently launch from either 0x0000 or the Boot Reset Address (0x3800 - from here, Table 27-13, I am using an AtMega328). Here is a simplistic setup to show my issue:
In prog.c (it just sets PB1 high and loops forever), I have
#include <avr/io.h> int main() { DDRB = (1 << DDB1); PORTB = (1 << PORTB1); while (1); }
and in boot.c (it just sets PB2 high and loops forever), I have
#include <avr/io.h> asm(" .section .text\n"); int main() { DDRB = (1 << DDB2); PORTB = (1 << PORTB2); while (1); }
My hypothesis is if I follow these steps:
- set fuses so that the program counter starts at 0x3800 on startup (set BOOTRST = 0, see Table 28-8 and 27-4 from the above link)
- program boot.c using avrdude to address 0x3800
- program prog.c using avrdude to address 0x0000
Then PB2 should high on and PB1 be low (since boot.c should be run on startup and never jumps to 0x0000). Instead, after step 2, PB2 goes high. Then after step 3, PB1 goes high and PB2 goes low. In other words, it seems that whenever I program either prog.c or boot.c, the chip just runs the latest one I programmed. However, I want it to start at address 0x3800, from which I will then run actual bootloader logic.
Here is how I accomplish step 1:
avrdude -e -c avrisp2 -p m328 -P usb -U hfuse:w:0xD8:m
Here is how I accomplish step 2:
#creating object file... avr-gcc -g -Wall -Os -mmcu=atmega328p -Wl,--section-start=.text=0x3800 -c boot.c #creating elf file file... avr-gcc -g -Wall -Os -mmcu=atmega328p -Wl,--section-start=.text=0x3800 -o boot.elf boot.o #creating hex file... avr-objcopy -j .text -j .data -O ihex boot.elf boot.hex #write to device sudo avrdude -c avrisp2 -p m328 -P usb -U flash:w:boot.elf
Here is how I accomplish step 3:
#creating object file... avr-gcc -g -Wall -Os -mmcu=atmega328p -c prog.c #creating elf file file... avr-gcc -g -Wall -Os -mmcu=atmega328p -o prog.elf prog.o #creating hex file... avr-objcopy -j .text -j .data -O ihex prog.elf prog.hex #load onto chip sudo avrdude -c avrisp2 -p m328 -P usb -U flash:w:prog.elf
Can you help me tell the chip to start at the bootloader address? Thanks!