The goal of the document is to illustrate how 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. 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 feeds watchdog periodically. Please follow the following steps.
1. Download source files of SP7021
Source files of SP7021 can be downloaded from GitHub. Refer to https://github.com/sunplus-plus1/SP7021 or 2. HOW TO GET SOURCE FILE AND PACKAGE.
2. Enable watchdog driver
Run make kconfig command in project top folder, or go to linux/kernel folder and run make menuconfig command. When “Linux/arm 5.4.35 Kernel Configuration” menu pops up, please use arrow key to move cursor down to “Device Drivers”. Refer to screenshot below, cursor is moved to “Device Drivers”:
Press <Enter> to enter “Device Driver” sub-menu. Next, move cursor down to “Watchdog Timer Support” and press <Space> or “y” to enable it. Refer to screenshot below, “Watchdog Timer Support” is enabled:
Next, press <Enter> to enter “Watchdog Timer Support” sub-menu. Next, move cursor down to “Sunplus watchdog support” and press <Space> or “y” to enable it. Refer to screenshot below, “Sunplus watchdog support” is enabled:
Finally, move cursor to <Save> button and press <Enter> to save configuration.
3. Build Linux image
Go to project top folder. Run make to build SP7021 Linux images.
4. Boot system
Boot system with the built image. You can find watchdog device in directory /dev by using ls -l /dev command:
~ # 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 feeding application
Refer to the following C example code which setups and feeds watchdog every 10 seconds. Note that the maximum feed time of watchdog of SP7021 is about 11 seconds.
#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 ret; int fd; int feed_time = 8; void* feedwdt_thd() { while (1) { ret = ioctl(fd, WDIOC_KEEPALIVE, 0); if (ret != 0) { printf("Failed to feed watchdog!\n"); close(fd); } else { printf("Feed watchdog every %d seconds.\n", feed_time); } sleep(feed_time); } } int main(void) { pthread_t watchdogThd; int timeout = 10; int option_en = WDIOS_ENABLECARD; int option_dis = WDIOS_DISABLECARD; char str[10]; int num = 0; char *ptr = str; fd = open("/dev/watchdog0", O_WRONLY); if (fd == -1) { perror("watchdog"); exit(EXIT_FAILURE); } ret = ioctl(fd, WDIOC_SETOPTIONS, &option_en); if (ret != 0){ printf("Failed to start watchdog!\n"); close(fd); return -1; } ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout); if (ret != 0){ printf("Failed to set timeout time!\n"); close(fd); return -1; } ret = pthread_create(&watchdogThd, NULL, feedwdt_thd, NULL); if (ret< 0) printf("Failed to create feeddog thread!\n"); while (1) { printf("If you want to quit, please press 'e' character!\n"); printf("If you want to modify the feeddog time, please input the val (default 8, timeout is 10)!\n"); scanf("%s", str); if (!strcmp(str,"e")) { ret = ioctl(fd, WDIOC_SETOPTIONS, &option_dis); if (ret != 0){ printf("Failed to stop watchdog!\n"); } printf("Stopped watchdog!\n"); close(fd); break; } if (strspn(str, "0123456789")==strlen(str)){ while (*ptr) { num *= 10; num += *ptr - '0'; ptr++; } feed_time = num; } else { printf("Invalid timeout time!\n"); } } return 0; }
Reference Makefile:
CC=/home/your_home_dir/you_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
Add Comment