Hello everyone.
I have an Arduino MEGA 2560 R3 that got semi bricked, God knows why. The thing was still working with the last programmed sketch, but won't receive any new code via the USB programming feature. Though the USB still connects (makes that windows USB in sound and all), and Windows see it as a COM port, trying to establish a serial connection (i.e. Serial monitor) fails. I suppose that somewhere in the board, the Serial over USB stopped working... the built in pin13 LED no longer lights up, neither do the TX and RX LEDs.
So I figured, if the MEGA2560 processor still working, why not just program it directly via the ICSP header, using a programmer? Well while I lack the programmer itself, the "semi" death of this arduino prompted me to get a few extra Arduino MEGA, so I found out about the MEGA ISP found on the arduino examples and built the wiring to connect one of my good arduinos, uploaded the sketch to it and started attempting to program.
Using the Arduino IDE, avrdude seems to run without problems. It writes and verify the sketch bytes, no error messages are reported. The issue is that though the upload happens, the sketch doesn't seem to be running. Obviously I choose using the Blink example to upload via programmer because nothing could be simpler, and even though the built in LED on the bad board isn't working, sticking a LED between GND and PIN13 should do the job.
Frustrated with the non-working of the sketches, I decided to burn the bootloader to the bad board, and I was surprise to see the LED on pin 13 blink in a quick fashion. I suppose that's what the bootloader does when it's just hanging in there with nothing else.
I grabbed another good Arduino and tried programming sketches on it. The result is always the same... uploads ok, nothing happens after that. I also found out that uploading sketches via the IDE eliminates the bootloader completely, so the good board is no longer programmable via USB, until you burn back the bootloader to it. For a few minutes I thought I bricked another one, those were sad minutes.
After lots of reading here and there, and talking to some friends who are into MCUs I was persuaded to install Atmel Studio 7 and try writing a program there to see how it would behave. So I figured I'd write the blink example, of course.
#ifdef F_CPU
#undef F_CPU
#endif
#define F_CPU 16000000 // Set MEGA 2560 R3 Clock
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
//setup
DDRB = 0x80; // pinMode(13, OUTPUT)
//loop
while (1)
{
PORTB = 0x80; // digitalWrite(13, HIGH)
_delay_ms(1000);
PORTB = 0x00; // digitalWrite(13, LOW)
_delay_ms(1000);
}
}
Then I setup a custom upload tool, which is basically a copy of the same line the arduino uses to upload the sketch, changing just the name of the HEX file output by Atmel Studio and WHOA! It works. :o
That prompted me with numerous head scratches trying to understand why in the world an HEX generated by the Arduino IDE won't work, and this one by the Atmel Studio 7 will.
I also tried manually running command lines for the HEX files generated by the "Export Compiled Binary" menu functions, both the Blink.ino.mega.hex, and Blink.ino.with_bootloader.mega.hex, still the same non working result.
TL;DR;
So then, I seek your guidance with the question that summarize the entire story above:
How can I get sketches made in the Arduino IDE, uploaded to an Arduino MEGA2560 R3 using another Arduino MEGA as the programmer, to run on the target Arduino as it would if it had been uploaded via USB?
Thank you in advance.