Hello! This is my first post, so... sorry in advance if everything is all over the place. ![]()
For the past few weeks I have been developing on the ESP32-S3-USB-OTG board using the ESP-IDF implementation for Arduino IDE . My project involves reading incoming serial data from a sensor at 30k CPS and storing it to and SD card (essentially a pretty fast datalogger). In order to simulate the data coming into the board, I have been using Realterm to send a .txt file with sample data at the same speed and same size of 252 bytes. However, I noticed that most of the data returns with significant errors, and so I wrote some debug code to figure out if the UART was causing issues or if the SD card was causing issues.
Through my experimentation I found that, by itself, the UART can keep up with the high baudrate and return data correctly without errors (tested by the error counter implemented in the code as well as by sending the data over WiFi). However, issues with reading the data occurs when adding the code to append to the SD card (which is an UHS class 1 card).
I have tried different implementations of reading data over the Serial port (I'm aware that there are some issues involving Strings, sadly different implementations without the usage of Strings still caused issues). It seems that no matter the implementation, the errors still occur as long as the SD_MMC append function is called. I have also tried the SdFat and SD libraries with similar results.
I was wondering if someone has encountered something similar, and if someone can offer some guidance as to why this error might be occurring and what I can do to improve my project's functionality.
Thanks! (below is my code and the data received from the SD card - which received about 289 errors)
CODE:
#include <FS.h>
#include <SD_MMC.h>
const char* filename = "/gd.txt"; // File name prefix for SD storage
uint16_t data_caught_num = 0; // amount of errors received via serial
void appendFile(fs::FS &fs, const char * path, const char * message){
//code grabbed from the examples in the ESP32 SD_MMC library
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to open file for appending");
return;
}
if(!file.print(message)){
Serial.println("Append failed");
}
file.close();
}
void setup() { // begins Serial and SD_MMC
Serial.begin(460800);
SD_MMC.begin();
}
void loop() {
if(Serial.available() > 0) { // if receiving serial data from UART
String raw_data = Serial.readStringUntil('\x0D'); // read incoming serial data as a string until terminator
if(raw_data.length() < 252) { // debug code to catch errors where the data is less than the expected 252 characters
data_caught_num += 1;
}
const char* raw_dat = raw_data.c_str(); // converts string and appends to SD card
appendFile(SD_MMC, filename, raw_dat);
} else {
Serial.println("ERROR DATA COUNT:");
Serial.println(data_caught_num - 1); // minus 1 because the last line in the sent data is half of a line for testing purposes, so this line is just omitted in the error tally.
}
}
When writing the mock data to the SD card, some of the data comes back bad (lines 3, 4, 6) while others come back good (1, 2, 5, 7).
SENSOR DATA WRITTEN TO SD:
00185.54,00184.72,00188.30,00216.77,00173.88,00176.04,00169.01,00170.61,00172.19,00173.55,00180.39,00196.65,00180.23,00188.19,00194.35,00193.42,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,000000000,00000200
00185.75,00184.98,00188.47,00216.45,00172.94,00174.74,00168.45,00170.24,00172.55,00174.86,00180.87,00196.67,00179.74,00186.53,00192.71,00193.28,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,000000000,00000208
00185.80,00184.81,00188.33,00216.62,00173.93,00176.23,00170.00,00171.28,00172.97,00173.51,00
00186.72,00185.48,00188.34,00215.47,00172.20,00174.87,00170.63,00172.96,00174.47,00175.41,00179.91,00195.69,00179.44,00187.63,00194.25,00195.89,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000000300
00186.52,00185.42,00188.40,00215.93,00171.98,00174.50,00169.64,00171.58,00173.97,00175.65,00180.38,00195.97,00179.45,00186.91,00193.09,00194.88,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,000000000,00000308
00186.07,00184.82,00188.46,00216.46,00173.17,00175.50,00169.34,00170.82,00173.02,00174.04,00180.36,00196.30,00180.00,00188.04,00194.09,00193.96,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,0009.82,00195.80,00180.27,00189.08,00195.13,00194.64,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,000000000,00000325
00186.27,00185.00,00187.90,00215.51,00172.69,00175.66,00171.00,00173.23,00174.40,00173.84,00179.55,00195.14,00179.73,00188.38,00195.19,00195.94,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,000000000,00000333