Electronic – Behavior of AVR microcontroller only containing bootloader

avrbootloadermicrocontroller

I'm working on an implementation of AVR109, where the bootloader times out after two seconds if a given series of bytes aren't received on the serial port, and then starts the main application using a function pointer to address 0.

The bootloader is working fine, and I'm able to program the AVR via serial port. But when the AVR only contains the bootloader, and the flash memory at location 0 is empty, it takes two seconds in addition to the bootloader timeout every time the AVR restarts. Why isn't this instantaneous? Why isn't the AVR resetting immediately when the bootloader sets the pointer to an empty area in the flash? Is this something I can configure?

EDIT: My bootloader conundrum actually proved to be caused by PEBCAK. An error in the initial timing loop in the bootloader caused the extra two second delay, and was fixed. When jumping to the start of a blank/erased flash memory, it actually only took a few milliseconds before the bootloader started up again, i.e. not a big problem. But the best approach is to check the flash memory location before jumping there. If it's erased, just restart the bootloader instead of jumping to the empty area.

Best Answer

Well, the answer its to the behavior of the system is "who knows" because you basically have an undefined behavior because there is nothing written to flash to the MCU will try to use whatever is at address 0 as the reset vector for the application, if is erased it will try to use 0xffff and I do not think that the AVR issues an exception when you go outside the memory bounds and it will execute some random instruction and probably hang in there until the watchdog or other invalid instruction causes a reset. Most boot loaders tend to do a check for the value at the reset vector (typically address 0) to verify it is not blank and if it is they just loop the boot loader indefinitely to prevent the MCU to run into undefined behavior.

Related Topic