Skip to content

Commit 897a24d

Browse files
authored
Merge pull request #235 from arduino-libraries/watchdog
Adding watchdog for SAMD architecture
2 parents 70efc00 + c6f0336 commit 897a24d

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

.github/workflows/compile-examples.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363
- name: ArduinoECCX08
6464
- name: RTCZero
6565
- name: WiFi101
66+
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
6667
sketch-paths: |
6768
- examples/utility/Provisioning
6869
# MKR WiFi 1010, Nano 33 IoT
@@ -77,6 +78,7 @@ jobs:
7778
- name: WiFiNINA
7879
- name: Arduino_JSON
7980
- name: ArduinoBearSSL
81+
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
8082
sketch-paths: |
8183
- examples/utility/Provisioning
8284
- examples/utility/SelfProvisioning
@@ -113,6 +115,7 @@ jobs:
113115
- name: ArduinoECCX08
114116
- name: RTCZero
115117
- name: MKRGSM
118+
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
116119
sketch-paths: |
117120
- examples/utility/Provisioning
118121
# NB boards
@@ -124,6 +127,7 @@ jobs:
124127
- name: ArduinoECCX08
125128
- name: RTCZero
126129
- name: MKRNB
130+
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
127131
sketch-paths: |
128132
- examples/utility/Provisioning
129133
# Portenta

src/ArduinoIoTCloudTCP.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
#include "cbor/CBOREncoder.h"
4545

46+
#include "utility/watchdog/Watchdog.h"
47+
4648
/******************************************************************************
4749
* EXTERN
4850
******************************************************************************/
@@ -262,11 +264,27 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
262264
}
263265
#endif /* OTA_STORAGE_SNU */
264266

267+
#ifdef ARDUINO_ARCH_SAMD
268+
/* Since we do not control what code the user inserts
269+
* between ArduinoIoTCloudTCP::begin() and the first
270+
* call to ArduinoIoTCloudTCP::update() it is wise to
271+
* set a rather large timeout at first.
272+
*/
273+
Watchdog.enable(SAMD_WATCHDOG_MAX_TIME_ms);
274+
#endif /* ARDUINO_ARCH_SAMD */
275+
265276
return 1;
266277
}
267278

268279
void ArduinoIoTCloudTCP::update()
269280
{
281+
#ifdef ARDUINO_ARCH_SAMD
282+
/* Feed the watchdog. If any of the functions called below
283+
* get stuck than we can at least reset and recover.
284+
*/
285+
Watchdog.reset();
286+
#endif /* ARDUINO_ARCH_SAMD */
287+
270288
/* Run through the state machine. */
271289
State next_state = _state;
272290
switch (_state)
@@ -522,13 +540,21 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
522540
#if OTA_ENABLED
523541
void ArduinoIoTCloudTCP::onOTARequest()
524542
{
543+
#ifdef ARDUINO_ARCH_SAMD
544+
Watchdog.reset();
545+
#endif /* ARDUINO_ARCH_SAMD */
546+
525547
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str());
526548

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

554+
#ifdef ARDUINO_ARCH_SAMD
555+
Watchdog.reset();
556+
#endif /* ARDUINO_ARCH_SAMD */
557+
532558
/* Trigger direct download to nina module. */
533559
uint8_t nina_ota_err_code = 0;
534560
if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code))

src/utility/watchdog/Watchdog.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifndef ARDUINO_AIOTC_UTILITY_WATCHDOG_H_
19+
#define ARDUINO_AIOTC_UTILITY_WATCHDOG_H_
20+
21+
/******************************************************************************
22+
* INCLUDE
23+
******************************************************************************/
24+
25+
#ifdef ARDUINO_ARCH_SAMD
26+
# include <Adafruit_SleepyDog.h>
27+
# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000)
28+
#endif /* ARDUINO_ARCH_SAMD */
29+
30+
#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */

0 commit comments

Comments
 (0)