From ab363fde4fe084afcc557f746597086e864d4ec2 Mon Sep 17 00:00:00 2001
From: Jeff Gough <gitlab@jeffreygough.com>
Date: Sat, 24 Aug 2019 06:54:41 +0000
Subject: [PATCH] feat(epicardium): Enable hardware-watchdog timer

---
 epicardium/main.c             | 10 ++--------
 epicardium/modules/hardware.c | 22 ++++------------------
 epicardium/modules/modules.h  |  1 +
 epicardium/modules/watchdog.c | 19 +++++++++++++++++++
 4 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/epicardium/main.c b/epicardium/main.c
index c7f58a05c..84dea0319 100644
--- a/epicardium/main.c
+++ b/epicardium/main.c
@@ -12,6 +12,8 @@
 
 int main(void)
 {
+	watchdog_init();
+
 	LOG_INFO("startup", "Epicardium startup ...");
 	LOG_INFO("startup", "Version " CARD10_VERSION);
 
@@ -127,14 +129,6 @@ int main(void)
 		abort();
 	}
 
-	/* Watchdog petting */
-#if 0
-	/*
-	 * Disabled for this release.
-	 */
-	watchdog_clearer_init();
-#endif
-
 	LOG_DEBUG("startup", "Starting FreeRTOS ...");
 	vTaskStartScheduler();
 
diff --git a/epicardium/modules/hardware.c b/epicardium/modules/hardware.c
index 6148513eb..cb983dae9 100644
--- a/epicardium/modules/hardware.c
+++ b/epicardium/modules/hardware.c
@@ -32,24 +32,7 @@ int hardware_early_init(void)
 	/*
 	 * Watchdog timer
 	 */
-#if 0
-	/*
-	 * Disabled for this release.
-	 */
-	sys_cfg_wdt_t wdt_cfg = NULL;
-	WDT_Init(MXC_WDT0, wdt_cfg);
-
-	if (WDT_GetResetFlag(MXC_WDT0)) {
-		WDT_ClearResetFlag(MXC_WDT0);
-		LOG_INFO("watchdog", "Reset due to watchdog timeout");
-	}
-
-	WDT_Enable(MXC_WDT0, 1);
-	WDT_SetResetPeriod(
-		MXC_WDT0,
-		WDT_PERIOD_2_27); /* Clocked by PCLK at 50MHz, reset at 2^27 ticks = 2.7 seconds */
-	WDT_EnableReset(MXC_WDT0, 1);
-#endif
+	watchdog_init();
 
 	/*
 	 * I2C bus for onboard peripherals (ie. PMIC, BMA400, BHI160, BME680,
@@ -207,6 +190,9 @@ int hardware_early_init(void)
  */
 int hardware_init(void)
 {
+	/* Watchdog clearer software timer */
+	watchdog_clearer_init();
+
 	/* Light Sensor */
 	LOG_DEBUG("init", "Starting light sensor ...");
 	epic_light_sensor_run();
diff --git a/epicardium/modules/modules.h b/epicardium/modules/modules.h
index 144b18fb7..9eea0cb61 100644
--- a/epicardium/modules/modules.h
+++ b/epicardium/modules/modules.h
@@ -36,6 +36,7 @@ int personal_state_enabled();
 void vPmicTask(void *pvParameters);
 
 /* ---------- Watchdog ----------------------------------------------------- */
+void watchdog_init();
 void watchdog_clearer_init();
 
 /* Critical battery voltage */
diff --git a/epicardium/modules/watchdog.c b/epicardium/modules/watchdog.c
index a538035d6..7256d21b5 100644
--- a/epicardium/modules/watchdog.c
+++ b/epicardium/modules/watchdog.c
@@ -14,8 +14,27 @@ static void watchdog_clearer_callback()
 	WDT_ResetTimer(MXC_WDT0);
 }
 
+void watchdog_init()
+{
+	sys_cfg_wdt_t wdt_cfg = NULL;
+	WDT_Init(MXC_WDT0, wdt_cfg);
+
+	if (WDT_GetResetFlag(MXC_WDT0)) {
+		WDT_ClearResetFlag(MXC_WDT0);
+		LOG_INFO("watchdog", "Last reset was due to watchdog timeout");
+	}
+
+	WDT_Enable(MXC_WDT0, 1);
+	WDT_SetResetPeriod(
+		MXC_WDT0,
+		WDT_PERIOD_2_28); /* Clocked by PCLK at 50MHz, reset at 2^28 ticks = 5.4 seconds */
+	WDT_EnableReset(MXC_WDT0, 1);
+}
+
 void watchdog_clearer_init()
 {
+	WDT_ResetTimer(MXC_WDT0);
+
 	clearer_timer = xTimerCreateStatic(
 		"watchdog_clearer_timer",
 		CLEAR_PERIOD,
-- 
GitLab