Skip to content

Commit c75fc07

Browse files
committed
Replace architecture specific watchdog handling functions with an unique implementation that takes care of the different architectures
1 parent d09c6dc commit c75fc07

File tree

6 files changed

+58
-73
lines changed

6 files changed

+58
-73
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,10 @@ 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)
290285
if (enable_watchdog) {
291-
mbed_watchdog_enable();
286+
watchdog_enable();
292287
}
293-
#endif
288+
294289

295290
return 1;
296291
}
@@ -300,11 +295,7 @@ void ArduinoIoTCloudTCP::update()
300295
/* Feed the watchdog. If any of the functions called below
301296
* get stuck than we can at least reset and recover.
302297
*/
303-
#ifdef ARDUINO_ARCH_SAMD
304-
samd_watchdog_reset();
305-
#elif defined(ARDUINO_ARCH_MBED)
306-
mbed_watchdog_reset();
307-
#endif
298+
watchdog_reset();
308299

309300

310301
/* Run through the state machine. */
@@ -324,11 +315,7 @@ void ArduinoIoTCloudTCP::update()
324315
* maximum watchdog window is 8388ms; despite this we feed it for all
325316
* supported ARCH to keep code aligned.
326317
*/
327-
#ifdef ARDUINO_ARCH_SAMD
328-
samd_watchdog_reset();
329-
#elif defined(ARDUINO_ARCH_MBED)
330-
mbed_watchdog_reset();
331-
#endif
318+
watchdog_reset();
332319

333320
/* Check for new data from the MQTT client. */
334321
if (_mqttClient.connected())

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

Lines changed: 10 additions & 10 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,48 @@ static bool is_watchdog_enabled = false;
4848
* FUNCTION DEFINITION
4949
******************************************************************************/
5050

51-
#ifdef ARDUINO_ARCH_SAMD
52-
void samd_watchdog_enable()
51+
void watchdog_enable()
5352
{
53+
#ifdef ARDUINO_ARCH_SAMD
5454
is_watchdog_enabled = true;
5555
Watchdog.enable(SAMD_WATCHDOG_MAX_TIME_ms);
56+
#elif ARDUINO_ARCH_MBED
57+
watchdog_config_t cfg;
58+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
59+
cfg.timeout_ms = PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms;
60+
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
61+
cfg.timeout_ms = NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms;
62+
#else
63+
# error "You need to define the maximum possible timeout for this architecture."
64+
#endif
65+
66+
if (hal_watchdog_init(&cfg) == WATCHDOG_STATUS_OK) {
67+
is_watchdog_enabled = true;
68+
}
69+
else {
70+
DEBUG_WARNING("%s: watchdog could not be enabled", __FUNCTION__);
71+
}
72+
#else
73+
# error "ARDUINO_ARCH not defined."
74+
#endif
5675
}
5776

58-
void samd_watchdog_reset()
77+
void watchdog_reset()
5978
{
79+
#ifdef ARDUINO_ARCH_SAMD
6080
if (is_watchdog_enabled) {
6181
Watchdog.reset();
6282
}
83+
#elif ARDUINO_ARCH_MBED
84+
if (is_watchdog_enabled) {
85+
hal_watchdog_kick();
86+
}
87+
#else
88+
# error "ARDUINO_ARCH not defined."
89+
#endif
6390
}
6491

92+
#ifdef ARDUINO_ARCH_SAMD
6593
/* This function is called within the WiFiNINA library when invoking
6694
* the method 'connectBearSSL' in order to prevent a premature bite
6795
* of the watchdog (max timeout on SAMD is 16 s). wifi_nina_feed...
@@ -70,47 +98,21 @@ void samd_watchdog_reset()
7098
*/
7199
void wifi_nina_feed_watchdog()
72100
{
73-
samd_watchdog_reset();
101+
watchdog_reset();
74102
}
75103

76104
void mkr_gsm_feed_watchdog()
77105
{
78-
samd_watchdog_reset();
106+
watchdog_reset();
79107
}
80108

81109
void mkr_nb_feed_watchdog()
82110
{
83-
samd_watchdog_reset();
111+
watchdog_reset();
84112
}
85113
#endif /* ARDUINO_ARCH_SAMD */
86114

87115
#ifdef ARDUINO_ARCH_MBED
88-
void mbed_watchdog_enable()
89-
{
90-
watchdog_config_t cfg;
91-
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
92-
cfg.timeout_ms = PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms;
93-
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
94-
cfg.timeout_ms = NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms;
95-
#else
96-
# error "You need to define the maximum possible timeout for this architecture."
97-
#endif
98-
99-
if (hal_watchdog_init(&cfg) == WATCHDOG_STATUS_OK) {
100-
is_watchdog_enabled = true;
101-
}
102-
else {
103-
DEBUG_WARNING("%s: watchdog could not be enabled", __FUNCTION__);
104-
}
105-
}
106-
107-
void mbed_watchdog_reset()
108-
{
109-
if (is_watchdog_enabled) {
110-
hal_watchdog_kick();
111-
}
112-
}
113-
114116
void mbed_watchdog_trigger_reset()
115117
{
116118
watchdog_config_t cfg;

src/utility/watchdog/Watchdog.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@
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 */
2925

26+
void watchdog_enable();
27+
void watchdog_reset();
3028
#ifdef ARDUINO_ARCH_MBED
31-
void mbed_watchdog_enable();
32-
void mbed_watchdog_reset();
3329
void mbed_watchdog_trigger_reset();
3430
#endif /* ARDUINO_ARCH_MBED */
3531

0 commit comments

Comments
 (0)