Arduino to Teensy 3.1 conversion problem?

Status
Not open for further replies.

wildview

Well-known member
Hi, I'm trying to convert a big Arduino code project to work on a Teensy 3.1 device. Being unfamiliar with the teensy device, I'm unsure why it doesn't allow serial output execution within the setup() function. The Arduino boards allow you to initialize the serial device and send serial output within the setup function, but not so with the Teensy device, not sure why, maybe because of the USB serial chip? The problem is the code I'm converting does a lot of hardware initialization within the setup function while outputting status to &Serial. This is broken and doesn't work on the Teensy 3.1 board. I tested the following code below and it works on Arduino, but not on Teensy 3.1 boards. If I move the serial print statements to the loop function, it works fine on the teensy hardware. Can someone please chime in and let me know why I cannot output serial data within the setup function below? Again, it works fine on Arduino boards.

I also attached the open source code I'm trying to convert that shows the hardware initialization within the setup function. I have resolved all compiler issues and it compiles error free, but I'm just trying to resolve why the code execution doesn't respond properly and why I can't output any serial information within the setup function.

Tnx,
Rob

#include <arduino.h>

#define MAIN_SERIAL_PORT &Serial
//HardwareSerial *main_serial_port;
usb_serial_class *main_serial_port;


void setup()
{
main_serial_port = MAIN_SERIAL_PORT;

main_serial_port->begin(115200); // USB is always 12 Mbit/sec
main_serial_port->println("Hello World..."); // no output to serial on Teensy, but works on Arduino using HardwareSerial class
}

void loop()
{
delay(1000);
}
 

Attachments

  • k3ng_keyer_teensy.zip
    84.8 KB · Views: 134
The code you posted works fine for me. I get Hello world. Are you comming out of the setup before you had chance to get the serial monitor connected?

Try just putting a delay(5000) as the first line in the setup().
 
This is why emulating Arduino Uno's restart-on-serial-monitor-open behavior is on the Teensyduino 1.21 feature list.

If you use Arduino Leonardo or Arduino Due's native port, the same thing happens: the sketch begins right after the upload and doens't restart again shortly after you open the serial monitor. The data gets sent before you can ever see it.
 
Yes, putting the delay(ms) ahead of all my initialization code in the setup function fixed the serial output issue. That's weird, I've never had to do that before on any of my uno/mega or nano Arduino boards. Now I have to figure out why the rest of my Ardunio code is not executing properly on the teensy. I'm hoping it's not because this board is operating at 3.3v versus 5v.
 
The reason is due to differences in the way USB is handled. In the Uno/Mega they have a separate microprocessor that handles the USB connection, and that processor watches the RX/TX pins. If a program opens up the serial monitor on the host side, the microprocessor resets the Uno/Mega. Similarly, on the Nano, you generally have an external FTDI converter to go to USB, which also resets the board when the serial monitor is opened.

On the Teensy 3.x side, the microprocessor manages the USB directly, and at present it is not reset when somebody opens the monitor. The ATmega32u4 microprocessor, which is used in the Leonardo and Teensy 2.x behaves similarly.
 
I sometimes put the following in the setup after my begin when I want to debug and want it to wait for me.

Code:
Serial.begin(115200);
do {
	Serial.print("Waiting for you before starting...\n\r");
	delay(1000);
} while (!Serial.available());
Serial.read();

Then you just hit the send button in your serial monitor and the program continues and you don't lose anything. Of course you need to remove it prior to going into production on headless mode.

As for your circuit 5v verses 3.3v. It should be OK, typically 5v chips see anything over 2.5v as a 1 and below as a 0 (but not always). So not sure.
Also remember that by default the teensy is running at 90mhz and executes instructions a lot faster which is a lot faster that an Arduino UNO so timing could be a issue here for anything you drive if they are expecting something slower. Try putting the teensy down to 24Mhz (any lower and you lose the USB Serial).
 
The ATmega32u4 microprocessor, which is used in the Leonardo and Teensy 2.x behaves similarly.

Actually, on Teensy 2.0 I wrote code to emulate Arduino Uno's reset behavior.

Arduino Leonardo was released while I was working on Teensy 3.0, and shortly before Teensy3's release, Arduino began publishing "while (!Serial) ;" in the beginning of their examples, to "solve" this problem. It appeared as if Teensy 3.x would not need Uno reset emulation.

Since then, I put a lot of work in OctoWS2811 and then the audio lib, and lots of other stuff, but I really slacked on getting the Uno reset emulation done. Now, after 2 years, it's become clear most people still publish plenty of examples and develop code with the expectation of Arduino Uno reset behavior. Leonardo and the updated examples didn't change much.

I'm definitely going to *finally* get this done in Teensyduino 1.21 (probably January 2015), so these types of programs for the '328-based board will work the way people expect when run on Teensy 3.1.
 
Thanks guys. I really appreciate the info and Paul thanks for adding that feature into Teensyduino for us. It will be very helpful. I thought I was going crazy yesterday, since I'm so used to the way the Arduino boards behave =)

Thanks again.
 
Status
Not open for further replies.
Back
Top