ArduinoISP @57600 baud instead of 19200

Hi,

Yesterday my extra Atmega328P arrived. Today I finally had time to play with it.

I have programmed my Arduino Duemilanove with the ArduinoISP from the Examples menu. So now I can program the new Atmega328P.

There's one thing I can get to work and searching the internet and forums isn't helping me out.
I've tried to increase the baud rate to speed up the programming. In the ArduinoISP's setup() is a Serial.begin(19200). So I thought changing this to 57600 would do the trick. It doesn't :frowning:

When I change it to 57600 (or anything else) I keep getting errors. e.g.

D:\Development\Arduino\temp>avrdude -p m328p -c avrisp -P COM3 -b 57600 -v -u -U flash:w:RgbMatrix.cpp.hex

avrdude: Version 5.10, compiled on Jan 19 2010 at 10:45:23
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Programs\WinAVR_20100110\bin\avrdude.conf"

         Using Port                    : COM3
         Using Programmer              : avrisp
         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 : STK500
         Description     : Atmel AVR ISP
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         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.13s

avrdude: Device signature = 0x1e950f
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed

         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "RgbMatrix.cpp.hex"
avrdude: input file RgbMatrix.cpp.hex auto detected as Intel Hex
avrdude: writing flash (3436 bytes):

Writing |                                                    | 0% 0.00s
avrdude: stk500_paged_write(): (a) protocol error, expect=0x14, resp=0x64
avrdude: stk500_cmd(): protocol error

D:\Development\Arduino\temp>

After this the target Atmega looks erased and doesn't do anything. (Switching back to 19200 baud allows me to program it again.)

How can I increase the programming speed of ArduinoISP?

Another question I'd like to be sure about:
When I write a program to the target Atmega using ArduinoISP, it overwrites the Arduino bootloader, right?

The programming speed is not limited by the baud rate it is determned largley by the rate the flash memory is programmed. There is nothing you can do to speed this up.

When I change it to 57600 (or anything else) I keep getting errors. e.g.

If you look at the source of ArduinoISP, there is a call to a blink led function called "heartbeat":

  // light the heartbeat LED
  heartbeat();
  if (Serial.available()) {
    avrisp();
  }

If you comment out this call to heartbeat, you should be able to run ArduinoISP at 57600 baud.

The problem is that heartbeat uses delay and while blinking at high baudrates, serial input data will overflow the input buffer and loss of data occurs. Baudrate is a limiting factor and so increasing it should give you a faster overall programming speed as well.

WARNING!
Please note that when changing the baudrate to 57600 there is no protection against incidentally invoking the Arduino bootloader rather than the ISP sketch it is running. As a consequence you may erase and reprogram your programmer (Arduino) rather then the target device.

Unless you are aware of this issue and understand how to safely disable auto-reset, I would recomend keeping speed at 19200 baud and not attempt this modification.

If you comment out this call to heartbeat, you should be able to run ArduinoISP at 57600 baud.

Interesting, gonna try it out too :slight_smile:

BenF, thanks for the very useful reply.

When I accidentally overwrite the programmer, that is within the bootloader right? So I should be able to reprogram it as usual, with just the Arduino board, over USB.

I'm still not sure when the bootloader is overwritten and when it's not. Anyone any comments on that?

As a consequence you may erase and reprogram your programmer (Arduino) rather then the target device.

Please explain how that is possible.

How would it program when the programmer is not available?

When I accidentally overwrite the programmer, that is within the bootloader right? So I should be able to reprogram it as usual, with just the Arduino board, over USB.

Yes, you should be able to reprogram the Arduino through the USB/IDE as usual.

The standard behavior of avrdude (the AVR upload tool used by Arduino) is to do a complete chip erase prior to programming. This step however is ignored by the bootloader to avoid self destruct.

If your Arduino is configured for auto-reset, the bootloader will start when avrdude attempts to access your board. If the commanded baudrate is different from that of the Arduino (the bootloader expects 57600 baud) it will time out and eventually sync up with the Arduino ISP sketch. This timeout, resynchronization and finally programming at a moderate baudrate add to total programming time. For users who just use the programmer for the occasional upload of a bootloader – the longer time may not be an issue at all.

How would it program when the programmer is not available?

Form the point of avrdude, a bootloader is just another programmer. When your Arduino resets, the bootloadrt starts and it behaves just like a programmer.

Form the point of avrdude, a bootloader is just another programmer. When your Arduino resets, the bootloadrt starts and it behaves just like a programmer.

You mean the 'sketch area' (iow where the ISP is loaded on the flash) of the Arduino acting as the programmer?

I have been just messing around, but I seem to have it running quite reliably at 115200bps.

Here are my modifications:

Add:

#define MAXZ (19200L * 30)
#define BAUDRATE 115200
#define HBDELAY MAXZ/BAUDRATE

Modify:

void setup() {
  Serial.begin(BAUDRATE);
  ...
}
uint8_t hbval=128;
int8_t hbdelta=3; // play with this to get what you like
void heartbeat() {
  if (hbval > 224) hbdelta = -hbdelta;
  if (hbval < 16) hbdelta = -hbdelta;
  hbval += hbdelta;
  analogWrite(LED_HB, hbval);
  delay(HBDELAY);
}

Proof? ::slight_smile:

>avrdude -Cavrdude.conf -cstk500v1 -P\\.\COM3 -b115200 -p atmega8  -U flash:w:ArduinoISP2.cpp.hex:i

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.04s

avrdude: Device signature = 0x1e9307
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "ArduinoISP2.cpp.hex"
avrdude: writing flash (4386 bytes):

Writing | ################################################## | 100% 2.78s

avrdude: 4386 bytes of flash written
avrdude: verifying flash memory against ArduinoISP2.cpp.hex:
avrdude: load data flash data from input file ArduinoISP2.cpp.hex:
avrdude: input file ArduinoISP2.cpp.hex contains 4386 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 2.56s

avrdude: verifying ...
avrdude: 4386 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done.  Thank you.