10
10
11
11
QSPIFBlockDevice root (QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000 );
12
12
mbed::MBRBlockDevice wifi_data (&root, 1 );
13
- mbed::MBRBlockDevice ota_data (&root, 2 );
14
13
mbed::FATFileSystem wifi_data_fs (" wlan" );
15
- mbed::FATFileSystem ota_data_fs (" fs" );
16
14
17
15
long getFileSize (FILE *fp) {
18
16
fseek (fp, 0 , SEEK_END);
@@ -22,13 +20,26 @@ long getFileSize(FILE *fp) {
22
20
return size;
23
21
}
24
22
23
+ void printProgress (uint32_t offset, uint32_t size, uint32_t threshold, bool reset) {
24
+ static int percent_done = 0 ;
25
+ if (reset == true ) {
26
+ percent_done = 0 ;
27
+ Serial.println (" Flashed " + String (percent_done) + " %" );
28
+ } else {
29
+ uint32_t percent_done_new = offset * 100 / size;
30
+ if (percent_done_new >= percent_done + threshold) {
31
+ percent_done = percent_done_new;
32
+ Serial.println (" Flashed " + String (percent_done) + " %" );
33
+ }
34
+ }
35
+ }
36
+
25
37
void setup () {
26
38
27
39
Serial.begin (115200 );
28
40
while (!Serial);
29
41
30
42
mbed::MBRBlockDevice::partition (&root, 1 , 0x0B , 0 , 1024 * 1024 );
31
- mbed::MBRBlockDevice::partition (&root, 2 , 0x0B , 1024 * 1024 , 14 * 1024 * 1024 );
32
43
// use space from 15.5MB to 16 MB for another fw, memory mapped
33
44
34
45
int err = wifi_data_fs.mount (&wifi_data);
@@ -42,14 +53,6 @@ void setup() {
42
53
err = wifi_data_fs.reformat (&wifi_data);
43
54
}
44
55
45
- err = ota_data_fs.mount (&ota_data);
46
- if (err) {
47
- // Reformat if we can't mount the filesystem
48
- // this should only happen on the first boot
49
- Serial.println (" No filesystem for OTA firmware was found, creating" );
50
- err = ota_data_fs.reformat (&ota_data);
51
- }
52
-
53
56
DIR *dir;
54
57
struct dirent *ent;
55
58
@@ -81,18 +84,65 @@ void setup() {
81
84
extern const unsigned char wifi_firmware_image_data[];
82
85
extern const resource_hnd_t wifi_firmware_image;
83
86
FILE* fp = fopen (" /wlan/4343WA1.BIN" , " wb" );
84
- int ret = fwrite (wifi_firmware_image_data, 421098 , 1 , fp);
87
+ const int file_size = 421098 ;
88
+ int chunck_size = 1024 ;
89
+ int byte_count = 0 ;
90
+
91
+ Serial.println (" Flashing /wlan/4343WA1.BIN file" );
92
+ printProgress (byte_count, file_size, 10 , true );
93
+ while (byte_count < file_size) {
94
+ if (byte_count + chunck_size > file_size)
95
+ chunck_size = file_size - byte_count;
96
+ int ret = fwrite (&wifi_firmware_image_data[byte_count], chunck_size, 1 , fp);
97
+ if (ret != 1 ) {
98
+ Serial.println (" Error writing firmware data" );
99
+ break ;
100
+ }
101
+ byte_count += chunck_size;
102
+ printProgress (byte_count, file_size, 10 , false );
103
+ }
85
104
fclose (fp);
86
105
87
- root.program (wifi_firmware_image_data, 15 * 1024 * 1024 + 1024 * 512 , 421098 );
106
+ chunck_size = 1024 ;
107
+ byte_count = 0 ;
108
+ const uint32_t offset = 15 * 1024 * 1024 + 1024 * 512 ;
109
+
110
+ Serial.println (" Flashing memory mapped firmware" );
111
+ printProgress (byte_count, file_size, 10 , true );
112
+ while (byte_count < file_size) {
113
+ if (byte_count + chunck_size > file_size)
114
+ chunck_size = file_size - byte_count;
115
+ int ret = root.program (wifi_firmware_image_data, offset + byte_count, chunck_size);
116
+ if (ret != 0 ) {
117
+ Serial.println (" Error writing firmware data" );
118
+ break ;
119
+ }
120
+ byte_count += chunck_size;
121
+ printProgress (byte_count, file_size, 10 , false );
122
+ }
88
123
124
+ chunck_size = 128 ;
125
+ byte_count = 0 ;
89
126
fp = fopen (" /wlan/cacert.pem" , " wb" );
90
- ret = fwrite (cacert_pem, cacert_pem_len, 1 , fp);
127
+
128
+ Serial.println (" Flashing certificates" );
129
+ printProgress (byte_count, cacert_pem_len, 10 , true );
130
+ while (byte_count < cacert_pem_len) {
131
+ if (byte_count + chunck_size > cacert_pem_len)
132
+ chunck_size = cacert_pem_len - byte_count;
133
+ int ret = fwrite (&cacert_pem[byte_count], chunck_size, 1 ,fp);
134
+ if (ret != 1 ) {
135
+ Serial.println (" Error writing certificates" );
136
+ break ;
137
+ }
138
+ byte_count += chunck_size;
139
+ printProgress (byte_count, cacert_pem_len, 10 , false );
140
+ }
91
141
fclose (fp);
92
142
93
143
fp = fopen (" /wlan/cacert.pem" , " rb" );
94
144
char buffer[128 ];
95
- ret = fread (buffer, 1 , 128 , fp);
145
+ int ret = fread (buffer, 1 , 128 , fp);
96
146
Serial.write (buffer, ret);
97
147
while (ret == 128 ) {
98
148
ret = fread (buffer, 1 , 128 , fp);
0 commit comments