I'm trying to write to a sd card but it fails when I use FreeRTOS tasks.
This is what I have:
#include "FS.h"
#include "SD.h"
const int32_t SD_SS_PIN = 2;
const int32_t SD_CS_PIN = 23;
const int32_t SD_MO_PIN = 5;
const int32_t SD_MI_PIN = 4;
void append_file(fs::FS &fs, const char * path, const char * message) {
File file = fs.open(path, FILE_APPEND);
if (!file)
{
Serial.println("Failed to open file for appending");
return;
}
else {
file.print(message);
file.close();
}
}
void task_1(void * parameters){
for (;;){
Serial.println("Task 1.");
append_file(SD, "/log.txt", "This will be the message.");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void task_2(void * parameters){
for (;;){
Serial.println("Task 2.");
append_file(SD, "/log.txt", "This will be the message.");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void setup(){
Serial.begin(115200);
SPIClass serial_peripheral_interface = SPIClass(VSPI);
serial_peripheral_interface.begin(SD_SS_PIN, SD_MI_PIN, SD_MO_PIN, SD_CS_PIN);
if(!SD.begin(SD_CS_PIN, serial_peripheral_interface, 80000000)){
Serial.println("SD card mount failed.");
}
else{
Serial.println("SD card mounted.");
}
TaskHandle_t task_1_handle;
TaskHandle_t task_2_handle;
xTaskCreatePinnedToCore(task_1, "Task 1", 4096, NULL, 6, &task_1_handle, 0);
xTaskCreatePinnedToCore(task_2, "Task 2", 4096, NULL, 6, &task_2_handle, 0);
}
void loop(){ }
I checked and the log.txt file does exist on the sd card.
This is the result when running on an ESP32 dev module:
SD card mounted.
Task 1.
Task 2.
[ 533][E][sd_diskio.cpp:126] sdSelectCard(): Select Failed
[ 533][E][sd_diskio.cpp:621] ff_sd_status(): Check status failed
assert failed: xQueueSemaphoreTake queue.c:1545 (( pxQueue ))
Edit: When replacing #include "SD.h"
with #include "SPIFFS.h"
then calling SPIFFS.begin(true)
in the setup everything works. But now it's not using the sd card.