Sketch upload via ArduinoISP

This requires a recompile of the Arduino software, which may include issues such as the inclusion of a 32-bit librxtxserial in the linux distro.

I purchased a Cricket (Atmega644) from (via ebay), and didn't have a way to program it, so I decided to adapt the Arduino to program it. After looking at diagrams and everything I could upload the Arduino bootloader, etc. After getting the Sanguino software loaded, and entering it in boards.txt, I could upload 1 sketch to it, via setting perferences in boards.txt. The second sketch failed. I could upload the bootloader then again get another sketch to upload. After finding the cause being the -D option passed universally from the arduino software to avrdude, I disabled this and it works to upload, and I suspect will work with any programmer. (I can not prove that.)

I propose adding an option upload.disable_erase which will default to true, and if true, will still enable the -D option. Only if it is false, will the default behavior be changed. (Though if the value is set to anything other than false it will be, unfortunately, I haven't programmed java in 10 years, can't find a .notequals("") function, and when I tried (!boardPreferences.get("upload.disable_erase").toLowerCase().equals("false"), it threw back a warning. So that's a known problem.)

    /* This option is to allow ISP uploading of sketches. 
       Otherwise 1 sketch can be uploaded and all others will fail. */
    if (boardPreferences.get("upload.disable_erase") == null ||
        boardPreferences.get("upload.disable_erase").toLowerCase().equals("true")) {
        commandDownloader.add("-D"); // don't erase

This would allow the comment on line 44 ( // XXX: add support for uploading sketches using a programmer) to be removed.

If there is a more appropriate place to send this (mailing list or such), if someone could refer me to that, I would appreciate it. (I looked, but couldn't find it, it's probably something that should be in the FAQ.)

(Also the comment isn't strictly true, if the sketch is the exact same, except for changing a few 1s to 0s, it can also upload fine. )

the 168/328's have a fuse setting to prptect the bootloader. Maybe you just need to set that?

bill2009, my apologies if I wasn't clear enough:

I can burn the bootloader just fine. This works. Flash is then 0xFF all the way to the bootloader's start (0x7800, if I recall correctly), as it erases until the address. I can then burn a sketch. This works. Flash then contains the sketch in the lower part, 0xFF to the bootloader. It works. I then try to burn a second sketch. This fails*, because the flash can't be erased (aka the erase area set to all 1s).

After modifying the arduino software (app/src/processing/app/debug/ to disable the -D option, a second (or more) can be burned successfully. I don't think it erased the bootloader. (Even if it did, there should still be the option to allow flash to be erased when writing a sketch to it with an ISP programmer, when you don't technically need the bootloader, which is the case here.)

*It doesn't fail all the time, say I have a variable that was previously say 001100100 (=100base10, if I counted correctly), I can then change it to say 4 (00000100), and it will work fine.

This makes sense, as how flash works is, that an 'erase' sets all values to either 1 or 0 (in this case 1) depending on how the logic is. A 'write' will swap particular bits from whatever the erased state is, (our case 1) to the other (0 in this case). However, a write is one way, and can not swap any bits back to the 'erased' state. To write something to a particular byte, if you have data you want to keep, read in however large the erase block is. Change that, erase the whole block, then write the data back. If you don't want to keep any data from the block: erase, then 'write' new data. Which explains why certain variable changes will work (all the code is exactly the same, just the variable changes in the compiled sketch.)

Ok, I've got it figured out, tracing back where it adds it, same as this guy's post which I found via searching for upload.using.

The documentation on boards.txt is severely lacking.