This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to use WDT to reset device in nRF52832

HI,

I am using ".\nRF5_SDK_15.3.0_59ac345\examples\peripheral\wdt" to test “watch dog timer function.

My use case is:

1. If the program is not execute over following pint (please find follow image) in some time, the system will reboot.

2. Otherwise, the system will not reboot.

How can I modify the program?

Thank you,

Chianglin

Parents Reply Children
  • Hi Vidar,,

    Please find the attach file.

    #include <stdbool.h>
    #include <stdint.h>
    
    #include "nrf.h"
    #include "bsp.h"
    #include "app_timer.h"
    #include "app_error.h"
    #include "nrf_drv_wdt.h"
    #include "nrf_drv_clock.h"
    #include "nrf_delay.h"
    #include "app_util_platform.h"
    
    #define FEED_BUTTON_ID          0                           /**< Button for feeding the dog. */
    
    nrf_drv_wdt_channel_id m_channel_id;
    
    /**
     * @brief WDT events handler.
     */
    void wdt_event_handler(void)
    {
        bsp_board_leds_off();
    
        //NOTE: The max amount of time we can spend in WDT interrupt is two cycles of 32768[Hz] clock - after that, reset occurs
    }
    
    /**
     * @brief Assert callback.
     *
     * @param[in] id    Fault identifier. See @ref NRF_FAULT_IDS.
     * @param[in] pc    The program counter of the instruction that triggered the fault, or 0 if
     *                  unavailable.
     * @param[in] info  Optional additional information regarding the fault. Refer to each fault
     *                  identifier for details.
     */
    void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
    {
        bsp_board_leds_off();
        while (1);
    }
    
    /**
     * @brief BSP events callback.
     */
    void bsp_event_callback(bsp_event_t event)
    {
        switch (event)
        {
            case BSP_EVENT_KEY_0:
                nrf_drv_wdt_channel_feed(m_channel_id);
                break;
    
            default :
                //Do nothing.
                break;
        }
    }
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        uint32_t err_code = NRF_SUCCESS;
    
        //BSP configuration for button support: button pushing will feed the dog.
        err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
        nrf_drv_clock_lfclk_request(NULL);
    
        err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_init(BSP_INIT_BUTTONS, bsp_event_callback);
        APP_ERROR_CHECK(err_code);
    
        //Configure all LEDs on board.
        bsp_board_init(BSP_INIT_LEDS);
    
        //Configure WDT.
        nrf_drv_wdt_config_t config = NRF_DRV_WDT_DEAFULT_CONFIG;
        err_code = nrf_drv_wdt_init(&config, wdt_event_handler);
        APP_ERROR_CHECK(err_code);
        err_code = nrf_drv_wdt_channel_alloc(&m_channel_id);
        APP_ERROR_CHECK(err_code);
        nrf_drv_wdt_enable();
    
        //Indicate program start on LEDs.
        for (uint32_t i = 0; i < LEDS_NUMBER; i++)
        {   nrf_delay_ms(200);
            bsp_board_led_on(i);
        }
         err_code = bsp_buttons_enable();
         APP_ERROR_CHECK(err_code);
    
        while (1)
        {
        	nrf_drv_wdt_channel_feed(m_channel_id);
            __SEV();
            __WFE();
            __WFE();
        }
    }

    I have add "nrf_drv_wdt_channel_feed(m_channel_id);" in while loop.,

    But the program still execute "wdt_event_handler()" function.

    Would you please tell me how can I fix this problem?

    Thank you,

    Chianglin

  • Hi Vidar,,

    It's work now, thank you.

    Can I ask an another question?

    What the purpose of "__SEV()" and "__WFE()“?

    Thank you,

    Chianglin

  • Hi Chianglin,

    These two instructions are used to clear the internal "event" register. I don't think it's strictly necessary here, it could have been just one __WFE instruction. Here's another thread which provides more details: https://devzone.nordicsemi.com/f/nordic-q-a/490/how-do-you-put-the-nrf51822-chip-to-sleep

Related