Skip to content

Enable watchdog for ArduinoCore-mbed based boards #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,28 +269,35 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
}
#endif /* BOARD_HAS_OFFLOADED_ECCX08 */

#ifdef ARDUINO_ARCH_SAMD
/* Since we do not control what code the user inserts
* between ArduinoIoTCloudTCP::begin() and the first
* call to ArduinoIoTCloudTCP::update() it is wise to
* set a rather large timeout at first.
*/
#ifdef ARDUINO_ARCH_SAMD
if (enable_watchdog) {
samd_watchdog_enable();
}
#endif /* ARDUINO_ARCH_SAMD */
#elif defined(ARDUINO_ARCH_MBED)
if (enable_watchdog) {
mbed_watchdog_enable();
}
#endif

return 1;
}

void ArduinoIoTCloudTCP::update()
{
#ifdef ARDUINO_ARCH_SAMD
/* Feed the watchdog. If any of the functions called below
* get stuck than we can at least reset and recover.
*/
#ifdef ARDUINO_ARCH_SAMD
samd_watchdog_reset();
#endif /* ARDUINO_ARCH_SAMD */
#elif defined(ARDUINO_ARCH_MBED)
mbed_watchdog_reset();
#endif


/* Run through the state machine. */
State next_state = _state;
Expand Down
45 changes: 45 additions & 0 deletions src/utility/watchdog/Watchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@

#include "Watchdog.h"

#include <AIoTC_Config.h>

#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE)
# include <Arduino_DebugUtils.h>
#endif

#ifdef ARDUINO_ARCH_SAMD
# include <Adafruit_SleepyDog.h>
# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000)
#endif /* ARDUINO_ARCH_SAMD */

#ifdef ARDUINO_ARCH_MBED
# include <watchdog_api.h>
# define PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms (32760)
# define NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms (32760)
#endif /* ARDUINO_ARCH_MBED */

/******************************************************************************
* GLOBAL VARIABLES
******************************************************************************/
Expand Down Expand Up @@ -56,3 +73,31 @@ void wifi_nina_feed_watchdog()
samd_watchdog_reset();
}
#endif /* ARDUINO_ARCH_SAMD */

#ifdef ARDUINO_ARCH_MBED
void mbed_watchdog_enable()
{
watchdog_config_t cfg;
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
cfg.timeout_ms = PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms;
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
cfg.timeout_ms = NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms;
#else
# error "You need to define the maximum possible timeout for this architecture."
#endif

if (hal_watchdog_init(&cfg) == WATCHDOG_STATUS_OK) {
is_watchdog_enabled = true;
}
else {
DEBUG_WARNING("%s: watchdog could not be enabled", __FUNCTION__);
}
}

void mbed_watchdog_reset()
{
if (is_watchdog_enabled) {
hal_watchdog_kick();
}
}
#endif /* ARDUINO_ARCH_MBED */
20 changes: 5 additions & 15 deletions src/utility/watchdog/Watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@
#ifndef ARDUINO_AIOTC_UTILITY_WATCHDOG_H_
#define ARDUINO_AIOTC_UTILITY_WATCHDOG_H_

/******************************************************************************
* INCLUDE
******************************************************************************/

#include <AIoTC_Config.h>

/******************************************************************************
* PREPROCESSOR SECTION
******************************************************************************/

#ifdef ARDUINO_ARCH_SAMD
# include <Adafruit_SleepyDog.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is sleepydog included somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's the only spot. But this isn't a change particular to this PR, I simply moved the inclusion from .h to .cpp.

# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000)
#endif /* ARDUINO_ARCH_SAMD */

/******************************************************************************
* FUNCTION DECLARATION
******************************************************************************/
Expand All @@ -42,4 +27,9 @@ void samd_watchdog_enable();
void samd_watchdog_reset();
#endif /* ARDUINO_ARCH_SAMD */

#ifdef ARDUINO_ARCH_MBED
void mbed_watchdog_enable();
void mbed_watchdog_reset();
#endif /* ARDUINO_ARCH_MBED */

#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */