Electronic – AVR self-programming


According to the datasheet, the ATMega168 can only execute self-programming instructions when running code from the bootloader block.

Once the bootloader is complete and the application starts, can the application then jump back into a subroutine in the bootloader block and have it write to the flash?

Or, is it the case that once the program leaves the boot block it's not allowed to self-program again until after a reset?

Best Answer

well... you can always execute a software initiated reset ala exploitation of the watchdog timer... and put data in eeprom to communicate to your bootloader code as to what you wanted it to do...


Looking at page 272 of the ATMega168 manual (section 6: Entering the Boot Loader Program), the first line states:

Entering the Boot Loader takes place by a jump or call from the application program. This may be initiated by a trigger such as a command received via USART, or SPI interface.

So it would seem to me that definitively you can and must be able to call the bootloader functions from application code. As long as you are executing code that resides in the Boot Loader section of memory, SPM instructions can be executed, and therefore you should be able to self-program any time your application program jumps into the boot loader code.