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.
Activity
me-no-dev commentedon Mar 4, 2021
logic looks fine. the only thing that I see is that you should not try to print/close the file if it had failed to open. Apart from that, I recently introduced some fixes in the SD class and will be happy if you can give them a shot
thevalo commentedon Mar 4, 2021
Thanks! Will re-formulate the logic.
I've actually found the problem, but no solution:
it's the
rfc.init()
which is causing the problem. If I initialize the MRFC522 object, the SD fails to append but reads fine. However, when I don't initialize its object, the SD operations (including append) work just fine. I've also tried to initialize SD before MRFC522, but no luck. As I said, I'm using two SPI with a 330Ohm resistor between MISO and microSD adapter module.Will try the new PR (#4876) soon and let you know.
thevalo commentedon Mar 4, 2021
Hmm, I've updated SD (cpp, h), sd_defines.h and sd_diskio.h but Arduino IDE compiler reports this and doesn't compile:
A problem on my side, or somewhere else?
me-no-dev commentedon Mar 4, 2021
on your side. seems sd_diskio.cpp did not compile?
me-no-dev commentedon Mar 4, 2021
could it be that MRFC522 is holding the MISO line in some way? Also, you should not control the SDCard's SS line yourself. That is done by the library
thevalo commentedon Mar 4, 2021
Yep, thanks! It seems like sd_diskio.cpp wasn't there at all. Don't know how it went missing.
I'm suspecting about this too. How would I confirm/resolve this issue, if it is the case?
Hmm, alright. I will try
SD.begin()
without any parameter.me-no-dev commentedon Mar 4, 2021
SD.begin without parameter will use the default SS pin. this is not what I said :) just do not control it through your code.
thevalo commentedon Mar 4, 2021
Alright, so you mean I shouldn't use the two functions (activate_RFID/activate_SD) and leave them on their own?
BTW, just updated the esp32 library to 1.0.5 and I'll replace existing SD files with the newest one from the aforementioned PR.
me-no-dev commentedon Mar 4, 2021
yes :)
thevalo commentedon Mar 4, 2021
As stranger as it gets! Updated the esp32 board to 1.0.5, replaced existing SD files with your PR, now here's what appears:
thevalo commentedon Mar 4, 2021
I can confirm that the above error appears only when I'm using my sketch. Using SD(esp32) example from sketches, work fine!
thevalo commentedon Mar 4, 2021
Just put SD.begin() immediately after SPI.begin (it was TFT, mfrc then SD) and now the SD errors above don't appear anymore! Except for the
addApbChangeCallback(): duplicate
.Though MFRC522 reports Firmware version v2.0, it doesn't work. I started to suspect that the microSD module is not releasing MISO properly?
me-no-dev commentedon Mar 4, 2021
one of the two is definitely the reason. What if you remove the resistors on MISO? SD + something else on the same bus is quite common thing
thevalo commentedon Mar 5, 2021
If I remove the resistor on MISO, the MFRC prints:
Firmware Version: 0xE1 = (unknown)
and it doesn't work, again.
Tried to also switch places of MISO: SD Card - Resistor - MISO+MFRC522 also MFRC522 - Resistor - MISO+SD Card but it's the same, 0xE1.
P.S.: I've also noticed that
[E][esp32-hal-cpu.c:93] addApbChangeCallback(): duplicate func=400E325C arg=3FFBDE14
appears after calling MFRC522 functions (PCD Init, dump version to serial). Weird enough, this didn't happen before updating esp32 board library to 1.0.5. I think this is what causing it not to work.me-no-dev commentedon Mar 6, 2021
which slave are you initializing first? Try to switch them around and do not forget to power-off the board completely before retry
6 remaining items