Skip to content

With ESP32S2, SD card read works in main loop, but will crash when called in a subroutine.  #4716

@RudyFiero

Description

@RudyFiero

Hardware:

Board: ESP32-S2-Saola-1M
Core Installation version: ?1.0.0? ?1.0.1-rc4? ?1.0.1? ?1.0.1-git? ?1.0.2? ?1.0.3?
IDE name: Arduino IDE 1,8.13
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 115200
Computer OS: Windows 10

Description:

I can read a file on a SD card if I try and read it from Setup() or in Loop(). But if I have the same routine in a subroutine I get a crash.

Sorry if I have not done a good enough job with the formatting. I always have difficulty with it, but I can't put the time in to fix it. I should have been in bed a couple of hours ago.

Sketch: (leave the backquotes for code formatting)

//Change the code below by your sketch
/*
   Connect the SD card to the following pins:

   SD Card | ESP32
      D2       -
      D3       SS
      CMD      MOSI
      VSS      GND
      VDD      3.3V
      CLK      SCK
      VSS      GND
      D0       MISO
      D1       -
*/
#include "FS.h"
#include "SD.h"
#include "SPI.h"

int    DACvalue = 0;
int count = 0;

//-------------------------------------------
#define VSPI FSPI
static const int spiClk = 1000000; // 1 MHz

//uninitalised pointers to SPI objects
SPIClass * vspi = NULL;

//-------------------------------------------
void doFile() {

  File profile = SD.open("/foo.txt", FILE_READ);

  if (!profile) {
    Serial.println("Opening file to read failed");
    return;
  }

  Serial.println("File Content:");

  while (profile.available()) {
    Serial.write(profile.read());
    delay(150);
  }
  profile.close();

  Serial.println("");
  Serial.println("File read done");
  Serial.println("=================");
}

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

  //------------------------------------
  SPIClass SDSPI(HSPI);

#define SD_miso  17
#define SD_mosi  16
#define SD_sck   15
#define SD_ss    14

  SDSPI.begin(SD_sck, SD_miso, SD_mosi, -1);
  pinMode(SD_ss, OUTPUT);        //HSPI SS  set slave select pins as output

  if (!SD.begin(SD_ss, SDSPI)) {
    Serial.println("Card Mount Failed");
    return;
  }
  //------------------------------------

  File profile = SD.open("/foo.txt", FILE_READ);

  if (!profile) {
    Serial.println("Opening file to read failed");
    return;
  }

  Serial.println("File Content:");

  while (profile.available()) {

    while (profile.available()) {
      Serial.write(profile.read());
      delay(50);
    }8.13

    Serial.println("");
    Serial.println("File read done");
    Serial.println("=================");
  }
}
  //===========================================
  void loop() {
    count++;

    if (count == 10) {
        doFile();      // This will cause a crash, but the same routine below works.
/*
//----------- 
  File profile = SD.open("/foo.txt", FILE_READ);

  if (!profile) {
    Serial.println("Opening file to read failed");
    return;
  }

  Serial.println("File Content:");

  while (profile.available()) {
    Serial.write(profile.read());
    delay(50);
  }
  profile.close();

  Serial.println("");
  Serial.println("File read done");
  Serial.println("=================");

//-----------
*/
    }
    Serial.println(count);
    delay(1000);

  }

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3ffe6100,len:0x8
load:0x3ffe6108,len:0x608
load:0x4004c000,len:0xa38
load:0x40050000,len:0x2848
entry 0x4004c190


7
8
9
File Content:
/Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/freertos/queue.c:1440 (xQueueSemaphoreTake)- assert failed!

abort() was called at PC 0x40029da5 on core 0

Backtrace:0x40027642:0x3ffc6ac0 0x40027f29:0x3ffc6ae0 0x4002c8ed:0x3ffc6b00 0x40029da5:0x3ffc6b80 0x40084858:0x3ffc6bc0 0x40083aa4:0x3ffc6be0 0x40082e92:0x3ffc6c10 0x4008cafd:0x3ffc6c40 0x4008eb2f:0x3ffc6c60 0x40090308:0x3ffc6ca0 0x40089bf2:0x3ffc6cd0 0x4001a1cb:0x3ffc6cf0 |<-CORRUPTED


ELF file SHA256: 0000000000000000

Rebooting...
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x3 (RTC_SW_SYS_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x40025f79
SPIWP:0xee
mode:DIO, clock div:1
load:0x3ffe6100,len:0x8
load:0x3ffe6108,len:0x608
load:0x4004c000,len:0xa38
load:0x40050000,len:0x2848
entry 0x4004c190
File Content:
1000
1100

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: StaleIssue is stale stage (outdated/stuck)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions