Can't pass 12Mbit/s in usb with teensy 4.1 in seending from pc.

soleyj

New member
Hi,

I have a teensy 4.1 board that I need to communicate to a Pc. I have used c# and python to communicate with the teensy, but in both cases the max speed is 12Mbit/s.

I have also tried to send data to pc with this script https://github.com/PaulStoffregen/USB-Serial-Print-Speed-Test/blob/master/usb_serial_print_speed.ino, and I am able to get about 1M/lines.

Python code.
Code:
# Importing Libraries
import serial
import time
import subprocess
import shlex
arduino = serial.Serial(port='/dev/ttyACM1', baudrate=4000000, timeout=.1)



while True:
    values = bytearray([0] * 1500000)
    start_time = time.time()
    arduino.write(values)
    print("--- %s seconds ---" % (time.time() - start_time))

Print

--- 1.09497308731 seconds ---
--- 1.09484314919 seconds ---

So 1.500.000 *8 = 12.000.000 bits/s.

Also, I have test other pcs with the same results, linux and windows.

Thanks
 
What sketch is running on the Teensy?
Did the data properly arrive?
That is did you receive 1500000 bytes?

Personally I never had good luck using pyserial. So not sure if the issue is there or not.
Or could be issues, like you are not reading the data on the teensy fast enough, and the USB stuff piles up...
 
Hi,

My code on the teensy side was this:

Code:
if(Serial.available()){
    
    uint8_t c = (Serial.read());
    counter++;
    if( c ==2)
    {
      Serial.println(counter);
      Serial.println("finish");
      counter = 0;
    }
    //Serial.println(refSer.read());
  }

Changing the if(Serial.available()) for a while the speed of the usb increase x5 to almost 75Mbits per seconds.

Thanks!!

Is possible to do the read in a more effecient way??
 
There are several ways to do this... But reading in 1.5mb of data you probably need to do something with it.. That is it won't all fit into memory.

But I often do something like:

Code:
bytes buffer[512];
int cb_avail;
int cb_read;
if ((cb_avail = Serial.available())) {
    if (cb_avail > sizeof(buffer)) cb_avail = sizeof(buffer);
    cb_read = Serial.readBytes(buffer, cb_avail);
};

Edit: typed on fly so probably issues...
 
You can also just call Serial.read() without first calling Serial.available() and handle the return being 0 or -1. Something along these lines...

Code:
uint8_t buffer[4096];
int n = Serial.read(buffer, sizeof(buffer));
if (n > 0) {
  // do something with n bytes...
}
 
So 1.500.000 *8 = 12.000.000 bits/s.

Also please keep in mind USB has considerable protocol overhead. For 480 Mbit/sec USB, bulk transfer protocol has 55 bytes overhead, not including bit stuffing overhead, which is data dependent. Transmitting all 0x00 or 0xFF is a worst case scenario for bit stuffing overhead. Ideally most packets will have 512 bytes for those 55 bytes overhead. In normal usage where the data is faster than the CPU can process, NYET and NAK responses are common, adding more overhead (but assuring flow control so no data is lost). The protocol also requires a portion of every micro-frame reserved for periodic transfers (SOF, interrupt, control). Allocating bandwidth to guarantee SOF timing is done by hardware in the host controller chip which usually needs to take a worst-case estimate with fairly simple hardware, which in practice often means a quiet period of at least 1 maximum size packet near the end of every micro-frame. So even in the very best case scenario there is quite a lot of overhead for the USB protocol.
 
Back
Top