From 2f05624545bc6d31e402e04fc9f1f7f786a7972e Mon Sep 17 00:00:00 2001
From: jfbblue0922 <jfbblue0922@gmail.com>
Date: Thu, 11 May 2023 16:06:28 +0900
Subject: [PATCH] AP_HAL_ChibiOS: support external watchdog gpio

Co-authored-by: Randy Mackay <rmackay9@yahoo.com>

optional support to toggle GPIO pin at 10hz
---
 libraries/AP_HAL_ChibiOS/Scheduler.cpp | 15 +++++++++++++++
 libraries/AP_HAL_ChibiOS/Scheduler.h   |  6 ++++++
 2 files changed, 21 insertions(+)

diff --git a/libraries/AP_HAL_ChibiOS/Scheduler.cpp b/libraries/AP_HAL_ChibiOS/Scheduler.cpp
index 92e252e190..60d134581c 100644
--- a/libraries/AP_HAL_ChibiOS/Scheduler.cpp
+++ b/libraries/AP_HAL_ChibiOS/Scheduler.cpp
@@ -765,8 +765,23 @@ void Scheduler::watchdog_pat(void)
 {
     stm32_watchdog_pat();
     last_watchdog_pat_ms = AP_HAL::millis();
+#if defined(HAL_GPIO_PIN_EXT_WDOG)
+    ext_watchdog_pat(last_watchdog_pat_ms);
+#endif
 }
 
+#if defined(HAL_GPIO_PIN_EXT_WDOG)
+// toggle the external watchdog gpio pin
+void Scheduler::ext_watchdog_pat(uint32_t now_ms)
+{
+    // toggle watchdog GPIO every WDI_OUT_INTERVAL_TIME_MS
+    if ((now_ms - last_ext_watchdog_ms) >= EXT_WDOG_INTERVAL_MS) {
+        palToggleLine(HAL_GPIO_PIN_EXT_WDOG);
+        last_ext_watchdog_ms = now_ms;
+    }
+}
+#endif
+
 #if CH_DBG_ENABLE_STACK_CHECK == TRUE
 /*
   check we have enough stack free on all threads and the ISR stack
diff --git a/libraries/AP_HAL_ChibiOS/Scheduler.h b/libraries/AP_HAL_ChibiOS/Scheduler.h
index 2a7187e86c..b46b26f7af 100644
--- a/libraries/AP_HAL_ChibiOS/Scheduler.h
+++ b/libraries/AP_HAL_ChibiOS/Scheduler.h
@@ -193,5 +193,11 @@ private:
 
     // check for free stack space
     void check_stack_free(void);
+
+#if defined(HAL_GPIO_PIN_EXT_WDOG)
+    // external watchdog GPIO support
+    void ext_watchdog_pat(uint32_t now_ms);
+    uint32_t last_ext_watchdog_ms;
+#endif
 };
 #endif