Skip to content

Commit 915a785

Browse files
committed
Storage: add example to test codeflash
1 parent 0686d38 commit 915a785

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Portenta C33 - Test Code Flash
3+
4+
The sketch shows how to use code flash and tests read write and
5+
erase operations.
6+
7+
This example code is in the public domain.
8+
*/
9+
10+
#include <Arduino.h>
11+
#include <BlockDevice.h>
12+
#include <CodeFlashBlockDevice.h>
13+
14+
CodeFlashBlockDevice& root = CodeFlashBlockDevice::getInstance();
15+
16+
#define WRITE_SIZE (16 * 1024)
17+
18+
byte w_buffer[WRITE_SIZE];
19+
byte r_buffer[WRITE_SIZE];
20+
21+
void setup() {
22+
Serial.begin(9600);
23+
while(!Serial) {
24+
25+
}
26+
27+
Serial.println("QSPIFlash Test");
28+
29+
int ret = root.init();
30+
if(ret) {
31+
Serial.println("Error opening QSPIFlash device");
32+
while(1){}
33+
}
34+
35+
Serial.println("QSPIFlash init done");
36+
37+
randomSeed(analogRead(A0));
38+
39+
}
40+
41+
void loop() {
42+
43+
// Select a random pattern to write flash
44+
byte pattern = random(255);
45+
46+
// Select a random size for the test buffer
47+
int number = random(128, WRITE_SIZE);
48+
int repeat = number & ~(root.get_program_size() - 1);
49+
50+
// Select a random start address in region 0 after the bootloader and compute block start offset
51+
int address = random(0x00004000, 0x00010000 - WRITE_SIZE);
52+
int offset = address & ~(root.get_erase_size(address) - 1);
53+
54+
// Start Test
55+
erase_write_read_compare(pattern, offset, repeat);
56+
57+
// Select a random start address in region 1 after the sketch and compute block start offset
58+
address = random(0x000A0000, root.size() - WRITE_SIZE);
59+
offset = address & ~(root.get_erase_size(address) - 1);
60+
61+
// Start Test
62+
erase_write_read_compare(pattern, offset, repeat);
63+
}
64+
65+
void erase_write_read_compare(byte pattern, int offset, int repeat) {
66+
Serial.print("Using pattern ");
67+
Serial.print(pattern, HEX);
68+
Serial.print(" ");
69+
Serial.print(repeat);
70+
Serial.print(" times starting from ");
71+
Serial.println(offset, HEX);
72+
73+
memset(&w_buffer[0], pattern, repeat);
74+
75+
// Check if we need to delete more consecutive blocks
76+
// Should happen only in region 0 due to write buffer size
77+
int erase_size = root.get_erase_size(offset);
78+
while(erase_size < repeat) {
79+
erase_size += erase_size;
80+
}
81+
root.erase(offset, erase_size);
82+
83+
// Write pattern
84+
root.program(&w_buffer[0], offset, repeat);
85+
86+
// Readback
87+
root.read(&r_buffer[0], offset, repeat);
88+
//dump_buffer(&r_buffer[0], repeat);
89+
90+
// Compare
91+
if(memcmp(&w_buffer[0], &r_buffer[0], repeat)) {
92+
Serial.println("Error comparing buffers, dumping content...");
93+
Serial.println("Write buffer:");
94+
dump_buffer(&w_buffer[0], repeat);
95+
Serial.println("Read buffer:");
96+
dump_buffer(&r_buffer[0], repeat);
97+
while(1){}
98+
}
99+
delay(5);
100+
}
101+
102+
void dump_buffer(uint8_t *b, uint32_t len) {
103+
if (b != nullptr) {
104+
Serial.println("");
105+
for(int i = 0; i < len; i++) {
106+
if(i != 0 && i % 32 == 0) {
107+
if(i != 0)
108+
Serial.println();
109+
}
110+
Serial.print(*(b + i) >> 4, HEX);
111+
Serial.print(*(b + i) & 0x0F,HEX);
112+
}
113+
Serial.println();
114+
Serial.println("");
115+
}
116+
}

0 commit comments

Comments
 (0)