21
21
**************************************************************************************/
22
22
23
23
#include < FlashStorage.h>
24
- #include " MKRGSM.h"
24
+ #include < MKRGSM.h>
25
+
25
26
26
27
/* *************************************************************************************
27
28
DEFINE
36
37
GLOBAL CONSTANTS
37
38
**************************************************************************************/
38
39
39
- static char const UPDATE_FILE_NAME[] = " UPDATE.BIN" ;
40
+ static constexpr char UPDATE_FILE_NAME[] = " UPDATE.BIN" ;
41
+ static constexpr char CHECK_FILE_NAME[] = " UPDATE.OK" ;
42
+
40
43
41
44
/* *************************************************************************************
42
45
GLOBAL VARIABLES
43
46
**************************************************************************************/
44
47
45
48
FlashClass mcu_flash;
46
- GSMFileUtils fileUtils (false );
49
+
50
+ GSMFileUtils fileUtils;
51
+
47
52
/* *************************************************************************************
48
53
FUNCTION DECLARATION
49
54
**************************************************************************************/
@@ -62,16 +67,18 @@ int main()
62
67
63
68
delay (1 );
64
69
65
- String filename = UPDATE_FILE_NAME ;
70
+ constexpr size_t blockSize = 512 ;
66
71
67
- const size_t blockSize = 512 ;
68
-
69
- MODEM.begin ();
70
72
fileUtils.begin ();
73
+
71
74
bool update_success = false ;
72
- if (fileUtils.listFile (" UPDATE.OK" ) == 1 ) {
73
- auto size = fileUtils.listFile (filename);
74
- auto cycles = (size / blockSize) + 1 ;
75
+
76
+ // Try to update only if update file
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
+
75
82
if (size > SSU_SIZE) {
76
83
size -= SSU_SIZE;
77
84
@@ -81,17 +88,17 @@ int main()
81
88
82
89
for (auto i = 0 ; i < cycles; i++) {
83
90
uint8_t block[blockSize] { 0 };
84
- digitalWrite (LED_BUILTIN,LOW);
85
- auto read = fileUtils.readBlock (filename , (i * blockSize) + SSU_SIZE, blockSize, block);
86
- digitalWrite (LED_BUILTIN,HIGH);
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);
87
94
mcu_flash.write ((void *)flash_address, block, read );
88
95
flash_address += read ;
89
96
}
90
97
update_success = true ;
91
98
}
92
99
if (update_success) {
93
- fileUtils.deleteFile (filename );
94
- fileUtils.deleteFile (" UPDATE.OK " );
100
+ fileUtils.deleteFile (UPDATE_FILE_NAME );
101
+ fileUtils.deleteFile (CHECK_FILE_NAME );
95
102
}
96
103
}
97
104
/* Jump to the sketch */
0 commit comments