Teensy 4.1 freeze after first save to onboard SD card

Status
Not open for further replies.

Muiota

Member
Hello,

After update to teensyduino 1.54-beta7 I have some problem and I cant find the cause of the problem.

After simple save redraw loop frozen. Audio continues plays.
Saving and redrawing are serial actions in single thread.
If I do not any save program works for a long time without freezing.
If I save simple test file then the file appears, but the next redraw method hangs.

part of initialize code
Code:
#define TFT_SDO 12 //t4 12 // * (MISO), connected to T_DO on the screen
#define TFT_LED 5 // 100 Ohm resistor
#define TFT_SCK 13 //t4 13
#define TFT_MOSI 11 //t4 11
#define TFT_RST 255 // RESET: +3.3V, connected on the screen
#define TFT_CS 9
#define TFT_DC 10

ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_SDO);

tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setScroll(0);

SPI.setMOSI(7); // Audio shield has MOSI on pin 7
SPI.setSCK(14); // Audio shield has SCK on pin 14
//SPI.setMISO(12);
if (SD.begin(BUILTIN_SDCARD))
{
    Serial.println("SD initialized successful");
    delay(300);
}

the part of the code where the freezing occurs
Code:
File file = SD.open("TEST", FILE_WRITE_BEGIN);
file.write("TEST");
file.close();

Serial.println("success save");

tft.fillScreen(ILI9341_BLACK);

Does anyone have any ideas? Thank you.


Code:
teensy_size*: Memory Usage on Teensy 4.1
teensy_size*: FLASH: code:162332, data:722928, headers:7212   free for files:7233992
teensy_size*: RAM1: code:163840, variables:234176   free for local variables:126272
teensy_size*: RAM2: variables:304608  free for malloc\new:219680
Uploading 'firmware' to 'Teensy 4.1' using 'usb:0/140000/0/2'
	The upload process has finished.

IMG_20210413_080030.jpgIMG_20210413_080051_b.jpg
 
Last edited:
Update: when I comment out all of the tft render methods freezing still happen. (pin 13 is down orange led off).
Probably my problem is saving on SD incorrectly.
 
Update: The save is successful without hang if I firing save in the main loop.
If I fire save in another UI loop (TeensyThreads) UI thread is hang.
The only way out I`ll be move the save method to the main thread loop.

My question. Can there be problems with parallel save to SD and access to SPI for draw on ILI9341?
 
Without the code as noted the needed detail is in the part not shown.

I just found a Fault occurring on the Teensy MicroMod thread - having the Fault code display info might give a better clue if it is a Fault hanging or a problem with a code crash.

The issue seen here is likely unrelated - if you had the Print Hardfault code handy it would indicate if it matches - but that code is stuck in a PR to cores.

This case might more likely relate to TeensyThreads doing context switch causing a Hang/Crash?

... but without complete Code or Fault indication ... it is just speculation ... and odd happenstance I just found such a thing where large malloc BMP is written to SD card with other crazy stuff activated with DMA/FlexIO ...
 
Minimal code for reproduce

Code:
#include <TeensyThreads.h>
#include <SD.h>

int led = 13;

void setup() {
  pinMode(led, OUTPUT);
  
  if (SD.begin(BUILTIN_SDCARD))
  {
    Serial.println("SD initialized successful");
    delay(300);
  }
  
  threads.addThread(threadLoop);
}

void threadLoop()
{  
  threads.delay(1000); 
   int iteration = 0;
  while (true)
  {   
    iteration++;
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    threads.delay(500);
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW  
    threads.delay(500);

    File file = SD.open("TEST", FILE_WRITE_BEGIN);
    file.write("TEST");
    file.close(); 
     Serial.print("Success save ");
     Serial.println(iteration);
    threads.yield();
  }
  Serial.println("stop loop");
};


void loop() { 
  delay(1000);               // wait for a second
}


Single flash orange led. Physically file with content "TEST" appears.
In console:
Success save 1


If comment this lines
Code:
// File file = SD.open("TEST", FILE_WRITE_BEGIN);
// file.write("TEST");
// file.close(
Flashing led continuously every second.
In console:
Success save 1
Success save 2
Success save 3
etc
 
Last edited:
That is short and sweet - out of runtime to repro now ... not even a second task doing anything - except loop just burning cycles in a delay

With the print of "Success save 1" the first SD write completed and closed ... And no return on of the LED then it didn't get back to start iteration #2

Does this change anything?
Code:
void loop() { 
  [B]threads.[/B]delay(1000);               // wait for a second
}
 
That is short and sweet - out of runtime to repro now ... not even a second task doing anything - except loop just burning cycles in a delay

With the print of "Success save 1" the first SD write completed and closed ... And no return on of the LED then it didn't get back to start iteration #2

Does this change anything?
Code:
void loop() { 
  [B]threads.[/B]delay(1000);               // wait for a second
}

Without changes. Single save and hang.
 
next guess would be to comment the actual SD file I/O - without that it is nothing and would point to TThreads getting bored with nothing to do between the loop delay and the .yield
 
Anyhow, this is a question to TeensyThread (ftrias)
maybe you cannot have TeensyTread AND loop together?
I would first try another thread that only does delay(1000);
 
Anyhow, this is a question to TeensyThread (ftrias)
maybe you cannot have TeensyTread AND loop together?
I would first try another thread that only does delay(1000);

I decided to give up of TeensyThread in my project.
If I place the code in the main loop everything works.
And it worked fine when the extra thread was only for ili9341 updates.
As soon as I added the save on SD this freeze appeared.

Thank you for helping to find the bug.
 
Last edited:
Status
Not open for further replies.
Back
Top