Skip to content

Commit 448510a

Browse files
erasing flash before creating a new partitioning scheme
1 parent 53a89c3 commit 448510a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

libraries/STM32H747_System/examples/QSPIFormat/QSPIFormat.ino

+26
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,32 @@ void setup() {
6060
Serial.println("Do you want to proceed? Y/[n]");
6161

6262
if (true == waitResponse()) {
63+
if (root.init() != QSPIF_BD_ERROR_OK) {
64+
Serial.println(F("Error: QSPI init failure."));
65+
return;
66+
}
67+
68+
const size_t pts = 512; // MBR partition table size = 512b
69+
const size_t erase_size = root.get_erase_size(0x0);
70+
71+
size_t remainder = pts % erase_size;
72+
size_t division = pts / erase_size;
73+
74+
uint8_t *buf = nullptr;
75+
76+
// save the data that exceeded the size of MBR partition table and write it back
77+
if(remainder != 0) {
78+
buf = (uint8_t*) malloc(erase_size - remainder);
79+
root.read(buf, division*erase_size, sizeof(buf));
80+
}
81+
82+
root.erase(0x0, erase_size*(division + remainder == 0? 0 : 1)); // clear the MBR partition table
83+
84+
if(remainder != 0) {
85+
root.program(buf, division*erase_size, sizeof(buf));
86+
free(buf);
87+
}
88+
6389
mbed::MBRBlockDevice::partition(&root, 1, 0x0B, 0, 1024 * 1024);
6490
if(default_scheme) {
6591
mbed::MBRBlockDevice::partition(&root, 3, 0x0B, 14 * 1024 * 1024, 14 * 1024 * 1024);

0 commit comments

Comments
 (0)