5.18 IWDG Guide

This chapter describes the file composition and 7 public functions of the IWDG driver in the N32H49x standard peripheral library, together with the IWDG_Reset example. The IWDG is clocked by the LSI and runs independently of the main program, recovering from a dead loop or a locked-up MCU by a system reset; for the key values, prescaler and reload register access protection and configuration flow, see the “Independent watchdog (IWDG)” chapter of the user manual.

5.18.1 Related files

Relative path Purpose
firmware/n32h49x_std_periph_driver/inc/n32h49x_iwdg.h Defines the key values, write access configuration, prescaler and status flag enums, and declares the 7 public functions
firmware/n32h49x_std_periph_driver/src/n32h49x_iwdg.c Implements the 7 functions above: write access key sequence, writing the prescaler and reload values, reloading and starting the counter, runtime freeze and status readback
projects/n32h49x_EVAL/examples/IWDG/ IWDG example root directory, containing the single IWDG_Reset example
projects/n32h49x_EVAL/examples/IWDG/IWDG_Reset/ Project directory composition of this example: MDK-ARM/, EWARM/, inc/, src/, readme.txt
projects/n32h49x_EVAL/bsp/ Common board files referenced by this example: the UART log and millisecond delay functions

5.18.2 API reference

Search API7 functions

The following 7 public functions are listed in the declaration order of n32h49x_iwdg.h in 3 groups; each entry gives a summary and the original comment above the function in n32h49x_iwdg.c.

Configuration and reload

IWDG_WriteConfig, IWDG_SetPrescalerDiv, IWDG_CntReload, IWDG_ReloadKey

Enable and freeze

IWDG_Enable, IWDG_Freeze_Enable

Status

IWDG_GetStatus

void IWDG_WriteConfig(IWDOG_WRITE_CONFIG IWDG_WriteAccess)Enables or disables write protection to IWDG_PREDIV and IWDG_RELV registers.
Source comment
name    IWDG_WriteConfig.
fun     Enables or disables write protection to IWDG_PREDIV and IWDG_RELV registers.
param   IWDG_WriteAccess
        - IWDG_WRITE_ENABLE
        - IWDG_WRITE_DISABLE
return  none

Use note: The implementation writes the parameter value directly to IWDG->KEY; IWDG_WRITE_ENABLE is 0x5555 and opens write access to IWDG_PREDIV and IWDG_RELV, while IWDG_WRITE_DISABLE is 0x0000.

void IWDG_SetPrescalerDiv(uint8_t IWDG_Prescaler)IWDG_Prescaler specifies the IWDG prescaler value.
Source comment
name    IWDG_SetPrescalerDiv.
fun     IWDG_Prescaler specifies the IWDG prescaler value.
param   IWDG_Prescaler :
          - IWDG_PRESCALER_DIV4
          - IWDG_PRESCALER_DIV8
          - IWDG_PRESCALER_DIV16
          - IWDG_PRESCALER_DIV32
          - IWDG_PRESCALER_DIV64
          - IWDG_PRESCALER_DIV128
          - IWDG_PRESCALER_DIV256
return  none
void IWDG_CntReload(uint16_t Reload)Sets IWDG reload value.
Source comment
name    IWDG_CntReload.
fun     Sets IWDG reload value.
param   Reload :
          -0x000 ~ 0xFFF
return  none
void IWDG_ReloadKey(void)Reload IWDG counter with value defined in IWDG_RELV register.
Source comment
name    IWDG_ReloadKey.
fun     Reload IWDG counter with value defined in IWDG_RELV register.
param   none
return  none
void IWDG_Enable(void)Start watch dog counter.
Source comment
name    IWDG_Enable.
fun     Start watch dog counter.
param   none
return  none
void IWDG_Freeze_Enable(FunctionalState Cmd)Freeze or unfreeze IWDG while IWDG is running in run mode.
Source comment
name    IWDG_Freeze_Enable.
fun     Freeze or unfreeze IWDG while IWDG is running in run mode.
param  Cmd :
          - ENABLE
          - DISABLE
return  none

Use note: The implementation writes IWDG->KEY according to Cmd: ENABLE writes 0x4567 to freeze the counter, any other value writes 0x89AB to unfreeze it.

FlagStatus IWDG_GetStatus(IWDG_STATUS_FLAG IWDG_FLAG)Checks whether the specified IWDG flag is set or not.
Source comment
name    IWDG_GetStatus.
fun     Checks whether the specified IWDG flag is set or not.
param   IWDG_FLAG :
          - IWDG_PVU_FLAG
          - IWDG_CRVU_FLAG
          - IWDG_FRZF_FLAG
return  FlagStatus :
          - RESET
          - SET

5.18.3 Demo descriptions

This section describes the single IWDG example: IWDG_Reset configures the IWDG timeout and reloads the counter periodically in the main loop to demonstrate a system reset triggered by a feed timeout.

5.18.3.1 IWDG_Reset IWDG reset

Example directory: projects/n32h49x_EVAL/examples/IWDG/IWDG_Reset/

Initialize the UART log and the indicators→Check whether the last reset was caused by the IWDG and light LED1→Open write access, configure the prescaler and reload value→Start the IWDG counter→Toggle LED3 and reload the counter after a delay in the main loop

Item Source analysis
Function Configures the IWDG counter clock to LSI/32 with a timeout of about 250 ms, and the main loop reloads the counter every 249 ms;
during normal operation LED3 keeps blinking, a feed timeout triggers an IWDG reset, after which LED1 lights up and reset by IWDG is printed on the UART.
Source files src/main.c: main together with LedInit, LedOn, LedOff, LedBlink, and GTIM1_ConfigForLSI under LSI_TIM_MEASURE;
inc/main.h: includes n32h49x_iwdg.h, n32h49x_usart.h, misc.h, log.h;
src/n32h49x_it.c, inc/n32h49x_it.h: interrupt service routines (including GTIM1_IRQHandler);
readme.txt: function, environment and usage notes;
MDK-ARM/, EWARM/: Keil and IAR projects.
Key configuration Clock: the IWDG is driven by the LSI with prescaler IWDG_PRESCALER_DIV32;
Reload value: IWDG_CntReload(LsiFreq / 128) with LsiFreq set to 32000 Hz giving a timeout of about 250 ms;
Write access: IWDG_WriteConfig(IWDG_WRITE_ENABLE) opens write access to IWDG_PREDIV and IWDG_RELV;
Indicators: LED1 is PA3 and LED3 is PA8, push-pull output.
Program and data path main calls log_init, initializes and turns off LED1 and LED3;
checks RCC_FLAG_IWDGRSTF: when set it lights LED1, prints reset by IWDG and clears the reset flags, otherwise it turns off LED1;
then IWDG_WriteConfig, IWDG_SetPrescalerDiv, IWDG_CntReload, IWDG_ReloadKey and IWDG_Enable configure and start the IWDG in turn;
while(1) toggles PA8 (LED3), waits 249 ms and then reloads the counter with IWDG_ReloadKey.
Main APIs IWDG_WriteConfig, IWDG_SetPrescalerDiv, IWDG_CntReload, IWDG_ReloadKey, IWDG_Enable
Verification After building, downloading and resetting, LED3 toggles about every 249 ms and blinks continuously, showing that the feed is working;
change the systick_delay_ms argument to 251 or more so that the feed times out, and the system will keep resetting, LED1 lights up and the UART prints reset by IWDG.
Use note The hardware development environment is the full-featured board N32H497ZGL7_EVB V1.0;
the development environment is KEIL MDK-ARM 5.34 or IAR EWARM 8.50.1;
when LSI_TIM_MEASURE is not defined the timeout is taken directly as LsiFreq / 128 without measuring the actual LSI frequency, so the actual timeout varies slightly with LSI frequency dispersion.
Core-code walkthrough and porting

The example first detects whether this reset came from the IWDG, then performs write access, prescaler, reload and start in turn, and finally feeds the watchdog periodically in the main loop.

Data path: RCC_FLAG_IWDGRSTF reset flag → IWDG_WriteConfig, IWDG_SetPrescalerDiv, IWDG_CntReload, IWDG_Enable write IWDG_PREDIV, IWDG_RELV and IWDG_KEY → the main loop IWDG_ReloadKey reloads the counter → LED3 toggles and LED1 lights up on a reset

1. Initialize and turn on the indicators. main first calls log_init, initializes LED1 (PA3) and LED3 (PA8) as push-pull outputs and turns them off.

src/main.csrc/main.c · main
    log_init();
    log_info("--- IWDG demo reset ---\n");
    LedInit(LED1_GPIO_PORT, LED1_GPIO_PIN);
    LedInit(LED3_GPIO_PORT, LED3_GPIO_PIN);
    LedOff(LED1_GPIO_PORT, LED1_GPIO_PIN);
    LedOff(LED3_GPIO_PORT, LED3_GPIO_PIN);

When porting to another board, only the LED1_GPIO_PORT, LED1_GPIO_PIN, LED3_GPIO_PORT and LED3_GPIO_PIN macros need to change; the LED initialization is unrelated to the IWDG itself.

2. Check the reset source and display it. PWR clock is enabled first, then RCC_FLAG_IWDGRSTF is read: if this reset was caused by the IWDG, LED1 lights up, a message is printed and the reset flags are cleared, otherwise LED1 is turned off.

src/main.csrc/main.c · main
    /* Check if the system has resumed from IWDG reset */
    if(RCC_GetFlagStatus(RCC_FLAG_IWDGRSTF) != RESET)
    {
        /* IWDGRST flag set */
        /* Turn on LED1 */
        LedOn(LED1_GPIO_PORT, LED1_GPIO_PIN);
        log_info("reset by IWDG\n");
        /* Clear reset flags */
        RCC_ClearResetFlag();
    }
    else
    {
        /* IWDGRST flag is not set */
        /* Turn off LED1 */
        LedOff(LED1_GPIO_PORT, LED1_GPIO_PIN);
    }

When porting, keep the reset flag clear call, otherwise the next power-up still reads a stale reset source; the PWR clock must be enabled before reading this flag.

3. Configure and start the IWDG. main opens write access, writes the prescaler and reload values, then reloads and starts the counter.

src/main.csrc/main.c · main
    /* IWDG timeout equal to 250 ms (the timeout may varies due to LSI frequency
       dispersion) */
    /* Enable write access to IWDG_PR and IWDG_RLR registers */
    IWDG_WriteConfig(IWDG_WRITE_ENABLE);

    /* IWDG counter clock: LSI/32 */
    IWDG_SetPrescalerDiv(IWDG_PRESCALER_DIV32);

    /* Set counter reload value to obtain 250ms IWDG TimeOut.
       Counter Reload Value = 250ms/IWDG counter clock period
                            = 250ms / (LSI/128)
                            = 0.25s / (LsiFreq/32)
                            = LsiFreq/(32 * 4)
                            = LsiFreq/128 */
    log_debug("LsiFreq is: %d\n", LsiFreq);

    IWDG_CntReload(LsiFreq / 128);
    /* Reload IWDG counter */
    IWDG_ReloadKey();

    /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
    IWDG_Enable();

LsiFreq defaults to 32000 Hz, which with prescaler IWDG_PRESCALER_DIV32 gives a timeout of about 250 ms; when changing the timeout, check the prescaler factor and the IWDG_CntReload argument together.

4. Feed the watchdog periodically in the main loop. The loop toggles LED3, waits 249 ms and then reloads the counter with IWDG_ReloadKey.

src/main.csrc/main.c · main
    while(1)
    {
        /* Toggle LED3 */
        LedBlink(LED3_GPIO_PORT, LED3_GPIO_PIN);
        /* Insert 249 ms delay */
        systick_delay_ms(249);
        /* Reload IWDG counter */
        IWDG_ReloadKey();
    }

The feed interval must be shorter than the timeout (here 249 ms is less than 250 ms); changing the delay beyond the timeout triggers an IWDG reset.

Corresponding source files: src/main.c

5.18.4 Notes and troubleshooting

Scenario Check & action Source
Changing the prescaler and reload value These two registers are write-protected, so 0x5555 must first be written to IWDG_KEY.KEYV[15:0] to open write access;
writing any other value re-enables write protection, and a reload (writing 0xAAAA) also makes the registers write-protected again.
User manual
Update flags not cleared Before changing the prescaler, confirm IWDG_STS.PVU is 0;
before changing the reload value, confirm IWDG_STS.CRVU is 0;
reads of IWDG_PREDIV.PD[2:0] or IWDG_RELV.REL[11:0] during an update are not valid.
User manual
Enabling Write 0xCCCC to IWDG_KEY.KEYV[15:0] to start the counter;
after the hardware watchdog is enabled through the option bytes, it runs automatically at power-up.
User manual
Reset enable bit When PWR_CTRL2.IWDGRSTEN is 1, the counter reaching 0 generates a system reset;
when this bit is 0 the IWDG counts but does not generate a reset.
User manual
Debug mode Whether the IWDG counter stops when the core is halted is determined by the DBG_CTRL.IWDG_STOP bit of the debug module: when it is 1 the counter stops, when it is 0 it keeps counting. User manual
Stopping the counter Once the IWDG is enabled it will not stop counting unless a system reset occurs or 0x4567 is written to IWDG_KEY.KEYV[15:0] to configure the runtime freeze. User manual
Nations Technologies DevKit Docs

Datasheets, SDK, driver APIs and demo guides for Nations Technologies MCU development kits, organized by chip series.

© 2026 Nations Technologies Inc. All rights reserved.粤ICP备11069485号This site uses cookie-free anonymous analytics.