Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: Teensy 4.1 freeze after first save to onboard SD card

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    13

    Teensy 4.1 freeze after first save to onboard SD card

    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.
    Click image for larger version. 

Name:	IMG_20210413_080030.jpg 
Views:	11 
Size:	97.5 KB 
ID:	24428Click image for larger version. 

Name:	IMG_20210413_080051_b.jpg 
Views:	8 
Size:	154.1 KB 
ID:	24429
    Last edited by Muiota; 04-13-2021 at 06:01 AM.

  2. #2
    Junior Member
    Join Date
    Jan 2018
    Posts
    13
    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.

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    13
    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?

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,154
    The error is in the code you do not show

  5. #5
    Junior Member
    Join Date
    Jan 2018
    Posts
    13
    Quote Originally Posted by WMXZ View Post
    The error is in the code you do not show
    My source code looks pretty confusing.)
    I'll try to make a simple example with saving to SD in a another thread (TeensyThread).

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,916
    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 ...

  7. #7
    Junior Member
    Join Date
    Jan 2018
    Posts
    13
    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 by Muiota; 04-13-2021 at 08:10 AM.

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,916
    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() { 
      threads.delay(1000);               // wait for a second
    }

  9. #9
    Junior Member
    Join Date
    Jan 2018
    Posts
    13
    Quote Originally Posted by defragster View Post
    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() { 
      threads.delay(1000);               // wait for a second
    }
    Without changes. Single save and hang.

  10. #10
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,916
    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

  11. #11
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,154
    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);

  12. #12
    Junior Member
    Join Date
    Jan 2018
    Posts
    13
    Quote Originally Posted by WMXZ View Post
    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 by Muiota; 04-13-2021 at 10:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •