Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The goal of the document is to illustrate how to enable and use watchdog of SP7021.

As an embedded system, to minimize the use of system resources, SP7021 Linux only enables necessary kernel features by default. Watchdog of SP7021 is not enabled by default. To enable watchdog, first, you need to download source files of SP7021 from GitHub. Second, you need to run kernel menuconfig to enable watchdog drivers. Third, you need to re-build SP7021 Linux images. Finally, you need to build and run an application which activates and feeds watchdog periodically. Please follow the following steps.

...

Source files of SP7021 can be downloaded from GitHub or Yocto server of SP7021. Refer to https://github.com/sunplus-plus1/SP7021 or 2. HOW TO GET SOURCE FILE AND PACKAGE.

...

Boot system with the built image. You can find watchdog device devices in directory /dev by using ls -l /dev command:

Code Block
~ # ls -l /dev/watchdog*
crw-rw----    1 root     root       10, 130 Jan  1 00:00 /dev/watchdog
crw-rw----    1 root     root      252,   0 Jan  1 00:00 /dev/watchdog0
~ #

5. Run watchdog setup and feeding application

Refer to the following C example code which setups and of watchdog setup and feeding application. The application setups, starts, and then feeds watchdog every 10 seconds. Note that the maximum feed time-out time of watchdog of SP7021 is now about 11 seconds. Any value higher than 11 is invalid.

watchdog.c

Code Block
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>

#define WATCHDOG_IOCTL_BASE     'W'

#define WDIOC_SETOPTIONS        _IOR(WATCHDOG_IOCTL_BASE, 4, int)
#define WDIOC_KEEPALIVE         _IOR(WATCHDOG_IOCTL_BASE, 5, int)
#define WDIOC_SETTIMEOUT        _IOWR(WATCHDOG_IOCTL_BASE, 6, int)

#define WDIOS_DISABLECARD       1
#define WDIOS_ENABLECARD        2

int retwd_fd;
int fd;wd_feed_time = 8;           // Feed watchdog every 8 second by default.
int feedwd_timetimeout = 8;

10;            // Timeout time of watchdog. Reset event occurs if watchdog is not fed within this time.


// A thread for feeding watchdog.
void* feedwdt_thd()
{
	int ret;

	while (1) {
		// Feed (reset) watchdog.
		ret = ioctl(wd_fd, WDIOC_KEEPALIVE, 0);
		if (ret != 0) {
	    		printf("Failed to feed watchdog!\n");
	    		close(wd_fd);
		} else {
	    		printf("Feed watchdog every %d seconds.\n", wd_feed_time);
		}

		sleep(wd_feed_time);
	}
}

int main(void)
{
	pthread_t watchdogThdwd_thread;
	int timeout = 10;
	int option_en = WDIOS_ENABLECARD;
	int option_dis = WDIOS_DISABLECARDwd_option;
	char str[10];
	int num = 0;
	int ret;

	char *ptr = str;

	// Get file handle of watchdog device.
	wd_fd = open("/dev/watchdog0", O_WRONLY);
	if (wd_fd == -1) {
		perror("watchdog");
		exit(EXIT_FAILURE);
	}

	// Enable wacthdog.
	wd_option = WDIOS_ENABLECARD;
	ret = ioctl(wd_fd, WDIOC_SETOPTIONS, &wd_option_en);
	if (ret != 0){
		printf("Failed to start watchdog!\n");
		close(wd_fd);
		return -1;
	}

	// Setup timeout time.
	ret = ioctl(wd_fd, WDIOC_SETTIMEOUT, &wd_timeout);
	if (ret != 0){
		printf("Failed to set timeout time!\n");
		close(wd_fd);
		return -1;
	}

	// Create a thread for feeding watchdog.
	ret = pthread_create(&watchdogThdwd_thread, NULL, feedwdt_thd, NULL);
	if (ret< 0)
	printf("Failed to create feeddog thread!\n");

	while (1) {
		printf("If you wantPress 'e' to quit,disable pleasewatchdog press 'e' character!and exit.\n");
		printf("IfEnter youa wantnumber tofor modifynew the feeddogfeed time, please(default inputfeed thetime valis (default8 8seconds, timeout time is 10 seconds)!:\n");
		scanf("%s", str);

		if (!strcmp(str,"e")) {
			// Disable watchdog.
			wd_option = WDIOS_DISABLECARD;
			ret = ioctl(wd_fd, WDIOC_SETOPTIONS, &wd_option_dis);
			if (ret != 0) {
				printf("Failed to stop watchdog!\n");
			}
			printf("StoppedDisabled watchdog!\n");
			close(wd_fd);
			break;
		}

		if (strspn(str, "0123456789") == strlen(str)) {
			while (*ptr) {
	// Get new feed time.
			num *= 10atoi(str);
				if (num += *ptr - '0';> 0) {
				ptr++wd_feed_time = num;
			} else {
				feed_time = num;printf("Feed time can not be zero!\n");
			}
		} else {
			printf("Invalid timeoutfeed time!\n");
		}
	}

	return 0;
}

Reference Makefile:

Code Block
CC=/home/your_home_dir/youyour_sp7021_project_folder/crossgcc/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
LDFLAGS=-lpthread

.PHONY: all clean
all: watchdog

watchdog: watchdog.c
	$(CC) $< -o $@ $(LDFLAGS)

clean:
	rm -f watchdog

...