Description
Hardware:
Board | lilygo ttgo t-display esp32 |
Version/Date | ets Jun 8 2016 00:22:57 |
IDE name | Arduino IDE |
Flash Frequency | 80Mhz |
PSRAM enabled | no |
Upload Speed | 115200 |
Computer OS | Windows 10 |
Description:
I've got a microSD module adapter from the far east, and so far so good it can create files using writeFile
, but it does not append messages. It returns true
, i.e: message appended successfully on the first try, but when you read the file on the computer or when ESP32 reads on reboot, it's blank. I am using two (in fact, three) SPI devices: TFT display, MFRC522, and microSD module. When working with SD operations, I disable MFRC522 and enable SD, as seen in the code blocks below.
I use writeFile
if readFile
returns false
, that is, when the file does not exist, it invokes writeFile(SD, sdFileName, "")
Specifically, when fs.open(path)
of readFile
returns false
, I instruct to create a new file by invoking writeFile
.
This is readFile
, by the way:
bool readFile(fs::FS &fs, const char * path) {
activate_SD();
bool readSuccess = false;
if (DEBUG == 1) {
Serial.print("Reading file: ");
Serial.println(path);
}
File file = fs.open(path);
if (!file) {
if (DEBUG == 1) {
Serial.println("Failed opening the file.");
}
readSuccess = false;
} else {
readSuccess = true;
}
while (file.available()) {
char c = file.read();
fileContent += c;
}
file.close();
activate_RFID();
if (readSuccess) {
return true;
} else {
return false;
}
}
Code:
This is the suspected code that it's not doing its job, as instructed:
char* sdFileName = "/v.ea";
if (appendFile(SD, sdFileName, "Heyy")) {
//success
} else {
//failed
}
bool appendFile(fs::FS &fs, const char * path, const char * message) {
activate_SD();
bool appendSuccess = false;
if (DEBUG == 1) {
Serial.print("Appending to file: ");
Serial.println(path);
}
File file = fs.open(path, FILE_APPEND);
if (!file) {
appendSuccess = false;
if (DEBUG == 1) {
Serial.println("Failed opening the file.");
}
}
if (file.print(message)) {
appendSuccess = true;
if (DEBUG == 1) {
Serial.println("Appended successfully.");
}
} else {
appendSuccess = false;
if (DEBUG == 1) {
Serial.println("Appending failed.");
}
}
file.close();
activate_RFID();
if (appendSuccess) {
return true;
} else {
return false;
}
}
And, I use these two functions to disable/enable SD/MFRC522:
void activate_SD() {
digitalWrite(MFRC522_SSPin, HIGH);
digitalWrite(SDMOD_SS, LOW);
if (DEBUG == 1) {
Serial.println("SD is active.");
}
}
void activate_RFID() {
digitalWrite(SDMOD_SS, HIGH);
digitalWrite(MFRC522_SSPin, LOW);
if (DEBUG == 1) {
Serial.println("RFID is active.");
}
}
Debug Messages:
Interestingly, on the first appendFile
call, it returns true
and displays just this [W]
level:
[W][sd_diskio.cpp:149] sdCommand(): token error [17] 0x17
However, on the second appendFile
call, it returns false
and displays these:
[W][sd_diskio.cpp:149] sdCommand(): token error [17] 0x17
[W][sd_diskio.cpp:149] sdCommand(): token error [17] 0x5
[W][sd_diskio.cpp:149] sdCommand(): token error [17] 0x5
[E][vfs_api.cpp:265] VFSFileImpl(): fopen(/sd/v.ea) failed
SD card space is 1GB, it is a generic brand, and its type is "SD". I've even also used appendFile
but with FILE_WRITE
parameter then with file.seek(EOF)
but still no luck. What makes me even more, puzzled, is that the default example SD(esp32)
works well: creating files, appending, deleting, renaming, etc. Therefore I suspect the problem is seeded hidden in my logic somewhere.