Teensy LC Serial2 9-bit hangs on open

Dear all,

I have a Teensy LC with which I want to do 9-bit serial communication. For this, I have uncommented the
Code:
#define SERIAL_9BIT_SUPPORT
line in HardwareSerial.h. I attempt to open the serial port using
Code:
Serial2.begin(9600, SERIAL_9O1);
and I get no compiler errors. (I.e. the 9-bit support was successfully enabled, or the SERIAL_9O1 wouldn't have been defined.)

However, when I run this 'sketch,' it seems to hang on the Serial2.begin line. Indeed, modifying the setup function to
Code:
Serial.begin(115200);
Serial.print("A");
Serial2.begin(9600, SERIAL_9O1);
Serial.print("B");
shows an A but not a B in the serial monitor in Arduino.

The same code does work on a Teensy 3.2.

Any ideas what's going wrong?


Full disclosure: I am using the official mavlink-2 C library and a JetiEX library together, to pass data from an autopilot over the JETI telemetry system. This setup worked on 3.2 but when I tried on LC it hung. I narrowed it down to the Serial2.begin statement of the library; putting this statement (Serial2.begin(9600, SERIAL_9O1)) manually in setup() caused the same problem. Then I had no more time to make a minimal sketch that only includes the Serial begin calls and nothing else, and now I don't have the teensies available until tomorrow. I will of course try to make an MWE (well, not-working example) then, but the program really does hang on the Serial2.begin statement.
 
Mwe

Alright, as promised a MWE with full source code:
Code:
void setup(){
  Serial.begin(115200);
  Serial.println("Serial started"); Serial.flush();
  Serial1.begin(9600);
  Serial.println("Serial1 started"); Serial.flush();
  Serial2.begin(9600);
  Serial.println("Serial2 started"); Serial.flush();
  
  Serial1.begin(9600, SERIAL_8N2);
  Serial.println("Serial1 reconfigured SERIAL_8N2"); Serial.flush();
  Serial2.begin(9600, SERIAL_8N2);
  Serial.println("Serial2 reconfigured SERIAL_8N2"); Serial.flush();
  
  Serial1.begin(9600, SERIAL_9O1);
  Serial.println("Serial1 reconfigured SERIAL_9O1"); Serial.flush();
  Serial2.begin(9600, SERIAL_9O1);
  Serial.println("Serial2 reconfigured SERIAL_9O1"); Serial.flush();
  
}

void loop(){
  //nop
}
Gives output (on teensy LC fresh in mailbox):
Teensy LC said:
Serial started
Serial1 started
Serial2 started
Serial1 reconfigured SERIAL_8N2
If I comment line 11+12 (first reconfigure of Serial2) then I get to "Serial1 reconfigured SERIAL_9O1" so there really seems to be an issue with starting Serial2 in something else than standard config?

Running this same code on a 3.2:
Teensy 3.2 said:
Serial started
Serial1 started
Serial2 started
Serial1 reconfigured SERIAL_8N2
Serial2 reconfigured SERIAL_8N2
Serial1 reconfigured SERIAL_9O1
Serial2 reconfigured SERIAL_9O1
 
Looking at this now and I see what's wrong.

First, a bit of bad news. Teensy LC doesn't have 9 bit hardware capability on Serial2 and Serial3. Only Serial1 can do 9 bit mode on Teensy LC.

The code could do much better and not crash. I've fixed it on github. You can grab the latest, or just put these 2 files into your hardware/teensy/avr/cores/teensy3 folder.
 

Attachments

  • serial2.c
    18 KB · Views: 75
  • serial3.c
    12.6 KB · Views: 71
Ah, that's cool.
Which modes does the LC support on 2/3? I noticed that also e.g. SERIAL_8N1 causes my teensy to crash. But… isn't that the default config?
Does this warrant a bit closer look on which configurations to allow?

Good news for me: I only need 9-bit on one serial port, so I've simply moved the JETI connection to Serial1 and the Mavlink (which is default mode, SERIAL_8N1?) to Serial3.

[edit] Have pulled your new serial23.c files, and now my test sketch as reported above works:
Teensy LC said:
Serial started
Serial1 started
Serial2 started
Serial1 reconfigured SERIAL_8N2
Serial2 reconfigured SERIAL_8N2
Serial1 reconfigured SERIAL_9O1
Serial2 reconfigured SERIAL_9O1
However, I assume that Serial2 isn't actually working at 9O1 now, so which mode would it be in exactly?
 
Last edited:
Looking at the code a bit, it seems that the serial drivers are just ignoring some of the flags in the SERIAL_XXXX mode options. I suppose this is something that can only easily be checked at run-time, at which point it would be difficult to issue a warning?

Well, for me it would be good enough if the UART page had a good description of which modes are allowed on which ports, plus an explanation of the behaviour when using an unsupported mode. (Seems to me: quiet fallback to the nearest possible mode?)
 
I've edited the serial page with this:

Teensy 3.0, 3.1, 3.2 support 9 bit mode on Serial1, Serial2 and Serial3.
Teensy 3.5, 3.6 support 9 bit mode on Serial1, Serial2, Serial3, Serial4, Serial5 and Serial6.
Teensy LC supports 9 bit mode only on Serial1. Serial2 and Serial3 do not have 9 bit mode.
 
I'm a little confused, and I may be looking in the wrong location, but the current Teensy LC uses the MKL26Z64VFT4 (according to the schematic). According to the KL26 sub-family reference manual (https://www.pjrc.com/teensy/KL26P121M48SF4RM.pdf) which specifically lists that processor part number, UART0 (chapter 39) supports 8, 9, and 10 bit characters and UARTs 1&2 (chapter 40) support 8 or 9 bit characters. So, am I reading the manual incorrectly and there really is not 9 bit capable hardware on UARTs 1&2, or is this a software issue with the HardwareSerial library?
 
Last edited:
Back
Top