Writing to internal FLASH on Arduino


In LCD88 I use internal flash as storage for model definitions. Of course, these should be editable without reflashing all firmware:-)

On AVR chips writing to flash is only possible from bootloader section, so I needed such feature in my bootloader.
I adapted my old X-MODEM bootloader to provide some entry points to functions writing flash. But because of using X-MODEM as communication protocol, using it was quite difficult for ordinary people. Then, I came up with idea that bootloader compatible with Arduino could solve this accessibility issue.
Nowadays, all things that can be invented, are invented and done by some people, so I started to find ready-made bootloader with all needed features. Most promising was Optiboot, but it lacked possibility to use it for writing flash by application.

Time was passing by and I was very surprised that I can’t find ready-made solution. More surprising was that on Arduino forum writing to flash by application was perceived as ‘mission impossible’. Every topic about it ended by failure or with getting external memory for storage (flash, fram or simply SD card), or advice to write your own bootloader:-)

My conclusion after all this research was: I must write my own bootloader or contribute to some other project to add such functionality. Then I gave a first look at the code of Optiboot, it’s issues and I found issue #52 with one promising patch at the end. But this patch didn’t fit to my needs, as I need fill-erase-write sequence to not using RAM as buffer, and it doesn’t do that that way.

A year passed by and I returned to Optiboot. This time I was going to do it by mysef. It was time of learning C, hacks used in avr-gcc, bugs in different verions of avr-gcc, learning Github flows, branching, merging, even writing in Arduino as I needed to provide some working example of using this feature. But finally I made it! ­čÖé

Writing to flash

Optiboot with possibility to write to flash by applications could be downloaded from my ‘supermaster’ branch (also with some fixes not available yet in upstream): https://github.com/majekw/optiboot/tree/supermaster. It includes also example Arduino sketch (flash_program, heavily commented) writing to flash using new Optiboot.

So, feel free to use this long waited feature in Arduino world, and spread new, now better Optiboot bootloader.

This entry was posted in Electronics and tagged , , . Bookmark the permalink.

3 Responses to Writing to internal FLASH on Arduino

  1. kosmin says:

    Hello, first, good job for you work, I have used your library with example code writing to flash, but everything I do, only write a specific address. I tried (no so advanced programmer) to write flash to different address, and nothing. I’m experimenting on Atmega328 (Arduino) but failed. I want, at one command (timer, event …) to delete all flash (with bootloader or not). The demo code you provide only delete or write a specific page (I don’t know his address but I open hex file after writing to flash), no matter how I change the code. Please can you provide me a solution how to use your library to delete all flash, or at least, page, by page the flash from Atmega328.

    kosmin.dumitru (AT) gmail (dot) com

  2. uzi18 says:

    Da sie wykryc czy bootloader ma taka mozliwosc? Zalozylem issue w github.

    • majek says:

      Odpisa┼éem na githubie, ale w skr├│cie, to ja bym sprawdzi┼é najpierw rozmiar bootloadera czytaj─ůc odpowiednie ‘fusy’, je┼╝eli to b─Ödzie si─Ö zgadza─ç z Optibootem, to sprawdzi─ç pierwsze 2 s┼éowa bootloadera czy zawieraj─ů instrukcje ‘rjmp’. Wtedy jest du┼╝a szansa, ┼╝e to moja wersja Optiboota ­čÖé

Leave a Reply

Your email address will not be published. Required fields are marked *