Applications - Watchdog Timer

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 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.


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 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-block-diagram.png

Buildroot Configuration

Objective: Using Buildroot, build a bootable image and FLASH 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.


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 include files (DTSI and DTS) are located in the Buildroot output directory: /output/build/linux-linux4sam_6.0/arch/arm/boot/dts/.

1

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, 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.

2

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.


Kernel

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

1

From the buildroot directory, run the Linux kernel menuconfig:

$ make linux-menuconfig

The top-level menu will be displayed:

linux-config-top-level.png

Device Driver

2

Select Device Drivers - - ->

3

Select [*] Watchdog Timer Support - - ->

4

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

atmel-watchdog-timer.png

Rootfs

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


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
watchdog-script.png

Rebuild Buildroot with 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 $?

How to Verify the Watchdog Will Reboot the System

1. 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 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

2. 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

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.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.