FAR void *watchdog_register(FAR const char *path, FAR struct watchdog_lowerhalf_s *lower) { FAR struct watchdog_upperhalf_s *upper; int ret; DEBUGASSERT(path && lower); wdinfo("Entry: path=%s\n", path); /* Allocate the upper-half data structure */ upper = (FAR struct watchdog_upperhalf_s *) kmm_zalloc(sizeof(struct watchdog_upperhalf_s)); if (!upper) { wderr("Upper half allocation failed\n"); goto errout; } /* Initialize the watchdog timer device structure (it was already zeroed * by kmm_zalloc()). */ sem_init(&upper->exclsem, 0, 1); upper->lower = lower; /* Copy the registration path */ upper->path = strdup(path); if (!upper->path) { wderr("Path allocation failed\n"); goto errout_with_upper; } /* Register the watchdog timer device */ ret = register_driver(path, &g_wdogops, 0666, upper); if (ret < 0) { wderr("register_driver failed: %d\n", ret); goto errout_with_path; } return (FAR void *)upper; errout_with_path: kmm_free(upper->path); errout_with_upper: sem_destroy(&upper->exclsem); kmm_free(upper); errout: return NULL; }
static int wdog_daemon(int argc, char *argv[]) { FAR struct file filestruct; int ret; /* Open the watchdog device for reading */ wdinfo("Opening.\n"); ret = file_open(&filestruct, CONFIG_WATCHDOG_DEVPATH, O_RDONLY); if (ret < 0) { wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, ret); goto errout; } /* Start the watchdog timer. */ wdinfo("Starting.\n"); ret = file_ioctl(&filestruct, WDIOC_START, 0); if (ret < 0) { wderr("ERROR: ioctl(WDIOC_START) failed: %d\n", errno); goto errout_with_dev; } nxsig_usleep(200); while(1) { nxsig_usleep((CONFIG_WDT_THREAD_INTERVAL)*1000); wdinfo("ping\n"); ret = file_ioctl(&filestruct, WDIOC_KEEPALIVE, 0); if (ret < 0) { wderr("ERROR: ioctl(WDIOC_KEEPALIVE) failed: %d\n", errno); goto errout_with_dev; } } errout_with_dev: file_close(&filestruct); errout: return ret; }
int sam_watchdog_initialize(void) { #if (defined(CONFIG_SAM34_WDT) && !defined(CONFIG_WDT_DISABLE_ON_RESET)) FAR struct file filestruct; int ret; /* Initialize tha register the watchdog timer device */ wdinfo("Initializing Watchdog driver...\n"); sam_wdtinitialize(CONFIG_WATCHDOG_DEVPATH); /* Open the watchdog device */ wdinfo("Opening.\n"); ret = file_open(&filestruct, CONFIG_WATCHDOG_DEVPATH, O_RDONLY); if (ret < 0) { wderr("ERROR: open %s failed: %d\n", CONFIG_WATCHDOG_DEVPATH, ret); goto errout; } /* Set the watchdog timeout */ wdinfo("Timeout = %d.\n", CONFIG_WDT_TIMEOUT); ret = file_ioctl(&filestruct, WDIOC_SETTIMEOUT, (unsigned long)CONFIG_WDT_TIMEOUT); if (ret < 0) { wderr("ERROR: ioctl(WDIOC_SETTIMEOUT) failed: %d\n", errno); goto errout_with_dev; } /* Set the watchdog minimum time */ wdinfo("MinTime = %d.\n", CONFIG_WDT_MINTIME); ret = file_ioctl(&filestruct, WDIOC_MINTIME, (unsigned long)CONFIG_WDT_MINTIME); if (ret < 0) { wderr("ERROR: ioctl(WDIOC_MINTIME) failed: %d\n", errno); goto errout_with_dev; } /* Start Kicker task */ #if defined(CONFIG_WDT_THREAD) sched_lock(); int taskid = kthread_create(CONFIG_WDT_THREAD_NAME, CONFIG_WDT_THREAD_PRIORITY, CONFIG_WDT_THREAD_STACKSIZE, (main_t)wdog_daemon, (FAR char * const *)NULL); DEBUGASSERT(taskid > 0); UNUSED(taskid); sched_unlock(); #endif return OK; errout_with_dev: file_close(&filestruct); errout: return ret; #else return -ENODEV; #endif }