Skip to content

Commit 8519134

Browse files
pennamgiulcioffi
authored andcommitted
WiFiFirmwareUpdated:do not format/partition OTA data. Improve progress feedback
1 parent 883c1e9 commit 8519134

File tree

1 file changed

+65
-15
lines changed

1 file changed

+65
-15
lines changed

libraries/STM32H747_System/examples/WiFiFirmwareUpdater/WiFiFirmwareUpdater.ino

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
QSPIFBlockDevice root(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000);
1212
mbed::MBRBlockDevice wifi_data(&root, 1);
13-
mbed::MBRBlockDevice ota_data(&root, 2);
1413
mbed::FATFileSystem wifi_data_fs("wlan");
15-
mbed::FATFileSystem ota_data_fs("fs");
1614

1715
long getFileSize(FILE *fp) {
1816
fseek(fp, 0, SEEK_END);
@@ -22,13 +20,26 @@ long getFileSize(FILE *fp) {
2220
return size;
2321
}
2422

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+
2537
void setup() {
2638

2739
Serial.begin(115200);
2840
while (!Serial);
2941

3042
mbed::MBRBlockDevice::partition(&root, 1, 0x0B, 0, 1024 * 1024);
31-
mbed::MBRBlockDevice::partition(&root, 2, 0x0B, 1024 * 1024, 14 * 1024 * 1024);
3243
// use space from 15.5MB to 16 MB for another fw, memory mapped
3344

3445
int err = wifi_data_fs.mount(&wifi_data);
@@ -42,14 +53,6 @@ void setup() {
4253
err = wifi_data_fs.reformat(&wifi_data);
4354
}
4455

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-
5356
DIR *dir;
5457
struct dirent *ent;
5558

@@ -81,18 +84,65 @@ void setup() {
8184
extern const unsigned char wifi_firmware_image_data[];
8285
extern const resource_hnd_t wifi_firmware_image;
8386
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+
}
85104
fclose(fp);
86105

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+
}
88123

124+
chunck_size = 128;
125+
byte_count = 0;
89126
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+
}
91141
fclose(fp);
92142

93143
fp = fopen("/wlan/cacert.pem", "rb");
94144
char buffer[128];
95-
ret = fread(buffer, 1, 128, fp);
145+
int ret = fread(buffer, 1, 128, fp);
96146
Serial.write(buffer, ret);
97147
while (ret == 128) {
98148
ret = fread(buffer, 1, 128, fp);

0 commit comments

Comments
 (0)