@@ -29,7 +29,7 @@ static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK";
29
29
30
30
FlashClass mcu_flash;
31
31
32
- NBFileUtils fileUtils;
32
+ NBFileUtils fileUtils ( true ) ;
33
33
34
34
extern " C" void __libc_init_array (void );
35
35
@@ -43,44 +43,49 @@ int main()
43
43
44
44
constexpr size_t blockSize = 512 ;
45
45
fileUtils.begin ();
46
- Serial1.begin (115200 );
47
- Serial1.print (" SBU start. " );
48
46
49
47
bool update_success = false ;
50
48
51
49
// Try to update only if update file
52
50
// has been download successfully.
53
51
54
- if (fileUtils.listFile (CHECK_FILE_NAME)) {
55
- Serial1.println (" Update file exists" );
52
+ if (fileUtils.existFile (CHECK_FILE_NAME)) {
56
53
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 ;
58
56
59
57
if (updateSize > SBU_SIZE) {
60
58
updateSize = updateSize - SBU_SIZE - SBU_START;
59
+ size_t cycles = (updateSize / blockSize);
60
+ size_t spare_bytes = (updateSize % blockSize);
61
61
/* Erase the MCU flash */
62
62
uint32_t flash_address = (uint32_t )SKETCH_START;
63
63
mcu_flash.erase ((void *)flash_address, updateSize);
64
64
65
65
for (auto i = 0 ; i < cycles; i++) {
66
66
uint8_t block[blockSize] { 0 };
67
67
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);
69
79
digitalWrite (LED_BUILTIN, HIGH);
70
80
mcu_flash.write ((void *)flash_address, block, read_bytes);
71
81
flash_address += read_bytes;
72
82
}
73
83
update_success = true ;
74
84
}
75
85
if (update_success) {
76
- fileUtils.deleteFile (UPDATE_FILE_NAME);
77
- fileUtils.deleteFile (CHECK_FILE_NAME);
86
+ fileUtils.deleteFiles ();
78
87
}
79
88
}
80
- else {
81
- Serial1.println (" Update file does not exist" );
82
- delay (100 );
83
- }
84
89
85
90
boot:
86
91
/* Jump to the sketch */
0 commit comments