Using Watchdog in Bluetooth Applications
In general, use Watchdog to reset your device should the Bluetooth stack stop working for any reason. This software example will show you how to use a Watchdog timer in the Bluetooth application context.
The Watchdog API is fairly simple. The Watchdog has to be initialized once after reset with
WDOG_Init(&init) and it has to be fed from time to time before the Watchdog timer expires using
WDOG_Feed(). In a Bluetooth context, the most suitable way to feed the Watchdog is by using a soft timer. A soft timer event is triggered by the stack each time the timer expires. If the stack stops working, a soft timer event is not generated and the Watchdog is not fed. Ultimately, this will reset the device.
To determine whether you recovered from a crash with a Watchdog reset or whether the device was reset normally, use the function
Create a new SoC - Empty project in Simplicity Studio.
Copy em_wdog.c and em_wdog.h into the project from:
Copy gpiointerrupt.c into the project from:
Copy the attached app.c file to your project (overwriting the existing one).
Change DEBUG_LEVEL to 1 in app.h (enable log output).
Build and flash the project to your board.
The attached example application sets a Watchdog period of 2 seconds and sets up a soft timer to feed the Watchdog every second (1 s). An LED is also toggled on every soft timer event to make the heartbeat visible. Pushbutton PB0 is set to trigger an interrupt when pressed down. The callback from the ISR will stop the soft timer from feeding and cause the Watchdog to reset the device. After the device boots again, a log message indicating a Watchdog reset is printed to UART. To see log messages, use any terminal program (e.g., TeraTerm).
Note: If you are implementing a low-energy application, set the timer to a period longer than 1 second because waking up the device every second results in high consumption.