I have a bunch of ATMega328P (dip) to program. If I use the IDE (1.8.15) it erased flash before uploading the sketch and there is no option to stop that. If I use AVRDude or AVRDudess I have the option to erase flash (and eeprom)or not.
I uploaded the bootloader to all the chips (just to set the fuses). Then I went back and uploaded the compiled sketch (hex file) to the chips but in using AVRDudess I told it not to erase flash. Now I am wondering if I should have had it erase flash. Again all that is on it is the bootloader.
Does flash need to be erased before an upload? Or does it matter?
Disable auto erase for flash. When the -U option with flash memory is specified, avrdude will perform a chip erase before starting any of the programming operations, since it generally is a mistake to program the flash without performing an erase first. This option disables that. Auto erase is not used for ATxmega devices as these devices can use page erase before writing each page so no explicit chip erase is required. Note however that any page not affected by the current operation will retain its previous contents.
Causes a chip erase to be executed. This will reset the contents of the flash ROM and EEPROM to the value ‘0xff’, and clear all lock bits. Except for ATxmega devices which can use page erase, it is basically a prerequisite command before the flash ROM can be reprogrammed again. The only exception would be if the new contents would exclusively cause bits to be programmed from the value ‘1’ to ‘0’. Note that in order to reprogram EERPOM cells, no explicit prior chip erase is required since the MCU provides an auto-erase cycle in that case before programming the cell.
I loaded the bootloader using a Canaduino bootloader loader. The chip was factory new prior to that. I assume that it erased the chip prior to uploading that bootloader to the chip.
Then I used AVRDudess to upload a hex file to flash memory but did not have it erase flash prior to that. I was uploading using Programmer AVR ISP hat through a Uno that had gotten the AVR ISP example sketch loaded on it. AVRDudess said it loaded the hex file successfully but then I got to wondering how well that would work if it had not erased flash first.
In that scenario, the risk is the interrupt vector table. I assume, while uploading the bootloader, the fuses are set so the bootloader's table is in place. Then, while uploading the application, the fuses are set so the application's table is in place.
Is there a reason you did not use the bootloader to upload the application?
I did not use the bootloader to upload the application because I had 60 of these chips to program and AVRDude is much faster than using the IDE. I saw no easy way to set the fuses in the IDE even though it can be easily done in AVRDude and AVRDudess. I could skip the bootloader completely by just burning the fuses. I failed to do that on the first batch and the program used the ATMega328P internal 1mhz oscillator instead of the external 16mhz clock that I needed it to use.
I tried to combine the bootloader into the sketch at compile time with the IDE as it creates a Intel Hex file of the sketch and also a second hex file that is the sketch plus bootloader but when I tried to upload the combined file I got an error that said there was not enough room on the ATMega328P so I reverted to putting bootloader on first and then upload the sketch. I am still a bit confused as I had AVRDude do a flash and eeprom erase before uploading the hex file to the chip so the bootloader is gone (or I assume it is).