Our Latest News

Use of STM32 Standalone Watchdog and Window Watchdog

Watchdog (watch dog) is a timer, you can set a timer time, the timer will reset the MCU, so the MCU must be reset before the timer time (this action is usually called feeding the dog).

If the software runs into a dead loop, or there are other errors that prevent the watchdog timer from being reset in time, the MCU will be reset when the time is up and return to the initial state, thus jumping out of the error state.

Stm32 comes with internal standalone watchdog and window watchdog. The standalone watchdog can be used as long as the dog feeding time is not greater than the set time, and is the most commonly used watchdog usage; while the window watchdog requires the dog feeding time to be within an upper and lower limit in order to reset the watchdog timer.

Here we will learn the use of these two watchdogs respectively.

This image has an empty alt attribute; its file name is image-415.png

1) stm32’s independent watchdog (IWDG)

We modify on the previous serial interrupt project to make it easier to print the value view.

Use cubemx to open the project of serial interrupt, then save as IWDG project, open the independent watchdog, and set the crossover factor and reload count value:.

poYBAGNPsTOAGEeRAALZ8pJBXdE533.png

Then, switching to the clock configuraTIon page, you can see that the IWDG is clocked from the internal low-speed RC oscillator at 40 kHz.

poYBAGNPsViAVhjBAAKlKfgTAm4189.png

Combining the set dividing factor and the reload count value, we can calculate the timing time of the watchdog as follows

64*1000/40kHz = 1.6s

Generate the keil project file, open it, and we can see that the main function already has the code to initialize the IWDG. The function MX_IWDG_Init() sets our crossover frequency and count value and starts the watchdog.

In use, we need to feed the dog within every 1.6s in the main loop (one call to the HAL_IWDG_Refresh() function). As shown in the following figure, the code is feeding the dog every 1s, and the results of the run on the right side show that the program has been running normally and has not been reset:.

poYBAGNPsXKATbpbAACDEanmFd0770.png

And if we change the parameters of the delay time to feed the dog every 2s, we can see that the program is reset: the

pYYBAGNPsY6AHlH1AAB-ltd2dN8988.png

2) Window Watchdog (WWDG) for stm32

Still modify on the previous serial interrupt project for printing value view.

Use cubemx to open the project of serial interrupt, then save it as WWDG project, open the window watchdog and set the crossover factor, window value, count value, and enable early wakeup interrupt as follows.

poYBAGNPsaWAIZNuAAMRVoH40J0108.png

The meaning of these parameters is explained.

The frequency division factor is relatively well understood, the main clock of WWDG is PCLK1 (the clock of APB1), set to 8 here, then the frequency division is PCLK1/8/4096;

Window watchdog timer is subtractive counting, from the set WWDG free-running downcounter value down to count, if not feed the dog, count to 0x3F, will reset the MCU; therefore WWDG free-running downcounter value set value must be greater than 0x3F; and its maximum value The value of WWDG free-running downcounter value must be greater than 0x3F; and its maximum value cannot exceed 0x7F, which is the maximum value 0x7F in the figure;

The value of WWDG window value must be between 0x3F and WWDG free-running downcounter value, and the value set in the figure is 0x70, which means that when WWDG timer decreases from 0x7F downward, it must decrease to below 0x70 before it can effectively feed the dog (i.e., the window interval for feeding the dog is between 0x3F ~ WWDG window value). window value).

Enable early wake-up interrupts and turn on interrupts. Interrupts are used here mainly for the convenience of indicating whether the window time has been reached.

pYYBAGNPsb-AT940AAK8Bfi5mP0770.png

Then, under the clock configuraTIon page, you can see that the frequency of PCLK1 is 36M:.

pYYBAGNPsd-AEWCCAAKsJuonR80260.png

Based on the above information, the window for feeding the dog can be calculated as follows

PCLK1/8/4096 = 1099Hz;

(0x7F-0x3F)/ 1099 = 58ms;

(0x7F-0x70)/1099 = 14ms;

So, the window time for feeding the dog is about 14ms~58ms.

Generate the keil project, open it, and add the early window watchdog interrupt amount callback function in wwdg.c, just to set a flag bit:.

poYBAGNPsfKADh7HAAApB7RfV28656.png

In the main loop, the flag bit is detected, and if it is present, it means that the window time has been reached and the dog needs to be fed as soon as possible;

The program code and running results are as follows.

pYYBAGNPsgyANYGBAACJXEkYUVY028.png

As you can see, the operation of feeding the dog played a role and the program was not reset.

3) Several points about the use of the watchdog

In the actual engineering application, the use of watchdog should pay attention to several points, otherwise it may not play its role:.

a) the operation of feeding the dog, must be in the main loop, and can not be placed in the timer interrupt! This is a mistake that many beginners tend to make! Because, if the main loop runs away or into a dead loop, the timer interrupt may still be running normally, and regularly into the interrupt to feed the dog, the watchdog can not reset the system, can not play a role in monitoring the normal operation of the system;

b) If it is a multitasking system, such as the use of real-time operating system, there are multiple tasks running, the use of watchdog, you need to set the feeding rules based on the function of the task: in a certain period of time will certainly run a set of tasks, each task needs to be executed before feeding the dog once to ensure that all tasks are monitored; may be blocked, or a long interval (more than the watchdog timing time) to run The tasks that may be blocked, or run at long intervals (more than the watchdog timing time), cannot be associated with the dog feeding operation to avoid being reset accidentally during normal operation.

    GET A FREE QUOTE

    FPGA IC & FULL BOM LIST

    We'd love to

    hear from you

    Highlight multiple sections with this eye-catching call to action style.

      Contact Us

      Exhibition Bay South Squre, Fuhai Bao’an Shenzhen China

      • Sales@ebics.com
      • +86.755.27389663