Watchdog reset not working properly

Started by ivars211, August 08, 2015, 06:18:46 AM

ivars211

I have my custom atmega1284p board with Moteino bootloader, but for some reason the watchdog reset inside wireless programming library is not executed properly. The whole sketch is transferred successfully, however after wdt_enable(WDTO_15MS) the sketch stops but no reset. After turning the power on and off I have the old sketch.

Hopefully someone has had such issue as well and there is an easy fix that I am not aware of.

EDIT: Looks like it's a hardware issue since the same code works fine on original MoteinoMEGA. How can it be a hardware issue, if the reset is through software though? I double checked the fuse settings, even if I create a simple sketch with  wdt_enable(WDTO_15MS), it still doesn't work. Running on 3.3V 16MHz

Question to someone who is smarter than me. Can I somehow manually set the  _BV(WDRF) flag, then go back to line 0 with
asm volatile ("  jmp 0");

So that bootloader acknowledge that the reset was caused by watchdog (although the flag was set manually) and then execute the
CheckFlashImage()
from dual optiboot?

ivars211

Sorry for double post, but this is really strange, if I upload this sketch,
//  Watchdog Timer Example
#include <avr/wdt.h>

unsigned long ToggleDelay;  //  When this delay grows to longer than the WDT interval the WDT resets the Arduino

const int LEDpin = 13;

void toggle_led()
{
  digitalWrite(LEDpin, !digitalRead(LEDpin));
}

void setup()
{
  wdt_disable();
  ToggleDelay = 1;   // Start with a very short delay
  pinMode(LEDpin, OUTPUT);
  wdt_enable(WDTO_250MS);  // Set watchdog to 1/4 second
}

void loop()
{
  wdt_reset();
  toggle_led();   // Blinking goes slower and slower until the WDT causes reset, then starts over.
  delay(ToggleDelay);
  ToggleDelay += 5;   // Increase the delay by 5 milliseconds.
}

watchdog reset works fine, so now I have no idea what's wrong.

ivars211

#2
As usual, tried pretty much everything. Turns out when you use Arduino IDE to upload the sketch through ISP programmer you loose the bootloader. That explains why everything worked fine when uploading through FT232RL. I only realised that when I modified the bootloader to always check for SPI flash and nothing happened anyway.

So for those who are stuck without USB interface and want to upload both bootloader and sketch through ISP, here are the steps:

1) Get your sketch .hex, to find out the folder Go to Arduino IDE --  File -> Preferences -> verbose during compilation
2) Open your sketch .hex file with notepad
3) Remove the last line (:00000001FF)
4) Get bootloader .hex (from the hardware\bootloader folder)
5) Open bootloader .hex with notepad
6) Copy all the text from the bootloader .hex to the end of your sketch .hex
7) Paste it to the end of your sketch .hex
8) Save and upload via avrdude

Felix

ISP programming in most cases erases anything in the FLASH, including DualOptiboot. I think there's an option which will skip erasing the chip before writing but the bootloader is there to allow programming via FTDI instead of ISP.