1
\$\begingroup\$

This is the code I have for a green LED blinking every 2s with watchdog mode for MSP432.

#include "msp.h"

void main(void)
 {
     volatile uint32_t i;
     WDT_A->CTL = WDT_A_CTL_PW & 0xFF00;
     WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_IS_5;
     P2DIR |= BIT1;
     while (1) // continuous loop
     {
         P2OUT ^=BIT1; // Blink P1.0 LED
         for (i =6000000; i> 0; i--); // Delay with 3Mhz clock.
     }
 }

How to design Watchdog timer that reset the system if Watchdog counter is not cleared at least every 1/4 seconds?

\$\endgroup\$
6
  • \$\begingroup\$ Make an outer loop around your delay loop. Shorten the delay time. Trigger the watchdog inside the outer loop. \$\endgroup\$
    – Janka
    Commented Oct 28, 2018 at 18:09
  • \$\begingroup\$ Thanks for your reply. So Something like this, for(j= 30000; j> 0; j--) { WDT_A->CTL = WDT_A_CTL_PW & 0XFF00; for(I=3000000; I>0; I-- ); } I apologize I am very good at programming. \$\endgroup\$
    – dhruv
    Commented Oct 28, 2018 at 18:23
  • \$\begingroup\$ if 6000000 means 2s, and your watchdog needs to be triggered each 1/4s, you have to divide number this by eight, and your outer loop has to count to eight to make it the same delay. \$\endgroup\$
    – Janka
    Commented Oct 28, 2018 at 18:41
  • 1
    \$\begingroup\$ @Janka Ok, so you answered his question, how will he mark it as accepted? \$\endgroup\$
    – pipe
    Commented Oct 28, 2018 at 20:40
  • 1
    \$\begingroup\$ @Mike But he did not. He chose to ignore the information when posting his comment, which states "avoid answering questions in comments." The question will now remain listed as unsolved. \$\endgroup\$
    – pipe
    Commented Oct 30, 2018 at 8:48

3 Answers 3

1
\$\begingroup\$

The purist/best practice solution is that your program should only kick the watchdog once, at the top of the eternal loop in main(). Thus the best practice is this:

void main (void)
{
  ...

  uint32_t count = 0;

  for(;;)
  {
    kick_dog();

    if(count < max)
    {
      blink_led();
      delay(n);
      count++;
    }
  } // for(;;)
}

But of course busy-delay loops is an amateur solution in itself. Professional code would instead utilize on-chip hardware timers. Such as:

void main (void)
{
  ...
  init_timer();
  ...

  uint32_t count = 0;
  timer_busy = false;

  for(;;)
  {
    kick_dog();
    led();
  }
}

static void led (void)
{
  if(!timer_busy)
  {
    blink_led();
    timer_busy = true;          
    start_timer(); // some hw timer that will clear the timer_busy flag from its ISR

    count++;
    if(count == max)
    {
      // do something
    }
  }
}
\$\endgroup\$
1
\$\begingroup\$

I am not really familiar with MSP controller, but the answer should be something like this:

#include "msp.h"

void main(void)
{
    volatile uint32_t i, j;
    WDT_A->CTL = WDT_A_CTL_PW & 0xFF00;
    WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_IS_5;
    P2DIR |= BIT1;
    while (1) // continuous loop
    {
        P2OUT ^=BIT1; // Blink P1.0 LED
        for (j = 8; j > 0; j--)
        {
             WDT_A->CTL = WDT_A_CTL_PW & 0XFF00;
             for (i = 750000; i > 0; i--);      // Delay with 3Mhz clock, 0,26s.
        }
    }
 }
\$\endgroup\$
1
\$\begingroup\$

You have to setup and Reset your Watchdog Timer in one go. If you setup and reset separately then it keeps on resetting.

#include "msp.h"
void main(void)
{
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;     // stop watchdog timer

//RGB Leds
P2->DIR = BIT2 | BIT1 | BIT0;
P2->OUT &= ~(BIT2 | BIT1 | BIT0);

//Startup Sequence
P2->OUT = BLUE_LED;
delay_ms(1000);
P2->OUT = GREEN_LED;
delay_ms(1000);
P2->OUT &= ~(BLUE_LED | GREEN_LED);

while(1){

    //Resetting and setting-up simultaneously 
    WDT_A->CTL = WDT_A_CTL_PW|              // Password
                 WDT_A_CTL_CNTCL|           // Clear the timer counter
                 WDT_A_CTL_SSEL__ACLK |     // clock- source = 32.728kHz
                 WDT_A_CTL_IS_4;            // 1-SEC Interval

    P2->OUT ^= RED;
    delay_ms(500);// you can test different times here to check your watchdog working
}

}

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.