watchdog with the mega does not work?

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: Gammon Forum : MUSHclient : General : More relaxed Lua sandbox / security tips

// 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

That bootloader should not have a watchdog timer problem. Which .hex file are you converting exactly, that gives all those errors?

The latest board programmer sketch should have this in the bootloader_atmega2560_v2.h file:

// 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 [] = {

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:

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

paulinchen

There seem to be some dodgy lines in that .hex file:

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

// 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:

byte PROGMEM stk500boot_v2_mega2560_hex [] = {

to:

byte PROGMEM atmega2560_v2 [] = {

I will test it...

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

paulinchen

I got that file from:

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 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

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

    if (Serial1.available()) {
     ...some other code...
    }

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

paulinchen

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

Are you talking about this:

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?

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:

: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

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? :frowning:

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

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

THS_WatchDog_003.zip (4.43 KB)

See this WRT building the bootloader: Code too big when compiled with gcc4.3.2 · Issue #3 · msproul/Arduino-stk500v2-bootloader · GitHub

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

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

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.

MD5 sum of bootloader = 8A F4 7A 29 43 A0 D8 7C DB ED 09 A3 8F 40 24 1E

westfw:
See this WRT building the bootloader: Code too big when compiled with gcc4.3.2 · Issue #3 · msproul/Arduino-stk500v2-bootloader · GitHub

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

paulinchen

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

watchdog_ok.ino (4.4 KB)

watchdog_error.ino (4.39 KB)