Upload/Flash Bootloader + Sketch in one Step

Hi,

I have a question.
I want to flash the Bootloader and my sketch I made in one step.

At the moment:
I am able to use this Programmer to flash the Bootloader on a brand new Atmega328 using my Arduino Uno Board.
Using this Setup: http://www.gammon.com.au/images/Arduino/Minimal_Arduino4.jpg
As I want to use an external 16MHZ Clock I added an 16MHZ quarz on the breadboard.

I used the Board Detector, but I think it is not possible each time.
Then I use the "Atmega_Board_Programmer"
Here i have to Press 'U' for 16MHZ and then 'G' to programm the bootloader at the end I have to press 'C' to programm another chip.

Now Is it possible to modify this like this.
I dont want to choose between 16MHZ and the 8MHZ. I want 16MHZ all the time. and when this is finished it should flash for example "mysketch1.ino".
When it is Done I want a message like "Press 'S' to Programm new Sensor".

Thanks

It sounds like you want to combine the bootloader HEX file and your sketch HEX file into one. There are tools out there that can help you, or it is possible to do manually.

The files are called "Intel HEX 8-Bit" files (or INHX8 files), and the format is described here: Intel HEX - Wikipedia

I could do it manually for sure.
These are the steps I have to do.

  1. Open the Arduino IDE and open the Sketch "Atmega_Board_Programmer"
  2. Upload the sketch
  3. Run Serial Monitor
  4. Send 'U' for 16MHZ
  5. Send 'G' to programm
  6. Open another Arduino IDE and load "mysketch1" and upload it.
  7. Repeat this.

Now my solution would be. I edit the Sketch "Atmega_Board_Programmer" and remove the choice in step 4 and remove the verification line from step 5.

Now I just need to find a way to upload "mysketch1" automaticaly after the bootloader has been written.

No, by "manually" I mean manually combine the files - they're just text after all. That way you end up with one single .hex file that is both the bootloader and your sketch combined into one file. Currently the bootloader .hex already contains a sketch - Blink.ino. You would need to strip that portion out first and replace it with your compiled sketch instead.

Then you just upload your modified bootloader .HEX file instead of the normal bootloader .HEX file and you have your sketch on there ready to rumble.

I attached the code.

but I dont know where I can find the raw data of the used hex file to compilpe the new hex file.

Atmega_Board_Programmer.ino (21.2 KB)

When you press the compile button in the IDE it makes a .HEX file. Turn on verbose compilation to see where it makes it. Copy that file somewhere useful, along with the .HEX file for the bootloader. Then take a look at them and see the similarities. Reference the Wikipedia page I pointed at to understand the format, and work which of the bootloader HEX file need to be replaced or augmented with the contents of your sketch .HEX file.

Currently the bootloader .hex already contains a sketch - Blink.ino.

Is that true still?

CrossRoads:

Currently the bootloader .hex already contains a sketch - Blink.ino.

Is that true still?

Well, the Mega2560 I bought the other day had blink on it out of the box. I don't know about bootloaders you'd download though.

Certainly if it has blink in there you'd be replacing it - if it hasn't then you obviously won't be.

I don't know if that's the bootloader, or if blink was installed after the bootloader. I install blink on uCs after bootloading as a quick test to confirm the USB function is working.

ok sorry I think I am really stupid, but I really have no idea what to do, eben after reading the hex file format thing on wikipedia :frowning:

Ok, the simplest way is to get a program called "srecord" - SRecord download | SourceForge.net

Install the windows version of that (or install it however you do for your OS if you're not windows - it's in the Ubuntu packages for instance).

Then put the .hex file for the bootloader and the .hex file for your sketch together in a folder.

Then, from a command line, navigate to that folder and join the two files together with srec_cat. On Windows you will need to tell it where srec_cat is I would imagine because windows is Stupid™.

For example, to join the Uno optiboot and the Blink sketch you would use:

srec_cat Blink.hex -intel optiboot_atmega328.hex -intel -output joined.hex -intel

That would give you "joined.hex" which you would upload instead of the bootloader.