Skip to content

machine/mimxrt1062: add USB device (CDC) support for Teensy 4.x - #5691

Open
sago35 wants to merge 3 commits into
devfrom
teensy-usb
Open

sago35 wants to merge 3 commits into
devfrom
teensy-usb

Conversation

@sago35

@sago35 sago35 commented Sep 17, 2026

Copy link
Copy Markdown
Member

Blocked by #5690

This connects the USB1 device controller to the machine/usb stack.
CDC, HID keyboard, HID mouse, HID joystick, and MIDI work with the existing class drivers.

  • The DMA descriptors and endpoint buffers are in a 4 KiB non cacheable region at the top of OCRAM, because the USB DMA cannot access DTCM.
  • The hardware supports high speed (480 Mbps), but the driver forces full speed (12 Mbps) because the machine/usb stack uses 64 byte endpoints. USB is slower than Teensyduino for now.
  • VID and PID are 16C0:0483, the same values Teensyduino uses for USB serial. The serial number comes from the unique chip ID.

Changes for teensy40 and teensy41:

  • The default serial output is now USB CDC. Use -serial uart for the old behavior.
  • tinygo monitor finds the port, and tinygo flash starts the bootloader with the 1200 bps touch. No button press is needed. When no port is found, flash falls back to the normal button wait.

Tested on a Teensy 4.0. teensy41 compiles but is not tested on hardware yet.

@sago35
sago35 marked this pull request as draft September 17, 2026 11:43
@github-actions

github-actions Bot commented Sep 17, 2026

Copy link
Copy Markdown

Size difference with the dev branch:

Binary size difference
 flash                          ram
 before   after   diff          before   after   diff
  19652   19652      0   0.00%    7052    7052      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/adafruit4650
  63180   63180      0   0.00%    6852    6852      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go
  11212   11212      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go
  15388   15388      0   0.00%    7460    7460      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/amg88xx
  10428   10428      0   0.00%    5364    5364      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go
  12988   12988      0   0.00%    7196    7196      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/apds9960/proximity/main.go
  12504   12504      0   0.00%    5376    5376      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go
   8608    8608      0   0.00%    2312    2312      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/at24cx/main.go
   9916    9916      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bh1750/main.go
   9212    9212      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/blinkm/main.go
  71736   71736      0   0.00%    3720    3720      0   0.00% tinygo build -size short -o ./build/test.hex -target=pinetime     ./examples/bma42x/main.go
  67788   67788      0   0.00%    6820    6820      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go
  29020   29020      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp180/main.go
  66444   66444      0   0.00%    6852    6852      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go
  13468   13468      0   0.00%    5436    5436      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bmp388/main.go
  24308   24308      0   0.00%    6284    6284      0   0.00% tinygo build -size short -o ./build/test.hex -target=metro-rp2350 ./examples/bno08x/i2c/main.go
   9104    9104      0   0.00%    3344    3344      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/sram/main.go
  22680   22680      0   0.00%    3548    3548      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/time/main.go
  30964   30964      0   0.00%    5600    5600      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/alarms/main.go
  44820   44820      0   0.00%    5600    5600      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/basic/main.go
   4752    4752      0   0.00%    2272    2272      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/easystepper/main.go
  72784   72784      0   0.00%    7672    7672      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/flash/console/spi
  68624   68624      0   0.00%    9712    9712      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/flash/console/qspi
   7452    7452      0   0.00%    2276    2276      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/gc9a01/main.go
  23608   23608      0   0.00%    8580    8580      0   0.00% tinygo build -size short -o ./build/test.bin -target=esp32-coreboard-v2 ./examples/gdew0154m09/main.go
  69504   69504      0   0.00%    7020    7020      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/i2c/main.go
  70008   70008      0   0.00%    7124    7124      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/uart/main.go
   9956    9956      0   0.00%    5420    5420      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/hcsr04/main.go
   5824    5824      0   0.00%    2272    2272      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/customchar/main.go
   5904    5904      0   0.00%    2272    2272      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/text/main.go
  11932   11932      0   0.00%    5388    5388      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/hd44780i2c/main.go
  15852   15852      0   0.00%    7196    7196      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/hts221/main.go
  16460   16460      0   0.00%    2420    2420      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hub75/main.go
  11564   11564      0   0.00%    7596    7596      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/basic
  13996   13996      0   0.00%    5556    5556      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/basic
  30900   30900      0   0.00%   38756   38756      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/pyportal_boing
  11540   11540      0   0.00%    7596    7596      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/scroll
  14036   14036      0   0.00%    5556    5556      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/scroll
 265756  265756      0   0.00%   47440   47440      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/slideshow
  12140   12140      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis3dh/main.go
  15260   15260      0   0.00%    7196    7196      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/lps22hb/main.go
  26544   26544      0   0.00%    2320    2320      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/lsm303agr/main.go
  29692   29692      0   0.00%    7452    7452      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/lsm303dlhc/main.go
  13900   13900      0   0.00%    5412    5412      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/lsm6ds3/main.go
  12492   12492      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mag3110/main.go
  11268   11268      0   0.00%    5396    5396      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017/main.go
  11700   11700      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017-multiple/main.go
  11628   11628      0   0.00%    5364    5364      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp3008/main.go
  74180   74180      0   0.00%    6924    6924      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp2515/main.go
  30208   30208      0   0.00%    4144    4144      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/microbitmatrix/main.go
  30176   30176      0   0.00%    6188    6188      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit-v2 ./examples/microbitmatrix/main.go
   9948    9948      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mma8653/main.go
   9900    9900      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mpu6050/main.go
  79372   79372      0   0.00%    8076    8076      0   0.00% tinygo build -size short -o ./build/test.hex -target=p1am-100 ./examples/p1am/main.go
  15416   15416      0   0.00%    6232    6232      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/pca9685/main.go
   6620    6620      0   0.00%    3284    3284      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setbuffer/main.go
   5020    5020      0   0.00%    2276    2276      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setpixel/main.go
  15144   15144      0   0.00%    6216    6216      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/seesaw/soil-sensor
  14776   14776      0   0.00%    6216    6216      0   0.00% tinygo build -size short -o ./build/test.hex -target=qtpy-rp2040 ./examples/seesaw/rotary-encoder
   2873    2873      0   0.00%     560     560      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino ./examples/servo
  16944   16944      0   0.00%    6288    6288      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico     ./examples/sgp30
   9340    9340      0   0.00%    7404    7404      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/main.go
  58504   58504      0   0.00%    3720    3720      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
  58440   58440      0   0.00%    3720    3720      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go
  58504   58504      0   0.00%    3720    3720      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go
  14116   14116      0   0.00%    9076    9076      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao-ble ./examples/ssd1306/
  14776   14776      0   0.00%    6248    6248      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao-rp2040 ./examples/ssd1306/
  14712   14712      0   0.00%    6240    6240      0   0.00% tinygo build -size short -o ./build/test.hex -target=thumby ./examples/ssd1306/
   6284    6284      0   0.00%    2276    2276      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go
   7148    7148      0   0.00%    2276    2276      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go
   6828    6828      0   0.00%    2276    2276      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go
  18572   18572      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/thermistor/main.go
  11804   11804      0   0.00%    5172    5172      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-bluefruit ./examples/tone
  11868   11868      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/tm1637/main.go
  13900   13900      0   0.00%    6220    6220      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/touch/capacitive
  10676   10676      0   0.00%    7396    7396      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/fourwire/main.go
  13712   13712      0   0.00%    7656    7656      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/pyportal_touchpaint/main.go
  17236   17236      0   0.00%    5468    5468      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl53l1x/main.go
  15748   15748      0   0.00%    5468    5468      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl6180x/main.go
  26248   26248      0   0.00%   14360   14360      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840-sense ./examples/waveshare-epd/epd1in54/main.go
   6748    6748      0   0.00%    2316    2316      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13/main.go
   6380    6380      0   0.00%    2308    2308      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13x/main.go
   6620    6620      0   0.00%    2316    2316      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd4in2/main.go
  30776   30776      0   0.00%   19296   19296      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/waveshare-epd/epd2in66b/main.go
   8540    8540      0   0.00%    5396    5396      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/ws2812
   6118    6118      0   0.00%    8448    8448      0   0.00% tinygo build -size short -o ./build/test.bin -target=m5stamp-c3          ./examples/ws2812
  64500   64500      0   0.00%    6572    6572      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/is31fl3731/main.go
   1773    1773      0   0.00%     600     600      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino   ./examples/ws2812
   1280    1280      0   0.00%     182     182      0   0.00% tinygo build -size short -o ./build/test.hex -target=digispark ./examples/ws2812
   7722    7722      0   0.00%    8728    8728      0   0.00% tinygo build -size short -o ./build/test.bin -target=xiao-esp32c3 ./examples/ws2812
  33148   33148      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bme280/main.go
  18132   18132      0   0.00%    5340    5340      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/microphone/main.go
  12876   12876      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/buzzer/main.go
  13836   13836      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/veml6070/main.go
   8300    8300      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/simple/main.go
  10236   10236      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/speed/main.go
   8252    8252      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/simple/main.go
  10652   10652      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/speed/main.go
   8092    8092      0   0.00%    3316    3316      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-f103rb ./examples/shiftregister/main.go
   7508    7508      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=hifive1b ./examples/ssd1351/main.go
  15068   15068      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis2mdl/main.go
  12764   12764      0   0.00%    5380    5380      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/max72xx/main.go
  79624   79624      0   0.00%    6956    6956      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/dht/main.go
  39776   39776      0   0.00%    6768    6768      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8523/
  72792   72792      0   0.00%    7004    7004      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/alarm/
   8892    8892      0   0.00%    5396    5396      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/clkout/
  67496   67496      0   0.00%    7004    7004      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/time/
  72632   72632      0   0.00%    7004    7004      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/timer/
  15352   15352      0   0.00%    6224    6224      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/qmi8658c/main.go
  14440   14440      0   0.00%    6216    6216      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8591/
  10412   10412      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/ina260/main.go
  14460   14460      0   0.00%    5404    5404      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/ina219/main.go
  10352   10352      0   0.00%    5272    5272      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-l432kc ./examples/aht20/main.go
  76084   76084      0   0.00%   11468   11468      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/sdcard/console/
  62668   62668      0   0.00%    8860    8860      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/i2csoft/adt7410/
  11548   11548      0   0.00%    7412    7412      0   0.00% tinygo build -size short -o ./build/test.elf -target=wioterminal ./examples/axp192/m5stack-core2-blinky/
  12052   12052      0   0.00%    6156    6156      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/xpt2046/main.go
  12420   12420      0   0.00%    4688    4688      0   0.00% tinygo build -size short -o ./build/test.elf -target=m5stack-core2 ./examples/ft6336/basic/
  19968   19968      0   0.00%    4956    4956      0   0.00% tinygo build -size short -o ./build/test.elf -target=m5stack-core2 ./examples/ft6336/touchpaint/
  14216   14216      0   0.00%    5044    5044      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/sx126x/lora_rxtx/
  46468   46468      0   0.00%    9532    9532      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/sx127x/lora_rxtx/
  36912   36912      0   0.00%    7936    7936      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/ssd1289/main.go
  14840   14840      0   0.00%    7200    7200      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/irremote/main.go
  15104   15104      0   0.00%    6240    6240      0   0.00% tinygo build -size short -o ./build/test.hex -target=badger2040 ./examples/uc8151/main.go
  15456   15456      0   0.00%    6676    6676      0   0.00% tinygo build -size short -o ./build/test.hex -target=badger2040 ./examples/waveshare-epd/epd2in9v2/main.go
  17132   17132      0   0.00%    6268    6268      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/scd30/main.go
  13496   13496      0   0.00%    6244    6244      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/scd4x/main.go
   9428    9428      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=circuitplay-express ./examples/makeybutton/main.go
  11020   11020      0   0.00%    5380    5380      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ds18b20/main.go
 125484  125484      0   0.00%    8408    8408      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/lora/lorawan/atcmd/
  19732   19732      0   0.00%    7812    7812      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/as560x/main.go
  15112   15112      0   0.00%    6216    6216      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu6886/main.go
   9324    9324      0   0.00%    5356    5356      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ttp229/main.go
  70696   70696      0   0.00%    7696    7696      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/ndir/main_ndir.go
  63152   63152      0   0.00%    3784    3784      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ndir/main_ndir.go
  67484   67484      0   0.00%    6884    6884      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ndir/main_ndir.go
  12920   12920      0   0.00%    6208    6208      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu9150/main.go
  14740   14740      0   0.00%    6268    6268      0   0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/sh1106/macropad_spi
  11632   11632      0   0.00%    6656    6656      0   0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/encoders/quadrature-interrupt
  69112   69112      0   0.00%    7664    7664      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mcp9808/main.go
  46428   46428      0   0.00%    8276    8276      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/tmc2209/main.go
  14900   14900      0   0.00%    6164    6164      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/main.go
  13744   13744      0   0.00%    5160    5160      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=nicenano ./examples/sharpmem/main.go
  63528   63528      0   0.00%    6600    6600      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/max6675/main.go
  15736   15736      0   0.00%    6224    6224      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/ens160/main.go
  19600   19600      0   0.00%    6320    6320      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/si5351/main.go
  24512   24512      0   0.00%    7536    7536      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/w5500/main.go
  91800   91800      0   0.00%    7916    7916      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/sd
  90456   90456      0   0.00%    8024    8024      0   0.00% tinygo build -size short -o ./build/test.hex -target=challenger-rp2040 ./examples/net/ntpclient/
 385108  385108      0   0.00%   19644   19644      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal -stack-size 8kb ./examples/net/http-get/
 125568  125568      0   0.00%    8964    8964      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 -stack-size 8kb ./examples/net/tcpclient/
 365112  365112      0   0.00%   18588   18588      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/websocket/dial/
 107492  107492      0   0.00%   10944   10944      0   0.00% tinygo build -size short -o ./build/test.hex -target=metro-m4-airlift -stack-size 8kb ./examples/net/socket/
 431488  431488      0   0.00%   21308   21308      0   0.00% tinygo build -size short -o ./build/test.hex -target=matrixportal-m4 -stack-size 8kb ./examples/net/webstatic/
 173684  173684      0   0.00%   10852   10852      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-mkrwifi1010 -stack-size 8kb ./examples/net/tlsclient/
 162992  162992      0   0.00%    9968    9968      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/mqttclient/natiu/
 121552  121552      0   0.00%   14256   14256      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webclient/
 381076  381076      0   0.00%   24404   24404      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webserver/
 382320  382320      0   0.00%   24364   24364      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/mqttclient/paho/
 183116  183116      0   0.00%   14948   14948      0   0.00% tinygo build -size short -o ./build/test.hex -target=elecrow-rp2040 -stack-size 8kb ./examples/net/tlsclient/
 126620  126620      0   0.00%   13052   13052      0   0.00% tinygo build -size short -o ./build/test.hex -target=elecrow-rp2350 -stack-size 8kb ./examples/net/ntpclient/
7028070 7028070      0   0.00% 1138530 1138530      0   0.00%

@haruyama480

haruyama480 commented Sep 17, 2026

Copy link
Copy Markdown

I tested this PR on Teensy 4.1. It seems to work. https://x.com/haruyama480/status/2100600916680573108

Connect the USB1 device controller to the machine/usb stack. The
controller runs at full speed because the stack uses 64 byte
endpoints. The DMA descriptors and the endpoint buffers are in a non
cacheable region at the top of OCRAM, because the USB DMA cannot
access DTCM. The serial number comes from the unique chip ID.

The default serial output is now USB CDC. Use -serial uart for the
old behavior. Set serial-port so tinygo monitor can find the port.
Enable the 1200 bps touch so tinygo flash can start the bootloader
without a button press.

Tested on a Teensy 4.0 with CDC, HID keyboard, HID mouse, HID
joystick, and MIDI.
@sago35
sago35 marked this pull request as ready for review September 18, 2026 13:00
@sago35

sago35 commented Sep 18, 2026

Copy link
Copy Markdown
Member Author

I fixed some problems and force pushed the branch. Teensy 4.0 and Teensy 4.1 now work with the existing machine/usb stack.

The fixes are:

  • stdin now reads from machine.Serial. The old code polled UART1, which no -serial usb program configures, so stdin always blocked.
  • A bus reset no longer stops the CDC transmit path. The driver records an IN transfer that the reset cancelled and reports it as complete when the host configures the device again.
  • initEndpoint flushes an endpoint before it writes the dQH and dTD. A repeated SET_CONFIGURATION is now safe.
  • SendUSBInPacket reports false when the data does not fit in one packet or when the previous transfer is still active. The old code cut the data to 64 bytes and always reported true.
  • putchar drops output while machine.Serial is nil, so a print from a fault handler before serial init does not cause a second fault.

I tested on a Teensy 4.0. Serial output, serial input, and recovery after a bus reset (device disable and enable on the host) work.

Now ready for review.

…cript

The address 0x2027F000 was in three places, the linker script, the
MPU setup, and the USB driver. Define _usb_dma_start in the linker
script and use the symbol from the Go code. An ASSERT makes sure the
region keeps the 2 KiB alignment that ENDPTLISTADDR requires.
Keep comments no more than 2 lines, as AGENTS.md specifies. Move the
notes from the long file header next to the code they describe.
@sago35

sago35 commented Sep 18, 2026

Copy link
Copy Markdown
Member Author

I fixed some small nits (comment style and a hard coded address).

@deadprogram

Copy link
Copy Markdown
Member

Thank you @sago35 for this work. Here are some findings, edited from an automated review.

  1. -serial none does not compile any more. In src/runtime/runtime_mimxrt1062.go the new guard in putchar compares machine.Serial with nil. For serial.none the type of machine.Serial is NullSerial, which is a struct value. You cannot compare a struct value with nil.
# runtime
src/runtime/runtime_mimxrt1062.go:130:23: invalid operation: machine.Serial == nil
  (mismatched types machine.NullSerial and untyped nil)

-serial uart and -serial rtt are not affected, because both use a pointer type. The same command builds on dev. The rp2 port has the same USB serial default and no guard in putchar. Possibly the guard can go in a helper with a serial.usb build tag.

  1. usbPrime sets ENDPTPRIME with a read modify write. The hardware clears the bits of this register at any time. If the hardware clears a bit between the read and the write, the write sets the bit again and primes a different endpoint a second time with an old dTD. The same function writes ENDPTFLUSH and ENDPTCOMPLETE directly. Is a direct write of the prime mask better here?

  2. sendUSBPacket primes the EP0 OUT status stage only when n > 0. SendZlp has two different functions in the shared stack. It sends the status stage of a control write, which is correct now. It also sends a zero length data stage of a control read, and then the host sends an OUT status stage. sendDescriptor uses the second path in its default case. An example is a BOS descriptor request from a Windows host. Then the status OUT stage is not primed and the controller sends NAK until the host stops. sendDescriptorString with maxLen < 2 uses the same path.

  3. getchar and buffered in src/runtime/runtime_mimxrt1062.go do not have the guard that putchar has. With serial.usb the value of machine.Serial is a nil interface until InitSerial runs. A correction for finding 1 can include all three functions.

  4. Configure enables the UEI and SLI interrupts, but handleUSBIRQ reads only URI and UI. These two interrupts cause an entry to the handler with no effect. Please handle them, or remove them from the mask.

  5. The comment change for ITCM in src/runtime/runtime_mimxrt1062_mpu.go is not related to USB.

Builds that I did with the branch on top of dev at eec9ff6. The targets teensy40 and teensy41 build, and -serial uart and -serial rtt build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants