Skip to content

Commit bc96f6e

Browse files
committed
Portenta: add update bootloader sketch
1 parent b0ea6ed commit bc96f6e

File tree

2 files changed

+10959
-0
lines changed

2 files changed

+10959
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "FlashIAP.h"
2+
#include "bootloader.h"
3+
4+
#define BOOTLOADER_ADDR (0x8000000)
5+
mbed::FlashIAP flash;
6+
7+
uint8_t* bootloader_data = (uint8_t*)(0x801F000);
8+
9+
void setup() {
10+
// put your setup code here, to run once:
11+
Serial.begin(115200);
12+
while (!Serial) {}
13+
14+
Serial.println("Validation: " + String(bootloader_data[0], HEX));
15+
Serial.println("BL version: " + String(bootloader_data[1]));
16+
Serial.println("Clock source: " + String(bootloader_data[2]));
17+
Serial.println("USB Speed: " + String(bootloader_data[3]));
18+
Serial.println("Ethernet: " + String(bootloader_data[4]));
19+
Serial.println("Wifi: " + String(bootloader_data[5]));
20+
Serial.println("RAM size: " + String(bootloader_data[6]));
21+
Serial.println("QSPI size: " + String(bootloader_data[7]));
22+
Serial.println("Video: " + String(bootloader_data[8]));
23+
Serial.println("Crypto: " + String(bootloader_data[9]));
24+
25+
if (bootloader_data[1] < 15) {
26+
Serial.println("New bootloader version available");
27+
}
28+
Serial.println("Update bootloader? Y/[n]");
29+
bool confirmation = false;
30+
while (confirmation == false) {
31+
if (Serial.available()) {
32+
char choice = Serial.read();
33+
switch (choice) {
34+
case 'y':
35+
case 'Y':
36+
applyUpdate(BOOTLOADER_ADDR);
37+
confirmation = true;
38+
break;
39+
case 'n':
40+
case 'N':
41+
confirmation = true;
42+
break;
43+
default:
44+
continue;
45+
}
46+
}
47+
}
48+
}
49+
50+
void applyUpdate(uint32_t address)
51+
{
52+
long len = envie_bootloader_mbed_bin_len;
53+
54+
flash.init();
55+
56+
const uint32_t page_size = flash.get_page_size();
57+
char *page_buffer = new char[page_size];
58+
uint32_t addr = address;
59+
uint32_t next_sector = addr + flash.get_sector_size(addr);
60+
bool sector_erased = false;
61+
size_t pages_flashed = 0;
62+
uint32_t percent_done = 0;
63+
64+
while (true) {
65+
66+
if (page_size * pages_flashed > len) {
67+
break;
68+
}
69+
70+
// Erase this page if it hasn't been erased
71+
if (!sector_erased) {
72+
flash.erase(addr, flash.get_sector_size(addr));
73+
sector_erased = true;
74+
}
75+
76+
// Program page
77+
flash.program(&envie_bootloader_mbed_bin[page_size * pages_flashed], addr, page_size);
78+
79+
addr += page_size;
80+
if (addr >= next_sector) {
81+
next_sector = addr + flash.get_sector_size(addr);
82+
sector_erased = false;
83+
}
84+
85+
if (++pages_flashed % 3 == 0) {
86+
uint32_t percent_done_new = page_size * pages_flashed * 100 / len;
87+
if (percent_done != percent_done_new) {
88+
percent_done = percent_done_new;
89+
Serial.println("Flashed " + String(percent_done) + "%");
90+
}
91+
}
92+
}
93+
Serial.println("Flashed 100%");
94+
95+
delete[] page_buffer;
96+
97+
flash.deinit();
98+
Serial.println("Bootloader update complete. You may now disconnect the board.");
99+
}
100+
101+
void loop() {
102+
// put your main code here, to run repeatedly:
103+
delay(1000);
104+
}

0 commit comments

Comments
 (0)