Detailed usage of STM32's IWDG (independent watchdog)

Publisher:皮球Latest update time:2018-09-05 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Introduction to IWDG:

  STM32 has two watchdogs, one is an independent watchdog and the other is a window watchdog. The independent watchdog is known as a pet dog, and the window watchdog is known as a police dog. In this chapter, we mainly analyze the functional block diagram and application of the independent watchdog. In layman's terms, the independent watchdog is a 12-bit down counter. When the value of the counter decreases from a certain value to 0, the system will generate a reset signal, namely IWDG_RESET. If the value of the counter is refreshed before the count is reduced to 0, no reset signal will be generated. This action is what we often call feeding the dog. The watchdog function is powered by the VDD voltage domain and can still work in stop mode and standby mode.  

2. Analysis of IWDG functional block diagram

 ①Independent watchdog clock  


  The clock of the independent watchdog is provided by an independent RC oscillator LSI. Even if the main clock fails, it is still effective and very independent. The frequency of LSI is generally between 30~60KHZ. There will be a certain drift depending on the temperature and working environment. We generally use 40KHZ, so the timing of the independent watchdog is not necessarily very accurate and is only suitable for occasions with relatively low time accuracy requirements.


②Counter clock  


  The clock of the down counter is obtained by LSI through an 8-bit prescaler. We can operate the prescaler register IWDG_PR to set the division factor. The division factor can be: [4,8,16,32,64,128,256,256]. The counter clock CK_CNT = 40/ 4*2^PRV. The counter is reduced by one for each counter clock.


③Counter


  The counter of the independent watchdog is a 12-bit down counter with a maximum value of 0XFFF. When the counter is reduced to 0, a reset signal: IWDG_RESET is generated to restart the program. If the counter value is refreshed before the counter is reduced to 0, no reset signal will be generated. The action of refreshing the counter value is commonly known as feeding the dog.


④Reload register


  The reload register is a 12-bit register that contains the value to be refreshed to the counter. The size of this value determines the overflow time of the independent watchdog. Timeout time Tout = (4*2^prv) / 40 * rlv (s), prv is the value of the prescaler register, and rlv is the value of the reload register.

⑤Key value register


  The key value register IWDG_KR can be said to be a control register of the independent watchdog. There are three main control methods. Writing the following three different values ​​to this register has different effects.

Starting the watchdog by writing 0XCCC to the key register is a software startup method. Once the independent watchdog is started, it cannot be turned off and can only be turned off by resetting.


⑥Status register


  Only bit 0: PVU and bit 1: RVU are valid in the status register SR. These two bits can only be operated by hardware, not software. RVU: Watchdog counter reload value update. Hardware set to 1 indicates that the reload value update is in progress. After the update is completed, it is cleared to 0 by hardware. PVU: Watchdog prescaler value update. Hardware set to '1' indicates that the prescaler value update is in progress. After the update is completed, it is cleared to 0 by hardware. Therefore, the reload register/prescaler register can only be updated when RVU/PVU is equal to 0.


3. How to use IWDG


  Independent watchdogs are generally used to detect and resolve faults caused by programs. For example, the normal running time of a program is 50ms. After running this segment of the program, the watchdog is fed immediately. We set the independent watchdog's timing overflow time to 60ms, which is a little longer than the 50ms of the program we need to monitor. If the watchdog is not fed within 60ms, it means that the program we are monitoring has failed and run away, then a system reset will be generated to allow the program to run again.

4. IWDG overtime experiment


hardware design:



1-IWDG, which is an internal resource and does not require external hardware

2-KEY One

3-LED two, use the RGB lights that come with the development board


experimental design


The timeout period of IWDG is configured to be 1S. If the dog is not fed within 1S, the system will be reset and indicated by the status change of the LED light.


Programming Tips


1- How to configure the timeout of IWDG?

2-How to write a dog feeding function?

3-Where is the appropriate place to feed the dog in the main function?


 Configure the IWDG timeout period.



/*

 * Set the timeout period of IWDG

 * All = prv/40 * rlv(s)

 * prv can be [4,8,16,32,64,128,256]

 * prv: prescaler value, the value is as follows:

 *     @arg IWDG_Prescaler_4: IWDG prescaler set to 4

 *     @arg IWDG_Prescaler_8: IWDG prescaler set to 8

 *     @arg IWDG_Prescaler_16: IWDG prescaler set to 16

 *     @arg IWDG_Prescaler_32: IWDG prescaler set to 32

 *     @arg IWDG_Prescaler_64: IWDG prescaler set to 64

 *     @arg IWDG_Prescaler_128: IWDG prescaler set to 128

 *     @arg IWDG_Prescaler_256: IWDG prescaler set to 256

 *

 * Independent watchdog uses LSI as clock.

 * The frequency of LSI is generally between 30~60KHZ, and there will be a certain drift depending on the temperature and working environment.

 * Generally, we use 40KHZ, so the timing of the independent watchdog is not necessarily very accurate, and is only suitable for time accuracy.

 * For occasions with relatively low requirements.

 *

 * rlv: reload register value, value range: 0-0XFFF

 * Function call example:

 * IWDG_Config(IWDG_Prescaler_64, 625); // IWDG 1s timeout overflow 

 *                        (64/40)*625 = 1s

 */


void IWDG_Config(uint8_t prv ,uint16_t rlv)

{    

    // Enable the prescaler register PR and reload register RLR to be writable

    IWDG_WriteAccessCmd( IWDG_WriteAccess_Enable );

    

    // Set the prescaler value

    IWDG_SetPrescaler( prv );

    

    // Set the reload register value

    IWDG_SetReload( rlv );

    

    // Put the value of the reload register into the counter

    IWDG_ReloadCounter();

    

    // Enable IWDG

    IWDG_Enable();    

}

Feed the dog function:


// Feed the dog

void IWDG_Feed(void)

{

    // Put the value of the reload register into the counter, feed the dog, and prevent IWDG from resetting

    // When the counter value reaches 0, the system will be reset

    IWDG_ReloadCounter();

}


  

#include "stm32f4xx.h"

#include "./led/bsp_led.h"

#include "./key/bsp_key.h" 

#include "./iwdg/bsp_iwdg.h"



static void Delay(__IO u32 nCount); 



int main(void)

{

    /* LED port initialization */

    LED_GPIO_Config();     

    

    Delay(0X8FFFFF);    


    /* Check if it is an independent watchdog reset */

  if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)

  {

    /* Independent watchdog reset */

    /* Turn on red light*/

    LED_RED;


    /* Clear flag */

    RCC_ClearFlag();

        

        /* If you don't feed the dog, it will keep resetting, plus the previous delay, you will see the red light flashing

        If you feed the dog within 1 second, the green light will remain on*/

  }

  else

  {

    /*Not an independent watchdog reset (maybe a power-on reset or manual button reset, etc.) */

    /* Turn on blue light*/

    LED_BLUE;

  }        

  

  /*Initialize buttons*/

  Key_GPIO_Config();    

    

    // IWDG 1s timeout overflow

    IWDG_Config(IWDG_Prescaler_64 ,625); 

 

    //The while part is the code we need to write in the project. This part of the program can be monitored by an independent watchdog

  //If we know the execution time of this part of the code, for example, 500ms, then we can set the independent watchdog

    //The overflow time is 600ms, a little more than 500ms. If the program to be monitored does not run away and executes normally, then

    //After the execution is completed, the program for feeding the dog will be executed. If the program runs away, the program will time out and cannot reach the point where the dog is fed.

    // program, then the system will be reset. But it is not ruled out that the program ran away and ran back, just feeding the dog,

    //A lucky coincidence. So if you want a more accurate monitoring program, you can use a window watchdog. The window watchdog requires

    //Feed the dog within the specified window time.

    while(1)                            

    {       

        if( Key_Scan(KEY1_GPIO_PORT,KEY1_PIN) == KEY_ON  )

        {

            // Feed the dog. If you don't feed the dog, the system will reset. After reset, the red light will turn on. If

            // If you feed the dog on time, the green light will turn on

            IWDG_Feed();        

            // After feeding the dog, the green light turns on

            LED_GREEN;

        }

    }

}


static void Delay(__IO uint32_t nCount) //Simple delay function

{

    for(; nCount != 0; nCount--);

}


/*********************************************END OF FILE**********************/


The program first checks whether it is an independent watchdog reset. If it is an independent watchdog reset, the red light will be on. If the watchdog is not fed, it will keep resetting. With the previous delay, you will see the red light flashing. If the watchdog is fed within 1S, the green light will continue to be on. 


Keywords:STM32 Reference address:Detailed usage of STM32's IWDG (independent watchdog)

Previous article:STM32 AD DMA mode
Next article:STM32 IWDG dog feeding time calculation

Recommended ReadingLatest update time:2024-11-16 04:50

STM32 boot mode ISP download
mcuisp flymcu same type DTR low level reset, RTS high level enter BootLoader One-click download circuit: First, let RTS pull BOOT0 high (set BOOT0 to 1), and then let DTR control RST to be low (STM32 is reset at a low level). Then release RTS to be high (stop reset), and then start downloading the code through t
[Microcontroller]
STM32 boot mode ISP download
Steps to create an STM32 project in IAR (register version)
First of all, it is based on register programming. 1. Download the STM32 firmware library from the official website. I used 3.5 2. Create an STM32 folder (not for storing projects) to store the basic files required to create STM32 projects. When you create a project again in the future, just copy the contents of thi
[Microcontroller]
About STM32 serial port printf output debugging information problem
1. Problems encountered (using HAL library) In the process of using STM32, we usually use printf to redirect the serial port to output debugging information for program development and debugging. We found the redirection code part on the Internet and added it to the serial port code file, as follows: UART_HandleTy
[Microcontroller]
Program transplantation of different STM32 chip models
1. Change the startup file to the corresponding capacity. 2. Select the chip model in Device. 3. Change the FLSAH in the Preprocessor Symbols in C/C++ to the size suitable for your processor 4. Select the FLASH capacity in Programming Algorithm that is suitable for your processor size.
[Microcontroller]
Program transplantation of different STM32 chip models
Summary of STM32 program transplantation skills
1. The project replaces different STM32 chips eg: stm32f103rct6 ---- stm32f103c8t6: 1.1. Modify the chip Click the magic wand, and in the menu bar that appears, select the chip in the Device option. 1.2. Modify the startup file Here is an example of changing RCT6 to C8T6. Because the flash capacity is different
[Microcontroller]
Summary of STM32 program transplantation skills
STM32 uses PA13/PA14/PA15 as a normal IO port
When STM32 uses JTMS (PA13) and JTCK (PA14) as normal I/O ports, add the following code before initialization (the order cannot be reversed):   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); Choose one of the following two sentences: If GPIO_Remap_SWJ_Disable is used, the SWD download function cannot be used. W
[Microcontroller]
Personal understanding of NVIC in STM32
STM32 has 43 channels of settable interrupt sources; AIRC (Application Interrupt and Reset Register) register has 4 bits for specifying priority. /* Preemption Priority Group -----------------------------------------------------------------*/ #define NVIC_PriorityGroup_0 ((u32)0x700) /* 0 bits for pre-emption priori
[Microcontroller]
Multi-channel voltage measurement design based on STM32
1. Introduction In recent years, data acquisition and its application have received more and more attention, and data acquisition systems have also developed rapidly. It can be widely used in various fields. Data acquisition technology is one of the important branches of information science. Data acquisition is also t
[Power Management]
Multi-channel voltage measurement design based on STM32
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号