Hello, I am aware that watchdog RESET works perfect when arduino freeze or stuck in some infinite loop. But I wonder how well does watchdog INTERRUPT work in same condition or in case it stopped working because of SRAM overflow? I dont want to reset everything so I am just calling "WDTCSR |= _BV(WDIE)"
WDIE just enables interrupt. WDE bit must be 0 - no reset and of course fuse WDTON must be set. See the datasheet for "Watchdog Timer Configuration" table.
BUT, in case of SRAM overflow what will do? Some memory space is needed even to run the ISR. First thing which happens at interrupt is that the program counter (next command to be executed) will be saved to the stack. It overwrites heap data or cannot be saved over the SRAM beginning. Hmm, very interesting problem BTW, probably worth for further investigation. However, I am not sure if it helps to your situation. If the MCU stops responding, the reset via WTD is the solution otherwise if no reset it will probably do nothing. You need some kind of check of available memory amount at each allocation or periodical (then watchdog - yes).
EDIT: I can imagine some action (in case if the stack has the room in memory even to owerwrite heap, of course) to provide fake program counter for return from ISR to the function which will clear the memory and will treat corrupted data etc. Similar technique like in RTOS but very complicated. Isn't the reset better?
Thank you Budvar10. Very useful information.
Problem with reset- The bootloader I am using does not clear Watchdog flag at startup, so if reset is done by watchdog, it goes to infinite reset loop. You have to cut the power to take arduino back to life.
And just to try I inserted bunch of garbage data to fill up the SRAM. During execution when it reached the point when there is no memory available, it crashed. And when watchdog timer overflows, my Watchdog ISR does following thing to jump to address 0 to restart the program. I thought it would not work and not recommended but it works perfect. Instead anything else can be done to improve it other-than updating bootloader?
ISR(WDT_vect)
{
void(* resetFunc) (void) = 0;
resetFunc();
}
Why not fix the bootloader issue so you can use WDT reset?
I know that optiboot got capabilities to clear the watchdog flag on startup. But I am running things on 8 Mhz crystal. I am able to burn optiboot but then sketch does not upload. I changed board.txt configuration too, but doesn't help. So, I am trying to go with WD interrupt instead of ED system reset.
Ah - so you never got that sorted out? I don't get it - I have a ton of boards working fine with optiboot on 8mhz, as do many others here; you're doing something wrong.
WDT ISR can't be guaranteed to run if you've gotten the chip into a sufficiently bad state, as I understand it - that's what WDT reset is meant for.
The correct course of action, IMO, is to figure out what you're doing wrong with the bootloader issue and do it right instead...
Yeah I should try again. I will be thankful if you can let me know which optiboot version do you use? I modified boards.txt file. Do you need to modify optiboot hex file to support 8Mhz?
Optiboot hex file need to be created for correct F_CPU value. 8MHz in your case. Did you used 8MHz version? It could explain the problem with uploading if not.
The normal solution for 8mhz Optiboot on a '328p is to use the 16mhz hex as used in the Uno - and upload at half the normal baud rate.
Similar thread Array - IR - SdCArd - #2 by gpb01 - Generale - Arduino Forum
They may have a solution over there - it looks like they do, anyway