Atmega Self programming

I am new to atmel family ... I am looking for a feature as Atmel Image(hex file) in the external EEPROM(I2C) and make a bootloader(of ATMega8) capable of self programming from the EEPROM and execute the sketch/program.

If any one gone with this kind of feature please help me.

I am using ATMega8 connected to EEPROM. and every time new hex file updated to EEPROM.

I think that is not possible. The bootloader would have to include I2C code, and that would make it big.
The most used bootloader is OptiBoot : GitHub - Optiboot/optiboot: Small and Fast Bootloader for Arduino and other Atmel AVR chips

Here is a topic about it : Self burn by i2c EEPROM (bootloader) - Other Hardware Development - Arduino Forum

Tell us about your project. Perhaps there are other (and better) ways to achieve your goal.

How does the EEPROM get loaded?
Bootloader sizes of 256 bytes, 512 bytes, 1K bytes, and 2Kbytes are supported.

How much of the Atmega8's 8K of flash is needed to access the external EEPROM?
– 8KBytes of In-System Self-programmable Flash program memory
– 512Bytes EEPROM
– 1KByte Internal SRAM

Got a similar setup, but using an external 4Mbit SPI Flash, mainly because of speed and size. But should be very similar using I2C.

The basic idea is to have the "Main program" responsible for storing a new firmware into the SPI Flash, in my project the new firmware is sent over-the-air (RF) from a base node. After the new firmware is received and verified a few parameters are stored on a dedicated area of the MCU EEPROM.

Every time the MCU boots the bootloader will read the MCU EEPROM, looking for a new firmware configuration on the same dedicated are, including which address and size the new firmware is stored in the external SPI Flash. If everything is ok the bootloader will run a few CRC checks before and start programming. If no EEPROM info is found, the process logs the error and restart, which will fall-back to the standard Serial Port programming.

The operation result, being an error or success gets logged back into the MCU EEPROM, so the main program will know what happen after the next reset. It happen only once per "new firmware", so the bootloader won't try indefinitely to install the firmware, avoiding to keep writing to EEPROM every boot.

I also included a feature to "factory reset": If the board is powered while holding a button for a few seconds, it check a reserved area of the Flash and, if there's a valid image there, it writes to the board. The "factory firmware" in this case is a "Main program" capable of receiving and storing a new firmware, in other words, you would never need to connect the board to a PC again to program.

All of that is running on top of the OptiBoot and takes around 1.6K and there's probably room for a few optimizations, I just decided to keep the code more readable. The project is based on Atmega328P, so 2K is not that much compared to the functionalities you get. To keep size small, the code doesn't use any Arduino libraries, just a few from the AVR Libc.

Advanced features like, reverting the previous/factory firmware in case the new image is broken, or the new Main Program is unable to contact the base station is also possible.

If you're interested the code for this bootloader and additional documentation, everything will be release very soon, as part of a board launching I'm organizing. You can get more details of those links Bitbucket and Whisper Node - AVR - Wisen


Hi Koepel, CrossRoads

Thank you guys for reply...sorry for my late reply...

As of now my thought of project is as shown in below flow

ESP8266 will receive latest hex file from the WiFi and stores into external EEPROM and it will send a update command to ATmega8. ATmega8 will initiate soft reset, and updates the hex file from eeprom (bootloader) and performs self flash.

I have no choice, I have to try code size of bootloader as minimum as possbile to fit in bootloader area along with I2C. I will take care of hex file should not reach the beyond capacity of external eeprom and ATmega8(8kBytes).

EEPROM is as of now only hex file holder only, No other contents I am going to load in this.

And Hi musskopf,

Thanks a lot for your project...It looks more similar to my plan. I try with your references and will update you once I get any update.

Hi Chippevijaya,

As promised before, here's the link for the modified version of OptiBoot with support for self-programming from an external SPI Flash: Bitbucket

The code has been written to be shipped with our boards (Whisper Node - AVR - Wisen), but it should work with any other board running atmega328p without many changes. Also, there's probably room for optimization and code reduction but I prefer to keep it more readable so everybody can understand what's happening.