Skip to content

Commit 288489d

Browse files
committed
Adding watchdog for SAMD architecture which is continually fed within ArduinoIoTCloud::update() to reset the board in case some function becomes stuck.
1 parent 70efc00 commit 288489d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 27 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,36 @@ 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(120 * 1000);
274+
#endif /* ARDUINO_ARCH_SAMD */
275+
265276
return 1;
266277
}
267278

268279
void ArduinoIoTCloudTCP::update()
269280
{
281+
#ifdef ARDUINO_ARCH_SAMD
282+
/* Now that we are within the regularly called function
283+
* ArduinoIoTCloudTCP::update() it's a wise idea to reduce
284+
* the watchdog timeout to a smaller amount of time.
285+
*/
286+
static bool is_watchdog_set = false;
287+
if (!is_watchdog_set) {
288+
Watchdog.enable(30 * 1000);
289+
is_watchdog_set = true;
290+
}
291+
/* Feed the watchdog. If any of the functions called below
292+
* get stuck than we can at least reset and recover.
293+
*/
294+
Watchdog.reset();
295+
#endif /* ARDUINO_ARCH_SAMD */
296+
270297
/* Run through the state machine. */
271298
State next_state = _state;
272299
switch (_state)

src/utility/watchdog/Watchdog.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#endif /* ARDUINO_ARCH_SAMD */
28+
29+
#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */

0 commit comments

Comments
 (0)