Skip to content

Commit e936ed1

Browse files
committed
Add watchdog support for Portenta H7 on Arduino IoT Cloud.
1 parent 339d10f commit e936ed1

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
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_MBED
31+
# include <watchdog_api.h>
32+
# define PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms (32760)
33+
#endif /* ARDUINO_ARCH_MBED */
34+
2435
/******************************************************************************
2536
* GLOBAL VARIABLES
2637
******************************************************************************/
@@ -56,3 +67,29 @@ void wifi_nina_feed_watchdog()
5667
samd_watchdog_reset();
5768
}
5869
#endif /* ARDUINO_ARCH_SAMD */
70+
71+
#ifdef ARDUINO_ARCH_MBED
72+
void mbed_watchdog_enable()
73+
{
74+
watchdog_config_t cfg;
75+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M)
76+
cfg.timeout_ms = PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms;
77+
#else
78+
# error "You need to define the maximum possible timeout for this architecture."
79+
#endif
80+
81+
if (hal_watchdog_init(&cfg) == WATCHDOG_STATUS_OK) {
82+
is_watchdog_enabled = true;
83+
}
84+
else {
85+
DEBUG_WARNING("%s: watchdog could not be enabled", __FUNCTION__);
86+
}
87+
}
88+
89+
void mbed_watchdog_reset()
90+
{
91+
if (is_watchdog_enabled) {
92+
hal_watchdog_kick();
93+
}
94+
}
95+
#endif /* ARDUINO_ARCH_MBED */

src/utility/watchdog/Watchdog.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +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-
2721
/******************************************************************************
2822
* PREPROCESSOR SECTION
2923
******************************************************************************/
@@ -42,4 +36,9 @@ void samd_watchdog_enable();
4236
void samd_watchdog_reset();
4337
#endif /* ARDUINO_ARCH_SAMD */
4438

39+
#ifdef ARDUINO_ARCH_MBED
40+
void mbed_watchdog_enable();
41+
void mbed_watchdog_reset();
42+
#endif /* ARDUINO_ARCH_MBED */
43+
4544
#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */

0 commit comments

Comments
 (0)