Microchip® Advanced Software Framework

Quick Start Guide for WDT - Callback

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

  • System reset after 4096 clocks of the Watchdog generic clock
  • Always on mode disabled
  • Early warning period of 2048 clocks of the Watchdog generic clock

This use case sets up the Watchdog to force a system reset after every 4096 clocks of the Watchdog's Generic Clock channel, with an Early Warning callback being generated every 2048 clocks. Each time the Early Warning interrupt fires the board LED is turned on, and each time the device resets the board LED is turned off, giving a periodic flashing pattern.

Setup

Prerequisites

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

Code

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

void watchdog_early_warning_callback(void)
{
port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
}
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_4096CLK;
config_wdt.early_warning_period = WDT_PERIOD_2048CLK;
/* Initialize and enable the Watchdog with the user settings */
wdt_set_config(&config_wdt);
}
void configure_wdt_callbacks(void)
{
wdt_register_callback(watchdog_early_warning_callback,
}

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

configure_wdt();
configure_wdt_callbacks();

Workflow

  1. Configure and enable the Watchdog driver.
    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 and early warning periods 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_4096CLK;
      config_wdt.early_warning_period = WDT_PERIOD_2048CLK;
    4. Sets up the WDT hardware module with the requested settings.
      wdt_set_config(&config_wdt);
  2. Register and enable the Early Warning callback handler.
    1. Register the user-provided Early Warning callback function with the driver, so that it will be run when an Early Warning condition occurs.
      wdt_register_callback(watchdog_early_warning_callback,
    2. Enable the Early Warning callback so that it will generate callbacks.

Quick Start Guide for WDT - Callback

Code

Copy-paste the following code to your user application:

port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
while (true) {
/* Wait for callback */
}

Workflow

  1. Turn off the board LED when the application starts.
    port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
  2. Enable global interrupts so that callbacks can be generated.
  3. Enter an infinite loop to hold the main program logic.
    while (true) {
    /* Wait for callback */
    }