Go Down

Topic: watchdog with the mega does not work? (Read 12764 times) previous topic - next topic

paulinchen

Hi,
here are a test-sketch, please show the comments in the code.

If i compile it and after this runs the sketch and the serial monitor in the IDE is open, when hangs the sketch after the watchdog-event.
If i power-off the board and power-on again, and it is running without the IDE, it works?

paulinchen

westfw

See this WRT building the bootloader:  https://github.com/msproul/Arduino-stk500v2-bootloader/issues/3

I haven't heard of any cases where watchdog operation depends on program size.  That's pretty weird.

nickgammon

I can't reproduce your problem. With your sketch uploaded I get this:

Code: [Select]

The testprogram was now started, please wait a moment...0001
The testprogram was now started, please wait a moment...0002
The testprogram was now started, please wait a moment...0003
...
The testprogram was now started, please wait a moment...0097
The testprogram was now started, please wait a moment...0098
The testprogram was now started, please wait a moment...0099
The testprogram was now started, please wait a moment...0100
20128128128128128128128128128128128128128128128128128128128128128012812812812800000000000012812812812812812812812812812812812812801281281281281281281301281110000000001281283206128000128128128128128128128128128128128128128128128128128======================================================
The watchdog was now started, please wait 8 seconds...
======================================================

The testprogram was now started, please wait a moment...0001
The testprogram was now started, please wait a moment...0002
The testprogram was now started, please wait a moment...0003
...
The testprogram was now started, please wait a moment...0097
The testprogram was now started, please wait a moment...0098
The testprogram was now started, please wait a moment...0099
The testprogram was now started, please wait a moment...0100
20128128128128128128128128128128128128128128128128128128128128128012812812812800000000000012812812812812812812812812812812812812801281281281281281281301281110000000001281283206128000128128128128128128128128128128128128128128128128128======================================================
The watchdog was now started, please wait 8 seconds...
======================================================

The testprogram was now started, please wait a moment...0001
The testprogram was now started, please wait a moment...0002
The testprogram was now started, please wait a moment...0003
...
The testprogram was now started, please wait a moment...0097
The testprogram was now started, please wait a moment...0098
The testprogram was now started, please wait a moment...0099
The testprogram was now started, please wait a moment...0100
20128128128128128128128128128128128128128128128128128128128128128012812812812800000000000012812812812812812812812812812812812812801281281281281281281301281110000000001281283206128000128128128128128128128128128128128128128128128128128======================================================
The watchdog was now started, please wait 8 seconds...
======================================================


As you can see the serial monitor is open, and the sketch is restarting as expected. Also it uploaded OK.

I am using the standard bootloader from my bootloader-uploader sketch.

Code: [Select]

MD5 sum of bootloader = 8A F4 7A 29 43 A0 D8 7C DB ED 09 A3 8F 40 24 1E
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

paulinchen


See this WRT building the bootloader:  https://github.com/msproul/Arduino-stk500v2-bootloader/issues/3


Thanks for the link. With the changes i can recompile the bootloader.

paulinchen

paulinchen

#19
Dec 14, 2013, 07:58 pm Last Edit: Dec 14, 2013, 08:04 pm by paulinchen Reason: 1
I have make more tests today.

1. I use a new MEGA R3 and flashing the standard bootloader from the sketch "Atmega_Board_Programmer" from nick.
2. I use the sketch "watchdog_ok" and after 8 seconds the board is reseting, fine.
3. I use the sketch "watchdog_error" and after 8 seconds the board is hanging, no reset.
4. I use the sketch "watchdog_ok" again and after 8 seconds the board is also hanging.

When i flashing the bootloader again, the sketch "watchdog_ok" is running ok.

paulinchen

nickgammon

Here's an odd thing. This somewhat simpler sketch also does not restart:

Code: [Select]

#include <avr/wdt.h>

void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
 
  //starting the watchdog with 8 seconds...
  wdt_enable(WDTO_8S);
  Serial.println("======================================================");
  Serial.println("The watchdog was now started, please wait 8 seconds...");
  Serial.println("======================================================");
  Serial.println("");
}

void loop() {
  // Zo relaxen und watschen der blinkenlichten.
  digitalWrite(13,HIGH);
  delay(500);
  digitalWrite(13,LOW);
  delay(500);
  Serial.print(".");
}

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

paulinchen

@Nick,
can you reproduce my problem?

paulinchen


nickgammon

Yes I can, and this is slowly driving me insane.

First, the bootloader from my bootloader uploader sketch failed on the simpler version I posted (above).

I then reverted back to the official bootloader in the 1.0.5 IDE, and my simple test above then worked. However both of your sketches now fail to restart. Clearly there is something wrong.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon

How did you recompile the bootloader? I'll try adding a few extra lines.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

paulinchen

To recompile the bootloader i use the make.exe in the DOS commandline-tool.

I use this script (please adapt the paths of your IDE-location):

set BASEDIR=C:\Programme\arduino-1.0.5\hardware
set DIRAVRUTIL=%BASEDIR%\tools\avr\utils\bin
set DIRAVRBIN=%BASEDIR%\tools\avr\bin
set DIRAVRAVR=%BASEDIR%\tools\avr\avr\bin
set DIRLIBEXEC=%BASEDIR%\tools\avr\libexec\gcc\avr\4.3.2
set OLDPATH=%PATH%
@path %DIRAVRUTIL%;%DIRAVRBIN%;%DIRAVRAVR%;%DIRLIBEXEC%;%PATH%
cd C:\Programme\arduino-1.0.5\hardware\arduino\bootloaders\stk500v2
%DIRAVRUTIL%\make.exe mega2560
@path %OLDPATH%



Note: after the recompiling the hex-file was too big. To resolve this problem, you must change the makefile bevor.

https://github.com/msproul/Arduino-stk500v2-bootloader/issues/3

After the recompile i use your lua-script to convert the bootloader hex-file and copy it in your programmer-script.

paulinchen

nickgammon

Well, that was hard work. After quite a bit of experimenting with the source in the 1.0.5 distribution I came up with these diffs:

Code: [Select]

*** /Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/bootloaders/stk500v2/stk500boot.c  2013-05-17 20:20:18.000000000 +1000
--- stk500boot.c  2013-12-15 10:17:10.000000000 +1100
***************
*** 423,432 ****
--- 423,435 ----
   */
  void __jumpMain (void) __attribute__ ((naked)) __attribute__ ((section (".init9")));
  #include <avr/sfr_defs.h>
+ #include <avr/wdt.h>
+
 
  //#define SPH_REG 0x3E
  //#define SPL_REG 0x3D
 
+
  //*****************************************************************************
  void __jumpMain(void)
  {
***************
*** 524,532 ****
    return UART_DATA_REG;
  }
 
- //* for watch dog timer startup
- void (*app_start)(void) = 0x0000;
-
 
  //*****************************************************************************
  int main(void)
--- 527,532 ----
***************
*** 563,580 ****
    //* handle the watch dog timer
    uint8_t mcuStatusReg;
    mcuStatusReg  = MCUSR;
!
    __asm__ __volatile__ ("cli");
    __asm__ __volatile__ ("wdr");
    MCUSR = 0;
    WDTCSR  |=  _BV(WDCE) | _BV(WDE);
    WDTCSR  = 0;
    __asm__ __volatile__ ("sei");
    // check if WDT generated the reset, if so, go straight to app
    if (mcuStatusReg & _BV(WDRF))
!   {
!     app_start();
!   }
    //************************************************************************
  #endif
 
--- 563,591 ----
    //* handle the watch dog timer
    uint8_t mcuStatusReg;
    mcuStatusReg  = MCUSR;
!   
    __asm__ __volatile__ ("cli");
    __asm__ __volatile__ ("wdr");
    MCUSR = 0;
    WDTCSR  |=  _BV(WDCE) | _BV(WDE);
    WDTCSR  = 0;
+   wdt_disable ();
+   
    __asm__ __volatile__ ("sei");
    // check if WDT generated the reset, if so, go straight to app
    if (mcuStatusReg & _BV(WDRF))
!     {
!     
!     boot_rww_enable();        // enable application section
!     
!     
!     asm volatile(
!                  "clr r30   \n\t"
!                  "clr r31   \n\t"
!                  "ijmp  \n\t"
!                  );
!     
!     }
    //************************************************************************
  #endif


Resulting source and .hex file attached.

Your "error" sketch now appears to work:

Code: [Select]

===============================================
the following is only to create a big sketch...
===============================================

132798
164798
68798
100798
4798
36798
======================================================
The watchdog was now started, please wait 8 seconds...
======================================================

........
===============================================
the following is only to create a big sketch...
===============================================

132798
164798
68798
100798
4798
36798
======================================================
The watchdog was now started, please wait 8 seconds...
======================================================

........
===============================================
the following is only to create a big sketch...
===============================================
...


Can you confirm if this works for you?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

paulinchen

#27
Dec 15, 2013, 03:48 pm Last Edit: Dec 15, 2013, 03:51 pm by paulinchen Reason: 1
Hi Nick,
with your new bootloader all my sketches works fine  :)

Many thanks for your support, great work!

There is just one small thing:
then i use your new programmer-sketch, i can compile the sketch, but the uploader shows a error:

Binäre Sketchgröße: 22.842 Bytes (von einem Maximum von 258.048 Bytes)
avrdude: verification error, first mismatch at byte 0x24e4
        0x0f != 0xff
avrdude: verification error; content mismatch


After this, i delete the last 20 lines in the file "bootloader_atmega2560_v2.h"

(always 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,)

and without this i can upload the bootloader. The checksum are identical and it works.

I have check this with 2 boards.

paulinchen

nickgammon

Oh yes, there is a problem with repeated 0xFF in a sketch. I'd forgotten about that, and in my defence it didn't happen to me. You can certainly delete all the (trailing) lines with 0xFF in them in that .h file, and that saves space as well.

I'll update my copy.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon

I think I did an Optiboot loader which does not suffer from the 0xFF issue. I can't recall if that is the one that this sketch uploads or not, but that would explain why I didn't have the issue with the 0xFFs at the end, and you did. Of course in this case you need to upgrade the board which uploads the sketches, rather than the Mega2560 board. :)
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up