sd card read/write guru meditation error

lucadjr
Posts: 2
Joined: Sun Mar 22, 2020 12:28 pm

sd card read/write guru meditation error

Postby lucadjr » Sun Mar 22, 2020 12:48 pm

Hi!!
Hi all!
i started apart a project to test the sd read/write functions with succes than i created separeted functions to use in my code.

Code: Select all

void scrivi(String path, String message)
{
  Serial.println(F("Initializing SD card..."));
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(5, OUTPUT);

  SPIClass SPISD;
  SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS

  if (!SD.begin(5, SPISD))
  {
    Serial.println(F("initialization failed!"));
    return;
  }
  Serial.println(F("initialization done."));

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File myFile = SD.open(path, FILE_APPEND);

  // if the file opened okay, write to it:
  if (myFile)
  {
    Serial.print(F("Writing to: "));
    Serial.println(path);
    myFile.println(message);

    // close the file:
    myFile.close();
    Serial.println(F("done."));
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println(F("error opening: "));
    Serial.println(path);
  }
}
Every 10 seconds i need to append data in a txt file.
I write once in the setup and then in the loop every 10sec using same call to function.

Code: Select all

scrivi("/test.txt", "message");
For testing i make the function write in the "/test.txt" file always "test" string.
In the setup it works (i also checked the file in sd it is there written even multiple lines if didnt cancel the file) but when got the first call in the loop i got guru meditation error, it seems to happen here:

Code: Select all

File myFile = SD.open(path, FILE_APPEND);

Code: Select all

Initializing SD card...
initialization done.
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x4014f799  PS      : 0x00060230  A0      : 0x800da592  A1      : 0x3ffb18b0 
A2      : 0x000f4240  A3      : 0x3ffb1ca4  A4      : 0x3ffb1c50  A5      : 0x3ffc87b8 
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x800ef511  A9      : 0x3ffb19d0 
A10     : 0x3ffb1c1c  A11     : 0x3ffc777c  A12     : 0x00000000  A13     : 0x00000000 
A14     : 0x3ffd4e03  A15     : 0x00000001  SAR     : 0x00000009  EXCCAUSE: 0x0000001c 
EXCVADDR: 0x000f4240  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffd 

Backtrace: 0x4014f799:0x3ffb18b0 0x400da58f:0x3ffb18d0 0x400da849:0x3ffb1900 0x400dad0d:0x3ffb1930 0x400f0a26:0x3ffb1970 0x400ed1f1:0x3ffb1990 0x400ee52e:0x3ffb19b0 0x400ee67d:0x3ffb19d0 0x400ef50e:0x3ffb1a00 0x400f064c:0x3ffb1c70 0x4010a4c9:0x3ffb1de0 0x4000bcc5:0x3�
In the stand alone project for testing the sd everything is working properly even in the loop calls.

Code: Select all

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

int a;

void leggi(String path)
{
    Serial.println(F("Initializing SD card..."));

  pinMode(5, OUTPUT);

  SPIClass SPISD;
  SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS

  if (!SD.begin(5, SPISD))
  {
    Serial.println(F("initialization failed!"));
    return;
  }
  Serial.println(F("initialization done."));

  Serial.print("------------Leggo:");
  Serial.println(path);
  // re-open the file for reading:
  File myFile = SD.open(path);
  if (myFile)
  {
    Serial.println(path);

    // read from the file until there's nothing else in it:
    while (myFile.available())
    {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println(F("error opening test.txt"));
  }
}
void scrivi(String path, String message)
{
  Serial.println(F("Initializing SD card..."));
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(5, OUTPUT);

  SPIClass SPISD;
  SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS

  if (!SD.begin(5, SPISD))
  {
    Serial.println(F("initialization failed!"));
    return;
  }
  Serial.println(F("initialization done."));

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File myFile = SD.open(path, FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile)
  {
    Serial.print(F("Writing to:"));
    Serial.println(path);
    myFile.println(message);

    // close the file:
    myFile.close();
    Serial.println(F("done."));
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println(F("error opening: "));
    Serial.println(path);
  }
}
void sovrascrivi(String path, String message)
{
  Serial.println(F("Initializing SD card..."));
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(5, OUTPUT);

  SPIClass SPISD;
  SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS

  if (!SD.begin(5, SPISD))
  {
    Serial.println(F("initialization failed!"));
    return;
  }
  Serial.println(F("initialization done."));

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File myFile = SD.open(path, FILE_APPEND);

  // if the file opened okay, write to it:
  if (myFile)
  {
    Serial.print(F("Writing to: "));
    Serial.println(path);
    myFile.println(message);

    // close the file:
    myFile.close();
    Serial.println(F("done."));
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println(F("error opening: "));
    Serial.println(path);
  }
}
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  a=0;
}

void loop()
{
  String messaggio =  String(a); 
  messaggio = messaggio + ": ";
  messaggio = messaggio + millis();
  messaggio = messaggio  + "\n";
  scrivi("/test_scrivi.txt",messaggio);
  delay(100);
  sovrascrivi("/test_sovrascrivi.txt", messaggio);
  delay(1000);
  leggi("/test_scrivi.txt");
  delay(100);
  leggi("/test_sovrascrivi.txt");
  a++;
}
If i comment the call to function everything work as normal.
I dont know what to do, seems to be some memory problem, anyone had already experienced it???

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

Re: sd card read/write guru meditation error

Postby ESP_Sprite » Mon Mar 23, 2020 6:35 pm

You probably only need to do the initialization bit once, so keep that in your setup() function. As you have it now, it's called multiple times, which likely messes things up.

lucadjr
Posts: 2
Joined: Sun Mar 22, 2020 12:28 pm

Re: sd card read/write guru meditation error

Postby lucadjr » Mon Mar 23, 2020 10:51 pm

ESP_Sprite wrote:
Mon Mar 23, 2020 6:35 pm
You probably only need to do the initialization bit once, so keep that in your setup() function. As you have it now, it's called multiple times, which likely messes things up.
Thx for reply!
For initialization bit do you mean this?

Code: Select all

pinMode(5, OUTPUT);

  SPIClass SPISD;
  SPISD.begin(18, 19, 23, 5); //SCK MISO MOSI SS
there is no problem for the non-local declaration? the spi connection will not be active all the time this way? anyway i cannot understand why with smaller project is working, the only difference i note is that the working project had more than 300kb heap free, not working less than 200.
For the moment i "solved" the problem by mooving all the loop inside a while(true) in the setup and leaving loop empty, is working but i would like to understand why!!! :x

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

Re: sd card read/write guru meditation error

Postby ESP_Sprite » Tue Mar 24, 2020 8:14 am

Well, sure, you'd have to take the 'SPIClass SPISD;' and put it out of the function to make it global, but otherwise it would work. By adding the 'while(true)' line, you've effectively made your own loop, and on the ESP32 specifically this also works. (Other chips may require you to exit the setup() function before certain functions start to work, but on the ESP32, the RTOS takes care of that.)

marvin01
Posts: 1
Joined: Wed Nov 11, 2020 2:34 pm

Re: sd card read/write guru meditation error

Postby marvin01 » Wed Nov 11, 2020 2:38 pm

I have exactly the same problem.

My code is below, To see the problem uncomment the first line in <loop>

void setup() {
Serial.begin(115200);

Wire.begin();
delay(3000);

Serial.println("\n\n\n\n\n\n\n\n\n\n");

I2C_scan();
delay(3000);

am2320.begin();
Serial.println("Checking DS1307 Temperature / Humidity Sensor");
print_temp_humidity();
Serial.println("DS1307 Sensor check Complete");
delay(3000);

rtc.begin();
Serial.println("\nRTC check");
set_timedate ();
print_time_date ();
Serial.println("RTC check Complete");

// define pins for SPI bus (For SD card bus)
#define PIN_MISO 27 // GPIO27
#define PIN_MOSI 26 // GPIO26
#define PIN_SCK 25 // GIPO25
#define PIN_CS 33 // GPIO33
SPIClass SPI1(HSPI);
SPI1.begin(PIN_SCK, PIN_MISO, PIN_MOSI, PIN_CS);

Serial.println("\nSD Card Mount");
if (!SD.begin(PIN_CS, SPI1)) {
Serial.println("Card Mount Failed");
} else {
Serial.println("Card Mount Suceeded");
}

Serial.println("\nSD Listing check");
File root;
root = SD.open("/");
printDirectory(root, 0);
Serial.println("SD Listing Complete");
create_logfile();
show_logfile();
PM_logfile = SD.open("/PM_logfile.csv", FILE_WRITE);

tft.init();
tft.setRotation(1);
tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour
clear_display(TFT_BLACK);

Serial.println("\nADS1015 ADC Sensor check");
get_adc_sensors();
Serial.println("ADS1015 ADC Sensor check Complete\n");
}

Who is online

Users browsing this forum: Bing [Bot] and 80 guests