Arduino Forum

Using Arduino => Microcontrollers => Topic started by: paulinchen on Dec 11, 2013, 09:31 pm

Title: watchdog with the mega does not work?
Post by: paulinchen on Dec 11, 2013, 09:31 pm
Hello,
i will use the watchdog on my arduino mega 2560 R3. I use the Atmega_Board_Programmer sketch on Nick Gammons homepage to programm your new bootloader on my mega. After this i test the watchdog with a small sketch. All works fine.

Now i use the watchdog in my very large sketch (webserver). It works also fine, until the watchdog is (intentionally) startet (after 8 seconds). Now the programm is hanging (on the bootloader?), no reset...

question: Use this bootloader the parameter -DWATCHDOG_MODS ?

Is here a other trick?




After this i would test a other bootloader. Also i use the MUSHclient to convert the hex-file into the *.h
But i have some  errors:


Welcome to MUSHclient version 4.84!
Written by Nick Gammon.

Compiled: Sep 30 2012.
Using: Lua 5.1.4, PCRE 8.31, PNG 1.5.12, SQLite3 3.7.14, Zlib 1.2.5

For information and assistance about MUSHclient visit our forum at:
  http://www.gammon.com.au/forum/
Can you trust your plugins?  See: http://www.gammon.com.au/security
------------------------------------------------------------
// File =  stk500boot_v2_mega2560.hex
// Loader start: 3E000, length: 8192
Address 40000 out of expected range.
Address 40010 out of expected range.
Address 40020 out of expected range.
Address 40030 out of expected range.
Address 40040 out of expected range.
Address 40050 out of expected range.
Address 40060 out of expected range.
Address 40070 out of expected range.
Address 40080 out of expected range.
Address 40090 out of expected range.
Address 400A0 out of expected range.
Address 400B0 out of expected range.
Address 400C0 out of expected range.
Address 400D0 out of expected range.
Address 400E0 out of expected range.
Address 400F0 out of expected range.
Address 40100 out of expected range.
// MD5 sum = 36 23 EA B9 F9 CC B9 19 2F 62 A6 C8 18 68 E3 A6

byte PROGMEM stk500boot_v2_mega2560_hex [] = {
0x0D, 0x94, 0xDF, 0xF2, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3,
0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3,
0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3,
0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3,
0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3,

...

0xFC, 0xCF, 0x80, 0x91, 0xC0, 0x00, 0x80, 0x64, 0x80, 0x93, 0xC0, 0x00, 0x8A, 0xE0, 0x80, 0x93,
0xC6, 0x00, 0x80, 0x91, 0xC0, 0x00, 0x86, 0xFF, 0xFC, 0xCF, 0x80, 0x91, 0xC0, 0x00, 0x80, 0x64,
0x80, 0x93, 0xC0, 0x00, 0x80, 0xE0, 0x90, 0xE0, 0x20, 0xED, 0x37, 0xE0, 0xF9, 0x01, 0x31, 0x97,
}; // end of stk500boot_v2_mega2560_hex


Please help me and sorry for my bad english, i'm from germany...

many thanks, paulinchen
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 11, 2013, 09:36 pm
That bootloader should not have a watchdog timer problem. Which .hex file are you converting exactly, that gives all those errors?
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 11, 2013, 09:44 pm
The latest board programmer sketch should have this in the bootloader_atmega2560_v2.h file:

Code: [Select]

// File =  stk500boot_v2_mega2560_fixes_watchdog_problem.hex
// Loader start: 3E000, length: 8192
// MD5 sum = 8A F4 7A 29 43 A0 D8 7C DB ED 09 A3 8F 40 24 1E
// https://github.com/arduino/Arduino-stk500v2-bootloader/blob/master/goodHexFiles/stk500boot_v2_mega2560.hex

byte PROGMEM atmega2560_v2 [] = {
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 11, 2013, 09:59 pm
First i use the bootloader from your sketch "atmega_board_programmer" (for the mega2560-board). It works on a little sketch, but not on the real sketch (175 kb)? Here the programm are not reset, it hangs...

After this i use the bootloader under:

https://github.com/msproul/Arduino-stk500v2-bootloader/blob/master/goodHexFiles/stk500boot_v2_mega2560.hex (https://github.com/msproul/Arduino-stk500v2-bootloader/blob/master/goodHexFiles/stk500boot_v2_mega2560.hex)

and with this file i get the errors in your lua-script.

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 11, 2013, 10:19 pm
There seem to be some dodgy lines in that .hex file:

Code: [Select]

:020000024000BC
:10000000F1F7019684369105C9F727982F9880E07B
:1000100090E020ED37E0F9013197F1F70196843651
:100020009105C9F700008091C0008D7F8093C000CA
:1000300081E180935700E895EE27FF270994FFCFD1
:1000400090E00D94ECF597FB092E07260AD077FD7A
:1000500004D02ED006D000201AF4709561957F4F01
:100060000895F6F7909581959F4F0895A1E21A2E75
:10007000AA1BBB1BFD010DC0AA1FBB1FEE1FFF1F4C
:10008000A217B307E407F50720F0A21BB30BE40B9C
:10009000F50B661F771F881F991F1A9469F76095E3
:1000A0007095809590959B01AC01BD01CF0108959D
:1000B000AA1BBB1B51E107C0AA1FBB1FA617B7078E
:1000C00010F0A61BB70B881F991F5A95A9F78095AA
:1000D0009095BC01CD010895F999FECF92BD81BDE7
:1000E000F89A992780B50895262FF999FECF1FBA5F
:1000F00092BD81BD20BD0FB6F894FA9AF99A0FBE51
:0801000001960895F894FFCF69
:040000033000E000E9
:00000001FF


0x40000 is the highest possible address (with 256 kB of RAM) so those addresses at the end are just wrong.

Delete those lines from the .hex file (they are at the end) and run the conversion again. It looks to me like you should get the same result:

Code: [Select]

// File =  stk500boot_v2_mega2560_paulinchen.hex
// Loader start: 3E000, length: 8192
// MD5 sum = 36 23 EA B9 F9 CC B9 19 2F 62 A6 C8 18 68 E3 A6

byte PROGMEM stk500boot_v2_mega2560_paulinchen_hex [] = {
0x0D, 0x94, 0xDF, 0xF2, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3,
0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3,
0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3, 0x0D, 0x94, 0x08, 0xF3,
...
0xFC, 0xCF, 0x80, 0x91, 0xC0, 0x00, 0x80, 0x64, 0x80, 0x93, 0xC0, 0x00, 0x8A, 0xE0, 0x80, 0x93,
0xC6, 0x00, 0x80, 0x91, 0xC0, 0x00, 0x86, 0xFF, 0xFC, 0xCF, 0x80, 0x91, 0xC0, 0x00, 0x80, 0x64,
0x80, 0x93, 0xC0, 0x00, 0x80, 0xE0, 0x90, 0xE0, 0x20, 0xED, 0x37, 0xE0, 0xF9, 0x01, 0x31, 0x97,
}; // end of stk500boot_v2_mega2560_paulinchen_hex


Then use that in the bootloader uploader sketch. Rename the array from:

Code: [Select]

byte PROGMEM stk500boot_v2_mega2560_hex [] = {


to:

Code: [Select]

byte PROGMEM atmega2560_v2 [] = {
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 11, 2013, 10:29 pm
I will test it...

But the question is: Use your bootloader in your sketch the parameter -DWATCHDOG_MODS ?

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 11, 2013, 10:54 pm
I got that file from:

https://github.com/arduino/Arduino-stk500v2-bootloader

I don't know exactly how it was compiled. However I did test the watchdog problem after installing it. I may not have tested a large sketch, but since that file came from the Arduino part of GitHub and was labelled "goodHexFiles" I was kind-of hoping it would be, well, good.
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 12, 2013, 06:41 pm
I will use now the original bootloader (which come with the IDE 1.0.5) for testing.

I have new compiled this bootloader, but the new hex-file is also to big? I make no changes in the sourcecode and in the makefile?

Here are the results:


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

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

-------- begin --------
avr-gcc (WinAVR 20081205) 4.3.2
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Compiling: stk500boot.c
avr-gcc -c -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=16000000UL  -Os -funsigned-cha
r -funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables -Wall -Wstri
ct-prototypes -Wa,-adhlns=stk500boot.lst  -std=gnu99 -D_MEGA_BOARD_ -MD -MP -MF
.dep/stk500boot.o.d stk500boot.c -o stk500boot.o

Linking: stk500boot.elf
avr-gcc -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=16000000UL  -Os -funsigned-char -
funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables -Wall -Wstrict-
prototypes -Wa,-adhlns=stk500boot.o  -std=gnu99 -D_MEGA_BOARD_ -MD -MP -MF .dep/
stk500boot.elf.d stk500boot.o --output stk500boot.elf -Wl,-Map=stk500boot.map,--
cref    -lm -Wl,--section-start=.text=3E000

Creating load file for Flash: stk500boot.hex
avr-objcopy -O ihex -R .eeprom stk500boot.elf stk500boot.hex

Creating load file for EEPROM: stk500boot.eep
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
        --change-section-lma .eeprom=0 -O ihex stk500boot.elf stk500boot.eep
c:\Programme\arduino-1.0.5\hardware\tools\avr\bin\avr-objcopy.exe: --change-sect
ion-lma .eeprom=0x00000000 never used

Creating Extended Listing: stk500boot.lss
avr-objdump -h -S stk500boot.elf > stk500boot.lss

Creating Symbol Table: stk500boot.sym
avr-nm -n stk500boot.elf > stk500boot.sym

Size after:
AVR Memory Usage
----------------
Device: atmega2560

Program:    9146 bytes (3.5% Full)
(.text + .data + .bootloader)

Data:         14 bytes (0.2% Full)
(.data + .bss + .noinit)



-------- end --------

mv stk500boot.hex stk500boot_v2_mega2560.hex

C:\Programme\arduino-1.0.5\hardware\arduino\bootloaders\stk500v2>@path %OLDPATH%
C:\Programme\arduino-1.0.5\hardware\arduino\bootloaders\stk500v2>

What is the reason of this? My idee was, to compile the bootloader with the parameter -DWATCHDOG_MODS...

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 12, 2013, 06:47 pm

I got that file from:

https://github.com/arduino/Arduino-stk500v2-bootloader

I don't know exactly how it was compiled. However I did test the watchdog problem after installing it. I may not have tested a large sketch, but since that file came from the Arduino part of GitHub and was labelled "goodHexFiles" I was kind-of hoping it would be, well, good.


I have testing with your bootloader again. Then i use a code like that in my large sketch:

Code: [Select]
    if (Serial1.available()) {
     ...some other code...
    }


than the programm hangs after the watchdog-triggering, no newstart ?

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 12, 2013, 09:06 pm
Can you post a sketch that reproduces the bootloader issue, not just a couple of lines. Then I'll test that.
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 12, 2013, 09:27 pm

Can you post a sketch that reproduces the bootloader issue, not just a couple of lines. Then I'll test that.


Many thanks for your help, Nick.
My webserver-sketch has many libs and need a lot of external hardware. But i will create a test-sketch for this problem.

Can you give me please a answer for the problem with the bootloader-size?


paulinchen
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 12, 2013, 09:51 pm
Are you talking about this:

Code: [Select]

Program:    9146 bytes (3.5% Full)
(.text + .data + .bootloader)


You are compiling the bootloader, right? So I don't see how the data plus bootloader makes any sense.

Can you post the first few, and last dozen, lines from that .hex file please?
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 12, 2013, 10:21 pm
Yes, i have compile the original bootloader again, which comes with the IDE 1.0.5.
No changes in the sourcefiles or the makefile.

After compiling the bootloader is to long?

Here are the start- and endlines:

Code: [Select]
:020000023000CC
:10E000000D9489F10D94B3F10D94B3F10D94B3F126
:10E010000D94B3F10D94B3F10D94B3F10D94B3F1EC
:10E020000D94B3F10D94B3F10D94B3F10D94B3F1DC
:10E030000D94B3F10D94B3F10D94B3F10D94B3F1CC
:10E040000D94B3F10D94B3F10D94B3F10D94B3F1BC
:10E050000D94B3F10D94B3F10D94B3F10D94B3F1AC
:10E060000D94B3F10D94B3F10D94B3F10D94B3F19C
:10E070000D94B3F10D94B3F10D94B3F10D94B3F18C
:10E080000D94B3F10D94B3F10D94B3F10D94B3F17C
:10E090000D94B3F10D94B3F10D94B3F10D94B3F16C
:10E0A0000D94B3F10D94B3F10D94B3F10D94B3F15C
:10E0B0000D94B3F10D94B3F10D94B3F10D94B3F14C
:10E0C0000D94B3F10D94B3F10D94B3F10D94B3F13C
:10E0D0000D94B3F10D94B3F10D94B3F10D94B3F12C
:10E0E0000D94B3F141546D656761323536300041AE
:10E0F000726475696E6F206578706C6F72657220DE
:10E1000073746B3530305632206279204D4C530099
:10E11000426F6F746C6F616465723E004875683F52
:10E1200000436F6D70696C6564206F6E203D200048
:10E130004350552054797065202020203D20005FF9
:10E140005F4156525F415243485F5F3D2000415658
:10E1500052204C696243205665723D20004743437C
:10E160002056657273696F6E203D20004350552024
:10E1700049442020202020203D20004C6F7720663D
:10E18000757365202020203D20004869676820665F
:10E190007573652020203D200045787420667573D6
:10E1A00065202020203D20004C6F636B2066757336
:10E1B000652020203D20004465632031322032302C
:10E1C000313300312E362E3400342E332E320056A9
:10E1D00023202020414444522020206F7020636F70
:10E1E00064652020202020696E73747275637469E1
:10E1F0006F6E2061646472202020496E74657272B3
:10E20000757074006E6F20766563746F7200726A49

a lot of lines....

:10FFA000E89507B600FCFDCFCE5CDE4F88819981D5
:10FFB000AA81BB81C253D14080509F4FAF4FBF4FEA
:10FFC000CE5CDE4F88839983AA83BB83C253D14022
:10FFD000C05EDE4F488159816A817B81C052D14029
:10FFE000DE011B9631E08C9111962C91119712969F
:10FFF000C75CDE4F2883C953D140C85CDE4F1882EE
:020000024000BC
:10000000C853D14090E0C85CDE4FE881F981C85305
:10001000D1408E2B9F2B0C01FA0160935B00309333
:100020005700E89511244E5F5F4F6F4F7F4F02508E
:100030001040C9F685E0C05EDE4FE880F9800A8195
:100040001B81C052D140F70100935B0080935700A1
:10005000E89507B600FCFDCF81E180935700E89555
:1000600036C0C05EDE4F88819981AA81BB81C052B3
:10007000D140B695A795979587959801FE013B9637
:1000800010C04081F999FECF31961FBA92BD81BD53
:1000900040BD0FB6F894FA9AF99A0FBE0196215016
:1000A00030402115310569F7A80160E070E0440F88
:1000B000551F661F771FC05EDE4F088119812A8198
:1000C0003B81C052D140400F511F621F731F1A82E3
:1000D000C05EDE4F488359836A837B83C052D14020
:1000E0007FC01A81C55CDE4F1883CB53D140C65CFC
:1000F000DE4F1882CA53D1408B81C82EDD24C65CE6
:10010000DE4F28813981CA53D140C22AD32A1A82AC
:100110008981BE016D5F7F4F843121F59601C05EFC
:10012000DE4FE880F9800A811B81C052D1400BBFAD
:10013000F70187919691DB018C9311969C936E5FEA
:100140007F4FD801C7010296A11DB11DC05EDE4FD1
:1001500088839983AA83BB83C052D1402250304008
:10016000F1F636C0C05EDE4F288139814A815B815D
:10017000C052D1400894C108D108760100E010E0D7
:100180000894C11CD11C0894E11CF11C011D111D17
:10019000E20EF31E041F151F21BDBB27A52F942FB0
:1001A000832F82BD2F5F3F4F4F4F5F4FF89A80B52F
:1001B000DB018D93BD012E153F054007510761F707
:1001C000C05EDE4F288339834A835B83C052D140AF
:1001D00096012D5F3F4FFB01108204C080EC8A83A3
:1001E00022E030E08BE18093C6008091C00086FF62
:1001F000FCCF8091C00080648093C000C15DDE4F61
:10020000F881CF52D140F093C6008091C00086FFA4
:10021000FCCF8091C00080648093C000432F309356
:10022000C6008091C00086FFFCCF8091C000806432
:100230008093C000922F2093C6008091C00086FF5B
:10024000FCCF8091C00080648093C0008EE08093DA
:10025000C6008091C00086FFFCCF8091C000806402
:100260008093C00065E1C15DDE4FE880CF52D14090
:100270006E2569276427FE01319610C09081909306
:10028000C6008091C00086FFFCCF31968091C000EF
:1002900080648093C00069272150304021153105CA
:1002A00069F76093C6008091C00086FFFCCF809103
:1002B000C00080648093C00085B1805885B99920C2
:1002C00099F4C15DDE4F0881CF52D1400F5FC15D0F
:1002D000DE4F0883CF52D14090E0A0E0B0E08E0125
:1002E0000F5F1F4F0D9496F427982F9880E090E0B1
:1002F00020ED37E0F9013197F1F701968436910549
:10030000C9F700008091C0008D7F8093C00081E11B
:1003100080935700E895EE27FF270994FFCF90E0E0
:100320000D9496F497FB092E07260AD077FD04D08A
:100330002ED006D000201AF4709561957F4F089555
:10034000F6F7909581959F4F0895A1E21A2EAA1B6A
:10035000BB1BFD010DC0AA1FBB1FEE1FFF1FA21775
:10036000B307E407F50720F0A21BB30BE40BF50B72
:10037000661F771F881F991F1A9469F760957095FB
:10038000809590959B01AC01BD01CF010895AA1BFA
:10039000BB1B51E107C0AA1FBB1FA617B70710F070
:1003A000A61BB70B881F991F5A95A9F780959095A2
:0A03B000BC01CD010895F894FFCFC1
:040000033000E000E9
:00000001FF

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 12, 2013, 10:32 pm
Yes it seems too long. However you shouldn't really need to recompile the bootloader. If you post a sketch that reproduces the problem we can look into it.
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 12, 2013, 11:09 pm

Yes it seems too long. However you shouldn't really need to recompile the bootloader. If you post a sketch that reproduces the problem we can look into it.


I would recompile the bootloader with the parameter -DWATCHDOG_MODS. I hope, this changes solved the problem.

I'm a little bit confused with the bootloader, why i can't recompile it?  :(

I have write a test-sketch, but i can't reproduces the problem, only some times it hangs...
But tomorrow is a new day...

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 13, 2013, 01:06 pm
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
Title: Re: watchdog with the mega does not work?
Post by: westfw on Dec 13, 2013, 06:17 pm
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.
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 13, 2013, 08:46 pm
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
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 14, 2013, 07:50 pm

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
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 14, 2013, 07:58 pm
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
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 14, 2013, 08:39 pm
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(".");
}

Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 14, 2013, 09:43 pm
@Nick,
can you reproduce my problem?

paulinchen

Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 14, 2013, 10:03 pm
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.
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 14, 2013, 10:05 pm
How did you recompile the bootloader? I'll try adding a few extra lines.
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 14, 2013, 10:36 pm
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 (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
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 15, 2013, 12:15 am
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?
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 15, 2013, 12:32 am
Changes pushed to GitHub (for my bootloader programmer):

https://github.com/nickgammon/arduino_sketches/tree/master/Atmega_Board_Programmer
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 15, 2013, 03:48 pm
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
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 15, 2013, 08:29 pm
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.
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 15, 2013, 08:52 pm
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. :)
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 15, 2013, 08:59 pm
Thanks for your answer, nick.

I think, the problem is now resolved.

Many, many thanks for your great work!

Please update the sketch on your great website also, because i use this place normally.

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 15, 2013, 09:05 pm

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. :)


I'm not sure I understand you correctly. My English is not so good. Sorry.

But i use only MEGA2560 boards, so i must use as programmer-board also a MEGA2560. You mean, than i use a UNO-board, the problem with the 0xff issue are not present?

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 15, 2013, 09:58 pm

Thanks for your answer, nick.

I think, the problem is now resolved.

Many, many thanks for your great work!

Please update the sketch on your great website also, because i use this place normally.

paulinchen


I updated my website.
Title: Re: watchdog with the mega does not work?
Post by: nickgammon on Dec 15, 2013, 10:00 pm

But i use only MEGA2560 boards, so i must use as programmer-board also a MEGA2560. You mean, than i use a UNO-board, the problem with the 0xff issue are not present?


You can use a Uno to program a Mega2560 and you can use a Mega2560 to program a Uno. The issue with the 0xFF is a problem in some bootloaders because they assume that memory has been cleared to 0xFF when it hasn't. You occasionally find that people have problems with sketches with a lot of 0xFF in them, for example:

Code: [Select]

int foo = -1;   // this is 0xFFFF


This occurs if this variable is the last one in the sketch (and thus causes trailing 0xFF in the hex file).
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 15, 2013, 10:25 pm
Hi Nick,
thanks for your declaration of the 0xff-issue, I learn a lot from you.

I give you a little bit more karma  :)

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: Scott216 on Dec 23, 2013, 06:24 pm

I updated my website.

Can you provide a link to this on your website? I'd like to check it out.
Title: Re: watchdog with the mega does not work?
Post by: paulinchen on Dec 23, 2013, 06:55 pm

http://www.gammon.com.au/forum/?id=11635 (http://www.gammon.com.au/forum/?id=11635)

paulinchen
Title: Re: watchdog with the mega does not work?
Post by: chandan0000 on Dec 02, 2017, 09:57 am
How to implement watchdog on arduino mega 2560
Anyone please reply with the code
Because arduino is going to infinite reset loop when watchdog overflow is happening.
In some article I read like we need to upgrade bootloader firmware. How do I know which firmware version I'm using. How to do firmware upgrade.
Title: Re: watchdog with the mega does not work?
Post by: chandan0000 on Dec 02, 2017, 10:06 am
How to  implement watchdog timer to arduino 2560 because the code which I'm using is going to infinite reset loop when watchdog overflow happens.
I'm flashing the code using arduino  latest IDE below is the code it is working in the positive condition if i comment out reset function in loop then it is going to infinite reset loop.
But the same code worked exactly in arduino UNO
For my project i have to use mega because i need 2 UARTs.
 

#include <Adafruit_SleepyDog.h>

void setup() {
  Serial.begin(115200);
  while (!Serial) ; // wait for Arduino Serial Monitor (native USB boards)
  Serial.println("Adafruit Watchdog Library Demo!");
  Serial.println();

  // First a normal example of using the watchdog timer.
  // Enable the watchdog by calling Watchdog.enable() as below.  This will turn
  // on the watchdog timer with a ~4 second timeout before reseting the Arduino.
  // The estimated actual milliseconds before reset (in milliseconds) is returned.
  // Make sure to reset the watchdog before the countdown expires or the Arduino
  // will reset!
  int countdownMS = Watchdog.enable(4000);
  Serial.print("Enabled the watchdog with max countdown of ");
  Serial.print(countdownMS, DEC);
  Serial.println(" milliseconds!");
  Serial.println();

  // Now loop a few times and periodically reset the watchdog.
  Serial.println("Looping ten times while resetting the watchdog...");
  for (int i = 1; i <= 10; ++i) {
    Serial.print("Loop #"); Serial.println(i, DEC);
    delay(1000);
    // Reset the watchdog with every loop to make sure the sketch keeps running.
    // If you comment out this call watch what happens after about 4 iterations!
    Watchdog.reset();
  }
  Serial.println();

  // Disable the watchdog entirely by calling Watchdog.disable();
  Watchdog.disable();

  // Finally demonstrate the watchdog resetting by enabling it for a shorter
  // period of time and waiting a long time without a reset.  Notice you can pass
  // a _maximum_ countdown time (in milliseconds) to the enable call.  The library
  // will try to use that value as the countdown, but it might pick a smaller
  // value if the hardware doesn't support it.  The actual countdown value will
  // be returned so you can see what it is.
  countdownMS = Watchdog.enable(4000);
  Serial.print("Get ready, the watchdog will reset in ");
  Serial.print(countdownMS, DEC);
  Serial.println(" milliseconds!");
  Serial.println();
  delay(countdownMS+1000);

  // Execution will never get here because the watchdog resets the Arduino!
}

void loop() {
  // We'll never actually get to the loop because the watchdog will reset in
  // the setup function.
  Serial.println("You shouldn't see this message.");
  delay(1000);



In some articles i read like if bootloader firmware is upgraded this problem may occur.
May i know how to know the version of bootloader firmware i'm using. And what is the procedure to upgrade the firmware.
Title: Re: watchdog with the mega does not work?
Post by: westfw on Dec 03, 2017, 02:43 am
Do you have a Mega with the new (fixed) bootloader that was discussed in this thread?
If you have a non-genuine MEGA (or one that's just old), that would be ... uncertain.