Advertisements

Teensy 3.6 USB Serial Startup

The Arduino Serial doc says the USB hardware on the (now obsolescent) Leonardo requires a test-for-open before using the serial port:

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
}

As it happens, you must also use that test on the ARM-based Teensy 3.6.

The gotcha happens when the USB port doesn’t become available, in which case the conditional remains true and the loop continues forever, which is precisely what happened when I powered the Teensy from a USB battery pack on the Squidwrench Operating Table.

After some flailing around, this startup snippet falls through after ahem awhile:

#define BUILTIN_LED 13

... snippage ...

Serial.begin(115200);

int waited = 0;
while (!Serial && waited < 3000) {
  delay(1);
  waited++;
  if (! (waited % 50))
    FlipPin(BUILTIN_LED);
}

... snippage ...

Serial.printf(" serial wait: %d ms\n\n",waited);

The serial startup delay seems to vary unpredictably between 800 and 1800 ms, so 3000 ms may be too short:

serial wait: 1033 ms
serial wait: 899 ms
serial wait: 907 ms

The ARM Teensy connects the board's built-in LED to the same SPI clock as on the AVR Arduinos, so it's only useful during startup, but having some hint will come in handy the next time it jams for another reason.

Advertisements

,

  1. #1 by GremlinWrangler on 2018-04-27 - 02:34

    Wishing the canonical examples used some other means to control serial being since that little line catches so many people moving to battery power and leads down so many rabbit holes. Seems to be one every 6 weeks or so on the forum.

  2. #2 by scruss2 on 2018-04-28 - 13:24

    Sending CDC serial over an unconnected USB port can do some very unexpected things. The most annoying I’ve seen is having the µc lock up, waiting for USB handshakes that never come. Maybe check with PJRC support on what the recommended approach would be: their support is great.

    The Leonardo isn’t obsolescent and is back in production again, BTW. Arduino seem to be slowly recovering after their split and legal issues. Just as well they still make the Leonardo and the Micro: the assistive tech not-for-profit I work for based its flagship product on it: http://www.makersmakingchange.com/lipsync/

    • #3 by Ed on 2018-05-01 - 17:15

      Mmmmph, I’d been hoping to avoid wrapping all the serial outputs with a conditional. Maybe there’s a (simple) way to conditionalize the s_putc() function (formerly) required to make printf() work, so as to avoid doing it much higher in the calling stack. More study is needed, as the saying goes.