Lesept
Well-known member
Thanks to all your explanation, I finally understood how all this works!
Now I have a code that compiles and can create a file to store a large array, and read it afterwards.
The output is correct:
It take 520 ms to read 100 000 float values (53 ms for 10 000 and 2.7 ms for 100).
The array data is stored in PSRAM (using DMAMEM), which is 8 MB. It should be able to store float arrays up to 2000000 values. But when I increase the value of N (size of the array) to 300 000 I get this error message:
Why does it not use the PSRAM?
Another question: isn't it possible to read and write float numbers directly as groups of 4 bytes? The resulting file would be smaller in the Flash memory.
Now I have a code that compiles and can create a file to store a large array, and read it afterwards.
Code:
/*
Test LittleFS write, read
*/
#include <LittleFS.h>
LittleFS_QPINAND myfs; // QSPI NAND Flash attached to SPI
#include "onnx__Conv_110.h"
const int N = 100000;
const int total = 921600;
DMAMEM float data[N] = { 0 };
float last = 0;
elapsedMicros chrono;
void setup() {
Serial.begin(9600);
while (!Serial) {
// wait for serial port to connect.
}
Serial.print("Initializing SPI FLASH...");
if (!myfs.begin()) {
Serial.printf("Error starting %s\n", "SPI FLASH");
while (1)
;
}
// uncomment only the first time
// Serial.println("Formatting SPI FLASH...");
// myfs.lowLevelFormat('.');
Serial.println("Searching file...");
File dir = myfs.open("/");
bool found = false;
while (true) {
File entry = dir.openNextFile();
if (!entry) break;
if (strcmp(entry.name(), "onnx__Conv_110.h") == 0) {
found = true;
Serial.println("File exists!");
Serial.printf("Size: %d\n", entry.size());
break;
}
entry.close();
}
if (!found) {
Serial.println("Creating file...");
chrono = 0;
File dataFile = myfs.open("onnx__Conv_110.h", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
for (int j = 0; j < total; j++) {
dataFile.println(onnx__Conv_110_output_0[j], 8);
if (j % 100000 == 0) Serial.printf("i = %d data = %f\n", j, onnx__Conv_110_output_0[j]);
}
Serial.printf("Execution time: %.3f ms\n", (float)chrono / 1000.0);
} else Serial.println("error opening onnx__Conv_110.h");
Serial.printf("Size: %d\n", dataFile.size());
dataFile.close();
delay(200);
}
last = onnx__Conv_110_output_0[N - 1];
Serial.println("Now reading data...");
chrono = 0;
File file2 = myfs.open("onnx__Conv_110.h", FILE_READ);
char buffer[15]; // 8 digits after .
int index = 0;
int i = 0;
if (file2) {
while (file2.available()) {
buffer[index++] = file2.read();
if (buffer[index - 1] == '\r') {
buffer[index - 1] = '\0'; // NULL terminate the array
data[i++] = atof(buffer);
index = 0;
file2.read();
if (i % 10000 == 0) Serial.println(i);
}
if (i > N) break;
}
Serial.printf("Execution time: %.3f ms\n", (float)chrono / 1000.0);
} else Serial.println("error opening file");
file2.close();
Serial.printf("Checking data number %d...\nData written: %f\n", N, last);
Serial.printf("Data read : %f\n", data[N - 1]);
}
void loop() {
// nope
}
The output is correct:
Code:
Initializing SPI FLASH...NAND Flash Memory Size = 265289728 bytes / 253 Mbyte / 2 Gbit
Flash initialized.
Searching file...
File exists!
Size: 11546225
Now reading data...
10000
20000
30000
40000
50000
60000
70000
80000
90000
100000
Execution time: 520.251 ms
Checking data number 100000...
Data written: -0.021341
Data read : -0.021341
It take 520 ms to read 100 000 float values (53 ms for 10 000 and 2.7 ms for 100).
- If I set the PSRAM frequency to 133 MHz, the time reduces to 509 ms for 100 000 data.
- Using compilation options 'Fastest with LTO' reduces again to 504 ms
- Overclocking the CPU to 860MHz reduces down to 370 ms.
Code:
Initializing SPI FLASH...NAND Flash Memory Size = 265289728 bytes / 253 Mbyte / 2 Gbit
Flash initialized.
Searching file...
File exists!
Size: 11546225
Now reading data...
10000
20000
30000
40000
50000
60000
70000
80000
90000
100000
Execution time: 370.558 ms
Checking data number 100000...
Data written: -0.021341
Data read : -0.021341
The array data is stored in PSRAM (using DMAMEM), which is 8 MB. It should be able to store float arrays up to 2000000 values. But when I increase the value of N (size of the array) to 300 000 I get this error message:
Code:
c:/users/fa125436/appdata/local/arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\fa125436\AppData\Local\arduino\sketches\BE3F2F28DD8D15A2286C6FF9565BEE79/Teensy_test_LittleFS_Flash.ino.elf section `.bss.dma' will not fit in region `RAM'
c:/users/fa125436/appdata/local/arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld.exe: region `RAM' overflowed by 688128 bytes
collect2.exe: error: ld returned 1 exit status
Why does it not use the PSRAM?
Another question: isn't it possible to read and write float numbers directly as groups of 4 bytes? The resulting file would be smaller in the Flash memory.
Last edited: