diff --git a/src/Arduino_Portenta_OTA.cpp b/src/Arduino_Portenta_OTA.cpp index ed4585a..e64f68d 100644 --- a/src/Arduino_Portenta_OTA.cpp +++ b/src/Arduino_Portenta_OTA.cpp @@ -90,6 +90,17 @@ void Arduino_Portenta_OTA::reset() NVIC_SystemReset(); } +void Arduino_Portenta_OTA::setFeedWatchdogFunc(ArduinoPortentaOtaWatchdogResetFuncPointer func) +{ + _feed_watchdog_func = func; +} + +void Arduino_Portenta_OTA::feedWatchdog() +{ + if (_feed_watchdog_func) + _feed_watchdog_func(); +} + /****************************************************************************** * PROTECTED MEMBER FUNCTIONS ******************************************************************************/ diff --git a/src/Arduino_Portenta_OTA.h b/src/Arduino_Portenta_OTA.h index 5e22260..e2e2bd1 100644 --- a/src/Arduino_Portenta_OTA.h +++ b/src/Arduino_Portenta_OTA.h @@ -42,6 +42,8 @@ #define APOTA_LITTLEFS_FLAG (1 << 6) #define APOTA_MBR_FLAG (1 << 7) +#define ARDUINO_PORTENTA_OTA_HAS_WATCHDOG_FEED + /****************************************************************************** * TYPEDEF ******************************************************************************/ @@ -53,6 +55,8 @@ enum StorageTypePortenta { SD_FATFS_MBR = APOTA_SDCARD_FLAG | APOTA_FATFS_FLAG | APOTA_MBR_FLAG, }; +typedef void(*ArduinoPortentaOtaWatchdogResetFuncPointer)(void); + /****************************************************************************** * CLASS DECLARATION ******************************************************************************/ @@ -87,6 +91,8 @@ class Arduino_Portenta_OTA */ int download(const char * url, bool const is_https); int decompress(); + void setFeedWatchdogFunc(ArduinoPortentaOtaWatchdogResetFuncPointer func); + void feedWatchdog(); protected: @@ -103,6 +109,7 @@ class Arduino_Portenta_OTA private: void write(); + ArduinoPortentaOtaWatchdogResetFuncPointer _feed_watchdog_func = 0; }; diff --git a/src/decompress/lzss.cpp b/src/decompress/lzss.cpp index 41c6664..8ae0917 100644 --- a/src/decompress/lzss.cpp +++ b/src/decompress/lzss.cpp @@ -36,22 +36,27 @@ unsigned char buffer[N * 2]; static char write_buf[FPUTC_BUF_SIZE]; static size_t write_buf_num_bytes = 0; static size_t bytes_written_fputc = 0; +static ArduinoPortentaOtaWatchdogResetFuncPointer wdog_feed_func = 0; /************************************************************************************** PUBLIC FUNCTIONS **************************************************************************************/ -void lzss_init(FILE * update_file_ptr, FILE * target_file_ptr, uint32_t const lzss_file_size) +void lzss_init(FILE * update_file_ptr, FILE * target_file_ptr, uint32_t const lzss_file_size, ArduinoPortentaOtaWatchdogResetFuncPointer wdog_feed_func_ptr) { update_file = update_file_ptr; target_file = target_file_ptr; LZSS_FILE_SIZE = lzss_file_size; + wdog_feed_func = wdog_feed_func_ptr; } void lzss_flush() { bytes_written_fputc += write_buf_num_bytes; + if (wdog_feed_func) + wdog_feed_func(); + fwrite(write_buf, 1, write_buf_num_bytes, target_file); write_buf_num_bytes = 0; diff --git a/src/decompress/lzss.h b/src/decompress/lzss.h index 129ce44..0907600 100644 --- a/src/decompress/lzss.h +++ b/src/decompress/lzss.h @@ -7,12 +7,13 @@ #include #include +#include "Arduino_Portenta_OTA.h" /************************************************************************************** FUNCTION DEFINITION **************************************************************************************/ -void lzss_init(FILE * update_file_ptr, FILE * target_file_ptr, uint32_t const lzss_file_size); +void lzss_init(FILE * update_file_ptr, FILE * target_file_ptr, uint32_t const lzss_file_size, ArduinoPortentaOtaWatchdogResetFuncPointer wdog_feed_func_ptr); void lzss_decode(); void lzss_flush(); diff --git a/src/decompress/utility.cpp b/src/decompress/utility.cpp index f5db105..d10f80e 100644 --- a/src/decompress/utility.cpp +++ b/src/decompress/utility.cpp @@ -137,6 +137,8 @@ int Arduino_Portenta_OTA::decompress() uint32_t crc32, bytes_read; uint8_t crc_buf[128]; + feedWatchdog(); + /* Read the OTA header ... */ fread(ota_header.buf, 1, sizeof(ota_header.buf), update_file); @@ -147,6 +149,8 @@ int Arduino_Portenta_OTA::decompress() return static_cast(Error::OtaHeaderLength); } + feedWatchdog(); + /* ... and the CRC second ... rewind to start of CRC verified header ... */ fseek(update_file, sizeof(ota_header.header.len) + sizeof(ota_header.header.crc32), SEEK_SET); /* ... initialize CRC ... */ @@ -161,6 +165,9 @@ int Arduino_Portenta_OTA::decompress() } fread(crc_buf, 1, ota_header.header.len - bytes_read, update_file); crc32 = crc_update(crc32, crc_buf, ota_header.header.len - bytes_read); + + feedWatchdog(); + /* ... then finalise ... */ crc32 ^= 0xFFFFFFFF; /* ... and compare. */ @@ -170,6 +177,8 @@ int Arduino_Portenta_OTA::decompress() return static_cast(Error::OtaHeaderCrc); } + feedWatchdog(); + if (ota_header.header.magic_number != 0x2341025b) /* 0x2341:025b = VID/PID Portenta H7 */ { fclose(update_file); @@ -184,7 +193,7 @@ int Arduino_Portenta_OTA::decompress() FILE* decompressed = fopen(UPDATE_FILE_NAME, "w"); - lzss_init(update_file, decompressed, LZSS_FILE_SIZE); + lzss_init(update_file, decompressed, LZSS_FILE_SIZE, _feed_watchdog_func); /* During the process of decoding UPDATE.BIN.LZSS * is decompressed and stored as UPDATE.BIN.