Very Basic Teensy3.5 query

Status
Not open for further replies.

MogaRaghu

Well-known member
This is my code to Read/Write to a SD Card. OK not mine but adapted from the Example !! I have used Serial1. What I expect :
1. The T3.5 is powered via the USB port.
2. The Serial1 is connected to a Terminal on the same PC via another USB port.
3. I RESET the T3.5 by connecting the Reset pin to 3.3V momentarily.
4. I expect the code to run and tell me what it did over the Serial1 port.
5. But nothing happens. What am i missing ? This is very different from UNO experience. ( In fact with T3.5 the ReBoot button on the downloader is always disabled )

I know i am missing something basic -- need to know which !

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

File myFile;

const int chipSelect = BUILTIN_SDCARD;

void setup()
{
  Serial1.begin(9600);
  Serial1.print("Initializing SD card...");

  if (!SD.begin(chipSelect))
  {
    Serial1.println("initialization failed!");
    return;
  }
  Serial1.println("initialization done.");

  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile)
  {
    Serial1.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    myFile.close();                             // Only now the actual Write to SDC happens
    Serial1.println("done.");
  } else {
    Serial1.println("error opening test.txt");
  }


  myFile = SD.open("test.txt");                 // re-open the file for reading:
  if (myFile) {
    Serial1.println("test.txt:");

    while (myFile.available())
    {
      Serial1.write(myFile.read());
    }
    myFile.close();
  }
  else
  {
    Serial1.println("error opening test.txt");    // if the file didn't open, print an error:
  }
}

void loop()
{
  // nothing happens after setup
}
 
Did you mean to use Serial1 on pins 0and1 via a TTL to USB adaptor, or via the 'Serial' which is on the USB port?

Given pin 13 with the LED isn't used for the T3.5 SD card may be informative to set it to blink during the various stages.
 
Did you mean to use Serial1 on pins 0and1 via a TTL to USB adaptor, or via the 'Serial' which is on the USB port?

Given pin 13 with the LED isn't used for the T3.5 SD card may be informative to set it to blink during the various stages.

First I tried the normal Serial. After the program download when I invoked the Arduino serial monitor, it printed once. But that's it. I was trying to ReBoot which I could not ( the button was disabled )

Then I tried the Serial1 option and Reset method. No luck.

I think (?) The behaviour of the Bootloader on the Teensy is different from the way a normal UNO bootloader behaves - upon Reset check if its a code download. Else execute code. Simple!!
 
Was reset pad wired and used? The button on Teensy takes it to program mode, just stops execution awaiting new code. The button on the loader app is only active IIRC after a manual upload of code - when AUTO is not selected that button then restarts the Teensy.

I use TyCommander which offers a RESET button on the GUI. That may act as desired?

Didn't look much at your code - it is possible the writes are not committed to the flash for reading on the next setup() as written and interrupted. Perhaps at the end of setup() add a read/print of the current file after opening it again and then apply the reset making sure the data written gets flushed out.
 
Hello,

First: < 3. I RESET the T3.5 by connecting the Reset pin to 3.3V momentarily. >, ; I think you have to connect RESET to GND shortly to reset T3.5 (RESET is internally pulled up)

Second: < I think (?) The behaviour of the Bootloader on the Teensy is different from the way a normal UNO bootloader behaves - upon Reset check if its a code download. Else execute code. Simple!! > ; YES, teensy 3.5 uses an extra chip (MKL02...) for uploading code, therefore no bootloader in MK64FX512
 
Was reset pad wired and used? The button on Teensy takes it to program mode, just stops execution awaiting new code. The button on the loader app is only active IIRC after a manual upload of code - when AUTO is not selected that button then restarts the Teensy.

I use TyCommander which offers a RESET button on the GUI. That may act as desired?

Didn't look much at your code - it is possible the writes are not committed to the flash for reading on the next setup() as written and interrupted. Perhaps at the end of setup() add a read/print of the current file after opening it again and then apply the reset making sure the data written gets flushed out.

Nothing complicated about the code.. as I mentioned it is a simple Write and Read from SD card. Works fine on UNO.

As to the TyCommander I am just downloading the Win64 version 0.8.8 . Let me check it out. Thanks
 
Hello,

First: < 3. I RESET the T3.5 by connecting the Reset pin to 3.3V momentarily. >, ; I think you have to connect RESET to GND shortly to reset T3.5 (RESET is internally pulled up)

Second: < I think (?) The behaviour of the Bootloader on the Teensy is different from the way a normal UNO bootloader behaves - upon Reset check if its a code download. Else execute code. Simple!! > ; YES, teensy 3.5 uses an extra chip (MKL02...) for uploading code, therefore no bootloader in MK64FX512

OK got it... but a bit surprised to note that the Reset is active low. If then it should have been RESET with a bar on top of it ?? Or atleast /Reset ?
 
First I tried the normal Serial. After the program download when I invoked the Arduino serial monitor, it printed once.

When printing with Serial, it's common to add code like this:

Code:
  while (!Serial) {  }  // wait for Arduino Serial Monitor to open

With Arduino Uno & Mega, the board has a dedicated USB-serial chip. On Teensy (and some Arduino boards, like Leonardo, Micro, mkr1000) the USB is natively built into the chip which reboots after you upload. Your program can begin running and print stuff before your PC has completed detecting the freshly connected USB device. Even though you didn't physically unplug the cable, as far as your PC is concerned the USB disconnects and quickly reconnects at the end of uploading. That's why you'll find this waiting code at the beginning of most modern Arduino examples.

A brief delay also works, but the exact time can vary. Especially some Windows systems are quite slow. The while (!Serial) wait is best.
 
When printing with Serial, it's common to add code like this:

Code:
  while (!Serial) {  }  // wait for Arduino Serial Monitor to open

With Arduino Uno & Mega, the board has a dedicated USB-serial chip. On Teensy (and some Arduino boards, like Leonardo, Micro, mkr1000) the USB is natively built into the chip which reboots after you upload. Your program can begin running and print stuff before your PC has completed detecting the freshly connected USB device. Even though you didn't physically unplug the cable, as far as your PC is concerned the USB disconnects and quickly reconnects at the end of uploading. That's why you'll find this waiting code at the beginning of most modern Arduino examples.

A brief delay also works, but the exact time can vary. Especially some Windows systems are quite slow. The while (!Serial) wait is best.

Ok got it. Its working now. Just to understand this whole interaction, created a simple Blink as below. The only thing I notice which is different from the UNO behaviour is that the initial " while(!Serial){} ; is blocking ( as it should) after every Reset. Once I Reset the Arduino Serial monitor is closed and our Teensy3.5 faithfully waits for the Serial Monitor to be invoked again. Instaed of this a fixed delay of about 2 secs is good - as it goes into the code anyway.

Code:
byte ledPin = 13;
bool ledState ;  

void setup()
{
  Serial.begin(9600);
  while (!Serial) {}; 

  pinMode(ledPin, OUTPUT); 
  
  Serial.println("Starting after Reset ....");
}

void loop()
{
  static unsigned int blinkCount ; 
  
  ledState = !ledState; 
  digitalWrite(ledPin, ledState); 

  blinkCount++; 
  if ( blinkCount > 100 ) blinkCount = 0; 
  Serial.print( " Number of blinks = " );
  Serial.println( blinkCount ); 

  delay(1000); 

}

Incidentally as defragster pointed out, I downloaded the TyCommander and that is exactly the one I was looking for - all operations in one simple screen. And everything works it should. Looks like there is an option to integrate this with Arduino. Will try and do that . May be then I don't need the HalfKay bootloader ??
 
Last edited:
TyCommander still uses the "on Teensy" HalfKay/PJRC Bootloader process - but with Integration it takes the TeensyLoader (teensy.exe) out of the loop and provides better control over multiple Teensy's and less hassle for me with SerMon start and stop - and the other added GUI features as you note ... 'in one simple screen'.

Not the " while (!Serial) {}; " will hang until USB serial connects - and won't proceed if running from battery or other. " while (!Serial && millis()<2000) {}; " gives a way to adjust a timeout - that is 2 seconds if you just want to watch blinks and not Serial some days.
 
i havnt used tycommander yet but i use teensyloader while testing master/slave sketch in other thread with both connected debugging both at same time
only one teensy programs automatically
the other one will say uploaded done, but i have to press the program button after that to ACTUALLY upload
good enough for now, for bench testing :)
 
i havnt used tycommander yet but i use teensyloader while testing master/slave sketch in other thread with both connected debugging both at same time
only one teensy programs automatically
the other one will say uploaded done, but i have to press the program button after that to ACTUALLY upload
good enough for now, for bench testing :)

tonton81 - take it from @MogaRaghu and give it a try: TyCommander would program both automatically when integrated - individually if different sketch - or together if same sketch as you choose. Also each can open their own Serial Monitor TAB (all devices listed) or WINDOW (Ctrl+N). Makes bench testing much faster - more fun and informative (having both show SerMon) and a Reset GUI button.
 
Status
Not open for further replies.
Back
Top