Library 20- Independent watchdog timer on STM32F4

Sometimes you need watchdog timer to look at your system if it gets stuck. I made a little library to work with it.

Watchdog in STM32F4xx device has it’s own clock which is independent from main system clock. You have to constantly reset it’s counter value or it will elapse and reset MCU.

Watchdog Library

Features

  • Detect if system was reset by watchdog
  • Supports different timeouts
  • Reset watchdog

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx IWDG
  • TM
    • defines.h

Timeout structure

I made a structure with different timings for watchdog reset.

Initialize watchdog timer

Before you can use it, you have to initialize it. Function below initializes watchdog timer.

As parameter you have to pass one of members of watchdog structure.

This function returns 1 if system was reset by watchdog, otherwise 0.

Reset watchdog timer

You want system reset by watchdog only if system stuck somewhere. So you don’t want that watchdog reset your system all the time. For this, you have to reset timer’s counter periodically faster than he can count to it’s top value.

Reset timer with

Example

Project available on Github, download library below.

Icon
TM STM32F4 Watchdog Library

Internal Watchdog on STM32F4xx

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Gary

    Hi TM,

    I have a question about this function – I guess I am not clear on how to use it.
    If I have an RTOS task that reloads the WDT counter every “x” milliseconds, what’s the right way to implement your function? I have some system calls that can take a bit longer, such as erasing a flash sector, that are causing the WDT to reset. I’d like to have to “kick the dog” about every 2 or 3 seconds.

    Thanks!
    Gary

    • Hi,

      you will have to call it a multiple time.
      Flash erasing can be a mess with IWDG. You can avoid that by not waiting FLASH to complete erasing, but to use interrupts. Once erasing is done, you handle this in FLASH_IRQ and you can start another erase there.

      You will have to call this function probably in every thread you are using.

      • Gary

        Hi TM,

        Actually, it worked very well as is, when needed. The flash initialization for erasing a sector takes about
        750ms (slow device) and that’s the only problem code. Your WDT function is good as it allows the right range/value to be selected, avoiding a reset. I “feed the dog” at least twice as fast, now works great.

        Great work! 😎

  • Pingback: Is sprawl an issue? – disparity of urban sprawl()

  • vanaja

    Hi,

    In my application I am using sleep mode. IWDG is waking up controller. is it possible to disable IWDG before entering into sleep.

    • Once it is enabled, you can’t disable it.
      Use RTC wakeup interrupt to reset watchdog. But! Do not reload IWDG in interrupt. Do it in main program.

      • vanaja

        For me RTC interrupt occurs for 3hrs. But, the max timeout value for IWDG is 32sec.

        • Wakeup IRQ can not be set for 3 hours I would say. Maybe you have alarm IRQ enabled!

          • vanaja

            yeah I do. what would be the issue if I enable alarm for 3hrs. Do you mean the issue will be with IWDG or any thing else.

          • No i dont see any issue at all. Rtc can trigger 3 completelly different irq types. You use alarm irq, use wakeup also for reloading iwdg.

          • vanaja

            It makes sense. Thanks for your suggestion and time.