3 Replies Latest reply on Mar 26, 2018 3:31 PM by e.pratt_1639216

    Simple watchdog timer implementation

    user_418894472

      Hi!

      I want to implement a very simple watchdog timer that will reset the controller if not cleared within a specified counter ticks.

      My main() function begins as follows:

       

          CyGlobalIntEnable; /* Enable global interrupts. */

          CySysWdtDisable(CY_SYS_WDT_COUNTER0_MASK);

          CySysWdtWriteMode(0,CY_SYS_WDT_MODE_RESET);

          CySysWdtWriteMatch(0,60000);

          CySysWdtEnable(CY_SYS_WDT_COUNTER0_MASK);

       

      However, as soon as the WDT is enabled the CPU stops running.

      I am using CySysWdtResetCounters(CY_SYS_WDT_COUNTER0_RESET) function in a continuous main loop to clear the counter.

      What am I missing? Please help.

       

      Thank you

        • 1. Re: Simple watchdog timer implementation
          e.pratt_1639216

          You missed setting up the interrupt handler:

          WDT_ISR_StartEx(WDT_Int_Handler);//setup WDT interrupt ISR

          Otherwise, the unhandled interrupt will cause the CPU to lock into a NOP loop (default int handler or some other name)

          1 of 1 people found this helpful
          • 2. Re: Simple watchdog timer implementation
            user_418894472

            Thanks for the quick response.

            I am not using any interrupt handler. And I am not sure why this is required.

            My understanding is:

            -  enabling the WDT in reset on match mode will reset the cpu once counter reaches the match value

            - the main function continuously resets the counter to 0

            - if main function hangs, the WDT will reset the cpu

            I do not see where the interrupt handler is required.

            Please could you explain about the purpose of ISR in watch dog timer and also how to implement it.

             

            Thank you!

            • 3. Re: Simple watchdog timer implementation
              e.pratt_1639216

              Hmmmm; If you are using it as a reset on reaching the match value, and are continuously resetting it, then I would agree with you that it should work.

              The WDT can also be used as a generic timer, that runs a function/code upon reaching the matching value.

              Thus, you need to start the WDT using the WDT_ISR_StartEx(Int_Handler) with the Int_Handler being the address of the function to run upon the timer interrupting. (You would then handle clearing the interrupt flag, and/or doing other code)

              I misunderstood that you didn't want to use it as a timer for firing cleanup code or shutdown/restart code

              Going back to your use/method: The CySysWdtResetCounters() function requires you to unlock and lock the WDT before and after changing values/resetting the cound. This mitigates program code running WDT reset code by accident from random address jumping without first running the unlock sequence beforehand. The document describing WDT usage and notes: http://www.cypress.com/file/233616/download

              Looking at your code, it should be working as is I think.

              You could try running the unit with a debugger, and set up an interrupt to see if the WDT is firing an interrupt, or if the program is crashing before it reaches the WDT reset function(s).

              I think the issue might be more towards something else than your code;

              If you attach an archive of your project, we can all look at your settings and give more information hopefully