Adding Watchdog in NCP Mode


Description

This document describes how to implement the Watchdog on a Bluetooth NCP firmware. The Watchdog is an EFR32 peripheral which allows you to detect and reset/reboot an EFR32/BGM device when it hangs and becomes unresponsive (e.g., software stalemate). The example below can be applied to the standard NCP target - Empty from the Bluetooth SDK for any EFR32/BGM device or WSTK radio board.

For more information about NCP mode,see AN1042: Using the Silicon Labs Bluetooth® Stack in Network Co-Processor Mode. For more details about the Watchdog, see the relevant EFR32 Reference Manual.

The sample code described in this document was implemented on Bluetooth SDK 2.11.x but it can be used with later SDK versions. The basic idea is to feed the Watchdog timer each time the soft timer ticks. If the soft timer stops ticking, the Watchdog will expire and reset the device.

Adding the Watchdog to the NCP Firmware

  1. Create a new NCP target - Empty project in Simplicity Studio for the specific radio board or OPN where you want to run this example.
  2. Copy em_wdog.c and em_wdog.h into the project folders platform\emlib\src and platform\emlib\inc respectively. The em_wdog.c/h files are located in the following SDK path:

    C:\SiliconLabs\SimplicityStudio\v4\developer\sdks\gecko_sdk_suite\<sdk_version>\platform\emlib ('src' folder for em_wdog.c and 'inc' folder for em_wdog.h)

  1. Add #include "em_wdog.h" to main.c

  2. Configure the Watchdog timer in main.c by adding the code below before gecko_init() function. This is where you can set the threshold timeout in addition to other configuration options.

WDOG_Init_TypeDef init =
{
 .enable = true,             /* Start Watchdog when the initialization is done */
 .debugRun = false,          /* WDOG not counting during debug halt */
 .em2Run = true,             /* WDOG counting when in EM2 */
 .em3Run = true,             /* WDOG counting when in EM3 */
 .em4Block = false,          /* EM4 can be entered */
 .swoscBlock = false,        /* Do not block disabling LFRCO/LFXO in CMU */
 .lock = false,              /* Do not lock WDOG configuration (if locked, reset needed to unlock) */
 .clkSel = wdogClkSelULFRCO, /* Select 1 kHZ ULFRCO as WDOG clock source */
 .perSel = wdogPeriod_4k,    /* Set the Watchdog period to 4097 clock periods (i.e., ~4 seconds)*/
};

WDOG_Init(&init);
  1. In the local_handle_event function in main.c, initialize the soft timer with a suitable timeout for your application requirements (2 seconds in this example) after the boot event and add a soft timer event handler to feed the Watchdog.
 static uint32_t local_handle_event(struct gecko_cmd_packet *evt)
{
  bool evt_handled = false;
  switch (BGLIB_MSG_ID(evt->header)) {

    case gecko_evt_system_boot_id:
      //Set a soft timer for 2 seconds. If the firmware hangs, the soft timer will stop feeding the Watchdog and cause it to reset.
      gecko_cmd_hardware_set_soft_timer(32768*2,0,0);
    break;
    case gecko_evt_hardware_soft_timer_id:
      //Feed the Watchdog if the firmware hangs and the soft timer stops running.
      WDOG_Feed();
    break;
    default:
    break;
}
return evt_handled;
}
  1. (Optional) To check if the device was reset because of the Watchdog, you can use the RMU (Reset Management Unit). Check if em_rmu.c/.h are added to your project (this is part of the NCP target - empty project by default as of SDK 2.11.x), in the same folders where em_wdog.c/h files are located. If not, copy em_rmu.c/h into the project from the same SDK path as the em_wdog.c/h files.

    6.1 Add #include "em_rmu.h" to main.c

    6.2 Check at the beginning of main() if the reset was caused by Watchdog and any suitable actions for you application.

uint32_t resetCause = RMU_ResetCauseGet();
RMU_ResetCauseClear();

/* Check if the Watchdog triggered the last reset */
if (resetCause & RMU_RSTCAUSE_WDOGRST)
{
  /* Watchdog reset occurred - add your code here */
}

Testing the Example

You can use BGTool to test the example described in this article. In the Simplicity Studio Launcher view, go to "Compatible Tools" launch BGTool and connect to your target.

Assuming you have flashed an NCP firmware modified as instructed here, you will observe soft timer events occurring every 2 seconds. In the BGTool command prompt you can stop the soft timer by sending the command shown below, which will cause the Watchdog to reset the device after the 4 second timeout.

BG Tool