boot: nuttx: Support application specific
wdg initialization.

Signed-off-by: Andrés Sánchez Pascual <tito97_sp@hotmail.com>
diff --git a/boot/nuttx/include/mcuboot_config/mcuboot_config.h b/boot/nuttx/include/mcuboot_config/mcuboot_config.h
index 6a8eb22..286a387 100644
--- a/boot/nuttx/include/mcuboot_config/mcuboot_config.h
+++ b/boot/nuttx/include/mcuboot_config/mcuboot_config.h
@@ -181,6 +181,15 @@
  */
 
 #ifdef CONFIG_MCUBOOT_WATCHDOG
+
+#ifndef CONFIG_MCUBOOT_WATCHDOG_DEVPATH
+#  define CONFIG_MCUBOOT_WATCHDOG_DEVPATH "/dev/watchdog0"
+#endif
+
+#ifndef CONFIG_MCUBOOT_WATCHDOG_TIMEOUT
+#  define CONFIG_MCUBOOT_WATCHDOG_TIMEOUT 10000      /* Watchdog timeout in ms */
+#endif
+
 #  define MCUBOOT_WATCHDOG_FEED()       do                           \
                                           {                          \
                                             mcuboot_watchdog_feed(); \
diff --git a/boot/nuttx/include/watchdog/watchdog.h b/boot/nuttx/include/watchdog/watchdog.h
index 01cf31d..b75b7f6 100644
--- a/boot/nuttx/include/watchdog/watchdog.h
+++ b/boot/nuttx/include/watchdog/watchdog.h
@@ -41,4 +41,25 @@
 
 void mcuboot_watchdog_feed(void);
 
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: mcuboot_watchdog_init
+ *
+ * Description:
+ *   Initialize the watchdog timer by setting the trigger timeout and 
+ *   starting it.
+ *
+ * Input Parameters:
+ *   None.
+ *
+ * Returned Value:
+ *   OK on success, ERROR if not.
+ *
+ ****************************************************************************/
+
+int mcuboot_watchdog_init(void);
+
 #endif /* __BOOT_NUTTX_INCLUDE_WATCHDOG_WATCHDOG_H */
diff --git a/boot/nuttx/main.c b/boot/nuttx/main.c
index 8973053..7921aa6 100644
--- a/boot/nuttx/main.c
+++ b/boot/nuttx/main.c
@@ -114,6 +114,15 @@
 
   syslog(LOG_INFO, "*** Booting MCUboot build %s ***\n", CONFIG_MCUBOOT_VERSION);
 
+#ifdef CONFIG_MCUBOOT_WATCHDOG
+  int ret = mcuboot_watchdog_init();
+  if (ret < 0)
+  {
+    syslog(LOG_ERR, "Unable to initialize the watchdog timer\n");
+    FIH_PANIC;
+  }
+#endif
+
   FIH_CALL(boot_go, fih_rc, &rsp);
 
   if (fih_not_eq(fih_rc, FIH_SUCCESS))
diff --git a/boot/nuttx/src/watchdog/watchdog.c b/boot/nuttx/src/watchdog/watchdog.c
index 12f2aee..59f4587 100644
--- a/boot/nuttx/src/watchdog/watchdog.c
+++ b/boot/nuttx/src/watchdog/watchdog.c
@@ -74,3 +74,57 @@
 
   close(fd);
 }
+
+/****************************************************************************
+ * Name: mcuboot_watchdog_init
+ *
+ * Description:
+ *   Initialize the watchdog timer by setting the trigger timeout and 
+ *   starting it.
+ *
+ * Input Parameters:
+ *   None.
+ *
+ * Returned Value:
+ *   OK on success, ERROR if not.
+ *
+ ****************************************************************************/
+
+int mcuboot_watchdog_init(void)
+{
+  int fd;
+  int ret;
+
+  fd = open(CONFIG_MCUBOOT_WATCHDOG_DEVPATH, O_RDONLY);
+  if (fd < 0)
+    {
+      BOOT_LOG_ERR("Failed to open %s", CONFIG_MCUBOOT_WATCHDOG_DEVPATH);
+      goto errout;
+    }
+
+  ret = ioctl(fd, WDIOC_SETTIMEOUT, (unsigned long)CONFIG_MCUBOOT_WATCHDOG_TIMEOUT);
+  if (ret < 0)
+    {
+      int errcode = errno;
+
+      BOOT_LOG_ERR("Failed to set timeout in watchdog device: %d", errcode);
+      goto errout_with_dev;
+    }
+
+  ret = ioctl(fd, WDIOC_START, 0);
+  if (ret < 0)
+    {
+      int errcode = errno;
+
+      BOOT_LOG_ERR("Failed to start watchdog device: %d", errcode);
+      goto errout_with_dev;
+    }
+
+  close(fd);
+  return OK;
+  
+errout_with_dev:
+  close(fd);
+errout:
+  return ERROR;
+}