|
| 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 | +#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) |
| 19 | + |
| 20 | +/****************************************************************************** |
| 21 | + * INCLUDE |
| 22 | + ******************************************************************************/ |
| 23 | + |
| 24 | +#include "OTA.h" |
| 25 | + |
| 26 | +#include <Arduino_DebugUtils.h> |
| 27 | + |
| 28 | +#include "../watchdog/Watchdog.h" |
| 29 | + |
| 30 | +/****************************************************************************** |
| 31 | + * FUNCTION DEFINITION |
| 32 | + ******************************************************************************/ |
| 33 | + |
| 34 | +int portenta_h7_onOTARequest(char const * ota_url) |
| 35 | +{ |
| 36 | + mbed_watchdog_reset(); |
| 37 | + |
| 38 | + Arduino_Portenta_OTA::Error ota_portenta_err = Arduino_Portenta_OTA::Error::None; |
| 39 | + /* Use 2nd partition of QSPI (1st partition contains WiFi firmware) */ |
| 40 | + Arduino_Portenta_OTA_QSPI ota_portenta_qspi(QSPI_FLASH_FATFS_MBR, 2); |
| 41 | + |
| 42 | + mbed_watchdog_reset(); |
| 43 | + |
| 44 | + /* Initialize the QSPI memory for OTA handling. */ |
| 45 | + if((ota_portenta_err = ota_portenta_qspi.begin()) != Arduino_Portenta_OTA::Error::None) { |
| 46 | + DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::begin() failed with %d", static_cast<int>(ota_portenta_err)); |
| 47 | + return static_cast<int>(ota_portenta_err); |
| 48 | + } |
| 49 | + |
| 50 | + mbed_watchdog_reset(); |
| 51 | + |
| 52 | + /* Just to be safe delete any remains from previous updates. */ |
| 53 | + remove("/fs/UPDATE.BIN"); |
| 54 | + remove("/fs/UPDATE.BIN.LZSS"); |
| 55 | + |
| 56 | + mbed_watchdog_reset(); |
| 57 | + |
| 58 | + /* Download the OTA file from the web storage location. */ |
| 59 | + int const ota_portenta_qspi_download_ret_code = ota_portenta_qspi.download(ota_url, true /* is_https */); |
| 60 | + DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::download(%s) returns %d", ota_url, ota_portenta_qspi_download_ret_code); |
| 61 | + |
| 62 | + mbed_watchdog_reset(); |
| 63 | + |
| 64 | + /* Decompress the LZSS compressed OTA file. */ |
| 65 | + int const ota_portenta_qspi_decompress_ret_code = ota_portenta_qspi.decompress(); |
| 66 | + DEBUG_VERBOSE("Arduino_Portenta_OTA_QSPI::decompress() returns %d", ota_portenta_qspi_decompress_ret_code); |
| 67 | + if (ota_portenta_qspi_decompress_ret_code < 0) |
| 68 | + { |
| 69 | + DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::decompress() failed with %d", ota_portenta_qspi_decompress_ret_code); |
| 70 | + return ota_portenta_qspi_decompress_ret_code; |
| 71 | + } |
| 72 | + |
| 73 | + mbed_watchdog_reset(); |
| 74 | + |
| 75 | + /* Schedule the firmware update. */ |
| 76 | + if((ota_portenta_err = ota_portenta_qspi.update()) != Arduino_Portenta_OTA::Error::None) { |
| 77 | + DEBUG_ERROR("Arduino_Portenta_OTA_QSPI::update() failed with %d", static_cast<int>(ota_portenta_err)); |
| 78 | + return static_cast<int>(ota_portenta_err); |
| 79 | + } |
| 80 | + |
| 81 | + /* Perform the reset to reboot - then the bootloader performs the actual application update. */ |
| 82 | + NVIC_SystemReset(); |
| 83 | +} |
| 84 | + |
| 85 | +#endif /* defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) */ |
0 commit comments