From e0dc2457c486f0388230a3450ad4d1aff006febb Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 22 Jul 2022 17:01:17 +0200 Subject: [PATCH 1/6] OTA: Use connection handler network adapter type to select between Ethernet and WiFi --- src/ArduinoIoTCloudTCP.cpp | 3 ++- src/utility/ota/OTA-portenta-h7.cpp | 13 +++++++++++-- src/utility/ota/OTA.h | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index f33f09c5e..af422522b 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -830,7 +830,8 @@ void ArduinoIoTCloudTCP::onOTARequest() #endif #ifdef BOARD_STM32H7 - _ota_error = portenta_h7_onOTARequest(_ota_url.c_str()); + bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false; + _ota_error = portenta_h7_onOTARequest(_ota_url.c_str(), use_ethernet); #endif } #endif diff --git a/src/utility/ota/OTA-portenta-h7.cpp b/src/utility/ota/OTA-portenta-h7.cpp index 4b0c635da..23bde618c 100644 --- a/src/utility/ota/OTA-portenta-h7.cpp +++ b/src/utility/ota/OTA-portenta-h7.cpp @@ -28,13 +28,16 @@ #include #include +#include +#include + #include "../watchdog/Watchdog.h" /****************************************************************************** * FUNCTION DEFINITION ******************************************************************************/ -int portenta_h7_onOTARequest(char const * ota_url) +int portenta_h7_onOTARequest(char const * ota_url, bool use_ethernet) { watchdog_reset(); @@ -63,7 +66,13 @@ int portenta_h7_onOTARequest(char const * ota_url) watchdog_reset(); /* Download the OTA file from the web storage location. */ - int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download(ota_url, true /* is_https */); + MbedSocketClass * download_socket = static_cast(&WiFi); +#if defined (ARDUINO_PORTENTA_H7_M7) + if(use_ethernet) { + download_socket = static_cast(&Ethernet); + } +#endif + int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download(ota_url, true /* is_https */, download_socket); DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::download(%s) returns %d", ota_url, ota_portenta_qspi_download_ret_code); watchdog_reset(); diff --git a/src/utility/ota/OTA.h b/src/utility/ota/OTA.h index ce47a3ad2..513ef53b5 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -63,7 +63,7 @@ int rp2040_connect_onOTARequest(char const * ota_url); #endif #ifdef BOARD_STM32H7 -int portenta_h7_onOTARequest(char const * ota_url); +int portenta_h7_onOTARequest(char const * ota_url, bool use_ethernet); #endif #endif /* ARDUINO_OTA_LOGIC_H_ */ From e1d9da0e5f2e12d332ec330570c97b007dabd777 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 25 Jul 2022 11:40:37 +0200 Subject: [PATCH 2/6] Watchdog: Enable feed function for Ethernet The implementation is done as for WiFi so we can reuse the same define ARDUINO_PORTENTA_H7_WIFI_HAS_FEED_WATCHDOG_FUNC Move watchdog network feed configuration into Watchdog module --- src/ArduinoIoTCloudTCP.cpp | 3 ++- src/utility/watchdog/Watchdog.cpp | 34 +++++++++++++++++++++++++++++++ src/utility/watchdog/Watchdog.h | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index af422522b..a94934a1b 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -308,7 +308,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, if (enable_watchdog) { watchdog_enable(); #if defined (WIFI_HAS_FEED_WATCHDOG_FUNC) || defined (ARDUINO_PORTENTA_H7_WIFI_HAS_FEED_WATCHDOG_FUNC) - WiFi.setFeedWatchdogFunc(watchdog_reset); + bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false; + watchdog_enable_network_feed(use_ethernet); #endif } #endif diff --git a/src/utility/watchdog/Watchdog.cpp b/src/utility/watchdog/Watchdog.cpp index d639d30a7..d41ef333e 100644 --- a/src/utility/watchdog/Watchdog.cpp +++ b/src/utility/watchdog/Watchdog.cpp @@ -29,11 +29,14 @@ #ifdef ARDUINO_ARCH_SAMD # include +# include # define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000) #endif /* ARDUINO_ARCH_SAMD */ #ifdef ARDUINO_ARCH_MBED # include +# include +# include # define PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms (32760) # define NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms (8389) #endif /* ARDUINO_ARCH_MBED */ @@ -63,6 +66,13 @@ static void samd_watchdog_reset() } } +#if defined (WIFI_HAS_FEED_WATCHDOG_FUNC) +static void samd_watchdog_enable_network_feed() +{ + WiFi.setFeedWatchdogFunc(watchdog_reset); +} +#endif + /* This function is called within the WiFiNINA library when invoking * the method 'connectBearSSL' in order to prevent a premature bite * of the watchdog (max timeout on SAMD is 16 s). wifi_nina_feed... @@ -114,6 +124,19 @@ static void mbed_watchdog_reset() } } +#if defined (ARDUINO_PORTENTA_H7_WIFI_HAS_FEED_WATCHDOG_FUNC) +static void mbed_watchdog_enable_network_feed(const bool use_ethernet) +{ + if(use_ethernet) { +#if defined(ARDUINO_PORTENTA_H7_M7) + Ethernet.setFeedWatchdogFunc(watchdog_reset); +#endif + } else { + WiFi.setFeedWatchdogFunc(watchdog_reset); + } +} +#endif + void mbed_watchdog_trigger_reset() { watchdog_config_t cfg; @@ -154,4 +177,15 @@ void watchdog_reset() mbed_watchdog_reset(); #endif } + +#if defined (WIFI_HAS_FEED_WATCHDOG_FUNC) || defined (ARDUINO_PORTENTA_H7_WIFI_HAS_FEED_WATCHDOG_FUNC) +void watchdog_enable_network_feed(const bool use_ethernet) +{ +#ifdef ARDUINO_ARCH_SAMD + samd_watchdog_enable_network_feed(); +#else + mbed_watchdog_enable_network_feed(use_ethernet); +#endif +} +#endif #endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */ diff --git a/src/utility/watchdog/Watchdog.h b/src/utility/watchdog/Watchdog.h index 879317684..b973e5a30 100644 --- a/src/utility/watchdog/Watchdog.h +++ b/src/utility/watchdog/Watchdog.h @@ -25,6 +25,7 @@ #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) void watchdog_enable(); void watchdog_reset(); +void watchdog_enable_network_feed(bool use_ethernet); #endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */ #ifdef ARDUINO_ARCH_MBED From f4afd119558ad351066d7520ac56bef21f2670f6 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 28 Jul 2022 10:47:16 +0200 Subject: [PATCH 3/6] Add const qualifier to use_ethernet function argument --- src/utility/ota/OTA-portenta-h7.cpp | 2 +- src/utility/ota/OTA.h | 2 +- src/utility/watchdog/Watchdog.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utility/ota/OTA-portenta-h7.cpp b/src/utility/ota/OTA-portenta-h7.cpp index 23bde618c..b4d7ddd7b 100644 --- a/src/utility/ota/OTA-portenta-h7.cpp +++ b/src/utility/ota/OTA-portenta-h7.cpp @@ -37,7 +37,7 @@ * FUNCTION DEFINITION ******************************************************************************/ -int portenta_h7_onOTARequest(char const * ota_url, bool use_ethernet) +int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet) { watchdog_reset(); diff --git a/src/utility/ota/OTA.h b/src/utility/ota/OTA.h index 513ef53b5..c1d3f79a0 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -63,7 +63,7 @@ int rp2040_connect_onOTARequest(char const * ota_url); #endif #ifdef BOARD_STM32H7 -int portenta_h7_onOTARequest(char const * ota_url, bool use_ethernet); +int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet); #endif #endif /* ARDUINO_OTA_LOGIC_H_ */ diff --git a/src/utility/watchdog/Watchdog.h b/src/utility/watchdog/Watchdog.h index b973e5a30..dbaf2abd6 100644 --- a/src/utility/watchdog/Watchdog.h +++ b/src/utility/watchdog/Watchdog.h @@ -25,7 +25,7 @@ #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) void watchdog_enable(); void watchdog_reset(); -void watchdog_enable_network_feed(bool use_ethernet); +void watchdog_enable_network_feed(const bool use_ethernet); #endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */ #ifdef ARDUINO_ARCH_MBED From 174132070e9a3c43742a1a8ec395fb4d024f3b5c Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 19 Sep 2022 15:28:10 +0200 Subject: [PATCH 4/6] Watchdog: include Arduino_ConnectionHandler.h instead of Wifi/Ethernet.h In this way we can also use the more meaningful define BOARD_HAS_ETHERNET --- src/utility/watchdog/Watchdog.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/utility/watchdog/Watchdog.cpp b/src/utility/watchdog/Watchdog.cpp index d41ef333e..eed2ce578 100644 --- a/src/utility/watchdog/Watchdog.cpp +++ b/src/utility/watchdog/Watchdog.cpp @@ -29,18 +29,17 @@ #ifdef ARDUINO_ARCH_SAMD # include -# include # define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000) #endif /* ARDUINO_ARCH_SAMD */ #ifdef ARDUINO_ARCH_MBED # include -# include -# include # define PORTENTA_H7_WATCHDOG_MAX_TIMEOUT_ms (32760) # define NANO_RP2040_WATCHDOG_MAX_TIMEOUT_ms (8389) #endif /* ARDUINO_ARCH_MBED */ +#include + /****************************************************************************** * GLOBAL VARIABLES ******************************************************************************/ @@ -127,13 +126,13 @@ static void mbed_watchdog_reset() #if defined (ARDUINO_PORTENTA_H7_WIFI_HAS_FEED_WATCHDOG_FUNC) static void mbed_watchdog_enable_network_feed(const bool use_ethernet) { +#if defined(BOARD_HAS_ETHERNET) if(use_ethernet) { -#if defined(ARDUINO_PORTENTA_H7_M7) Ethernet.setFeedWatchdogFunc(watchdog_reset); + } else #endif - } else { WiFi.setFeedWatchdogFunc(watchdog_reset); - } + } #endif From f39194793cb197756a594d0b37c57ecf48e32451 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 19 Sep 2022 15:28:19 +0200 Subject: [PATCH 5/6] Portenta OTA: include Arduino_ConnectionHandler.h instead of Wifi/Ethernet.h In this way we can also use the more meaningful define BOARD_HAS_ETHERNET --- src/utility/ota/OTA-portenta-h7.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/utility/ota/OTA-portenta-h7.cpp b/src/utility/ota/OTA-portenta-h7.cpp index b4d7ddd7b..784f71969 100644 --- a/src/utility/ota/OTA-portenta-h7.cpp +++ b/src/utility/ota/OTA-portenta-h7.cpp @@ -27,9 +27,7 @@ #include #include - -#include -#include +#include #include "../watchdog/Watchdog.h" @@ -67,7 +65,7 @@ int portenta_h7_onOTARequest(char const * ota_url, const bool use_ethernet) /* Download the OTA file from the web storage location. */ MbedSocketClass * download_socket = static_cast(&WiFi); -#if defined (ARDUINO_PORTENTA_H7_M7) +#if defined (BOARD_HAS_ETHERNET) if(use_ethernet) { download_socket = static_cast(&Ethernet); } From fdaac0f5f8de2dcbcb529f35757ca659bd440e71 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 20 Sep 2022 15:20:43 +0200 Subject: [PATCH 6/6] Fix build for Nano RP2040 connect This board is an mbed platform + WiFiNINA so needs special handling for watchdog network feed. --- src/ArduinoIoTCloudTCP.cpp | 2 -- src/utility/watchdog/Watchdog.cpp | 17 +++++------------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index a94934a1b..f34da15af 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -307,10 +307,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) if (enable_watchdog) { watchdog_enable(); -#if defined (WIFI_HAS_FEED_WATCHDOG_FUNC) || defined (ARDUINO_PORTENTA_H7_WIFI_HAS_FEED_WATCHDOG_FUNC) bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false; watchdog_enable_network_feed(use_ethernet); -#endif } #endif diff --git a/src/utility/watchdog/Watchdog.cpp b/src/utility/watchdog/Watchdog.cpp index eed2ce578..a45f02d4e 100644 --- a/src/utility/watchdog/Watchdog.cpp +++ b/src/utility/watchdog/Watchdog.cpp @@ -65,13 +65,6 @@ static void samd_watchdog_reset() } } -#if defined (WIFI_HAS_FEED_WATCHDOG_FUNC) -static void samd_watchdog_enable_network_feed() -{ - WiFi.setFeedWatchdogFunc(watchdog_reset); -} -#endif - /* This function is called within the WiFiNINA library when invoking * the method 'connectBearSSL' in order to prevent a premature bite * of the watchdog (max timeout on SAMD is 16 s). wifi_nina_feed... @@ -177,14 +170,14 @@ void watchdog_reset() #endif } -#if defined (WIFI_HAS_FEED_WATCHDOG_FUNC) || defined (ARDUINO_PORTENTA_H7_WIFI_HAS_FEED_WATCHDOG_FUNC) void watchdog_enable_network_feed(const bool use_ethernet) { -#ifdef ARDUINO_ARCH_SAMD - samd_watchdog_enable_network_feed(); -#else +#ifdef WIFI_HAS_FEED_WATCHDOG_FUNC + WiFi.setFeedWatchdogFunc(watchdog_reset); +#endif + +#ifdef ARDUINO_PORTENTA_H7_WIFI_HAS_FEED_WATCHDOG_FUNC mbed_watchdog_enable_network_feed(use_ethernet); #endif } -#endif #endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */