Skip to content

Commit 8e9220f

Browse files
authored
Merge pull request #239 from arduino-libraries/mbed-enable-watchdog
Enable watchdog for ArduinoCore-mbed based boards
2 parents 339d10f + 02e0f30 commit 8e9220f

File tree

3 files changed

+61
-19
lines changed

3 files changed

+61
-19
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,28 +269,35 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
269269
}
270270
#endif /* BOARD_HAS_OFFLOADED_ECCX08 */
271271

272-
#ifdef ARDUINO_ARCH_SAMD
273272
/* Since we do not control what code the user inserts
274273
* between ArduinoIoTCloudTCP::begin() and the first
275274
* call to ArduinoIoTCloudTCP::update() it is wise to
276275
* set a rather large timeout at first.
277276
*/
277+
#ifdef ARDUINO_ARCH_SAMD
278278
if (enable_watchdog) {
279279
samd_watchdog_enable();
280280
}
281-
#endif /* ARDUINO_ARCH_SAMD */
281+
#elif defined(ARDUINO_ARCH_MBED)
282+
if (enable_watchdog) {
283+
mbed_watchdog_enable();
284+
}
285+
#endif
282286

283287
return 1;
284288
}
285289

286290
void ArduinoIoTCloudTCP::update()
287291
{
288-
#ifdef ARDUINO_ARCH_SAMD
289292
/* Feed the watchdog. If any of the functions called below
290293
* get stuck than we can at least reset and recover.
291294
*/
295+
#ifdef ARDUINO_ARCH_SAMD
292296
samd_watchdog_reset();
293-
#endif /* ARDUINO_ARCH_SAMD */
297+
#elif defined(ARDUINO_ARCH_MBED)
298+
mbed_watchdog_reset();
299+
#endif
300+
294301

295302
/* Run through the state machine. */
296303
State next_state = _state;

src/utility/watchdog/Watchdog.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121

2222
#include "Watchdog.h"
2323

24+
#include <AIoTC_Config.h>
25+
26+
#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE)
27+
# include <Arduino_DebugUtils.h>
28+
#endif
29+
30+
#ifdef ARDUINO_ARCH_SAMD
31+
# include <Adafruit_SleepyDog.h>
32+
# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000)
33+
#endif /* ARDUINO_ARCH_SAMD */
34+
35+
#ifdef ARDUINO_ARCH_MBED
36+
# include <watchdog_api.h>
37+
# define PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms (32760)
38+
# define NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms (32760)
39+
#endif /* ARDUINO_ARCH_MBED */
40+
2441
/******************************************************************************
2542
* GLOBAL VARIABLES
2643
******************************************************************************/
@@ -56,3 +73,31 @@ void wifi_nina_feed_watchdog()
5673
samd_watchdog_reset();
5774
}
5875
#endif /* ARDUINO_ARCH_SAMD */
76+
77+
#ifdef ARDUINO_ARCH_MBED
78+
void mbed_watchdog_enable()
79+
{
80+
watchdog_config_t cfg;
81+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
82+
cfg.timeout_ms = PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms;
83+
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
84+
cfg.timeout_ms = NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms;
85+
#else
86+
# error "You need to define the maximum possible timeout for this architecture."
87+
#endif
88+
89+
if (hal_watchdog_init(&cfg) == WATCHDOG_STATUS_OK) {
90+
is_watchdog_enabled = true;
91+
}
92+
else {
93+
DEBUG_WARNING("%s: watchdog could not be enabled", __FUNCTION__);
94+
}
95+
}
96+
97+
void mbed_watchdog_reset()
98+
{
99+
if (is_watchdog_enabled) {
100+
hal_watchdog_kick();
101+
}
102+
}
103+
#endif /* ARDUINO_ARCH_MBED */

src/utility/watchdog/Watchdog.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@
1818
#ifndef ARDUINO_AIOTC_UTILITY_WATCHDOG_H_
1919
#define ARDUINO_AIOTC_UTILITY_WATCHDOG_H_
2020

21-
/******************************************************************************
22-
* INCLUDE
23-
******************************************************************************/
24-
25-
#include <AIoTC_Config.h>
26-
27-
/******************************************************************************
28-
* PREPROCESSOR SECTION
29-
******************************************************************************/
30-
31-
#ifdef ARDUINO_ARCH_SAMD
32-
# include <Adafruit_SleepyDog.h>
33-
# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000)
34-
#endif /* ARDUINO_ARCH_SAMD */
35-
3621
/******************************************************************************
3722
* FUNCTION DECLARATION
3823
******************************************************************************/
@@ -42,4 +27,9 @@ void samd_watchdog_enable();
4227
void samd_watchdog_reset();
4328
#endif /* ARDUINO_ARCH_SAMD */
4429

30+
#ifdef ARDUINO_ARCH_MBED
31+
void mbed_watchdog_enable();
32+
void mbed_watchdog_reset();
33+
#endif /* ARDUINO_ARCH_MBED */
34+
4535
#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */

0 commit comments

Comments
 (0)