I have numerous applications that store data in files on the SD card. One feature of the program is to print the file to the USB serial interface. "Because of Windows", and in particular "because of usbser.sys", I have to put code on the Teensy that will throttle the output or else Windows cannot keep up. Linux and Mac do not have trouble reading data from Teensy USB serial. To accomplish this I use Serial.availableForWrite() to test and see if space is available in the Teensy USB transmit buffer. This works perfectly for Teensy 4.0 and Teensy 4.1. Unfortunately it does not work on Teensy 3.6. On Teensy 3.6 it returns 64 when it should return 768.
I've been trying to dig through the teensy3 core files for USB to understand how it works. I can't figure it out, as it seems that the registers being used are 8-bit AVR registers, so I'm not sure I'm looking at the right code. For example, cores/usb_serial/usb_api.cpp refers to UENUM and UEBCLX which don't seem to be present on ARM.
Any idea how to proceed? I would offer a pull request to fix this but I have no idea how to begin fixing it.
Below are two different programs to explore this problem. One reports size of the transmit buffer. The other reports the buffer sizes compiled into the code and reports the connection speed.
Thanks,
Jim
I've been trying to dig through the teensy3 core files for USB to understand how it works. I can't figure it out, as it seems that the registers being used are 8-bit AVR registers, so I'm not sure I'm looking at the right code. For example, cores/usb_serial/usb_api.cpp refers to UENUM and UEBCLX which don't seem to be present on ARM.
Any idea how to proceed? I would offer a pull request to fix this but I have no idea how to begin fixing it.
Below are two different programs to explore this problem. One reports size of the transmit buffer. The other reports the buffer sizes compiled into the code and reports the connection speed.
Thanks,
Jim
C++:
/*
Teensy 3.6 - Full Speed - 64 - incorrect, should be 768
Teensy 4.1 - Full Speed - 6144 - incorrect, should be 768
Teensy MicroMod - Full Speed - 6144 - incorrect, should be 768
Teensy 4.1 - High Speed - 6144 - correct
Teensy MicroMod - High Speed - 6144 - correct
*/
// wait at most 4 seconds for USB to initialize, in microseconds
#define USBTimeout 4000000UL
void setup(void) {
unsigned long a;
pinMode(LED_BUILTIN, OUTPUT);
// initialize USB serial port
Serial.begin(115200);
a = micros();
while(!Serial) {
if ((micros() - a) > USBTimeout) {
break;
}
}
Serial.println();
Serial.println(";====================================");
}
void loop(void) {
int avail;
Serial.flush();
delay(100UL);
avail = Serial.availableForWrite();
Serial.printf("USB transmit buffer has %d bytes available\r\n", avail);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(5000UL);
}
C++:
// This will be either cores/teensy3/usb_desc.h or cores/teensy4/usb_desc.h
#include <usb_desc.h>
#if defined(__IMXRT1062__) /* Teensy 4.0 or Teensy 4.1 */
// the variable usb_high_speed is from cores/teensy4/usb.c
extern volatile uint8_t usb_high_speed;
#endif
// wait at most 4 seconds for USB to initialize, in microseconds
#define USBTimeout 4000000UL
void setup(void) {
unsigned long a;
pinMode(LED_BUILTIN, OUTPUT);
// initialize USB serial port
Serial.begin(115200);
a = micros();
while(!Serial) {
if ((micros() - a) > USBTimeout) {
break;
}
}
Serial.println();
Serial.println(";====================================");
}
void loop(void) {
int bufferSize = -1;
Serial.println();
#if defined(__MK66FX1M0__) /* Teensy 3.6 */
// USB0 in Teensy 3.6 is only capable of Full Speed
Serial.println("USB connected at Full Speed (12 Mbps)");
bufferSize = CDC_TX_SIZE;
#elif defined(__IMXRT1062__) /* Teensy 4.0 or Teensy 4.1 */
if (usb_high_speed) {
Serial.println("USB connected at High Speed (480 Mbps)");
bufferSize = CDC_TX_SIZE_480;
} else {
Serial.println("USB connected at Full Speed (12 Mbps)");
bufferSize = CDC_TX_SIZE_12;
}
#else
Serial.println("WARNING: unknown Teensy type");
#endif
Serial.printf("This Teensy is using %d USB transmit buffers\r\n",
NUM_USB_BUFFERS);
Serial.printf("Each transmit buffer is %d bytes\r\n", bufferSize);
Serial.printf("Total transmit buffer size is %d bytes\r\n",
NUM_USB_BUFFERS*bufferSize);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(10000UL);
}