ESP32-PsRamFS
ESP32-PsRamFS copied to clipboard
readBytes problem
Hello thanks for ESP32-PsRamFS ramdisk program. Im using this useful program. When I used readBytes(char *,sizs_t) function ,l could not read last char of this file. followin is test program
#include "./PSRamFS.h"
#include "./pfs.h"
void setup() {
Serial.begin(115200);
esp32Info();
if (!PSRamFS.begin()) {
log_e("PSRamFS Mount Failed");
return;
}
if (PSRamFS.exists("/test.txt")) {
PSRamFS.remove("/test.txt");;
}
File t = PSRamFS.open("/test.txt", FILE_WRITE);
if (t) {
t.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
Serial.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
Serial.printf("\n/test.txt size= %d \n", t.size());
t.close();
}
File r = PSRamFS.open("/test.txt", FILE_READ);
int rsize = r.size();
if (r) {
int c = 0;
char rbuf[32];
int len = 0;
int total = 0;
do {
len = r.readBytes(rbuf, sizeof(rbuf));
for (int i = 0; i < len; i++) {
Serial.print(rbuf[i]);
}
Serial.printf("\nlen=%d", len);
total += len;
} while (len);
t.close();
Serial.printf("\ntotal=%d", total);
if (total != rsize) {
Serial.printf("\nwe can read all data except file end..Z");
}
}
Serial.println("\nEND");
}
void loop() {
// put your main code here, to run repeatedly:
}
void esp32Info(void) {
Serial.println("---------------------------- -");
uint64_t chipid;
chipid = ESP.getEfuseMac(); //The chip ID is essentially its MAC address(length: 6 bytes).
Serial.printf("ESP32 Chip ID = % 04X\r\n", (uint16_t)(chipid >> 32)); //print High 2 bytes
Serial.printf("Chip Revision % d\r\n", ESP.getChipRevision());
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
Serial.printf("Number of Core: % d\r\n", chip_info.cores);
Serial.printf("CPU Frequency: % d MHz\r\n", ESP.getCpuFreqMHz());
Serial.printf("Flash Chip Size = % d byte\r\n", ESP.getFlashChipSize());
Serial.printf("Flash Frequency = % d Hz\r\n", ESP.getFlashChipSpeed());
Serial.printf("Free Heap Size = % d\r\n", esp_get_free_heap_size());
Serial.printf("Total PSRAM: %d\r\n", ESP.getPsramSize());
Serial.printf("Free PSRAM: %d\r\n", ESP.getFreePsram());
Serial.printf("ESP - IDF version = % s\r\n", esp_get_idf_version());
Serial.println();
}
hey @HideakiAbe thank you for your feedback.
I've edited your comment to make the code more readable. Now I will try to reproduce the issue and hopefully come back with a solution.
Can you please specify what OS / Arduino version / esp32-core version you're using ?
So, after updating to esp32 core 2.0.0 and editing your script, I can't reproduce the error :thinking:
[edit] My mistake, I didn't merge on the master the latest changes for 2.0.0 compliance. Please wait for propagation and update from the library manager, or pull the master from this repo.
This is what I get in my serial console:
11:00:22.860 -> ESP32 Chip ID = F554
11:00:22.860 -> Chip Revision 1
11:00:22.893 -> Number of Core: 2
11:00:22.893 -> CPU Frequency: 240 MHz
11:00:22.893 -> Flash Chip Size = 4194304 byte
11:00:22.893 -> Flash Frequency = 80000000 Hz
11:00:22.893 -> Free Heap Size = 4435719
11:00:22.893 -> Total PSRAM: 4192139
11:00:22.893 -> Free PSRAM: 4192139
11:00:22.893 -> ESP - IDF version = v4.4-dev-2313-gc69f0ec32
11:00:22.893 ->
11:00:22.893 -> [ 920][E][vfs_api.cpp:64] open(): /psram/test.txt does not exist
11:00:22.893 -> ABCDEFGHIJKLMNOPQRSTUVWXYZ
11:00:22.893 -> /test.txt size= 0
11:00:22.893 -> ABCDEFGHIJKLMNOPQRSTUVWXYZ
11:00:22.893 -> len=26
11:00:22.926 -> len=0
11:00:22.926 -> total=26
11:00:22.926 -> END
I have modified the #include as per recommendation for this library, here's the updated sketch I'm using:
#include <PSRamFS.h>
//#include "./pfs.h"
void setup() {
Serial.begin(115200);
esp32Info();
if (!PSRamFS.begin()) {
log_e("PSRamFS Mount Failed");
return;
}
if (PSRamFS.exists("/test.txt")) {
PSRamFS.remove("/test.txt");;
}
File t = PSRamFS.open("/test.txt", FILE_WRITE);
if (t) {
t.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
Serial.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
Serial.printf("\n/test.txt size= %d \n", t.size());
t.close();
} else {
Serial.println("Error: file could not be opened, halting");
while(1) vTaskDelay(1);
}
File r = PSRamFS.open("/test.txt", FILE_READ);
int rsize = r.size();
if (r) {
int c = 0;
char rbuf[32];
int len = 0;
int total = 0;
do {
len = r.readBytes(rbuf, sizeof(rbuf));
for (int i = 0; i < len; i++) {
Serial.print(rbuf[i]);
}
Serial.printf("\nlen=%d", len);
total += len;
} while (len);
t.close();
Serial.printf("\ntotal=%d", total);
if (total != rsize) {
Serial.printf("\nwe can read all data except file end..Z");
}
}
Serial.println("\nEND");
}
void loop() {
// put your main code here, to run repeatedly:
}
void esp32Info(void) {
Serial.println("---------------------------- -");
uint64_t chipid;
chipid = ESP.getEfuseMac(); //The chip ID is essentially its MAC address(length: 6 bytes).
Serial.printf("ESP32 Chip ID = % 04X\r\n", (uint16_t)(chipid >> 32)); //print High 2 bytes
Serial.printf("Chip Revision % d\r\n", ESP.getChipRevision());
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
Serial.printf("Number of Core: % d\r\n", chip_info.cores);
Serial.printf("CPU Frequency: % d MHz\r\n", ESP.getCpuFreqMHz());
Serial.printf("Flash Chip Size = % d byte\r\n", ESP.getFlashChipSize());
Serial.printf("Flash Frequency = % d Hz\r\n", ESP.getFlashChipSpeed());
Serial.printf("Free Heap Size = % d\r\n", esp_get_free_heap_size());
Serial.printf("Total PSRAM: %d\r\n", ESP.getPsramSize());
Serial.printf("Free PSRAM: %d\r\n", ESP.getFreePsram());
Serial.printf("ESP - IDF version = % s\r\n", esp_get_idf_version());
Serial.println();
}