Reading flash to a file using ArduinoISP

I have a sketch that I will program to many Nanos using a Selfrog. The Selfrog is a standalone programmer which doesn't connect targets through to the computer, so I setup a Nano as a programmer with ArduinoISP. It's easy to set up the sketch as a hex and program it, but I want them to have bootloaders as well.

Is it possible to upload the hex of the sketch without blowing out the bootloader? Is it possible to combine the sketch and a bootloader before uploading? I would seem that the simplest method would be to upload the sketch via USB, then download the contents of the flash into a file. However I haven't been having much luck with that.

I can upload a sketch to the target using the arduino programmer from within the Arduino IDE. However with avrdude I get the out of sync 0x00 error, using the same com port. I have D11, D12, D13 connected to the same pins on the target Nano. . D10 connects to reset on the target, there is a 10uf capacitor between reset and gnd on the programmer. Gnd connects to gnd and the programmer's 5v feeds the target's VIN.

Is there something else I can use besides avrdude for reading out the flash? I've tried AVR Studio, but it won't connect to the ArduinoISP apparently due to STK500 being too old of a version.

IanJohnson:
Is it possible to upload the hex of the sketch without blowing out the bootloader?

No.

Is it possible to combine the sketch and a bootloader before uploading?

Yes.

...and the programmer's 5v feeds the target's VIN.

Uh, what? VIN?

I suppose it should have been 5v instead of vin, but it still got enough power to be flashed by Arduino IDE

I removed the last line from optiboot_atmega328.hex and appended it to the beginning of the sketch .hex. The sketch works fine, but it won't sync when attempting to upload a new sketch via USB with the Arduino IDE so the bootloader doesn't seem to be working. When I appended Optiboot to the end of the sketch, the sketch didn't run properly.

I use AVR Studio to upload the hex to the programmer and set the fuses and lock bits. One of the fuses is BOOTSZ which sets "Boot Flash size" and "words start address". Does this need to be set in some way to correspond somehow with how the bootloader has been placed in the merged .hex?

I would still prefer to download from a working chip, but I can't figure out what is different between Arduino IDE and avrdude that is keeping avrdude from connecting to the nano-as-controller. This is the command I'm using to try and read the target Nano-

avrdude -C ..\etc\avrdude.conf -p m328p -P COM5 -c arduino -U flash:r:backup.bin:r

The tx/rx lights blink a couple of times, but nothing else happens until avrdude gives up and throws the "avrdude: stk500_getsync(): not in sync: resp=0x00" error.

The same thing happens when trying a write, however I can compile/upload a sketch through the programmer from the IDE no problem.

I have this partway solved- The ArduinoISP sketch sets the baud to 19200, and I had to add -b 19200 to the avrdude command line to override the default baud rate.

I'm using the Nano with some electronics I designed as a part of a kit, and I want the end user to be able to update the firmware via USB which is why I am trying to flash them with both the sketch and a bootloader.

I took a fresh Nano (Nano A) and uploaded the sketch to it with the Arduino IDE via usb. The sketch ran properly.

I used avrdude to read out the flash to a .hex, and also displayed the fuses which matched with what was in the boards.txt in Arduino.

I uploaded the .hex to a new Nano (Nano B), with the same fuse bits and lock bits. After flashing the .hex everything ran as expected.

I plugged Nano B into USB and uploaded the sketch with the Arduino IDE to verify that the bootloader was working and it could be updated via USB. It connected and the upload completed. However the sketch didn't run properly. A servo moved slowly when it should have been still, and a potentiometer that controls the speed of a motor via PWM didn't work over the full range of motion and the motor never went full speed.

What could uploading via USB change to make it stop working? Since it started with a hex made from a bootloader+sketch combo that was made by Arduino IDE, I would think that uploading that hex would put everything in the right place so that updating with the IDE wouldn't write over something or create some kind of incompatible setting.

Thank you for the follow-up.

I suspect the fuse bits are not correct (specifically the clock option). The target is very likely running at 1 MHz from the internal oscillator.

From the Arduino IDE, use Tools / Burn Bootloader with verbose enabled to see all the steps the Arduino IDE performs. If you mimic what it does (with your HEX file) I suspect everything will work correctly. (You could even use your HEX file from the IDE by modifying boards.txt.)

With a little scripting, you can copy everything from the 328 and then with a write script, create an exact duplicate to a blank chip. The Windoz script is here http://www.instructables.com/id/Copy-n-Paste-Arduino-Firmware/

I do not think much effort would be required to make this work on iOS or Linux.

Ray