SD card library does not open the file.

Hi,

Please have a look on the following code. It is a copy of example code but with FreeRTOS. This program fails to open the SD card file. However, it works when used as it is from example, also If the writing and reading a file from SD card code put into loop.
Please let me know if you have any idea to get it work. I have tried a lot.
I am using Arduino mega. The hardware setup us perfect as example code is working perfectly. Just my code is not able to open a file.

"myFile = SD.open("test.txt", FILE_WRITE);" this line has "0" as output always. I have not idea how to solve this.

//#include <stdio.h>
//#include <stdint.h>
//#include <ctype.h>
#include <Arduino_FreeRTOS.h>
#include <SPI.h>
#include <SD.h>

void TaskSDCard (void *pvParameters);

#ifndef TRUE
#define TRUE (1==1)
#define FALSE (!TRUE)
#endif

String dataString = "Prashant,Borhade";
const int chipSelect = 53;
File myFile;
TaskHandle_t xTaskSDCard;

void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.println(F("Initializing tasks"));
xTaskCreate(
TaskSDCard
, (const portCHAR *) "SD"
, 256 // This stack size can be checked & adjusted by reading Highwater
, NULL
, 1 // priority
, xTaskSDCard );

Serial.println(F("Initializing Done"));

//pinMode(chipSelect, OUTPUT);
//pinMode(53, OUTPUT);
Serial.println(F("Initializing SD card"));

}

void loop() {

}

void TaskSDCard (void *pvParameters){
(void) pvParameters;

if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (SD.exists("test.txt")){
SD.remove("test.txt");
Serial.println("Deleted Existing file.");
}
for(;;){

myFile = SD.open("test.txt", FILE_WRITE);
Serial.println(myFile);
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
myFile.close();
Serial.println("done.");
}
else {
myFile.close();
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

vTaskDelay(10000 / portTICK_PERIOD_MS);
}
}

Dear Prashbor,

I am developing the same project just like yours, and I also get the same problems.

You can check the File.cpp under SD library.

The constructor "File::File(SdFile f, const char *n)" calls malloc(), while "void File::close()" function calls free();.

In FreeRTOS task, the memory is managed under FreeRTOS; malloc() and free() are not allowed.

Please refer to : FreeRTOS - Memory management options for the FreeRTOS small footprint, professional grade, real time kernel (scheduler)

As the result, you may substitute malloc() and free() as pvPortMalloc() and vPortFree() if your task is controlled under FreeRTOS.

Take me as an example, I refer these functions in heap_4.c.

You need to use code tags </> and post your code in the proper manner. You have written two command lines to call pin 53 as OUTPUT. Only one is required. You have commented both and therefore taken both of them them out of play. This cannot be a good idea. Just calling

pinMode(53, OUTPUT);

might fix it.