@@ -29,7 +29,7 @@ static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK";
2929
3030FlashClass mcu_flash;
3131
32- NBFileUtils fileUtils;
32+ NBFileUtils fileUtils ( true ) ;
3333
3434extern " C" void __libc_init_array (void );
3535
@@ -43,44 +43,49 @@ int main()
4343
4444 constexpr size_t blockSize = 512 ;
4545 fileUtils.begin ();
46- Serial1.begin (115200 );
47- Serial1.print (" SBU start. " );
4846
4947 bool update_success = false ;
5048
5149 // Try to update only if update file
5250 // has been download successfully.
5351
54- if (fileUtils.listFile (CHECK_FILE_NAME)) {
55- Serial1.println (" Update file exists" );
52+ if (fileUtils.existFile (CHECK_FILE_NAME)) {
5653 uint32_t updateSize = fileUtils.listFile (UPDATE_FILE_NAME);
57- size_t cycles = (updateSize / blockSize) + 1 ;
54+ uint32_t tot_bytes = 0 ;
55+ uint32_t read_bytes = 0 ;
5856
5957 if (updateSize > SBU_SIZE) {
6058 updateSize = updateSize - SBU_SIZE - SBU_START;
59+ size_t cycles = (updateSize / blockSize);
60+ size_t spare_bytes = (updateSize % blockSize);
6161 /* Erase the MCU flash */
6262 uint32_t flash_address = (uint32_t )SKETCH_START;
6363 mcu_flash.erase ((void *)flash_address, updateSize);
6464
6565 for (auto i = 0 ; i < cycles; i++) {
6666 uint8_t block[blockSize] { 0 };
6767 digitalWrite (LED_BUILTIN, LOW);
68- uint32_t read_bytes = fileUtils.readBlock (UPDATE_FILE_NAME, (i * blockSize) + SBU_SIZE + SBU_START, blockSize, block);
68+ read_bytes = fileUtils.readBlock (UPDATE_FILE_NAME, (i * blockSize) + SBU_SIZE + SBU_START, blockSize, block);
69+ digitalWrite (LED_BUILTIN, HIGH);
70+ mcu_flash.write ((void *)flash_address, block, read_bytes);
71+ flash_address += read_bytes;
72+ tot_bytes += read_bytes;
73+ }
74+
75+ if (spare_bytes){
76+ uint8_t block[spare_bytes] { 0 };
77+ digitalWrite (LED_BUILTIN, LOW);
78+ read_bytes = fileUtils.readBlock (UPDATE_FILE_NAME, tot_bytes + SBU_SIZE + SBU_START, spare_bytes, block);
6979 digitalWrite (LED_BUILTIN, HIGH);
7080 mcu_flash.write ((void *)flash_address, block, read_bytes);
7181 flash_address += read_bytes;
7282 }
7383 update_success = true ;
7484 }
7585 if (update_success) {
76- fileUtils.deleteFile (UPDATE_FILE_NAME);
77- fileUtils.deleteFile (CHECK_FILE_NAME);
86+ fileUtils.deleteFiles ();
7887 }
7988 }
80- else {
81- Serial1.println (" Update file does not exist" );
82- delay (100 );
83- }
8489
8590boot:
8691 /* Jump to the sketch */
0 commit comments