Hi
I'm currently working on controlling an Arduino over wireless. One of the things I want is: being able to upload a new sketch.
I have it nearly all setup but some things are not working.
Because I'm communicating over the serial port directly (not using USB and as such not having a reset line) I send a command to Arduino to use the watch dog to hard reset Arduino. Trying to do an upload after this reset always fails :(. When pressing the reset button it works .
So I looked at the bootloader code at https://github.com/arduino/Arduino/blob/master/hardware/arduino/bootloaders/atmega/ATmegaBOOT_168.c and found this
/* main program starts here */
int main(void)
{
uint8_t ch,ch2;
uint16_t w;
#ifdef WATCHDOG_MODS
ch = MCUSR;
MCUSR = 0;
WDTCSR |= _BV(WDCE) | _BV(WDE);
WDTCSR = 0;
// Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
if (! (ch & _BV(EXTRF))) // if its a not an external reset...
app_start(); // skip bootloader
#else
asm volatile("nop\n\t");
#endif
This looks really great because I see indeed that a restart from a WDT skips the bootloader.
Also the whole section is in a #ifdef which may be the root cause why the Arduino mega and duemilenove do not handle watchdogs properly.
As my project is mega oriented I need a new bootloader anyway. Creating a new bootloader looks easy and promising ....... but
When I look at the code in github I see that all the code is 3 years old.
So my question is: "Is this the latest bootloader code?" "Is the bootloader code still open?"
[EDIT ANSWER]
The latest bootloader for the AVR chips (not the for the arduino mega) is in the optiboot folder. The code is delivered with each and every Arduino IDE so it is still open.
Have you tried looking at the bootloader for the Fio?
"The Arduino Fio is intended for wireless applications. The user can upload sketches with an a FTDI cable or Sparkfun breakout board. Additionally, by using a modified USB-to-XBee adaptor such as XBee Explorer USB, the user can upload sketches wirelessly. "
I have found that the atmega folder is an old folder (stil used for the duemilanove). The "new" bootloader code is in the optiboot folder.
It is really simple to compile the bootloader (just go to the folder in a command prompt and run omake). I compiled the code "as is" and it is 100% (binary compared) the same as the hex file delivered with arduino (tested with Arduino IDE 1.0.1).
But as soon as I change something in the file it compiles but when burned on a Arduino the L led starts flashing and never stops. I can upload code but it looks like the code never starts
I have tried several code changes, but all with the same result.
If I restore the code in original state, compile and "burn" all is fine.
I must be doing something wrong :~. And I guess it is something very basic.
Any help is appreciated
@crossroads
I'm not sure how the fio relates to my problem. I want to upload to a mega (need the extra pins and memory) via wifi ( I need 3000m² coverage).
Best regards
Jantje
The changes I tried in optiboot.c
 // Adaboot no-wait mod
 ch = MCUSR;
 MCUSR = 0;
 watchdogConfig(WATCHDOG_OFF); //added this line
 //if (!(ch & _BV(EXTRF))) appStart(); removed this line
 // Adaboot no-wait mod
 ch = MCUSR;
 MCUSR = 0;
 if (!(ch & _BV(EXTRF)) watchdogConfig(WATCHDOG_OFF); //added this line
 //if (!(ch & _BV(EXTRF))) appStart(); //removed this line
 // Adaboot no-wait mod
 //ch = MCUSR;
 //MCUSR = 0; //removed the whole Adaboot no-wait mod
 //if (!(ch & _BV(EXTRF))) appStart();
CrossRoads:
I was thinking if you were creating a modified bootloader, then borrowing xbee code from the Fio might help.
Well if I fail to remove a mod I don't think I have much chances of importing xbee code.
Could this be related to the fact I burn using the Arduino as isp?
Anyone any idea on why my (removing a) mod does not work?
Anyone any idea what this code is for in the original mod (see first post)
A update
I have learned plenty of things about the Arduino bootloader and AVR bootloading by now and I now know why my changes didn't work 8)
They didn't work because the mod for the watchdog has been used by now for many other implementations. In other words the bootloader itself uses the watchdog to reset itself.
The bad news is that I still don't know how to fix this in software.
The good news is that it does work with simply attaching a header (I used A5 for testing) to the reset pin to reboot and start the bootloader.
So I learned a lot and I have a solution
I just wished I had lost less time
Best regards
Jantje