Skip to content

Commit 5d055ae

Browse files
authored
Merge pull request #260 from pennam/watchdog_pr
Watchdog handling cleanup
2 parents d09c6dc + 616a4fe commit 5d055ae

File tree

6 files changed

+60
-44
lines changed

6 files changed

+60
-44
lines changed

src/ArduinoIoTCloudTCP.cpp

+11-15
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
282282
* call to ArduinoIoTCloudTCP::update() it is wise to
283283
* set a rather large timeout at first.
284284
*/
285-
#ifdef ARDUINO_ARCH_SAMD
286-
if (enable_watchdog) {
287-
samd_watchdog_enable();
288-
}
289-
#elif defined(ARDUINO_ARCH_MBED)
285+
#if defined (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED)
290286
if (enable_watchdog) {
291-
mbed_watchdog_enable();
287+
watchdog_enable();
288+
#ifdef WIFI_HAS_FEED_WATCHDOG_FUNC
289+
WiFi.setFeedWatchdogFunc(watchdog_reset);
290+
#endif
292291
}
292+
293293
#endif
294294

295295
return 1;
@@ -300,10 +300,8 @@ void ArduinoIoTCloudTCP::update()
300300
/* Feed the watchdog. If any of the functions called below
301301
* get stuck than we can at least reset and recover.
302302
*/
303-
#ifdef ARDUINO_ARCH_SAMD
304-
samd_watchdog_reset();
305-
#elif defined(ARDUINO_ARCH_MBED)
306-
mbed_watchdog_reset();
303+
#if defined (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED)
304+
watchdog_reset();
307305
#endif
308306

309307

@@ -321,13 +319,11 @@ void ArduinoIoTCloudTCP::update()
321319
_state = next_state;
322320

323321
/* This watchdog feed is actually needed only by the RP2040 CONNECT cause its
324-
* maximum watchdog window is 8388ms; despite this we feed it for all
322+
* maximum watchdog window is 8389ms; despite this we feed it for all
325323
* supported ARCH to keep code aligned.
326324
*/
327-
#ifdef ARDUINO_ARCH_SAMD
328-
samd_watchdog_reset();
329-
#elif defined(ARDUINO_ARCH_MBED)
330-
mbed_watchdog_reset();
325+
#if defined (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED)
326+
watchdog_reset();
331327
#endif
332328

333329
/* Check for new data from the MQTT client. */

src/utility/ota/OTA-nano-rp2040.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void URI::parse(const string& url_s)
8484

8585
int rp2040_connect_onOTARequest(char const * ota_url)
8686
{
87-
mbed_watchdog_reset();
87+
watchdog_reset();
8888

8989
int err = -1;
9090
FlashIAPBlockDevice flash(XIP_BASE + 0xF00000, 0x100000);
@@ -94,11 +94,11 @@ int rp2040_connect_onOTARequest(char const * ota_url)
9494
return static_cast<int>(OTAError::RP2040_ErrorFlashInit);
9595
}
9696

97-
mbed_watchdog_reset();
97+
watchdog_reset();
9898

9999
flash.erase(XIP_BASE + 0xF00000, 0x100000);
100100

101-
mbed_watchdog_reset();
101+
watchdog_reset();
102102

103103
mbed::FATFileSystem fs("ota");
104104
if ((err = fs.reformat(&flash)) != 0)
@@ -107,7 +107,7 @@ int rp2040_connect_onOTARequest(char const * ota_url)
107107
return static_cast<int>(OTAError::RP2040_ErrorReformat);
108108
}
109109

110-
mbed_watchdog_reset();
110+
watchdog_reset();
111111

112112
FILE * file = fopen("/ota/UPDATE.BIN.LZSS", "wb");
113113
if (!file)
@@ -117,7 +117,7 @@ int rp2040_connect_onOTARequest(char const * ota_url)
117117
return static_cast<int>(OTAError::RP2040_ErrorOpenUpdateFile);
118118
}
119119

120-
mbed_watchdog_reset();
120+
watchdog_reset();
121121

122122
URI url(ota_url);
123123
Client * client = nullptr;
@@ -135,7 +135,7 @@ int rp2040_connect_onOTARequest(char const * ota_url)
135135
return static_cast<int>(OTAError::RP2040_UrlParseError);
136136
}
137137

138-
mbed_watchdog_reset();
138+
watchdog_reset();
139139

140140
if (!client->connect(url.host_.c_str(), port))
141141
{
@@ -144,14 +144,14 @@ int rp2040_connect_onOTARequest(char const * ota_url)
144144
return static_cast<int>(OTAError::RP2040_ServerConnectError);
145145
}
146146

147-
mbed_watchdog_reset();
147+
watchdog_reset();
148148

149149
client->println(String("GET ") + url.path_.c_str() + " HTTP/1.1");
150150
client->println(String("Host: ") + url.host_.c_str());
151151
client->println("Connection: close");
152152
client->println();
153153

154-
mbed_watchdog_reset();
154+
watchdog_reset();
155155

156156
/* Receive HTTP header. */
157157
String http_header;
@@ -162,7 +162,7 @@ int rp2040_connect_onOTARequest(char const * ota_url)
162162
is_http_header_timeout = (millis() - start) > AIOT_CONFIG_RP2040_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms;
163163
if (is_http_header_timeout) break;
164164

165-
mbed_watchdog_reset();
165+
watchdog_reset();
166166

167167
if (client->available())
168168
{
@@ -208,7 +208,7 @@ int rp2040_connect_onOTARequest(char const * ota_url)
208208
is_http_data_timeout = (millis() - start) > AIOT_CONFIG_RP2040_OTA_HTTP_DATA_RECEIVE_TIMEOUT_ms;
209209
if (is_http_data_timeout) break;
210210

211-
mbed_watchdog_reset();
211+
watchdog_reset();
212212

213213
if (client->available())
214214
{

src/utility/ota/OTA-portenta-h7.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,33 @@
3434

3535
int portenta_h7_onOTARequest(char const * ota_url)
3636
{
37-
mbed_watchdog_reset();
37+
watchdog_reset();
3838

3939
Arduino_Portenta_OTA::Error ota_portenta_err = Arduino_Portenta_OTA::Error::None;
4040
/* Use 2nd partition of QSPI (1st partition contains WiFi firmware) */
4141
Arduino_Portenta_OTA_QSPI ota_portenta_qspi(QSPI_FLASH_FATFS_MBR, 2);
4242

43-
mbed_watchdog_reset();
43+
watchdog_reset();
4444

4545
/* Initialize the QSPI memory for OTA handling. */
4646
if((ota_portenta_err = ota_portenta_qspi.begin()) != Arduino_Portenta_OTA::Error::None) {
4747
DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::begin() failed with %d", static_cast<int>(ota_portenta_err));
4848
return static_cast<int>(ota_portenta_err);
4949
}
5050

51-
mbed_watchdog_reset();
51+
watchdog_reset();
5252

5353
/* Just to be safe delete any remains from previous updates. */
5454
remove("/fs/UPDATE.BIN");
5555
remove("/fs/UPDATE.BIN.LZSS");
5656

57-
mbed_watchdog_reset();
57+
watchdog_reset();
5858

5959
/* Download the OTA file from the web storage location. */
6060
int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download(ota_url, true /* is_https */);
6161
DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::download(%s) returns %d", ota_url, ota_portenta_qspi_download_ret_code);
6262

63-
mbed_watchdog_reset();
63+
watchdog_reset();
6464

6565
/* Decompress the LZSS compressed OTA file. */
6666
int const ota_portenta_qspi_decompress_ret_code = ota_portenta_qspi.decompress();
@@ -71,7 +71,7 @@ int portenta_h7_onOTARequest(char const * ota_url)
7171
return ota_portenta_qspi_decompress_ret_code;
7272
}
7373

74-
mbed_watchdog_reset();
74+
watchdog_reset();
7575

7676
/* Schedule the firmware update. */
7777
if((ota_portenta_err = ota_portenta_qspi.update()) != Arduino_Portenta_OTA::Error::None) {

src/utility/ota/OTA-samd.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838

3939
int samd_onOTARequest(char const * ota_url)
4040
{
41-
samd_watchdog_reset();
41+
watchdog_reset();
4242

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

48-
samd_watchdog_reset();
48+
watchdog_reset();
4949

5050
/* Trigger direct download to nina module. */
5151
uint8_t nina_ota_err_code = 0;

src/utility/watchdog/Watchdog.cpp

+27-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#ifdef ARDUINO_ARCH_MBED
3636
# include <watchdog_api.h>
3737
# define PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms (32760)
38-
# define NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms (32760)
38+
# define NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms (8389)
3939
#endif /* ARDUINO_ARCH_MBED */
4040

4141
/******************************************************************************
@@ -49,13 +49,13 @@ static bool is_watchdog_enabled = false;
4949
******************************************************************************/
5050

5151
#ifdef ARDUINO_ARCH_SAMD
52-
void samd_watchdog_enable()
52+
static void samd_watchdog_enable()
5353
{
5454
is_watchdog_enabled = true;
5555
Watchdog.enable(SAMD_WATCHDOG_MAX_TIME_ms);
5656
}
5757

58-
void samd_watchdog_reset()
58+
static void samd_watchdog_reset()
5959
{
6060
if (is_watchdog_enabled) {
6161
Watchdog.reset();
@@ -68,10 +68,12 @@ void samd_watchdog_reset()
6868
* is defined a weak function there and overwritten by this "strong"
6969
* function here.
7070
*/
71+
#ifndef WIFI_HAS_FEED_WATCHDOG_FUNC
7172
void wifi_nina_feed_watchdog()
7273
{
7374
samd_watchdog_reset();
7475
}
76+
#endif
7577

7678
void mkr_gsm_feed_watchdog()
7779
{
@@ -85,7 +87,7 @@ void mkr_nb_feed_watchdog()
8587
#endif /* ARDUINO_ARCH_SAMD */
8688

8789
#ifdef ARDUINO_ARCH_MBED
88-
void mbed_watchdog_enable()
90+
static void mbed_watchdog_enable()
8991
{
9092
watchdog_config_t cfg;
9193
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
@@ -104,7 +106,7 @@ void mbed_watchdog_enable()
104106
}
105107
}
106108

107-
void mbed_watchdog_reset()
109+
static void mbed_watchdog_reset()
108110
{
109111
if (is_watchdog_enabled) {
110112
hal_watchdog_kick();
@@ -132,3 +134,23 @@ void mbed_watchdog_trigger_reset()
132134

133135
}
134136
#endif /* ARDUINO_ARCH_MBED */
137+
138+
#if defined (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED)
139+
void watchdog_enable()
140+
{
141+
#ifdef ARDUINO_ARCH_SAMD
142+
samd_watchdog_enable();
143+
#else
144+
mbed_watchdog_enable();
145+
#endif
146+
}
147+
148+
void watchdog_reset()
149+
{
150+
#ifdef ARDUINO_ARCH_SAMD
151+
samd_watchdog_reset();
152+
#else
153+
mbed_watchdog_reset();
154+
#endif
155+
}
156+
#endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */

src/utility/watchdog/Watchdog.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
* FUNCTION DECLARATION
2323
******************************************************************************/
2424

25-
#ifdef ARDUINO_ARCH_SAMD
26-
void samd_watchdog_enable();
27-
void samd_watchdog_reset();
28-
#endif /* ARDUINO_ARCH_SAMD */
25+
#if defined (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED)
26+
void watchdog_enable();
27+
void watchdog_reset();
28+
#endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */
2929

3030
#ifdef ARDUINO_ARCH_MBED
31-
void mbed_watchdog_enable();
32-
void mbed_watchdog_reset();
3331
void mbed_watchdog_trigger_reset();
3432
#endif /* ARDUINO_ARCH_MBED */
3533

0 commit comments

Comments
 (0)