Applications - Watchdog Timer

Last modified by Microchip on 2024/06/20 12:43

Introduction

This article shows how the watchdog timer functionality of the SAMA5D2 Series ARM® Cortex®-A5 Microprocessor Unit (MPU) is enabled in the Linux® kernel.

The watchdog timer is used to monitor the system and recover it in case of a system malfunction. The basic principle is that once the watchdog timer is started, it begins a count to a predetermined time (with the default being 16 seconds). If the watchdog timer is not fed in that period, it will initiate a system reboot.

Prerequisites

This application is developed for the ATSAMA5D27-SOM1-EK1 development platform:

This application is developed using the Buildroot build system.

Back to Top

Hardware

The SAMA5D2 Series ARM Cortex-A5 MPU contains an integrated watchdog timer with the following features:

  • 12-bit Key-protected Programmable Counter
  • Watchdog Clock is Independent of the Processor Clock
  • Provides Reset or Interrupt Signals to the System
  • Counter May Be Stopped while the Processor is in Debug State or Idle mode

Watchdog Timer Block Diagram

Watchdog Timer Block Diagram

Back to Top

Buildroot Configuration

Objective: Using Buildroot, build a bootable image and Flash it onto an SD Memory Card for the ATSAMA5D27-SOM1-EK1 development board.

Follow the steps for building the image in the "Create Project with Default Configuration" article. You will use the default configuration file: atmel_sama5d27_som1_ek_mmc_dev_defconfig.

All necessary functions for the watchdog timer have been selected in the default configuration, however, the watchdog timer has not been enabled. It can be launched and fed with the Busybox watchdog command.

Back to Top

Device Tree

Objective: Observe how the watchdog timer was configured in the device tree. No changes are required.

Once Buildroot has completed its build, the watchdog timer definitions for the ATSAMA5D27-SOM1-EK1 were configured by a device tree. The device tree source includes files (DTSI and DTS) located in the Buildroot output directory: /output/build/linux-linux4sam_6.0/arch/arm/boot/dts/.

Examine the sama5d2.dtsi file and observe the watchdog timer assignments:

1296  watchdog@f8048040 {
1297     compatible = "atmel,sama5d4-wdt";
1298     reg = <0xf8048040 0x10>;
1299     interrupts = <4 IRQ_TYPE_LEVEL_HIGH 7>;
1300     clocks = <&clk32k>;
1301     status = "disabled";
1302  };
1303
1304  clk32k: sckc@f8048050 {
1305     compatible = "atmel,sama5d4-sckc";
1306     reg = <0xf8048050 0x4>;
1307
1308     clocks = <&slow_xtal>;                 
1309     #clock-cells = <0>;
1310  };

Line 1297 specifies which driver will be used for this watchdog device.

In line 1298, the watchdog base address is 0xf8048040, and the size of the register block is 0x10.

In line 1299, the PID of the watchdog is 4, high level triggered, priority is 7.

Line 1300 defines the watchdog clock source.

In line 1301, the default is 'disabled' and will be replaced with 'okay' in the file at91-sama5d27_som1_ek.dts.

Line 1305 specifies which driver will be used for this slow clock device.

In line 1306, the slow clock controller base address is 0xf8048050, the size of the register block is 0x4.

In line 1308, there are two clock sources for slow clock controller: 1) external 32.768 KHz crystal or 2) internal 64 KHz RC. Here we use the external 32.768 kHz crystal oscillator. 


Examine the at91-sama5d27_som1_ek.dts file and observe the watchdog timer assignments:

209   watchdog@f8048040 {
210      status = "okay";
211   };

In line 210, the status of the watchdog is set to 'okay', enabling the watchdog device.

Back to Top

Kernel

Objective: Observe how watchdog timer functionality was configured in the Linux kernel.

From the buildroot directory, run the Linux kernel menuconfig:

$ make linux-menuconfig

The top-level menu will be displayed:

Linux menuconfig top menu

Back to Top

Device Driver

Select Device Drivers ---->

Select [*] Watchdog Timer Support ---->

Observe that <*> Atmel SAMA5D4 Watchdog Timer is selected.

Watchdog Timer is selected

Back to Top

Rootfs

User Space: The following device node will be used to access the watchdog driver in userspace: /dev/watchdog.

Back to Top

Hands On

As shown above, all necessary functions for the watchdog timer have been selected in the configuration. All that’s needed is to launch and feed the watchdog timer with Busybox’s watchdog command.

Usage of watchdog command:

# watchdog
BusyBox v1.27.2 (2019-04-26 11:28:56 CST) multi-call binary.

Usage: watchdog [-t N[ms]] [-T N[ms]] [-F] DEV

Periodically write to watchdog device DEV

      -T N    Reboot after N seconds if not reset (default 60)
      -t N    Reset every N seconds (default 30)
      -F      Run in foreground

Use 500ms to specify period in milliseconds

Execute the following command to launch the watchdog manually, set the reboot time as 10 seconds, and reset the time as 5 seconds. (The watchdog command will feed the watchdog timer automatically):

# watchdog -T 10 -t 5 /dev/watchdog

Or you may add an init script for the watchdog timer. It will be launched and fed automatically after system boot-up. Configure Buildroot and select the Install the watchdog daemon startup script feature:

$ make menuconfig

Select Install the watchdog daemon startup script

Rebuild Buildroot with the following command:

$ make

A new init script for the watchdog timer will be generated as S15watchdog in /etc/init.d:

#!/bin/sh
#
# Start watchdog
#
case "$1" in
 start)
        echo "Starting watchdog..."
        watchdog -t 5 /dev/watchdog
        ;;
  stop)
        ;;
  restart|reload)
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac
exit $?

Back to Top

How to Verify the Watchdog Will Reboot the System

With default settings, Busybox’s watchdog command will run in the background.

Please do not try to kill the watchdog command to verify that the system reboots. You may use the following source code to find that the watchdog timer will be disabled before a task exits:

/output/build/busybox-1.27.2/miscutils/watchdog.c

You can use the -F option to make Busybox’s watchdog command run in the foreground and use Ctrl + z to suspend the command. In this case, the watchdog timer was enabled but won’t be fed anymore. After a few seconds (maximum 16 seconds), the system will be reset by the watchdog.

First, use the killall command to kill the watchdog command which is running in the background and then execute it in the foreground:

# killall watchdog
# watchdog -t 5 /dev/watchdog -F
watchdog: WDIOC_SETTIMEOUT: Invalid argument
^Z[1]+  Stopped      watchdog -t 5 /dev/watchdog -F

Input Ctrl + z from the Linux command line

# RomBOOT

AT91Bootstrap 3.8.11 (Fri Apr 26 11:49:16 CST 2019)

SD/MMC: Image: Read file u-boot.bin to 0x23f00000
MMC: ADMA supported
SD: Card Capacity: High or Extended
SD: Specification Version 3.0X
SD/MMC: Done to load image

Or you can put the system into Sleep mode. The system will reboot within seconds of entering Sleep mode.

Note: If the watchdog timer atmel,idle-halt property is enabled in the device tree, the system will not reboot after entering Sleep mode. This property is not enabled with the default settings. 

# echo mem > /sys/power/state
PM: suspend entry (deep)
PM: Syncing filesystems ... done.
Freezing user space processes ... (elapsed 0.000 seconds) done.
OOM killer disabled.
Freezing remaining freezable tasks ... (elapsed 0.001 seconds) done.
Suspending console(s) (use no_console_suspend to debug)
RomBOOT

AT91Bootstrap 3.8.11 (Fri Apr 26 11:49:16 CST 2019)

SD/MMC: Image: Read file u-boot.bin to 0x23f00000
MMC: ADMA supported
SD: Card Capacity: High or Extended
SD: Specification Version 3.0X
SD/MMC: Done to load image

Back to Top


Summary

In this article, you used Buildroot to build an image with watchdog timer support for the ATSAMA5D2 Series MPU. You walked through the Device Tree and Kernel to observe how the embedded Linux system configures the source code for building. Finally, you tried some hands-on exercises to see the watchdog in action.

Back to Top