Run code on Teency 4/0

mena

Active member
Hi,
I am new here and this is the first time that I use Teensy 4.0.
I'm working on code and I run this code on ESP32 , this code is working without any problem with esp32 . Now I'm trying to run the same code on teensy 4.0, the problem the USB stay in case of plug in and plug out. and when open serial monitor the (COM Teensy serial) be sometime online and then be offline ,Why?? what is the problem?
Where the execution of the program does not continue as is the case with ESP32!!!
 
Why?? what is the problem?

Are we supposed to guess why specific code isn't working if we can't even see the code or what happens on your screen?

I want to help you, but it seems impossible without actually being able to see or reproduce the problem.
 
Yes, you're right, but I can't share the code because it's too big, but I'm wondering are there any changes that I can make on the code or setting to run it on teensy? why does it work so well on ESP32?
 
Again you're asking the question "why", which can't possibly be answered with essentially zero visibility of the problem.

It might be a library or specific code using ESP32 specific hardware that doesn't exist on other boards.

Or it might be something simple, like Teensy is (sometimes) able to upload even if Tools > Ports isn't selected properly (it tries to find your Teensy automatically) but opening the Serial Monitor does require Tools > Ports set properly. Sometimes people familiar with other boards believe they must have set the port correct because it was able to upload.

Another common simple problem is with programs that print a message only at startup. Teensy boots up rapidly and that message might "print" before the serial monitor is open. The normal solution is to add "while (!Serial) ; //wait".

You did say "when open serial monitor the (COM Teensy serial) be sometime online and then be offline". Difficult to know what you're actually seeing on your screen. But maybe Teensy is rebooting, which causes the USB to disconnect (the USB is built into the chip, not a separate chip like most ESP boards). Teensy as a default fault handler which automatically reboots 8 seconds after a fault condition. A feature called CrashReport lets you print info about what went wrong before the reboot. Maybe that can help?

But this is all blind guesswork. I hope you can understand how writing these guesses takes a lot of time and has a low change of actually helping you. Usually questions like this get no answers, because who wants to put so much effort with so little change of actually solving anything?
 
Yes, you're right, but I can't share the code because it's too big, but I'm wondering are there any changes that I can make on the code or setting to run it on teensy? why does it work so well on ESP32?

As Paul stated it is really hard (impossible) to give you are reasonable answer without information. So far the only thing we know is you are using a T4.

Have no idea of what you are saying with:
Now I'm trying to run the same code on teensy 4.0, the problem the USB stay in case of plug in and plug out. and when open serial monitor the (COM Teensy serial) be sometime online and then be offline ,Why??

Throwing a few darts:

If you are saying, that your code runs fine when plugged in, but hangs if not...
Then maybe your code has something like: while (!Serial) ;
which will hang until it has a valid Serial connections. How to avoid, I will put in timeouts in loops like that: something like while (!Serial && (millis() < 5000)) ; // wait up to 5 seconds
Note: the above is only for program startup. Otherwise I would use elapsedMillis object or the like to time it...

But again guessing, don't even know what USB type you are building for.

Don't know the hardware setup...
 
This is part of my code :
every thing after the function is not printing , Why ?!! I run the same code on esp32 and it work well !
Code:
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("0"); 
 Serial.println("1"); 
 delay(1000);
 Serial.println("2"); 
 Serial.println(" before function");
 int value=256; 
size_t out;
 float *ar=(float*)malloc(value*sizeof(float));
unsigned char  bytes = function_trt(arr, out );
Serial.println("after function");
 }
 
Last edited:
As I mentioned in previous post: if your USB is plugged in and a connection to some serial port software like Serial Monitor: the code will not continue beyond your (!Serial)

But assuming plugged in... What do you get as output?
i.e. do you get the output: after function

if not maybe that function hung...

Also hint, in cases like this, I will often add a Serial.flush();
after some of the Serial prints in order to force it out. As for example if your sketch hard hang in bad way it may have had a few subsequent prints that have not gone out yet.
 
You really need to understand this is not the way this forum works. You're asking us to help you troubleshoot your code. We generally expect you to show us the code in question. At the top of every page on this forum is "Forum Rule: Always post complete source code & details to reproduce any issue!".

"Complete" means you show a program anyone can copy into Arduino and upload to a Teensy to reproduce the problem. Or at least try to reproduce the issue, if it's an intermittent problem (the really tough cases...)

If your program is very large, we generally expect you to whittle down the code to only the smallest program which still reproduces the problem. Yes, that is extra work for you. But it is well worth your time, because we really do try to help here. If you want this sort of help, you really need to do your part. You're not going to get a magical answer where we solve your problem by blind guessing based on virtually zero info. We're sometimes pretty good at guessing with partial or incomplete info, but nobody can do what you're asking, which is guessing with essentially zero info.

Already many words have been written, representing real time spent by real people, which is basically just wasted effort. That time could have been used to actually solve your problem, if only you had followed the forum rule. Please, if you keep posting here, follow the rule. Show us a complete program which can copied into Arduino and run on another Teensy board to reproduce the problem.
 
After upload the code , the output in serial monitor is just print
"before function " after that the COM be offline and then re printing
"before function " .... and so on...
 
Last edited:
Yet again, you're asking us to explain "why", but you didn't show us the code which is crashing or stalling inside that function!

The problem is not with the code because it works correctly with ESP32! but with teensy , after upload the code , the output in serial monitor is just print
"before function " after that the COM be offline and then re printing
"before function " .... and so on...
 
Last edited:
The problem is not with the code because it works correctly with ESP32!

The probably almost certainly is with that code.

It maybe the perfect code for ESP, but using some ESP-only feature.

Or it may have a bug which does not crash on ESP, but does cause a problem on Teensy. Accessing memory which does not exist or using a null pointer are examples of code which works on some boards but not on other boards. We have seen this many time on other code.

Do not assume the code is perfect just because it runs on ESP.
 
Would you expect a car to run if you put a petrol engine in a diesel car with the same ancillaries? No, it would not work!!
The same Here.
You are also contradicting yourself. You have said "every thing after the function is not printing" yet in your last post you said "the output in serial monitor is just print
"after function " after that the COM be offline and then re printing
"after function " .... and so on...".
So it is printing after the function. But you said it wasn't. Also the code you have presented is just in SetUp so how could it keep on repeating printing "After Function".

If you want help then follow the forum rules and give consistent posts!!
 
A "problem" is, ESP prints a lot of info with logging enabled. You get detailed info whats wrong with every tiny problem (yes, with the mentioned NULL-Pointers, too), it even prints warnings at runtime, etc. It even has by default running watchdogs etc. If you then run a Teensy, it prints just nothing, which can be pretty confusing if you're not used to that. The restarts (USB comes and goes) are a sure sign that something is crashing the teensy. As a new user you're pretty lost, then. ESP prints even messages when booting. If then there are absolutley NO messages you don't know what's happening - you don't even know that it is rebooting because there was a reset.

Search for "Crash" Report" in this forum to enable at least some basic info.

Do you think Teensy would exist if it could not run programs? :)
No.. so.. think logical. If'ts not the hardware - what else can it be?
Right: The program.

ESP tells you, in which sourcecode, in which line there was i.e. a null-pointer problem on the console.
Takes 1 second (in Platformio one mouseclick on the message) to see the line, and you can edit that.

Hopefully, in the distant future, Teensy is that helpful, too.
Debugging was announced..hm..5 years ago?
 
Last edited:
I agree a "Hardware independent" should be just fine. I was just trying to point out the hardware dependency with my post!
 
Doesn't ESP use a USB chip that is onboard and always online to the ESP MCU?

This p#6 code suggests KurtE's p#5 comment may be the issue:
Code:
void setup() {
  Serial.begin(9600);
 [B][COLOR="#FF0000"] while (!Serial) {[/COLOR][/B]
    ; // wait for serial port to connect. Needed for native USB port only
  }

Should be something like this is failing when no USB Host attached:
Code:
void setup() {
  Serial.begin(9600);
 [B] while (!Serial && millis() <2000) {[/B]
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
Doesn't ESP use a USB chip that is onboard and always online to the ESP MCU?

This p#6 code suggests KurtE's p#5 comment may be the issue:
Code:
void setup() {
  Serial.begin(9600);
 [B][COLOR="#FF0000"] while (!Serial) {[/COLOR][/B]
    ; // wait for serial port to connect. Needed for native USB port only
  }

Should be something like this is failing when no USB Host attached:
Code:
void setup() {
  Serial.begin(9600);
 [B] while (!Serial && millis() <2000) {[/B]
    ; // wait for serial port to connect. Needed for native USB port only
  }


The same problem "the output in serial monitor is just print
"before function " after that the COM be offline and then rebooting and re printing
"before function " .... and so on... "
 
Yes. Again: Your program crashes. In this case, the Teensy reboots - same as the ESP does in case of a crash.
 
Maybe spend a moment to consider what is important. Is arguing code code is correct what matters? Or is getting the bottom of what's really wrong the goal?
 
Yes. Again: Your program crashes. In this case, the Teensy reboots - same as the ESP does in case of a crash.

I used two types of microcontrollers and they work !! I used with the first one
Code:
Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
and and with the second :
Code:
Serial.begin(115200);
JUST with teensy I have problem!!!!
 
I used two types of microcontrollers and they work !! I used with the first one
Code:
Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
and and with the second :
Code:
Serial.begin(115200);
JUST with teensy I have problem!!!!

Obviously because the code is written to suit the ESP. This may or may not be intentional, but it looks like something in your code is causing the Teensy to crash.
If you post your code, or a small subset of it, that still causes the Teensy to crash I am sure that we will be able to help you.
 
Many boards do not have USB on the actual microcontroller. Most ESP boards fall into that category. A separate chip converts hardware serial to USB.

Some boards, like Teensy and Arduino Leonardo & Zero & MKR1000 have USB built into the microcontroller. This is much faster and allows for more USB protocols than only serial.

But the 2 types work slightly different. With a separate USB chip, the USB remains connected when the processor reboots. If you print something early in setup(), the USB chip is ready to send to your PC because the USB part did not reboot.

With native USB, the USB also reboots when the processor reboot. To your PC, it appears as if the USB cable was unplugged and reconnected. Driver software on your PC takes time to detect the USB port. During that time, if you use Serial.print(), there is no connection.

Both boards types of boards work as they should. But it is easy to write software which depends on the details on one type of hardware and then does not work properly on the other type of hardware.
 
Back
Top