Problem in SD card data logging in Tasks

nilyash81
Posts: 2
Joined: Wed Aug 30, 2023 3:59 am

Problem in SD card data logging in Tasks

Postby nilyash81 » Wed Aug 30, 2023 4:30 am

[/code] #include "Arduino.h"
#include "FS.h"
#include <SD.h>
#include <SPI.h>
#include "esp_system.h"

// Define CS pin for the SD card module
#define SD_MISO 39
#define SD_MOSI 15
#define SD_SCLK 17
#define SD_CS 5
SPIClass sdSPI(VSPI);

File file;

TaskHandle_t Task0;
TaskHandle_t Task1;
SemaphoreHandle_t SepHandle = NULL;

int s;

int sum(int, int);
String dataMessage;
unsigned long dt = 0;
unsigned long timeOld = 0;
//float pressure;
float temperature;
//float altimeter;
//char charRead;
//char runMode;
//byte i = 0; //counter
//char dataStr[100] = "";
//char buffer[7];

void loop0(void * parameter) {
int a, b;
a = 3;
b = 2;

for (;;) {
// a = a + 2;
Serial.print("Running on core: ");
Serial.println(xPortGetCoreID());
s = sum(a, b);
Serial.println(s);

delay(1);
}
}

void loop1(void * parameter) {
int a = 7, b = 8;
delay(12);
for (;;) {
a++;
Serial.print("Running on core: ");
Serial.println(xPortGetCoreID());
s = sum(a, b);

xSemaphoreTake(SepHandle, portMAX_DELAY);
file = SD.open("/data.txt", FILE_WRITE);
temperature = 13.4;
dataMessage = String(temperature) + "\r\n";
file.print(dataMessage);
file.close();
xSemaphoreGive(SepHandle);

delay(12);
}

}

void setup()
{
Serial.begin(115200);
Serial.println("Setup started.");
xTaskCreatePinnedToCore(
loop0, /* Function to implement the task */
"Task0", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
&Task0, /* Task handle. */
0); /* Core where the task should run */

xTaskCreatePinnedToCore(
loop1, /* Function to implement the task */
"Task1", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
&Task1, /* Task handle. */
1); /* Core where the task should run */

sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS, sdSPI)) {
Serial.println("Card Mount Failed");
return;
}

pinMode(32, OUTPUT);
Serial.println("Setup completed.");

SepHandle = xSemaphoreCreateBinary();
xSemaphoreGive(SepHandle);

xSemaphoreTake(SepHandle, portMAX_DELAY);
file = SD.open("/data.txt", FILE_WRITE);
temperature = 13.4;
dataMessage = String(temperature) + "\r\n";
file.print(dataMessage);
file.close();
xSemaphoreGive(SepHandle);
}

void loop()
{

delay(1);
}

int sum(int a, int b)
{
return (a + b);
}


I am trying to log a data in SD card. If I use above code in Normal Arduino style like, setup() and then loop() without any task everything works fine. When I tried to put it in Tasks as shown in above code, then it is not working. The errors I am getting are as follows. The SD card getting mounted but when write operation is put in Tasks it gives error for that particular task. What may be the cause of this??

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1240
load:0x40078000,len:13012
load:0x40080400,len:3648
entry 0x400805f8
Setup started.
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Setup completed.
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 0
5
Running on core: 1
Guru Meditation Error: Core 1 panic'ed (Unhandled debug exception).
Debug exception reason: Stack canary watchpoint triggered (Task1)
Core 1 register dump:
PC : 0x4008babe PS : 0x00060836 A0 : 0x80089c93 A1 : 0x3ffb9620
A2 : 0x3ffb2c8c A3 : 0x00060820 A4 : 0x3f400138 A5 : 0x3f400134
A6 : 0x3f400164 A7 : 0x3ffb2d08 A8 : 0x00000002 A9 : 0x00000002
A10 : 0x3ffb2d08 A11 : 0x3ffb98b4 A12 : 0x000000a0 A13 : 0x3f400138
A14 : 0x00ff0000 A15 : 0xff000000 SAR : 0x00000004 EXCCAUSE: 0x00000001
EXCVADDR: 0x00000000 LBEG : 0x40086795 LEND : 0x400867a5 LCOUNT : 0xfffffffe


Backtrace:0x4008babb:0x3ffb96200x40089c90:0x3ffb9660 0x40084690:0x3ffb96a0 0x4008478d:0x3ffb96d0 0x400e56fd:0x3ffb96f0 0x400e57ed:0x3ffb9850 0x400dde49:0x3ffb9870 0x400ec680:0x3ffb9890 0x400d21d6:0x3ffb98b0 0x400d194a:0x3ffb9910 0x400d130b:0x3ffb9940

ESP_Sprite
Posts: 9725
Joined: Thu Nov 26, 2015 4:08 am

Re: Problem in SD card data logging in Tasks

Postby ESP_Sprite » Wed Aug 30, 2023 8:40 am

Debug exception reason: Stack canary watchpoint triggered (Task1)
You should give your tasks more stack space. 1000 bytes is way too small.

nilyash81
Posts: 2
Joined: Wed Aug 30, 2023 3:59 am

Re: Problem in SD card data logging in Tasks

Postby nilyash81 » Fri Sep 01, 2023 2:11 pm

Thanks ESP Sprite,

Problem solved after increasing stack size. Code is working and I am able to log data now. This will help me in logging IMU data of my drone project in SD card. I am planning to log commanded and achieved roll and pitch angle.

Thanks for help bro!!!

Who is online

Users browsing this forum: No registered users and 39 guests