Why can't I get AVRDUDE to erase?

I have Arduino IDE 0018 running on a Mac OS machine. It works fine. I can download a sketch and it runs.

I am using an Ocean Controls KTA-223 "Relayduino". It is an ATMega328p. I guess I don't know how to determine the version of the arduino boot loader.

I have avrdude 5.9 on the same machine. -U flash:r:test.hex:i works and the file makes sense. When I read fuses, it tells me they are all 0 although I see that the boot loader starts at 0x7800 (0x3c00) implying that at least BOOTSZ1 is programmed.

This command: avrdude -p m328p -b 57600 -c arduino -P /dev/tty.usbserial-A700f0Vr -e says "erasing chip" but it is not erased and the sketch still runs.

I tried programming with an all 0xFF Intel Hex file. avrdude tells me my file has 0 bytes. So I can't be sure that my file is correct, but intentionally giving it a bad checksum elicits an appropriate error. I tried changing the first byte to non-0xFF, thinking avrdude might not count 0xff bytes but it still says 0 bytes and still doesn't change the device.

This seems simple enough, especially the -e chip erase. What could be wrong? Especially since the Arduino IDE works fine.

avrdude -p m328p -b 57600 -c arduino -P /dev/tty.usbserial-A700f0Vr -e

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f avrdude: erasing chip

avrdude: safemode: Fuses OK

avrdude done. Thank you.

I don't have an answer for you, but I do have a question. Why do you want to erase the sketch that was uploaded? What value does an unprogrammed chip have vs. one that has, for example, the blink sketch loaded?

I am asking the same question, and there is no real need to erase as it will do a full erase when you upload

anyway this

When I read fuses, it tells me they are all 0

doesn't sound right

how are you reading the fuses?

Related, does the IDE write an object file somewhere? I can't find it. I suspect it either writes directly from memory or erases the object after the download.

Why do you want to erase it?

Well, I know it's not normally necessary but I'm an engineer so everything has to work or I have to know why. I am working on an application that requires additional data to be written to the application program memory, beyond the sketch code. When the IDE writes, it doesn't erase the whole chip. I looks like it only erases a page at a time. Thus, if you load a shorter sketch, there are unerased words following it. This wouldn't matter but, as I said, I want to come along later and append data to the application. If I could figure out how the IDE causes a page erase, I could use that instead of avrdude but I thought I was going to avoid re-inventing the wheel.

How are you reading the fuses?

avrdude -p m328p -c arduino -b 57600 -P /dev/tty.usbserial-A700f0Vr -v

avrdude: Version 5.9, compiled on Aug 2 2010 at 14:30:33 Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/ Copyright (c) 2007-2009 Joerg Wunsch

System wide configuration file is "/usr/local/etc/avrdude.conf" User configuration file is "/Users/marksullivan/.avrduderc" User configuration file does not exist or is not a regular file, skipping

Using Port : /dev/tty.usbserial-A700f0Vr Using Programmer : arduino Overriding Baud Rate : 57600 AVR Part : ATMEGA328P Chip Erase delay : 9000 us PAGEL : PD7 BS2 : PC2 RESET disposition : dedicated RETRY pulse : SCK serial program mode : yes parallel program mode : yes Timeout : 200 StabDelay : 100 CmdexeDelay : 25 SyncLoops : 32 ByteDelay : 0 PollIndex : 3 PollValue : 0x53 Memory Detail :

Block Poll Page Polled Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack


eeprom 65 5 4 0 no 1024 4 0 3600 3600 0xff 0xff flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00 signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : Arduino Description : Arduino Hardware Version: 2 Firmware Version: 1.16 Vtarget : 0.0 V Varef : 0.0 V Oscillator : Off SCK period : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f avrdude: safemode: lfuse reads as 0 avrdude: safemode: hfuse reads as 0 avrdude: safemode: efuse reads as 0

avrdude: safemode: lfuse reads as 0 avrdude: safemode: hfuse reads as 0 avrdude: safemode: efuse reads as 0 avrdude: safemode: Fuses OK

avrdude done. Thank you.

After more experimenting this morning, I can sort of see what's going on.

  1. The chip erase does nothing.

  2. When avrdude programs the part, it uses page erase as needed to erase each 64 byte page before programming it.

  3. If a page (in the hex file) is all 0xff, then avrdude does not erase it at all. It must be assuming that it's already blank.

And I tried -D thinking avrdude would not then assume the chip was blank, but it didn't change anything.

So, as long as your program doesn't have 64 contiguous, page-aligned, 0xff in it, all is well. Since I want to program more bytes after writing my sketch, then I actually do need 64 contiguous 0xff bytes.

I guess I'll have to dig into the avrdude source...

FWIW - This turns out to be a known issue with avrdude, their bug #30061. I've submitted a proposed fix as bug #30680 and hacked my own copy to do what I want.

The basic problem is programming from a sparse file. If anyone else has this problem, let me know and I can give you my code, but I'm warning you it's a terrible hack.

for the fuses try going into terminal mode

averdude -p part -c programmer -etc -t

then enter

dump fusename

That’s a good suggestion, but it didn’t work. I have my original thing working well, at least. I’ll look at the fuse problem in avrdude. I suppose it’s possible that the boot loader doesn’t support it.

avrdude/avrdude-5.9/avrdude -c arduino -p m328p -P /dev/tty.usbserial-A700f0Vr -t

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude> dump lfuse
[ NOTHING HAPPENS FOR SEVERAL SECONDS ]

dump lfuse
avrdude: stk500_recv(): programmer is not responding