Skip to content

Commit cc8fdd6

Browse files
committed
Storage: add QSPIFReadPartitions example sketch
Former-commit-id: ae9a544
1 parent c60b751 commit cc8fdd6

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Portenta C33 - QSPI Read partitions
3+
4+
The sketch reads the partition table of QSPI flash and prints the
5+
results on the serial monitor
6+
7+
This example code is in the public domain.
8+
*/
9+
10+
#include <BlockDevice.h>
11+
12+
struct __attribute__((packed)) mbrEntry {
13+
uint8_t status;
14+
uint8_t chsStart[3];
15+
uint8_t type;
16+
uint8_t chsStop[3];
17+
uint32_t lbaOffset;
18+
uint32_t lbaSize;
19+
};
20+
21+
struct __attribute__((packed)) mbrTable {
22+
mbrEntry entries[4];
23+
uint8_t signature[2];
24+
};
25+
26+
unsigned long allocatedSpace {};
27+
28+
void setup()
29+
{
30+
Serial.begin(9600);
31+
for (const auto timeout = millis() + 2500; !Serial && millis() < timeout; delay(250))
32+
;
33+
34+
auto bd = BlockDevice::get_default_instance();
35+
auto ret = bd->init();
36+
if (ret) {
37+
Serial.println("ERROR! Unable to read the Block Device.");
38+
while (true)
39+
;
40+
}
41+
42+
// Allocate smallest buffer necessary to write MBR
43+
auto buffer_size = std::max<uint32_t>(bd->get_program_size(), sizeof(mbrTable));
44+
45+
// Prevent alignment issues
46+
if (buffer_size % bd->get_program_size() != 0) {
47+
buffer_size += bd->get_program_size() - (buffer_size % bd->get_program_size());
48+
}
49+
50+
auto buffer = new uint8_t[buffer_size];
51+
52+
// Check for existing MBR
53+
ret = bd->read(buffer, 512 - buffer_size, buffer_size);
54+
if (ret) {
55+
Serial.println("ERROR! Unable to read the Master Boot Record");
56+
57+
delete[] buffer;
58+
while (true)
59+
;
60+
}
61+
62+
auto table_start_offset = buffer_size - sizeof(mbrTable);
63+
auto table = reinterpret_cast<mbrTable*>(&buffer[table_start_offset]);
64+
65+
Serial.println();
66+
Serial.print("Looking for Partitions on the Flash Memory... ");
67+
68+
if (table->signature[0] != 0x55 || table->signature[1] != 0xAA) {
69+
Serial.println("MBR Not Found");
70+
Serial.println("Flash Memory doesn't have partitions.");
71+
} else {
72+
73+
Serial.println("MBR Found");
74+
Serial.print("Boot Signature: 0x");
75+
Serial.print(table->signature[0], HEX);
76+
Serial.println(table->signature[1], HEX);
77+
78+
Serial.println();
79+
Serial.println("Printing Partitions Table and Info...");
80+
81+
auto part { 1u };
82+
for (auto const& entry : table->entries) {
83+
Serial.println("================================");
84+
Serial.print("Partition: ");
85+
Serial.println(part++);
86+
87+
Serial.print("Bootable: ");
88+
Serial.println(entry.status == 0 ? "No" : "Yes");
89+
90+
Serial.print("Type: 0x");
91+
if (entry.type < 0x10)
92+
Serial.print(0);
93+
Serial.println(entry.type, HEX);
94+
95+
if (entry.type == 0x00)
96+
continue;
97+
98+
Serial.print("Size [KBytes]: ");
99+
Serial.println((entry.lbaSize * 4096) >> 10);
100+
101+
allocatedSpace += entry.lbaSize * 4096;
102+
103+
Serial.print("Start [C/H/S]: ");
104+
Serial.print(entry.chsStart[0]);
105+
Serial.print("/");
106+
Serial.print(entry.chsStart[1]);
107+
Serial.print("/");
108+
Serial.println(entry.chsStart[2]);
109+
110+
Serial.print("Stop [C/H/S]: ");
111+
Serial.print(entry.chsStop[0]);
112+
Serial.print("/");
113+
Serial.print(entry.chsStop[1]);
114+
Serial.print("/");
115+
Serial.println(entry.chsStop[2]);
116+
117+
Serial.println();
118+
}
119+
120+
Serial.println();
121+
Serial.println("No more partitions are present.");
122+
}
123+
124+
Serial.println();
125+
Serial.print("Total Space [KBytes]: ");
126+
Serial.println(bd->size() >> 10);
127+
Serial.print("Allocated Space [KBytes]: ");
128+
Serial.println(allocatedSpace >> 10);
129+
Serial.print("Unallocated Space [KBytes]: ");
130+
Serial.println((bd->size() - allocatedSpace) >> 10);
131+
}
132+
133+
void loop()
134+
{
135+
delay(10000);
136+
}

0 commit comments

Comments
 (0)