Go Down

Topic: Watchdog for MKR GSM 1400 (Read 512 times) previous topic - next topic

alanoatwork

I tried the following code to implement a watchdog timer:

Code: [Select]
void watchdog_setup(void){
  // Set up the generic clock (GCLK2) used to clock the watchdog timer at 1.024kHz
  REG_GCLK_GENDIV = GCLK_GENDIV_DIV(4) |            // Divide the 32.768kHz clock source by divisor 32, where 2^(4 + 1): 32.768kHz/32=1.024kHz
                    GCLK_GENDIV_ID(2);              // Select Generic Clock (GCLK) 2
  while (GCLK->STATUS.bit.SYNCBUSY);                // Wait for synchronization

  REG_GCLK_GENCTRL = GCLK_GENCTRL_DIVSEL |          // Set to divide by 2^(GCLK_GENDIV_DIV(4) + 1)
                     GCLK_GENCTRL_IDC |             // Set the duty cycle to 50/50 HIGH/LOW
                     GCLK_GENCTRL_GENEN |           // Enable GCLK2
                     GCLK_GENCTRL_SRC_OSCULP32K |   // Set the clock source to the ultra low power oscillator (OSCULP32K)
                     GCLK_GENCTRL_ID(2);            // Select GCLK2         
  while (GCLK->STATUS.bit.SYNCBUSY);                // Wait for synchronization

  // Feed GCLK2 to WDT (Watchdog Timer)
  REG_GCLK_CLKCTRL = GCLK_CLKCTRL_CLKEN |           // Enable GCLK2 to the WDT
                     GCLK_CLKCTRL_GEN_GCLK2 |       // Select GCLK2
                     GCLK_CLKCTRL_ID_WDT;           // Feed the GCLK2 to the WDT
  while (GCLK->STATUS.bit.SYNCBUSY);                // Wait for synchronization

  REG_WDT_CONFIG = WDT_CONFIG_PER_1K;               // Set the WDT reset timeout to 16 seconds
  while(WDT->STATUS.bit.SYNCBUSY);                  // Wait for synchronization
  REG_WDT_CTRL = WDT_CTRL_ENABLE;                   // Enable the WDT in normal mode
  while(WDT->STATUS.bit.SYNCBUSY);                  // Wait for synchronization
}

void watchdog_clear(void){
  if (!WDT->STATUS.bit.SYNCBUSY)                // Check if the WDT registers are synchronized
  {
    REG_WDT_CLEAR = WDT_CLEAR_CLEAR_KEY;        // Clear the watchdog timer
  }
}


I added watchdog_setup() to my setup function, but I never receive any resets.   I also tried the WDTZero library on github but that also didn't reset the MKR GSM 1400.

Any reason why watchdog code for the MKRZero wouldn't work on the MKR GSM 1400?  Is there watchdog code out there that does work?

MartinL

Hi alanoatwork,

I just tested the Watchdog timer code on my Arduino Zero using the latest core files and it works just fine. It should (in theory at least) also work on the MKR GSM 1400 as well.

If the Watchdog timer isn't cleared within 1 second the microcontroller is reset.

If you comment out the line:

Code: [Select]
REG_WDT_CLEAR = WDT_CLEAR_CLEAR_KEY;        // Clear the watchdog timer
...the board should continuously reset itself.

alanoatwork

Martin,

Yes, the code works fine. Not sure what I was doing wrong initially. Could be old age!

Go Up