Teensy 4.0 Freezes when opening multiple files on SD

lerxstrulz

Well-known member
Hey All,

I have a audio app that I have been running on Teensy 3.2, but am finally being forced to upgrade to Teensy 4 ;) It starts off playing a long (5 mins) audio file and while that file is playing it needs to be able to read a text file from the SD. This app worked on my Teensy 3.2 + Audio shield but for some reason it keeps locking up on the Teensy 4.0 + audio shield. I've posted sample code below. The weird thing is, it's not consistent. Sometimes it will open the text file while the audio is playing just fine the first time, then the next time it locks up with a buzzing sound. Sometimes it locks up the first time it reads the text file.

If I stop the audio file, it can read the text file 100 times with no issue. It's only while the audio is playing.

I'm using Teensyduino 1.56 with Arduino IDE 1.8.19.

Maybe the SD library? Is there an alternate to use? Thanks in advance!


Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           loopPlayer;     //xy=366,231
AudioMixer4              effectsMixer;   //xy=539,231
AudioOutputI2S           i2s2;           //xy=713,229
AudioConnection          patchCord1(loopPlayer, 0, effectsMixer, 1);
AudioConnection          patchCord2(effectsMixer, 0, i2s2, 0);
AudioConnection          patchCord3(effectsMixer, 0, i2s2, 1);
AudioControlSGTL5000     audioShield;    //xy=109,242
// GUItool: end automatically generated code

void setup() {

  Serial.begin(9600);

  delay(250);
  
  Serial.println("At startup");

  Serial.println("Allocate memory");
  
  // Always allocate memory for the audio shield!
  AudioMemory(16);

  Serial.println("Enable shield");
  // turn on audio shield
  audioShield.enable();

  Serial.println("Init SD");
  
  if (!(SD.begin(10))) {
     Serial.println("Unable to access the SD card.");
     Serial.println("Using default built-in profile");
  }

  Serial.println("Set shield volume");
  audioShield.volume(.7);
  
  Serial.println("Set mixer volume");
  effectsMixer.gain(0, 1);
  effectsMixer.gain(1, 1);

  Serial.println("Started...playing loop");
  loopPlayer.play("/LOOPS/TKLOOP.WAV");

  Serial.println("Set terminal to \"No line ending\" and enter \"test\" to test the text file read.
}

char received[255] = "";
char cmd_key[15] = "";
char cmd_val[255] = "";

void loop() {

  if (Serial.available()) { 
    Serial.readBytesUntil('\n', received, 255);
    Serial.println(received);
    memset(cmd_key, 0, sizeof(cmd_key));
    memset(cmd_val, 0, sizeof(cmd_val));
    char *key, *val, *buf;
    key = strtok_r(received, "=", &buf);
    strcpy(cmd_key, key);
    if (strcasecmp(cmd_key, "test") == 0) {
      showFile("TEST.TXT");
    }
  }
}

void showFile(const char *filename) {
  File file = SD.open(filename);
  if (file) {
    char c;
    while (file.available()) {
      c = file.read();
      Serial.print(c);
    }
    file.close();
  } else {
    Serial.println("Could not find file!");
  }
}
 
Teensy 4.0 Freezes when opening multiple files on SD - SOLVED

I was able to solve this by using the AudioNoInterrupts() and AudioInterrupts() functions:

Code:
void showFile(const char *filename) {
  [B]AudioNoInterrupts();[/B]
  File file = SD.open(filename);
  if (file) {
    char c;
    while (file.available()) {
      c = file.read();
      Serial.print(c);
    }
    file.close();
  } else {
    Serial.println("Could not find file!");
  }
  [B]AudioInterrupts();[/B]
}
 
Last edited:
Back
Top