Skip to content

Commit be94dcd

Browse files
authored
Backport esp8266#7514 (part3)
1 parent 06aa6cc commit be94dcd

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#include <BSTest.h>
2+
#include <ESP8266WiFi.h>
3+
4+
BS_ENV_DECLARE();
5+
6+
void preinit() {
7+
// (no C++ in function)
8+
// disable wifi
9+
ESP8266WiFiClass::preinitWiFiOff();
10+
}
11+
12+
void setup()
13+
{
14+
Serial.begin(115200);
15+
BS_RUN(Serial);
16+
}
17+
18+
bool pretest()
19+
{
20+
return true;
21+
}
22+
23+
bool compareBuffers(uint32_t *first, uint32_t *second, size_t offset, size_t len)
24+
{
25+
uint8_t *firstBytes = (uint8_t *)first;
26+
uint8_t *secondBytes = (uint8_t *)second;
27+
28+
for (size_t i = offset; i < offset + len; i++)
29+
{
30+
if (firstBytes[i] != secondBytes[i])
31+
{
32+
Serial.printf("Compare fail @ %u\n", i);
33+
for (size_t j = i & ~3; j < (i & ~3) + 4; j++)
34+
{
35+
Serial.printf("%02x ", firstBytes[j]);
36+
}
37+
Serial.println();
38+
for (size_t j = i & ~3; j < (i & ~3) + 4; j++)
39+
{
40+
Serial.printf("%02x ", secondBytes[j]);
41+
}
42+
Serial.println();
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
49+
bool testFlash(uint32_t start_offset, uint8_t data_offset, size_t amount)
50+
{
51+
static uint32_t *write_buffer = (uint32_t *)malloc(4096);
52+
static uint32_t *read_buffer = (uint32_t *)malloc(4096);
53+
54+
for (uint32_t i = 0; i < 1024; i++)
55+
{
56+
write_buffer[i] = (i + 100) * 33;
57+
read_buffer[i] = 0xAAAAAAAA;
58+
}
59+
Serial.println("---------------------------------------------------");
60+
ESP.flashEraseSector(start_offset / 0x1000);
61+
Serial.printf("Testing %d bytes @ %08x + %d\n", amount, start_offset, data_offset);
62+
unsigned long start = micros();
63+
64+
if (!ESP.flashWrite(start_offset, (uint8_t *)write_buffer + data_offset, amount))
65+
{
66+
Serial.printf("Write fail\n");
67+
return false;
68+
}
69+
if (!ESP.flashRead(start_offset, (uint8_t *)read_buffer + data_offset, amount))
70+
{
71+
Serial.printf("Read fail\n");
72+
return false;
73+
}
74+
if (!compareBuffers(write_buffer, read_buffer, data_offset, amount))
75+
{
76+
return false;
77+
}
78+
Serial.printf("Write took %lu us\n", micros() - start);
79+
return true;
80+
}
81+
82+
// Columns in test case names are as following:
83+
// 1. Offset -> +o (4 byte aligned), -o (unaligned)
84+
// 2. Memory pointer -> +m (4 byte aligned), -m (unaligned)
85+
// 3. Size -> +s (4 byte ), -s (unaligned)
86+
// 4. Number of pages crossed -> np
87+
88+
// Aligned offset
89+
// Aligned memory
90+
// Aligned size
91+
TEST_CASE("|+o|+m|+s|0p|", "[spi_flash]")
92+
{
93+
CHECK(testFlash(0xa0000, 0, 100));
94+
}
95+
TEST_CASE("|+o|+m|+s|1p|", "[spi_flash]")
96+
{
97+
CHECK(testFlash(0xa0000, 0, 512));
98+
}
99+
// Unaligned size
100+
TEST_CASE("|+o|+m|-s|0p|", "[spi_flash]")
101+
{
102+
CHECK(testFlash(0xa0000, 0, 101));
103+
}
104+
TEST_CASE("|+o|+m|-s|2p|", "[spi_flash]")
105+
{
106+
CHECK(testFlash(0xa0000, 0, 515));
107+
}
108+
// Unaligned memory
109+
// Aligned size
110+
TEST_CASE("|+o|-m|+s|0|", "[spi_flash]")
111+
{
112+
CHECK(testFlash(0xa0000, 1, 100));
113+
}
114+
TEST_CASE("|+o|-m|+s|1p|", "[spi_flash]")
115+
{
116+
CHECK(testFlash(0xa0000, 3, 512));
117+
}
118+
// Unaligned size
119+
TEST_CASE("|+o|-m|-s|0p|", "[spi_flash]")
120+
{
121+
CHECK(testFlash(0xa0000, 2, 101));
122+
}
123+
TEST_CASE("|+o|-m|-s|2p|", "[spi_flash]")
124+
{
125+
CHECK(testFlash(0xa0000, 1, 515));
126+
}
127+
// Unaligned offset
128+
// Aligned memory
129+
// Aligned size
130+
TEST_CASE("|-o|+m|+s|0p|", "[spi_flash]")
131+
{
132+
CHECK(testFlash(0xa0001, 0, 100));
133+
}
134+
TEST_CASE("|-o|+m|+s|1p|", "[spi_flash]")
135+
{
136+
CHECK(testFlash(0xa0001, 0, 260));
137+
}
138+
// Unaligned size
139+
TEST_CASE("|-o|+m|-s|0p|", "[spi_flash]")
140+
{
141+
CHECK(testFlash(0xa0001, 0, 105));
142+
}
143+
TEST_CASE("|-o|+m|-s|1p|", "[spi_flash]")
144+
{
145+
CHECK(testFlash(0xa0001, 0, 271));
146+
}
147+
// Unaligned memory
148+
// Aligned size
149+
TEST_CASE("|-o|-m|+s|0p|", "[spi_flash]")
150+
{
151+
CHECK(testFlash(0xa0001, 1, 100));
152+
}
153+
TEST_CASE("|-o|-m|+s|1p|", "[spi_flash]")
154+
{
155+
CHECK(testFlash(0xa0001, 2, 260));
156+
}
157+
// Unaligned size
158+
TEST_CASE("|-o|-m|-s|0p|", "[spi_flash]")
159+
{
160+
CHECK(testFlash(0xa0001, 3, 105));
161+
}
162+
TEST_CASE("|-o|-m|-s|1p|", "[spi_flash]")
163+
{
164+
CHECK(testFlash(0xa0001, 1, 271));
165+
}
166+
167+
TEST_CASE("Last bytes of page", "[spi_flash]")
168+
{
169+
CHECK(testFlash(0xa0000 + 255, 0, 1));
170+
CHECK(testFlash(0xa0000 + 255, 1, 1));
171+
CHECK(testFlash(0xa0000 + 254, 0, 2));
172+
CHECK(testFlash(0xa0000 + 254, 1, 2));
173+
CHECK(testFlash(0xa0000 + 253, 0, 3));
174+
CHECK(testFlash(0xa0000 + 253, 1, 3));
175+
}
176+
177+
TEST_CASE("Unaligned page cross only", "[spi_flash]")
178+
{
179+
CHECK(testFlash(0xa0000 + 254, 0, 3));
180+
CHECK(testFlash(0xa0000 + 254, 1, 3));
181+
CHECK(testFlash(0xa0000 + 255, 0, 2));
182+
CHECK(testFlash(0xa0000 + 255, 1, 2));
183+
}
184+
185+
void loop ()
186+
{
187+
}

0 commit comments

Comments
 (0)