|
23 | 23 | #include <FlashStorage.h>
|
24 | 24 | #include <MKRGSM.h>
|
25 | 25 |
|
| 26 | +#include "lzss.h" |
26 | 27 |
|
27 | 28 | /**************************************************************************************
|
28 | 29 | DEFINE
|
|
37 | 38 | GLOBAL CONSTANTS
|
38 | 39 | **************************************************************************************/
|
39 | 40 |
|
40 |
| -static constexpr char UPDATE_FILE_NAME[] = "UPDATE.BIN"; |
41 |
| -static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK"; |
42 |
| - |
| 41 | + const char * UPDATE_FILE_NAME = "UPDATE.BIN"; |
| 42 | + const char * UPDATE_FILE_NAME_LZSS = "UPDATE.BIN.LZSS"; |
| 43 | +static const char * CHECK_FILE_NAME = "UPDATE.OK"; |
43 | 44 |
|
44 | 45 | /**************************************************************************************
|
45 | 46 | GLOBAL VARIABLES
|
46 | 47 | **************************************************************************************/
|
47 | 48 |
|
48 | 49 | FlashClass mcu_flash;
|
49 |
| - |
50 |
| -GSMFileUtils fileUtils; |
| 50 | +GSMFileUtils fileUtils; |
51 | 51 |
|
52 | 52 | /**************************************************************************************
|
53 | 53 | FUNCTION DECLARATION
|
@@ -75,29 +75,59 @@ int main()
|
75 | 75 |
|
76 | 76 | // Try to update only if update file
|
77 | 77 | // has been download successfully.
|
78 |
| - if (fileUtils.listFile(CHECK_FILE_NAME) == 1) { |
79 |
| - uint32_t size = fileUtils.listFile(UPDATE_FILE_NAME); |
80 |
| - size_t cycles = (size / blockSize) + 1; |
81 |
| - |
82 |
| - if (size > SSU_SIZE) { |
83 |
| - size -= SSU_SIZE; |
84 |
| - |
85 |
| - /* Erase the MCU flash */ |
86 |
| - uint32_t flash_address = (uint32_t)SKETCH_START; |
87 |
| - mcu_flash.erase((void*)flash_address, size); |
88 |
| - |
89 |
| - for (auto i = 0; i < cycles; i++) { |
90 |
| - uint8_t block[blockSize] { 0 }; |
91 |
| - digitalWrite(LED_BUILTIN, LOW); |
92 |
| - uint32_t read = fileUtils.readBlock(UPDATE_FILE_NAME, (i * blockSize) + SSU_SIZE, blockSize, block); |
93 |
| - digitalWrite(LED_BUILTIN, HIGH); |
94 |
| - mcu_flash.write((void*)flash_address, block, read); |
95 |
| - flash_address += read; |
96 |
| - } |
| 78 | + if (fileUtils.listFile(CHECK_FILE_NAME) > 0) |
| 79 | + { |
| 80 | + /* This is for LZSS compressed binaries. */ |
| 81 | + if (fileUtils.listFile(UPDATE_FILE_NAME_LZSS) > 0) |
| 82 | + { |
| 83 | + /* Erase the complete flash starting from the SSU forward |
| 84 | + * because we've got no possibility of knowing how large |
| 85 | + * the decompressed binary will finally be. |
| 86 | + */ |
| 87 | + mcu_flash.erase((void*)SKETCH_START, 0x40000 - (uint32_t)SKETCH_START); |
| 88 | + /* Initialize the lzss module with the data which |
| 89 | + * it requires. |
| 90 | + */ |
| 91 | + lzss_init((uint32_t)SKETCH_START); |
| 92 | + /* During the process of decoding UPDATE.BIN.LZSS |
| 93 | + * is decompressed and stored as UPDATE.BIN. |
| 94 | + */ |
| 95 | + lzss_decode(); |
| 96 | + /* Write the data remaining in the write buffer to |
| 97 | + * the file. |
| 98 | + */ |
| 99 | + lzss_flush(); |
| 100 | + /* Signal a successul update. */ |
97 | 101 | update_success = true;
|
98 | 102 | }
|
| 103 | + /* This is for uncompressed binaries. */ |
| 104 | + else if (fileUtils.listFile(UPDATE_FILE_NAME) > 0) |
| 105 | + { |
| 106 | + uint32_t size = fileUtils.listFile(UPDATE_FILE_NAME); |
| 107 | + size_t cycles = (size / blockSize) + 1; |
| 108 | + |
| 109 | + if (size > SSU_SIZE) { |
| 110 | + size -= SSU_SIZE; |
| 111 | + |
| 112 | + /* Erase the MCU flash */ |
| 113 | + uint32_t flash_address = (uint32_t)SKETCH_START; |
| 114 | + mcu_flash.erase((void*)flash_address, size); |
| 115 | + |
| 116 | + for (auto i = 0; i < cycles; i++) { |
| 117 | + uint8_t block[blockSize] { 0 }; |
| 118 | + digitalWrite(LED_BUILTIN, LOW); |
| 119 | + uint32_t read = fileUtils.readBlock(UPDATE_FILE_NAME, (i * blockSize) + SSU_SIZE, blockSize, block); |
| 120 | + digitalWrite(LED_BUILTIN, HIGH); |
| 121 | + mcu_flash.write((void*)flash_address, block, read); |
| 122 | + flash_address += read; |
| 123 | + } |
| 124 | + update_success = true; |
| 125 | + } |
| 126 | + } |
| 127 | + /* Clean up in case of success */ |
99 | 128 | if (update_success) {
|
100 | 129 | fileUtils.deleteFile(UPDATE_FILE_NAME);
|
| 130 | + fileUtils.deleteFile(UPDATE_FILE_NAME_LZSS); |
101 | 131 | fileUtils.deleteFile(CHECK_FILE_NAME);
|
102 | 132 | }
|
103 | 133 | }
|
|
0 commit comments