Independent watchdog

Demonstration in this tutorial: Combining the serial port (UASRT1) and button functions to demonstrate the Independent Watchdog (IWDG) timeout reset function.

1. Software-Hardware

2. Brief principle

1. Hardware schematic diagram

image-20231030214729855

2. Physical connection diagram

image-20231030214808689

3. Control principle

By using the button to reset the count value of the independent watchdog (IWDG), the dog feeding function is realized.

STM32F103ZET6 has two built-in watchdogs (independent watchdog and window watchdog), which are mainly used for system fault detection and recovery.

WatchdogFunction
Independent WatchdogUsed to detect whether the system is running normally
Window WatchdogUsed to detect system failures

The independent watchdog (IWDG) is driven by a dedicated low-speed clock (LSI) and remains effective even if the main clock fails;

When the system does not feed the dog (reset the count value) within a certain period of time, the independent watchdog will trigger a reset operation to restart the system.

image-20231030220806595

Prescaler coefficientPR[2:0]Minimum time (ms): RL[11:0]=0x000Maximum time (ms): RL[11:0]=0xFFF
/400.1409.6
/810.2819.2
/1620.41638.4
/3430.83276.8
/6441.66553.6
/12853.213107.2
/2566 or 76.426214.4
T Timeout(s)=42PRReloadLSI=6462540000=1s

3. Project configuration

Project configuration: Prompt configuration options during STM32CubeIDE project configuration

1. Description

Omitted project configuration part: New project, chip selection, project configuration, SYS of pin configuration, RCC configuration, clock configuration and project configuration content

The project configuration part that is not omitted is the key point that needs to be configured in this tutorial.

2. Pin configuration

You can directly select the corresponding pin number in the pin view, and the corresponding options will appear when you left-click the mouse.

image-20231030222902233

image-20231030223142120

image-20231030223012316

image-20231030223255304

image-20231030223300746

image-20231030223412662

image-20231030145217688

4. Main functions

It mainly introduces the functional code written by the user. For detailed code, you can open the project file provided by us yourself and enter the Bsp folder to view the source code.

1. User function

Function: KeyX_is_Press

Function prototypeuint8_t Key1_is_Press(void)
Function descriptionDetect whether the button is pressed
Input parametersNone
Output parametersKey status: KEY_PRESS (1), KEY_RELEASE (0)

Function: KeyX_Long_Press

Function prototypeuint8_t Key1_Long_Press(uint16_t timeout)
Function descriptionDetect button long press status
Input parametersLong press time
Output parametersKey status: KEY_PRESS (1), KEY_RELEASE (0)

Function: Key1_State

Function prototypeuint8_t Key1_State(uint8_t mode)
Function descriptionRead key status
Input parametersMode: 0 means always returning 1, 1 means always returning 0
Output parametersKey status: KEY_PRESS (1), KEY_RELEASE (0)

2. HAL library function analysis

The HAL library functions that have been introduced in the previous tutorial will not be introduced again in the tutorial!

Function: HAL_IWDG_Init

Function prototypeHAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
Function descriptionInitialize IWDG peripheral parameters
Input parametershiwdg: IWDG handle address
Return valueHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT

Function: HAL_IWDG_Refresh

Function prototypeHAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
Function descriptionRefresh IWDG (feed the dog)
Input parametershiwdg: IWDG handle address
Return valueHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT

5. Experimental phenomena

After successfully downloading the program, press the RESET button on the development board to open the serial port debugging assistant to observe the phenomenon!

Phenomenon:

When the program is run for the first time, "**** IWDG Test Start *******" will be printed;

If you do not press the KEY1 key, the serial port debugging assistant will print the message "IWDG no no no reload!!!";

If the KEY1 key is pressed, the serial port debugging assistant will print the message "Refreshes the IWDG!!!".

image-20231102184529728