-
Notifications
You must be signed in to change notification settings - Fork 7.6k
SD lib file IO errors #5998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've tested a script that combines both approaches: I took SD lib card initialization but operated directly with the Arduino sketch with SD lib initialization and stdio.h file operations#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "esp_err.h"
#include "SD.h"
#define SDCARD_SPI_GPIO_MISO 2
#define SDCARD_SPI_GPIO_MOSI 15
#define SDCARD_SPI_GPIO_CLK 14
#define SDCARD_SPI_GPIO_CS 13
#define GPIO_LED_PIN 21
static const char *TAG = "sdcard";
bool stopped = false;
static bool testSDcardIOFailed() {
static uint8_t buff[512];
size_t bytes_written;
FILE *file = fopen("/sd/file1.bin", "w");
bool sdFailed = false;
for (int i = 0; i < 100; i++) {
bytes_written = fwrite(buff, sizeof(uint8_t), sizeof(buff), file);
if (bytes_written != sizeof(buff)) {
ESP_LOGE(TAG, "bytes_written %zu, expected %zu", bytes_written, sizeof(buff));
ESP_LOGE(TAG, "SD card IO error write");
sdFailed = true;
break;
}
fflush(file);
if (ftell(file) != sizeof(buff) * (i + 1)) {
ESP_LOGE(TAG, "SD card IO error flush");
sdFailed = true;
break;
}
}
fclose(file);
return sdFailed;
}
void setup() {
Serial.begin(115200);
delay(1000);
ESP_LOGI("main", "Freertos tick rate: %d", configTICK_RATE_HZ);
SPI.begin(SDCARD_SPI_GPIO_CLK,
SDCARD_SPI_GPIO_MISO,
SDCARD_SPI_GPIO_MOSI,
SDCARD_SPI_GPIO_CS); //SCK, MISO, MOSI, SS (CS)
while (!SD.begin(SDCARD_SPI_GPIO_CS, SPI)) delay(100);
ESP_LOGI("main", "STARTED");
pinMode(GPIO_LED_PIN, OUTPUT);
}
void loop() {
if (stopped) return;
if (testSDcardIOFailed()) {
digitalWrite(GPIO_LED_PIN, HIGH);
stopped = true;
}
delay(10);
} It has failed with the same error as in the first sketch. Although it lasted longer. |
I have a similar error. I had to revert my esp32 libs to 1.06 for my SD card to mount again. |
We are having similar issues. Hardware: Sketch Description Arduino-ESP32 : V2.0.1 Arduino-ESP32 : V2.0.2 |
Hardware:
Board: TTGO T8 V1.7 and M5Core2-AWS
Core Installation version: arduino-esp32 v2.0.1
IDE name: Arduino IDE
Flash Frequency: 240Mhz
PSRAM enabled: Enabled for M5Core2 and no such option for TTGO
Upload Speed: 115200
Computer OS: Ubuntu 20.04
Description:
There is a number of issues already addressed to the SD card library (#524 is the most cited one), but the majority if not all are related to mounting errors. I want to extend the horizon to which SD lib fails to function. This issue addresses file IO errors related to SD card lib. In short, writing to a file with the SD lib surely fails, sooner or later. I've been preparing the material for the issue for two days, and I hope it won't be ranked as a "please help dunno what to do" one.
I have two different SD cards and two boards: TTGO T8 V1.7 and M5Core2. They all perform equally (in the same fashion, either both fail or none), and I'll focus on the TTGO T8 just to pick one.
Until recently, I've been running the applications in ESP-IDF and had no SD-card-related errors. I was happy till I hit Arduino.
Sketch
I've come up with two minimal Arduino programs. One uses the SD card lib and constantly fails (in a flash, a minute, or a few hours; it often crashes immediately, in the first run after SD is successfully initialized), and the other does not (I've been running it for 20 hours in Arduino + months in ESP-IDF).
Arduino sketch using the arduino-esp32 SD lib. Always fails.
Output
Arduino sketch with a self-written SD SPI lib. Never failed.
Output
Internet gurus suggest connecting an internal pull-up resistor to the MISO line
pinMode(SDCARD_SPI_GPIO_MISO, INPUT_PULLUP)
, but this doesn't help either. There is no way to connect an external pullup resistor to the dedicated SD card slot of the M5Core2 and TTGO T8 boards I'm using. It won't help though - the problem exists in the arduino-esp library - I do not have any problems if I switch the project to ESP-IDF (in fact, the code for the second Arduino example is borrowed from an ESP-IDF project I'm using). I've been running ESP-IDF for months (!) with a similar heavy payload to an SD card, and I had no SD-card-related issues, using the same internal SD card slots.At least now you have things to compare. And I mean, the second example is just your orthodox code from ESP-IDF... I've tried to look into Arduino SD lib, but I recoiled from its code complexity.
The text was updated successfully, but these errors were encountered: