Skip to content

Adding watchdog for SAMD architecture #235

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 3 commits into from
Mar 16, 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
4 changes: 4 additions & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
- name: ArduinoECCX08
- name: RTCZero
- name: WiFi101
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
sketch-paths: |
- examples/utility/Provisioning
# MKR WiFi 1010, Nano 33 IoT
Expand All @@ -77,6 +78,7 @@ jobs:
- name: WiFiNINA
- name: Arduino_JSON
- name: ArduinoBearSSL
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
sketch-paths: |
- examples/utility/Provisioning
- examples/utility/SelfProvisioning
Expand Down Expand Up @@ -113,6 +115,7 @@ jobs:
- name: ArduinoECCX08
- name: RTCZero
- name: MKRGSM
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
sketch-paths: |
- examples/utility/Provisioning
# NB boards
Expand All @@ -124,6 +127,7 @@ jobs:
- name: ArduinoECCX08
- name: RTCZero
- name: MKRNB
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
sketch-paths: |
- examples/utility/Provisioning
# Portenta
Expand Down
26 changes: 26 additions & 0 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

#include "cbor/CBOREncoder.h"

#include "utility/watchdog/Watchdog.h"

/******************************************************************************
* EXTERN
******************************************************************************/
Expand Down Expand Up @@ -262,11 +264,27 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
}
#endif /* OTA_STORAGE_SNU */

#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.
*/
Watchdog.enable(SAMD_WATCHDOG_MAX_TIME_ms);
#endif /* ARDUINO_ARCH_SAMD */

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.
*/
Watchdog.reset();
#endif /* ARDUINO_ARCH_SAMD */

/* Run through the state machine. */
State next_state = _state;
switch (_state)
Expand Down Expand Up @@ -522,13 +540,21 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
#if OTA_ENABLED
void ArduinoIoTCloudTCP::onOTARequest()
{
#ifdef ARDUINO_ARCH_SAMD
Watchdog.reset();
#endif /* ARDUINO_ARCH_SAMD */

DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str());

#if OTA_STORAGE_SNU
/* Just to be safe delete any remains from previous updates. */
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS");
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP");

#ifdef ARDUINO_ARCH_SAMD
Watchdog.reset();
#endif /* ARDUINO_ARCH_SAMD */

/* Trigger direct download to nina module. */
uint8_t nina_ota_err_code = 0;
if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code))
Expand Down
30 changes: 30 additions & 0 deletions src/utility/watchdog/Watchdog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2020 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

#ifndef ARDUINO_AIOTC_UTILITY_WATCHDOG_H_
#define ARDUINO_AIOTC_UTILITY_WATCHDOG_H_

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

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

#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */