USB Output: Force 12mbit and change bPacketSize?

mcrc

Well-known member
Hey

I'm using the Teensy 4.1 and I was wondering, how I would forcefully set the data rate of the emulated USB to 12mbit (instead of 480mbit/sec)
Also I was wondering on how to change bMaxPacketSize0 without invalidating the emulated USB device (e.g. from 64 to 8).

Best regards
 
Edit usb.c in {Arduino}/hardware/teensy/avr/cores/teensy4. On Windows, the default Arduino location is C:\Program Files (x86)\Arduino. On MacOS, control-click Teensyduino to Show Package Contents and then look for "hardware" inside Contents/Java.

Find and uncomment this line.

Code:
        //USB1_PORTSC1 |= USB_PORTSC1_PFSC; // force 12 Mbit/sec

For bMaxPacketSize0, either edit usb_desc.c (search for "bMaxPacketSize0") or edit usb_desc.h and edit the EP0_SIZE define for the USB Type you're using. If editing usb_desc.c, keep in mind there are 2 copies of the huge config descriptor, one for 480 Mbit speed and other for 12 Mbit speed.

Why you would want to use a smaller packet size when Teensy 4.1 has plenty of memory is beyond me. But that's how. It should work, but please be aware all this code has never really been tested with smaller control endpoint packet sizes.
 
Sorry for reviving a dead thread. But, when I try to do this, my teensy 4.1 is no longer recognized by windows (even after restart).
The problem is that the teensy is able to run at 12Mbit/s with EP0_size set to 64 bytes but, when I try to set EP0_size to 8 bytes, it no longer gets recognized :/
 
Last edited:
There is no easy way to change the endpoint 0 max packet size. But here's some explanation and ideas about not easy ways...

Editing EP0_SIZE in usb_desc.h only changes the number inside the USB descriptor data. But it has no effect on the actual USB code. You can easily confirm this by searching for EP0_SIZE. It only occurs in usb_desc.h and usb_desc.c, but nowhere in usb.c which actually implements the endpoint 0 communication. So of course it doesn't work when you change EP0_SIZE from 64 to 8, because you're causing Teensy to lie to the USB host (Windows in this case) that it will use only 8 bytes, but the actual code in usb.c uses 64 no matter how to edit EP0_SIZE.

To make Teensy 4 actually use a smaller max packet size, you'll need to dive into the usb.c code. At the very least, find and edit these lines:

Code:
        endpoint_queue_head[0].config = (64 << 16) | (1 << 15);
        endpoint_queue_head[1].config = (64 << 16);

Whether this is enough, or other places in usb.c have subtle dependency on 64 bytes, I can not definitely answer (and with so much software backlog, I really can't do more to help than this message). I wrote this code nearly 4 years ago with only 64 bytes and I never tried anything else. Other sizes just hasn't ever been really tested. Hopefully this message helps, but really you're on your own from here to figure out how to make it actually work.
 
Just quickly wanted to let you know that changing
Code:
        endpoint_queue_head[0].config = (64 << 16) | (1 << 15);
        endpoint_queue_head[1].config = (64 << 16);

to
Code:
        endpoint_queue_head[0].config = (8 << 16) | (1 << 15);
        endpoint_queue_head[1].config = (8 << 16);
seems to work perfectly fine. thank you for the very quick respond!:D
 
Thanks! I had the same question on ep size as well, not EP0, but EP3. I'll try this solution.
 
Back
Top