Guru Meditation Error: Core 1 panic'ed

CobaltEcho
Posts: 7
Joined: Sun Aug 13, 2023 10:35 pm

Guru Meditation Error: Core 1 panic'ed

Postby CobaltEcho » Wed Aug 30, 2023 2:46 am

(ESP32 Dev Board) Im trying to read wav files off an SD card (and play them), but keep running into this error:

And contrary to the Serial Monitor, I do have three short wav files on the SD card.

Code: Select all

Starting setup...
SD Card initialized.
I2S output initialized.
Root directory opened.
No WAV files found.
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x400d1be4  PS      : 0x00060630  A0      : 0x800d63ec  A1      : 0x3ffb2270  
A2      : 0x3ffc2640  A3      : 0x3ffc2620  A4      : 0x00000000  A5      : 0x3ffc2664  
A6      : 0x3ffb8188  A7      : 0x80000001  A8      : 0x800d1da0  A9      : 0x3ffb2210  
A10     : 0x00000000  A11     : 0x00000013  A12     : 0x3f404e11  A13     : 0x3f40a4bf  
A14     : 0x3f40a4bf  A15     : 0x3ffc2620  SAR     : 0x00000019  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x40084551  LEND    : 0x40084559  LCOUNT  : 0x00000027  


Backtrace: 0x400d1be1:0x3ffb2270 0x400d63e9:0x3ffb2290




ELF file SHA256: dd159258c1c8dc84

Rebooting...
Not sure what's going on, did something happen with my ESP32?


Here is my full code:

Code: Select all

#include <Arduino.h>
#include <AudioFileSourceSD.h>
#include <AudioGeneratorWAV.h>
#include <AudioOutputI2S.h>
#include <SD.h> // Include the SD library

// Pin definitions for the I2S DAC
#define I2S_BCLK_PIN  26  // Bit clock pin for I2S
#define I2S_LRC_PIN   25  // Left/right channel clock pin for I2S
#define I2S_DOUT_PIN  22  // Data out pin for I2S

// Pin definitions for the SD card module
#define SD_CS_PIN     2   // Chip select (CS) pin
#define SD_MOSI_PIN   23  // MOSI pin for SPI communication
#define SD_MISO_PIN   19  // MISO pin for SPI communication
#define SD_SCK_PIN    18  // Clock pin for SPI communication

AudioGeneratorWAV *wav;
AudioOutputI2S *out;
AudioFileSourceSD *file;
File root;

bool openNextWavFile() {
  File entry = root.openNextFile();
  while (entry) {
    // Check if the file is a WAV file based on the extension
    if (entry.name() && strstr(entry.name(), ".WAV")) {
      const char *filePath = entry.name();
      Serial.print("Playing WAV file: ");
      Serial.println(filePath);

      // Open the WAV file
      file = new AudioFileSourceSD();
      if (file->open(filePath)) {
        wav = new AudioGeneratorWAV();
        if (wav->begin(file, out)) {
          // Successfully opened the WAV file
          Serial.println("WAV file playback started.");
          return true;
        } else {
          Serial.println("Could not start WAV file playback");
          delete wav;
          delete file;
        }
      } else {
        Serial.println("Could not open WAV file");
        delete file;
      }
    }

    entry = root.openNextFile();
  }

  // No more WAV files found
  return false;
}

void setup() {
  Serial.begin(115200);
  Serial.println("Starting setup...");

  // Set the pin modes manually
  pinMode(SD_CS_PIN, OUTPUT);
  pinMode(SD_MOSI_PIN, OUTPUT);
  pinMode(SD_MISO_PIN, INPUT);
  pinMode(SD_SCK_PIN, OUTPUT);

  // Initialize the SD library
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("SD Card initialization failed");
    while (1); // Halt program execution if SD initialization fails
  }
  Serial.println("SD Card initialized.");

  // Initialize the I2S output
  out = new AudioOutputI2S();
  out->SetOutputModeMono(true); // Set to true for mono audio
  Serial.println("I2S output initialized.");

  // Open the root directory of the SD card
  root = SD.open("/");
  if (!root) {
    Serial.println("Could not open root directory");
    while (1); // Halt program execution if root directory cannot be opened
  }
  Serial.println("Root directory opened.");

  // Try to open the first WAV file
  if (openNextWavFile()) {
    Serial.println("Playing WAV file...");
  } else {
    Serial.println("No WAV files found.");
    root.close();
  }
}

void loop() {
  // Check if the WAV file is still playing
  if (wav->isRunning()) {
    if (!wav->loop()) {
      // If the file has finished playing, close it and try the next one
      wav->stop();
      delete wav;
      delete out;
      delete file;
      Serial.println("WAV file playback finished.");
      
      // Try to open the next WAV file
      if (!openNextWavFile()) {
        Serial.println("No more WAV files found.");
        root.close();
      }
    }
  }
}

markdh102
Posts: 11
Joined: Sat Apr 18, 2020 12:03 pm

Re: Guru Meditation Error: Core 1 panic'ed

Postby markdh102 » Wed Aug 30, 2023 6:04 am

As it does not find any WAV files, wav is not initialised.
You then try and access it in loop().
I'm pretty sure this will cause the core panic.
As for why it's not seeing any files, I don't know the answer.

tanmanh0707
Posts: 15
Joined: Fri Jun 23, 2023 3:26 am

Re: Guru Meditation Error: Core 1 panic'ed

Postby tanmanh0707 » Thu Aug 31, 2023 10:55 am

Be careful when working with pointers.
Firstly, initiate all pointers to nullptr

Code: Select all

AudioGeneratorWAV *wav = nullptr;
AudioOutputI2S *out = nullptr;
AudioFileSourceSD *file = nullptr;
Secondly, before accessing to pointer or deleting pointer, make sure it is not null using:

Code: Select all

if (wav) {
  // do something
  delete wav;
  wav = nullptr; //Assign it to null for further use
}
You got Core paniced because you accessed to an undefined pointer in loop, the wav.

Code: Select all

void loop() {
  // Check if the WAV file is still playing
  if (wav->isRunning()) {
  }
  ...
}
Why the wav is undefined? Because wav is not initiated and it's not yet allocated - the

Code: Select all

wav = new AudioGeneratorWAV();
never be called.

Who is online

Users browsing this forum: No registered users and 93 guests