RISCV_Watchdog User Guide


REVISION HISTORY

Revision No.
Description
Date
1.0
  • Initial release
  • 04/21/2023

    1. OVERVIEW

    The hardware watchdog provides a timeout reset system function, which consists of a timer and a reset function. Users can set a timeout period and perform a "feed dog" operation within the timeout period to reset its internal count. If its internal calculation reaches the set timeout period, the watchdog will reset the system and restart it.

    2. KEYWORD DESCRIPTION

    • watchdog

      A watchdog is a timer circuit that typically has an input called "kick the dog/service the dog" and an output to the RST terminal of the MCU. When the MCU is working normally, it outputs a signal to the dog feeding terminal at regular intervals to reset the WDT. If the WDT timer exceeds the specified time (usually when the program runs out), it will give a reset signal to the MCU to reset it. Prevent MCU from crashing The role of a watchdog is to prevent the program from looping or running away.

    • watchdog timeout

      The timeout duration of the watchdog. If the watchdog is not fed after this duration, the WDT timer will exceed, and a reset signal will be sent to the MCU to reset it.

    • watchdog pretimeout

      The pre timeout duration of the watchdog. Pretimeout is the length of time from the trigger time to the system restart time point. For example, if you set the timeout to 60, Pretimeout is 10, so the mechanism will be triggered at 50 seconds (10 seconds before restart).

    3. FUNCTION DESCRIPTION

    The Linux kernel supports softdogs based on hrtimer and hardware watchdogs based on hardware, creating/dev/watchdog* device files to interact with user space programs. The watchdog program in user space will feed dogs periodically through the/dev/watchdog* device.

    Picture 1-1 watchdog Framework

    4. HARDWARE CONNECTION INTRODUCTION

    NA

    5. Usage Introduction In RTOS

    5.1. DRIVER PATH

    sc/driver/sysdriver/watchdog/drv/mdrv_wdt_api.h

    sc/driver/sysdriver/watchdog/drv/mdrv_wdt.c

    sc/driver/sysdriver/watchdog/drv/mdrv_wdt_reg.h

    sc/driver/sysdriver/watchdog/drv/mdrv_wdt_test.c

    5.2. CONFIG Configuration

    The config file is located at mak/options _pcupid_riscv _isw. mak, with CONFIG-WatchDOG_SUPPORT enabled`

    # Feature_Name = [DRV] WATCHDOG driver support
    # Description = WATCHDOG driver support
    # Option_Selection = TRUE, FALSE
    CONFIG_WATCHDOG_SUPPORT = TRUE
    

    5.3. SYSDESC Configuration

    No configuration is required.

    5.4. Padmux Configuration

    No configuration is required.

    5.5. Sample code

    int watchdog_test(void)
    {
        int wdt_timeout = 10; /* 10s */
        int i = 0;
    
        if(sstar_wdt_probe()!=0)
        {
            CamOsPrintf("sstar_wdt_probe failed to execute\n");
            return -1;
        }
    
        if (sstar_wdt_set_timeout(wdt_timeout)!=0)  /*Set watchdog timeout*/
        {
            CamOsPrintf("sstar_wdt_set_timeout failed to execute\n", argc);
            return -1;
        }
    
        if(sstar_wdt_start()!=0)  /*Start watchdog*/
        {
            CamOsPrintf("sstar_wdt_start failed to execute\n");
            return -1;
        }
    
        for(i = 0; i < 10; i++){
            CamOsMsSleep(wdt_timeout*1000/2); /*Feed the watchdog every 5 seconds, 10 times */
            if (sstar_wdt_ping()!=0)
            {
                    return -1;
            }
        }
    
        /*After 50 seconds, the system will no longer feed the dog. At the 60th second, the watchdog will trigger the system to be restarted*/
    
        return 0;
    }
    

    5.6. UT case

    Test case:

    (1) Open the Watchdog macro CONFIG_WATCHDOG_SUPPORT and VER_WATCHDOG

    (2) Test demo path: sc/driver/sysdriver/watchdog/drv/mdrv_wdt_test.c

    (3) Perform testing

    SS-RTOS # wdttest
    argv must like start/reset xs,num must be 2, now is: 0
        [start/reset] [time (s)]
        - Do some tests about wdt'
    
    Example:
      # wdttest start 10  /* After starting watchdog, the system will continuously feed the watchdog, so the system will not restart after 10 seconds*/
      # wdttest reset 10  /* After starting watchdog, the system will not feed the dog, so the system will restart after 10 seconds*/
    

    6. API reference

    This functional module provides the following interfaces:

    API Name Function
    sstar_wdt_probe Initialize the watchdog
    sstar_wdt_remove Release watchdog
    sstar_wdt_start Start watchdog
    sstar_wdt_stop Stop watchdog
    sstar_wdt_set_timeout Set watchdog timeout
    sstar_wdt_ping Feed watchdog

    6.1. sstar_wdt_probe

    • Function

      Initialize the watchdog

    • Grammar

      int sstar_wdt_probe(void);
      
    • Parameters

      Parameter Name Description
      NA NA
    • Return value

      Return value Description
      0 Successful
      other Failed

    6.2. sstar_wdt_remove

    • Function

      Release watchdog, if watchdog is running, stop watchdog first

    • Grammar

      int sstar_wdt_remove(void);
      
    • Parameters

      Parameter Name Description
      NA NA
    • Return value

      Return value Description
      0 Successful
      other Failed

    6.3. sstar_wdt_start

    • Function

      Start WDT according to the set or default timeout

    • Grammar

      int sstar_wdt_start(void);
      
    • Parameters

      Parameter Name Description
      NA NA
    • Return value

      Return value Description
      0 Successful
      other Failed

    6.4. sstar_wdt_stop

    • Function

      Stop watchdog

    • Grammar

      int sstar_wdt_stop(void);
      
    • Parameters

      Parameter Name Description
      void NA
    • Return value

      Return value Description
      0 Successful
      other Failed

    6.5. sstar_wdt_set_timeout

    • Function

      Set the length of the WDT timeout. If sstar_wdt_ping is not used periodically to "feed the dog" within this length of time, the system will reset, commonly known as "dog bite"

    • Grammar

      int sstar_wdt_set_timeout(unsigned int timeout);
      
    • Parameters

      Parameter Name Description
      timeout watchdog timeout
    • Return value

      Return value Description
      0 Successful
      other Failed

    6.6. sstar_wdt_ping

    • Function

      Reset the WDT counter, commonly known as "feeding the dog"

    • Grammar

      int sstar_wdt_ping(void);
      
    • Parameters

      Parameter Name Description
      void NA
    • Return value

      Return value Description
      0 Successful
      other Failed

    7. FAQ

    NA