Watchdog enables itself after reset

freescalemicrocontroller

I am using TWR-S08DC-PT60 board which has a MC9S08PT60 microcontroller.

I am trying to disable the watchdog timer in my application, just for ease of development. After configuring it in the software, it is disabled in the first run after programming, as wanted. However, after a reset occurs, either by reset button or power down, it is automatically enabled by itself. This happens when I debug, run or flash using CodeWarrior 10.4.

Here is how I disable it:

  _WDOG_CS1.Bits.UPDATE = 1; // Enable changes to WDOG.
  WDOG_CNT = 0xC520; // write the 1st unlock word
  WDOG_CNT = 0xD928; // write the 2nd unlock word

  _WDOG_CS1.Bits.EN = 0; // Kill the dog.
  _WDOG_CS1.Bits.INT = 1; // Enable WDOG interrupt.

From the reference manual:

23.3.2.1 Reconfiguring the Watchdog

In some cases (such as when supporting a bootloader function), users may want to reconfigure or
disable the watchdog without forcing a reset first. By setting the
WDOG_CS1[UPDATE] bit to a 1 on the initial configuration of the
watchdog after a reset, users can reconfigure the watchdog at any time
by executing an unlock sequence. (Conversely, if the WDOG_CS1[UPDATE]
remains 0, the only way to reconfigure the watchdog is by initiating a
reset.) The unlock sequence is similar to the refresh sequence but
uses different values.

When I debug the application, I can see that UPDATE bit is set and the watchdog timer is disabled, also, it behaves as desired when I reset using the debugger. However, in case of a reset by the reset button or power down, watchdog timer is enabled again and reset the MCU, since I do not feed it in the main loop.

Also, it doesn't create an interrupt before resetting the MCU, although its interrupt is enabled. I can see, by the help of SYS_SRS (system reset source) register that the MCU is last reset by watchdog timer.

Commenting out 2nd and 3rd line (WDOG_CNT=…) does not help.

Here is my full code:

Best Answer

Following is stated in page 615 of the reference manual, under section 23.3.2:

The new configuration takes effect only after all registers except WDOG_CNTH:L are written once after reset. Otherwise, the WDOG uses the reset values by default. If window mode is not used (WDOG_CS2[WIN] is 0), writing to WDOG_WINH:L is not required to make the new configuration take effect.

Adding these lines in initialize_CPU() function under file init.c, I can configure the watchdog timer, and disable it:

WDOG_CS1 = 0x60;
WDOG_CS2 = 1;
WDOG_TOVAL = 0x04;

Also, it is good to note that watchdog timer registers are write-once only. So, individual bit changes are not going to have an effect after first write to the same watchdog timer register.

If user wants to configure any of the watchdog timer registers later on, updates should be enabled in the first configuration. After that following is used to configure watchdog timer the second time. For more information, please have a look at the reference manual.

/* Initialize watchdog with ~1-kHz clock source, ~1s time-out */
DisableInterrupts; // disable global interrupt
WDOG_CNT = 0xC520; // write the 1st unlock word
WDOG_CNT = 0xD928; // write the 2nd unlock word
WDOG_TOVAL = 1000; // setting timeout value
WDOG_CS2 = WDOG_CS2_CLK_MASK; // setting 1-kHz clock source
WDOG_CS1 = WDOG_CS1_EN_MASK; // enable counter running
EnableInterrupts; // enable global interrupt