Search code examples
avravr-gccatmegaavrdude

How to replace the bootloader of an ATMEGA88


I have a board with an ATMEGA88 chip which has a bootloader at address 0x1800 and a firmware at address 0x0000. Both have access to USB. The bootloader can write a new firmware but now I need to write a new bootloader.

Sure, this is no problem when using the AVR programmer but is it also possible to do this via USB? My idea was flashing a special firmware via the old bootloader which then can write a new bootloader. Is that possible? If yes, how?

My current tries were not successfull. All commands (boot_page_erase, boot_page_fill and boot_page_write) are executed successfully the same way as the bootloader is doing it (only difference is that it starts writing at index 0x1800 and not 0x0000) but when I restart the device then the old bootloader is still there so nothing was really written to the flash.


Solution

  • The memory of the ATmega88 is divided into two sections, Read-While-Write (RWW) and Non-Read-While-Write (NRWW). Only from the NRWW-section it is possible to execute the SPM command (if executed from the RWW section, it silently fails). See section 2.2 (page 6) of the datasheet. The NRWW section is equivalent to the biggest possible bootloader section.

    So, to reprogram the bootloader using the SPM instruction, you need to have at least a minimal stub of code in the bootloader section, which you do not delete and which executes the SPM instruction for you.