Microchip® Advanced Software Framework

Quick Start Guide for WDT - Basic

In this use case, the Watchdog module is configured for:

  • System reset after 2048 clocks of the Watchdog generic clock
  • Always on mode disabled
  • Basic mode, with no window or early warning periods

This use case sets up the Watchdog to force a system reset after every 2048 clocks of the Watchdog's Generic Clock channel, unless the user periodically resets the Watchdog counter via a button before the timer expires. If the Watchdog resets the device, a LED on the board is turned off.

Setup

Prerequisites

There are no special setup requirements for this use-case.

Code

Copy-paste the following setup code to your user application:

void configure_wdt(void)
{
/* Create a new configuration structure for the Watchdog settings and fill
* with the default module settings. */
struct wdt_conf config_wdt;
/* Set the Watchdog configuration settings */
config_wdt.always_on = false;
#if !((SAML21) || (SAMC21) || (SAML22) || (SAMR30))
config_wdt.clock_source = GCLK_GENERATOR_4;
#endif
config_wdt.timeout_period = WDT_PERIOD_2048CLK;
/* Initialize and enable the Watchdog with the user settings */
wdt_set_config(&config_wdt);
}

Add to user application initialization (typically the start of main()):

configure_wdt();

Workflow

  1. Create a Watchdog module configuration struct, which can be filled out to adjust the configuration of the Watchdog.
    struct wdt_conf config_wdt;
  2. Initialize the Watchdog configuration struct with the module's default values.
    Note
    This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
  3. Adjust the configuration struct to set the timeout period and lock mode of the Watchdog.
    config_wdt.always_on = false;
    #if !((SAML21) || (SAMC21) || (SAML22) || (SAMR30))
    config_wdt.clock_source = GCLK_GENERATOR_4;
    #endif
    config_wdt.timeout_period = WDT_PERIOD_2048CLK;
  4. Setups the WDT hardware module with the requested settings.
    wdt_set_config(&config_wdt);

Quick Start Guide for WDT - Basic

Code

Copy-paste the following code to your user application:

if (reset_cause == SYSTEM_RESET_CAUSE_WDT) {
port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
}
else {
port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
}
while (true) {
if (port_pin_get_input_level(BUTTON_0_PIN) == false) {
port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
}
}

Workflow

  1. Retrieve the cause of the system reset to determine if the Watchdog module was the cause of the last reset.
  2. Turn on or off the board LED based on whether the Watchdog reset the device.
    if (reset_cause == SYSTEM_RESET_CAUSE_WDT) {
    port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
    }
    else {
    port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
    }
  3. Enter an infinite loop to hold the main program logic.
    while (true) {
  4. Test to see if the board button is currently being pressed.
    if (port_pin_get_input_level(BUTTON_0_PIN) == false) {
  5. If the button is pressed, turn on the board LED and reset the Watchdog timer.
    port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);