USBSerial FullSpeed for Teensy 3.2

Hi! (Hopefully) quick question here - I’ve been working with a Teensy 3.2 and Mbed OS 5.15.7. I’ m writing data to the terminal from the USB (virtual serial) with a stupid simple program like Mbed Test below. The data prints at around 19k characters per second.
An analogous test on Teensyduino (see Teensyduino Test) printing data from the main USB with Serial.print() prints over 200k characters per second .
Is there a way to make the Mbed virtual USB port print faster than the current ~115k Baud? Note: I believe the baud rate listed in Teensyduino Test is arbitrary, since the Teensyduino Serial should run at peak speeds regardless of baud rate. Thanks so much!

Mbed Test

USBSerial serial_to_pc; 
int main() 
{
    while(true)
    {
        for (uint8_t x = 0; x < 100; x++)
        {
            serial_to_pc.printf("aaaaaaaaaa");
        }
    }
}

Teensyduino Test

void setup() {
  // put your setup code here, to run once:
  Serial.begin(460800);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (uint8_t x = 0; x < 100; x++)
  {
    Serial.print("aaaaaaaaaa");
  }
}

Hello Tyler,

  • The printf function used in mbed programs has more formatting options (it’s more complex) than the print function used in aduino programs. Hence, it takes more time to execute the printf function than the print one.

  • The USBSerial connection differs from a UART one. No matter whether there is a receiver connected to the UART the bytes are sent at the same rate. However, in case of a USBSerial it matters (I refer here to the byte rate rather than to the bit rate).

You can verify both things by running the code below. Test first the printf function and then the send function. Run two tests to verify both functions. One when nothing is connected to the USB port and another one with a serial terminal connected to the USB port. Let each test run at least 5 minutes to get stabilized byte rate.

 #include "mbed.h"
#include "USBSerial.h"

uint8_t     buff[] = "aaaaaaaaaa";
Timer       timer;
USBSerial   usbSerial(false);

int main()
{
    printf("Starting...\r\n");

    while (1) {
        timer.reset();
        timer.start();

        // Testing the 'printf' byte speed
        for (uint8_t x = 0; x < 100; x++) {
            usbSerial.printf("%s", buff);
        }

        // Testing the 'send' byte speed
//        for (int i = 0; i < 100; i++) {
//            usbSerial.send(buff, sizeof(buff));
//        }

        timer.stop();
        auto elapsedTime = chrono::duration_cast<chrono::microseconds>(timer.elapsed_time()).count();
        printf("\r\nIt took %llu us to transmit 1000 bytes.\r\n", elapsedTime);
        ThisThread::sleep_for(2s);
    }
}

When running on NUCLEO-F411RE:

USBSerial printf test results:
It took 18650 us to transmit 1000 bytes. (Nothing was connected to the USB port)
It took 53654 us to transmit 1000 bytes. (Serial terminal was connected to the USB port)

USBSerial send test results:
It took 756 us to transmit 1000 bytes. (Nothing was connected to the USB port)
It took 5177 us to transmit 1000 bytes. (Serial terminal was connected to the USB port)

Hey Zoltan,
Thanks so much for the clear explanation - makes a lot more sense now! Interesting stuff - I’ll read up on how the USBSerial byte rate gets determined between the micro and the USB host.
My serial terminal results were similar on the Teensy 3.2. I’d tried the send_nb function earlier with no luck, which has a similar byte rate to printf. send should work great here though, thanks again!

Hi @hudakz ,

I would like to thank you for your contributions to the Mbed OS forums. You are helping lot of people and making this forum a better place. Thank you

Martin

1 Like

Thank you Team Mbed for the fantastic ecosystem of the open source Mbed & Mbed OS and all the associated free tools like the Online Compiler, Mbed Studio IDE, CLI1, CLI2, Forum, Online documentation, Personal accounts with Wicki pages and all the other great things (sorry for not mentioning them all). You have done an excellent job! Without that it would not be possible for me to share anything. You are on the good track. Just keep on!

Best regards,

Zoltan