File on SD card does not want to open

Posts: 5
Joined: Thu Feb 27, 2020 4:38 pm

File on SD card does not want to open

Postby sravyts » Wed Apr 01, 2020 9:25 am

I am trying to write some data to an SD card on an OLIMEX ESP32-EVB.
Starting from the examples in esp-idf and from olimex, this works fine.
However, when I want to put this in a freertos task, I always get errors:

E (16919) sdmmc_cmd: sdmmc_read_sectors_dma: sdmmc_send_cmd returned 0x107
E (16919) diskio_sdmmc: sdmmc_read_blocks failed (263)
E (16919) example: Failed to open file for writing
E (16929) FreeRTOS: FreeRTOS Task "task_SD_storage" should not return, Aborting now!
abort() was called at PC 0x40087fab on core 0

And then the system reboots. Everytime this task is started, this error pops up and the ESP32 restarts. The other tasks seems to work fine...

This is the task that needs to write and where the problem seems to occur:

void store_SD(void *ignore)
/* Low priority variables */
//uint8_t output_data=0;
// sample_read holds the measurement sample that is about to be processed in this task
// note: the variable is initialized to {0} to set all bits to zero
sample_t sample_read = {0};

while(1) {

/* Update the DAC setting */
//printf("Updating the DAC setting\n");
//dac_output_voltage(DAC_EXAMPLE_CHANNEL, output_data++);

/* Write buffer to file */

// The next element into the data_to_store_queue is copied into the memory at location &sample_read (pointing to the address of sample_read)
if(xQueueReceive(data_to_store_queue, &sample_read, 10000)){
// A log message flags that the process to open the file holding the data is opened
ESP_LOGI(TAG, "Opening file");
// Open the data file
//FILE* f = fopen("/sdcard/data19_03_2020.csv", "a");
//FILE* f = fopen("/sdcard/data2602.csv", "w");
FILE* f = fopen("/sdcard/testen.txt", "w");
// Flag an error if the file could not be opened
// todo Handle the file open error such that the data is discarded and the firmware continues operation
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for writing");
return; //THis will reboot the ESP
printf("Starting some writing to csv \n");

// Write the data to a csv file
uint8_t i = 0;
// Add the timestamp to the csv file
fprintf(f, "%lu%s", sample_read.timestamp_unix, CSV_SEPARATOR);
printf("%lu \n", sample_read.timestamp_unix);
fprintf(f, "%lu%s", sample_read.timestamp_us, CSV_SEPARATOR);
printf("%lu \n", sample_read.timestamp_us);
for(i = 0; i < COUNT_OF(; i++)
// Add the next value to the csv file
fprintf(f, "%f%s",, CSV_SEPARATOR);
// User feedback
printf("%u : %f \n", i,;
// User feedback
printf("%u : %f \n", i,;
// Add the final line to the csv file
fprintf(f, "%f%s",, CSV_NEWLINE);
ESP_LOGI(TAG, "File written");

/* Sleep */
//vTaskDelay(2000 / portTICK_RATE_MS);

Is there something which I am doing wrong?

THis is the creation of the tasks in app_main:

xTaskCreate(START, "task_start", 4096, NULL, 1, NULL);
printf("Task 1 OK \n");

xTaskCreate(DAQ, "task_DAQ", 4096, NULL, 1, NULL);
printf("Task 2 OK \n");

xTaskCreate(PROT, "task_PROT", 4096, NULL, 1, NULL);
printf("Task 3 OK \n");

xTaskCreate(control_converter, "task_CRTL", 4096, NULL, 1, xHandle_CTRL);
printf("Task 4 OK \n");

xTaskCreate(store_SD, "task_SD_storage", 8192, NULL, 1, NULL);
printf("Task 5 OK \n");

Who is online

Users browsing this forum: No registered users and 12 guests