|
| 1 | +/* |
| 2 | + * This example demonstrates how to use the SFU to update the firmware of the |
| 3 | + * Arduino Portenta C33 using a compressed firmware image stored in the QSPI flash. |
| 4 | + * In real applications, you will get the file from the internet and not include it |
| 5 | + * in a header file like we do here for simplicity. |
| 6 | + * |
| 7 | + * Steps: |
| 8 | + * 1) Create a sketch for the Portenta C33 and verify |
| 9 | + * that it both compiles and works on a board. |
| 10 | + * 2) In the IDE select: Sketch -> Export compiled Binary. |
| 11 | + * 3) Create an OTA update file utilising the tools 'lzss.py' and 'bin2ota.py' stored in |
| 12 | + * https://github.com/arduino-libraries/ArduinoIoTCloud/tree/master/extras/tools . |
| 13 | + * A) ./lzss.py --encode SKETCH.bin SKETCH.lzss |
| 14 | + * B) ./bin2ota.py PORTENTA_C33 SKETCH.lzss SKETCH.ota |
| 15 | + * 4) Create the header file to be included in this Sketch |
| 16 | + * xxd -i SKETCH.ota > update.h |
| 17 | + * 5) Upload this Sketch to perform the update |
| 18 | + */ |
| 19 | + |
| 20 | +/****************************************************************************** |
| 21 | + * INCLUDE |
| 22 | + ******************************************************************************/ |
| 23 | + |
| 24 | +#include <SFU.h> |
| 25 | +#include <BlockDevice.h> |
| 26 | +#include <MBRBlockDevice.h> |
| 27 | +#include <FATFileSystem.h> |
| 28 | +#include <Arduino_DebugUtils.h> |
| 29 | +#include "update.h" |
| 30 | + |
| 31 | +BlockDevice* block_device = BlockDevice::get_default_instance(); |
| 32 | +MBRBlockDevice mbr(block_device, 1); |
| 33 | +FATFileSystem fs("ota"); |
| 34 | + |
| 35 | + |
| 36 | +/****************************************************************************** |
| 37 | + * SETUP/LOOP |
| 38 | + ******************************************************************************/ |
| 39 | + |
| 40 | +void setup() |
| 41 | +{ |
| 42 | + Serial.begin(115200); |
| 43 | + while (!Serial) {} |
| 44 | + |
| 45 | + Debug.setDebugLevel(DBG_VERBOSE); |
| 46 | + |
| 47 | + int err = -1; |
| 48 | + if ((err = fs.reformat(&mbr)) != 0) |
| 49 | + { |
| 50 | + DEBUG_ERROR("%s: fs.reformat() failed with %d", __FUNCTION__, err); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + FILE * file = fopen("/ota/UPDATE.BIN.OTA", "wb"); |
| 55 | + if (!file) |
| 56 | + { |
| 57 | + DEBUG_ERROR("%s: fopen() failed", __FUNCTION__); |
| 58 | + fclose(file); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + DEBUG_INFO("Start copy update file on QSPI flash."); |
| 63 | + for(int i = 0; i < OTAUsage_ino_PORTENTA_C33_ota_len; i++) |
| 64 | + { |
| 65 | + char const c = OTAUsage_ino_PORTENTA_C33_ota[i]; |
| 66 | + |
| 67 | + if (fwrite(&c, 1, sizeof(c), file) != sizeof(c)) |
| 68 | + { |
| 69 | + DEBUG_ERROR("%s: Writing of firmware image to flash failed", __FUNCTION__); |
| 70 | + fclose(file); |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + fclose(file); |
| 76 | + |
| 77 | + /* Unmount the filesystem. */ |
| 78 | + if ((err = fs.unmount()) != 0) |
| 79 | + { |
| 80 | + DEBUG_ERROR("%s: fs.unmount() failed with %d", __FUNCTION__, err); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + delay(1000); |
| 85 | + |
| 86 | + DEBUG_INFO("Board reset to trigger the update."); |
| 87 | + DEBUG_INFO("Do not reset or disconnect the board."); |
| 88 | + DEBUG_INFO("Wait until the RGB LED lights up with different colours."); |
| 89 | + NVIC_SystemReset(); |
| 90 | +} |
| 91 | + |
| 92 | +void loop() |
| 93 | +{ |
| 94 | + |
| 95 | +} |
0 commit comments