Upload hangs midway with large sketches. "timeout"

UPDATED: I've made some progress with my original problem. Skip down to my third post for a more simplified question. I'll leave this up for reference to what was going on.

I can't seem to upload sketches or the bootloader through the USB port. But I can burn .hex files written in AVR Studio through AvrDude in a command window. The hardware I'm working with: Newer Arduino MEGA 2560 with the 8U2 chip Sparkfun AVR Pocket Programmer Windows XP desktop with Arduino 0022 and XP laptop with 0021 AVR Studio 4 I'll start at the beginning with the symptoms. I'm working on a fairly complicated project with tons of sensors and peripherals. To keep the debugging easy I just started with an analog accelerometer and SD card. Everything was working great. Sketches were uploading, sensors were fine, dataloging was writing to the SD, and it was printing to the serial port. Then I uploaded a revised sketch, opened the serial port, it output a few lines and froze. I reset, and the same thing, none of the interrupts were working. I added a Serial.Println were i thought it froze. It uploaded fine, output the new debug line and froze. I uploaded Blink and that worked fine. I tried burning a new bootloader but it wouldn't take. It just sat there, no TX RX LED and the Arduino IDE froze up. AT this point it's 1AM so i can't really remember everything i tried step by step. A few things I can remember. AT one point when I plugged in the MEGA into the USB, my computer would lag (without uploading a sketch or anything, just plugging it in). The USB mouse and keyboard were barely responsive but Windows media player kept on playing. I moved over to the laptop and it did NOT do it there. This symptom has since stopped. At one point, if it froze during upload and I unplugged it then plugged it back in, the led would blink what appears to be a default error message. ( On for about 300 milliseconds, OFF(100), ON(100), OFF(300) and repeat. I've reinstalled Arduino 0022 ( I just overwrote all the files without deleting the entire folder. ) I can NOT burn a new bootloader either through USB or USBtinyISP. Error: avrdude: stk500_getsync(): not in sync: resp=0x00 avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51 I've tried holding the reset button until right before it uploads a bunch of times but that didn't work. I've also got this error message but i can't remember what i did to get it. It was something about 0xff != 0x00 in memory location 0x0000 As of right now I CAN upload .hex files through AvrDude. I can burn a basic bare minimum AVR-GCC sketch. It doesn't do anything but setup(); for (;;) loop(); return 0;. It's 292 bytes. I believe this should have overwrote the bootloader. I can also burn the following code and it works just fine. It's about 4100 bytes long.

#include "wiring.h"

int main(void)
{
    init();
    setup();
    for (;;)
        loop();
    return 0;
}

int ledPin = 13;

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(ledPin, OUTPUT); 
    
}

void loop() {
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(100);              // wait for a second
}

Does this code put the bootloader back on the chip or does it just include everything needed for high level code like digitalWrite() and delay()? When I power up the MEGA either through USB or ICSP it takes about 30 seconds for the led to light up, then it runs fine. If I hit the reset button there is no 30 second delay. I would try uploading a larger sketch using the AVR Studio/AvrDude method but I was getting a lot of errors after building. At this point it appears to me that the 8U2 chip is shot. But why was it intermittently working? Right no only the RX LED blinks every few seconds when i try to upload, and pin13 LED keeps doing what it was doing. It's like it doesn't want to reset, maybe because there's no bootloader? - no error messages, it just keeps working. Any advice would be much appreciated. Also if anyone knows how to burn the bootloader and [u]only[/u] the bootloader using AvrDude and the Pocket Programmer, I'd like to try that.

Some progress... I found the bootloader at arduino-0022\hardware\arduino\bootloaders\stk500v2\stk500boot_v2_mega2560.hex Here's what AvrDude did.

C:\Program Files\arduino-0022\hardware\arduino\bootloaders\stk500v2>avrdude -c usbtiny -p m2560 -U flash:w:stk500boot_v2_mega2560.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9801 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 "stk500boot_v2_mega2560.hex" avrdude: input file stk500boot_v2_mega2560.hex auto detected as Intel Hex avrdude: writing flash (262106 bytes):

Writing | ################################################## | 100% 149.69s

avrdude: 262106 bytes of flash written avrdude: verifying flash memory against stk500boot_v2_mega2560.hex: avrdude: load data flash data from input file stk500boot_v2_mega2560.hex: avrdude: input file stk500boot_v2_mega2560.hex auto detected as Intel Hex avrdude: input file stk500boot_v2_mega2560.hex contains 262106 bytes avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 137.38s

avrdude: verifying ... avrdude: verification error, first mismatch at byte 0x3e000 0x0d != 0xff avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK

avrdude done. Thank you.

Can anyone explain the verification error or why it wrote 262K of data on a 256K chip with a file that's only 23K? I can upload BareMinimum and Blink fine, they both work. When i try my large sketch the program hangs. "timeout" Should i manually reset fuses at described here? http://ntsdt.net/2009/08/22/burning-the-arduino-bootloader/ I don't understand how they work, so I don't want to start playing around with them. I know the new bootloader works as proven by the bootloader monitor. CPU stats Arduino explorer stk500V2 by MLS Compiled on = Sep 9 2010 CPU Type = ATmega2560 AVR_ARCH = 6 GCC Version = 4.3.3 AVR LibC Ver = 1.6.7 CPU signature= 1E9801 Low fuse = FF High fuse = D8 Ext fuse = FD Lock fuse = FF Bootloader>

I can burn small sketches fine. SD datalogger is about 14K and that works. But if i burn my 19k file, it burns a bunch of code then stops. “timeout” error.
Any ideas?
also when i burn a new bootloader with avrdude in the command prompt, it writes 262k to the chip. That exceeds the 256k of flash on the chip correct?
at the end of the bootload process i get:
avrdude: verification error, first mismatch at byte 0x3e000
0x0d != 0xff
avrdude: verification error; content mismatch

There is an optiboot bug that prevents uploading sketches larger than about 30K on a mega328. I have not heard of similar problems with the MEGA, and it has a significantly different bootloader, but it COULD be something along those lines.

when i burn a new bootloader with avrdude in the command prompt, it writes 262k to the chip. That exceeds the 256k of flash on the chip correct?

No; 1K is 1024 bytes, so 256K is a bit more than 262000 bytes. The bootloader load process starts with a full chip erase, so it probably claims to have programmed every last byte.

A verification error after burning the bootloader isn’t good, though…

(your sketch doesn’t include a character sequence !!!, does it? (http://arduino.cc/forum/index.php/topic,28656.0.html )

HA HA HA 2 whole days I've been ripping my hair out. I thought i fried part of the memory (like a bad sector on a hard drive).

  // check smoke alarm status
  if (digitalRead(smokeSensor) == HIGH)  {
    Serial.println("FIRE!!!!!!!!");
    // open the co2 valve
    digitalWrite(co2Valve, HIGH);
    delay(10000);
    digitalWrite(co2Valve, LOW);

I just learned about the "!!!" command last night. So, because the bootloader reads all serial commands it caught it and interpreted it as a request for bootloader menu? THANK YOU!!!

This might save someone's sanity. I too was having trouble with sketched >29K on a mac. I was also running Parallels Virtual Machine with windows XP. On a hunch I stopped parallels and no more problems. DId not really research what the interplay was, but I am sure there was a war over who owned the USB port. I will be some cleaver lad/lassie know a Paralles parameter or two that would fix it.

srb

JetIgniter2k: THANK YOU!!!

I see what you did there. ;)

You just saved me hours of searching!!! i made same mistake! thanks! 8) :smiley: