Arduino DUE deadlock problem, watchdog not working I am using Arduino DUE in my project. The project is massive (about 7000 lines of code in total, with massive schematic), so I decided not to post the whole code and schematic, except explain the hardware and configuration I am working with:


Arduino DUE;  
12V switching power supply;  
LTC1660 DAC (used withstandard SPI library);  
Nextion HMI (Used with standard library #include Nextion.h        //INCLUDE NEXTION LIBRARY);  
DS3231 RTC module on I2C bus (Library used: Wire  #include &lt;DS3231.h&gt;)  
24LC256 EEPROM on same I2C bus (Library used: Ext EEPROM #include &lt;extEEPROM.h&gt;)


The base line, is that on power-on the controller just freezes besides, it can't be the problem with power as I get this problem using both 12&nbsp;V from switching PSU and 5&nbsp;V USB cable. I can not really analyse the problem, as the deadlock occurs even before the program start to execute content of void setup(). My best guess is that the deadlock occurs due to infinite loops in the Arduino's twi.cpp where infinite loop can occur if the controller receives no acknowledge 
bit. But then again I can not be sure here. However when I push restart button on the Arduino or enable Serial monitor in Arduino IDE when the circuit is powered on , it starts to work, and it works just as expected.

After reading the datasheet for the DUE's &mu;C, I've found that it have the watchdog option embedded:


  After a Processor Reset, the value of WDV is 0xFFF, corresponding to
  the maximum value of the counter with the external reset generation
  enabled (field WDRSTEN at 1 after a Backup Reset). This means that a
  default Watchdog is running at reset, i.e., at power-up. The user must
  either disable it (by setting the WDDIS bit in WDT_MR) if he does
  not expect to use it or must reprogram it to meet the maximum Watchdog
  period the application requires. The Watchdog Mode Register (WDT_MR)
  can be written only once. Only a processor reset resets it. Writing
  the WDT_MR register reloads the timer with the newly programmed mode
  parameters.


I've started to explore it, and as I got it, the watchdog is ON by default, and its timer period is 16 seconds by default, so as it is not normally used in DUE, I assumed, that somewhere it is just disabled, and in main.cpp I found the following:

int main( void )
{
   // Initialize watchdog
   watchdogSetup();
   init();
   initVariant();


where watchdogSetup() led to:

void watchdogDisable(void)
{
   WDT_Disable (WDT);
}
void watchdogReset(void)
{
   WDT_Restart (WDT);
}
extern "C"
void _watchdogDefaultSetup (void)
{
   WDT_Disable (WDT);
}
void watchdogSetup (void) __attribute__ ((weak, alias("_watchdogDefaultSetup")));


As I got it, due to this, the watchdog was disabled for DUE by default, and commenting out watchdogSetup(); in int main() would allow the watchdog to work (as watchdog is on at power on by default, according to datasheet). 

I've also added the WDT_Restart (WDT); to void loop() of the program in order to kick watchdog every program cycle.

Well, for some reason, that did not help me, and I'm still receiving the deadlock when I power on the controller, even through the watchdog is technically not disabled, after 16 seconds it still do not restart the system. And, pressing the reset button or the Serial monitor in Arduino IDE makes the system run as expected. Am I missing something out? Your help and suggestions are appreciated, shall any additional info be needed, just let me know, I need to get this project running so badly.

By the way, my void setup() function is quite long, and if I set the watchdog to 1 second, using 
void watchdogEnable (uint32_t timeout) instead of commenting out watchdogSetup() in main.cpp, and use reset button after power on, I can see the watchdog working as it can't get to void loop() in time in order to reset watchdog (as void setup() takes about 3 seconds due to delays).
Found people suffering with the same problem on Arduino's official forum. It turned out that the problem arises from the schematic of Arduino Due, that fails to hold 'reset' pin high for long enough period of time on power-on.

There are multiple solutions involving adding some capacitance between reset pin and ground pin, but this might damage circuit in long run (at power-off), so I managed to find other way around it: there is an ATmega16U2 used as USE-to-TLL serial chip that have access to reset pin of AT91SAM3X8E.

The ATmega16u2 can be reprogrammed so that on power-on it provide short "HIGH" period to AT91SAM3X8E reset pin. The program for ATmega16U2 can be found here: 
https://forum.arduino.cc/index.php?action=dlattach;topic=167492.0;attach=52901

The updating precision is given here:
https://www.arduino.cc/en/Hacking/Upgrading16U2Due

I've performed the given precision using Arduino UNO as programmer and can confirm that it worked properly.The problem's gone now.