/*
Test LittleFS write, read
*/
#include <LittleFS.h>
LittleFS_QPINAND myfs; // QSPI NAND Flash attached to SPI
#include "onnx__Conv_110.h"
const int total = 921600;
const int N = 100000;
EXTMEM float data[N];
float last = 0;
elapsedMicros chrono;
void readthefile (int chunks, float last) {
chrono = 0;
File file2 = myfs.open("onnx__Conv_110.h", FILE_READ);
if (file2) {
// file2.readBytes((char*)data, N * 4);
for (int i = 0; i < N / chunks; i++) {
char databytes[4 * chunks];
file2.readBytes(databytes, 4 * chunks);
memcpy(&data[i * chunks], &databytes, 4 * chunks);
}
int offset = N / chunks * chunks;
for (int i = 0; i < N % chunks; i++) {
uint8_t databytes[4 * chunks];
file2.readBytes(databytes, 4 * chunks);
memcpy(&data[offset + i * chunks], &databytes, 4 * chunks);
}
Serial.printf("Execution time: %.3f ms\n", (float)chrono / 1000.0);
} else Serial.println("error opening file");
file2.close();
// for (int i = 0; i < N; i++)
// Serial.printf("i = %d data = %f\n", i, data[i]);
Serial.printf("Checking data number %d...\nData written: %f\n", N, last);
Serial.printf("Data read : %f\n", data[N - 1]);
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
// wait for serial port to connect.
}
// set clock speed for PSRAM to 132 MHz
CCM_CBCMR &= ~(CCM_CBCMR_FLEXSPI2_PODF_MASK | CCM_CBCMR_FLEXSPI2_CLK_SEL_MASK);
CCM_CBCMR |= (CCM_CBCMR_FLEXSPI2_PODF(3) | CCM_CBCMR_FLEXSPI2_CLK_SEL(3));
Serial.print("Initializing SPI FLASH...");
// see if the Flash is present and can be initialized:
if (!myfs.begin()) {
Serial.printf("Error starting %s\n", "SPI FLASH");
while (1)
;
}
Serial.printf("NAND Flash Memory Size = %d bytes / ", myfs.totalSize());
Serial.printf("%d Mbyte / ", myfs.totalSize() / 1048576);
Serial.printf("%d Gbit\n", myfs.totalSize() * 8 / 1000000000);
Serial.println("Flash initialized.");
// 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.printf("File exists! 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 (dataFile) {
dataFile.write(onnx__Conv_110_output_0, 4 * total);
Serial.printf("Execution time: %.3f ms\n", (float)chrono / 1000.0);
} else Serial.println("error opening onnx__Conv_110.h");
Serial.printf("Size: %d (%d)\n", dataFile.size(), total * 4);
dataFile.close();
delay(200);
}
last = onnx__Conv_110_output_0[N - 1];
Serial.println("Now reading data...");
int chunks = 64
for (int i = 0; i < 30; ++i) {
chunks *= 2;
Serial.printf("chunks = %d\n", chunks);
readthefile(chunks, last);
}
}
void loop() {
// nope
}