How to add external file(s) to a HEX file before Arduino upload?

Hi all,

What I would like to do is add one (or possibly more) files to a compiled HEX file for upload to an AVR. Note that I am using a Makefile instead of the IDE to compile and upload. What I also need is some way to know WHERE (what address) the added file starts at and ends at (or starts at and it's size).

For example, let's say I had a small sketch that did [b]pgm_read_byte_far()[/b]calls to read the data contained in the externally added file and sent that data to a port which was connected to a DAC (to generate audio).

I need the finished HEX file to load into the AVR (in this order):

  • Regular program code
  • Start address of additional file (accessible by the program)
  • End address or size of additional file (accessible by the program)
  • All the bytes of the additional file (loaded into PROGMEM (flash) of course

The additional file format can be Intel HEX, raw binary or anything else - I don't care (but would prefer raw binary).

I cannot simply convert the file into a PROGMEM array because there is a SIGNED 16 bit size limit to the pointer (that is, the array cannot be more than 32767 bytes long) and the data can be (and usually is) much larger - up to filling all of the flash in a MEGA2560.

I have already successfully done this by uploading just the program, seeing how large it is, then converting the additional file into a HEX file (with a load offset so that it doesn't overwrite the sketch itself). The [b]pgm_read_byte_far() [/b]function works fine to access all of the data and send it to the DAC.

But, it's a manual method and a pain in the rear... having to see how large the sketch is, then manually converting the additional file to HEX with the proper offset, then loading it manually. Also, the flash has to be erased before each go-around or else the new code overwrites the old without erasing the old, making a mess (i.e. doesn't run).

And, getting the erase / program / program sequence right is a pain, otherwise I end up erasing the sketch or erasing the sound file.

If it were all integrated into the Makefile, I could just edit / compile / upload / repeat with ease.

I've already looked at this info: Atmel Website but I just don't get it (or at least I can't seem to make it work).

Any ideas how to do this will be greatly appreciated.