Go Down

Topic: HEX files, bootloader+firmware images for pre-programming CPUs (Read 944 times) previous topic - next topic


OK, you've used your Arduino(s) to prototype your project, and you're ready to build something.  You do your board designs, you order parts, and you want the chip vendor to pre-program the image onto the CPUs so you don't have to subsequently program EVERY! SINGLE! ONE!

But there's no switch or button that says "make a combined bootloader + firmware image".

Don't worry, it's pretty easy.  I'll show you how it's done.

The Intel HEX format is described here:  https://en.wikipedia.org/wiki/Intel_HEX .  It's a plain text file.  Yeah.  And it gets better.

The last line of the file marks the "end of file" (aka EOF).  It typically looks like this:

Whenever you build your project using the Arduino IDE, the output file will go to someplace similar to
  /tmp/build{alphabet soup}.tmp/YourProject.cpp.hex

That's the firmware image.  If you open it up with a text editor, you can delete the 'EOF' line at the end, AND THEN you'll just need to add the bootloader 'HEX' file information directly after it, to produce a single HEX file with your firmware, followed by the bootloader.

Arduino bootloaders come in a lot of flavors.  But there's a way to find out what the right one is, and the right fuse settings to go with it (a chip vendor will want these, too, in addition to the image).

The file 'boards.txt' (arduino directory, 'hardware/arduino/boards.txt') has several sections that describe the board types that you see in the 'Tools/Board' menu in the Arduino IDE.  So find the appropriate section (the one you used when you compiled your image) and find the 'boardname.bootloader.path' and 'boardname.bootloader.file' entries.  Bootloaders are located in 'hardware/arduino/bootloaders' under the appropriate bootloader sub-directory.  For the Uno, it might be 'optiboot/optiboot_atmega328.hex'.  For older Arduino boards and clone boards, it might be 'atmega/ATmegaBOOT_168_atmega328.hex'.  Your Mileage May Vary.

In the text editor in which you opened your firmware HEX file, and deleted the EOF line, simply paste the contents of the bootloader HEX file immediately after, with no blank lines.  Then save the image, something like MyProject_plus_bootloader.hex would do nicely.  You can now flash this image with an ISP programmer, a chip programmer, or basically ANYTHING capable of flashing a bootloader, except it will also flash the firmware image along with it.

On a related note...

the 'boards.txt' file is EXTREMELY useful if you are building a customized board using the Arduino IDE.  OK you started out with an Arduino, and now you want YOUR board to have "different somethings".  All of this can be specified in the boards.txt. file so that you can flash proper firmware and bootloaders.  Basically you can 'clone' an existing section, rename the tags to use 'myboard' instead of whatever it used to use, then re-load the Arduino IDE.  Voila, your board type shows up in the 'Tools/Boards' menu!  And, you'll have the right settings for it, assuming you added it correctly.

Oh, and if there's a better way to do 'boards.txt' additions, please don't hesitate to post here.  I found out about sketchbook/libraries while reading through something someone posted online a while back [it wasn't intuitively obvious at the time].  So maybe 'boards.txt' is similar, and there's a LOCAL version that adds to the list of entries already in the 'system' boards.txt file?  If there is, I certainly want to know about it.

Go Up