At first I wrote sketch and it worked. File "info.txt" was created correctly.
Then I tried to rewrite the sketch in the style
File file = FFat.open(path); file.print(message) etc.
But when I launched the sketch, unreadable garbage appeared in the text file, as if the file was written in some binary form.
I returned the old sketch, which seemed to work fine, but here it failed too.
The file is created, I get access to it via
MSC.begin(partition->size / LBA_SIZE, LBA_SIZE)
I can edit it, save it, everything works.
Failure:
- I open the file in Windows. I save a random set of numbers in it. When I launch the sketch, I get the following result.
- I delete the file. When I launch the sketch, it is created, but its contents completely correspond to the deleted one. That is, the numbers appear again as in the file that was destroyed.
ESP S3 Zero ESP32 Expressif Systems 3.2.0 Arduino IDE 2.3.5 Windows 10
void PrintInfo()
{
FFat.begin(true);
FILE* fp = fopen("/ffat/info.txt", "wt");
if (fp != NULL) {
fprintf(fp, "0.mp3 - Welcome\n");
fprintf(fp, "1.mp3 - Timer\n");
fprintf(fp, "2.mp3 - Notify\n");
fprintf(fp, "3.mp3 - Warning\n");
fprintf(fp, "4.mp3 - Parking\n");
fprintf(fp, "5.mp3 - Reverse\n");
fprintf(fp, "\n");
fprintf(fp, "This drive has FAT16 system.\n");
fprintf(fp, "Do not format this drive!\n");
fprintf(fp, "Drive will be automatically\n");
fprintf(fp, "formatted if an error occurs.\n");
fflush(fp);
fclose(fp);
}
FFat.end();
}
// USB CDC ON BOOT = ENABLED
// CPU FREQUENCY = 240MHZ
// CORE DEBUG LEVEL = NONE
// USB DFU ON BOOT = DISABLED
// ERASE ALL FLASH = DISABLED
// EVENTS RUN ON = CORE 1
// FLASH MODE = QIO 80MHZ
// ARDUINO RUN ON = CORE 1
// USB FIRMWARE MSC ON BOOT = ENABLED
// PARTITION SCHEME = NO OTA (1MB APP / 3MB FATFS)
// PSRAM = ENABLED
// UPLOAD MODE = USB-OTG CDC (TINY USB)
// UPLOAD SPEED = 921600
// USBMODE = USB-OTG (TINY USB)
What am I doing wrong? What am I not taking into account? I have a feeling that a new file is created on the old data area and picks up its old size.
I just need to destroy the old file and create a new one with text.
FFat
onESP32
devices, especially whenUSB MSC
is enabled. It seems like you're overwriting an old file, but remnants of the previous data remain or the file mysteriously “comes back” even after deletion. This usually has to do with howFFat
andUSB MSC
handle storage and caching. You can solve it by explicitly removing the file before writing.F_Fat(FSImplPtr impl); bool begin(bool formatOnFail = false, const char *basePath = "/ffat", uint8_t maxOpenFiles = 10, const char *partitionLabel = (char *)FFAT_PARTITION_LABEL);
I found this piece of code in theFFat.h
file. Maybe the file system was damaged or the number of simultaneously open files was exceeded. By default, it is 10.