Problem with opening file on SD card in freertos task

Started by sravyts, April 01, 2020, 12:41:08 PM

Previous topic - Next topic

sravyts

Hello
I am trying to open a csv file on an SD card using the olimex ESP32-EVB. Setting up the card works and I am able to read and write data to it in the app_main. However, when I transfer the code to a function (which is a freertos task), it's not able to open anymore and gives the following errors:

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

ELF file SHA256: 2f10d42ac9ace2cfb218e05cdae0d0650d1e2f89c2d98fdd60da4bb40ce829af

Backtrace: 0x40084e5d:0x3ffc50b0 0x400851ed:0x3ffc50d0 0x40087fab:0x3ffc50f0

Rebooting...

Does somebody have an idea of what might be wrong?
This is the initialitation of the task and the task itself:

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


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)
       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(sample_read.data)-1; i++)
      {
         // Add the next value to the csv file
         fprintf(f, "%f%s", sample_read.data, CSV_SEPARATOR);
         // User feedback
         printf("%u : %f \n", i, sample_read.data);
      }
      // User feedback
      printf("%u : %f \n", i, sample_read.data);
      // Add the final line to the csv file
      fprintf(f, "%f%s", sample_read.data, CSV_NEWLINE);
      fclose(f);
      ESP_LOGI(TAG, "File written");

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

    }
}