machine: add USBDevice.Attach and USBDevice.Detach - #5563
Conversation
The USB device is attached to the bus automatically during startup, before user code has a chance to finish its USB configuration (device identifiers, extra HID interfaces, ...). Composite devices such as keyboards may therefore be enumerated by the host with an incomplete configuration. Attach and Detach expose the soft-connect control (DP pull-up) so that an application or library can detach in an init function, complete its configuration, and attach again to let the host enumerate the finished device. They can also be used to force re-enumeration without replugging the cable. Implemented for atsamd21, atsamd51, nrf52840, rp2040 and rp2350.
|
Size difference with the dev branch: Binary size differenceflash ram before after diff before after diff 19676 19676 0 0.00% 7052 7052 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/adafruit4650 63220 63220 0 0.00% 6780 6780 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go 10016 10016 0 0.00% 5340 5340 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go 14620 14620 0 0.00% 7388 7388 0 0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/amg88xx 10176 10176 0 0.00% 5340 5340 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go 12584 12584 0 0.00% 7172 7172 0 0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/apds9960/proximity/main.go 11128 11128 0 0.00% 5360 5360 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go 7580 7580 0 0.00% 2304 2304 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/at24cx/main.go 9264 9264 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bh1750/main.go 8652 8652 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/blinkm/main.go 71392 71392 0 0.00% 3648 3648 0 0.00% tinygo build -size short -o ./build/test.hex -target=pinetime ./examples/bma42x/main.go 67112 67112 0 0.00% 6796 6796 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go 28944 28944 0 0.00% 5372 5372 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp180/main.go 65712 65712 0 0.00% 6820 6820 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go 13072 13072 0 0.00% 5404 5404 0 0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bmp388/main.go 24012 24012 0 0.00% 6220 6220 0 0.00% tinygo build -size short -o ./build/test.hex -target=metro-rp2350 ./examples/bno08x/i2c/main.go 8000 8000 0 0.00% 3344 3344 0 0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/sram/main.go 22108 22108 0 0.00% 3548 3548 0 0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/time/main.go 30548 30548 0 0.00% 5576 5576 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/alarms/main.go 44484 44484 0 0.00% 5576 5576 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/basic/main.go 4584 4584 0 0.00% 2264 2264 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/easystepper/main.go 71876 71876 0 0.00% 7580 7580 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/flash/console/spi 67988 67988 0 0.00% 9620 9620 0 0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/flash/console/qspi 7120 7120 0 0.00% 2268 2268 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/gc9a01/main.go 69288 69288 0 0.00% 6964 6964 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/i2c/main.go 69976 69976 0 0.00% 7108 7108 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/uart/main.go 9676 9676 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/hcsr04/main.go 5692 5692 0 0.00% 2264 2264 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/customchar/main.go 5732 5732 0 0.00% 2264 2264 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/text/main.go 11592 11592 0 0.00% 5340 5340 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/hd44780i2c/main.go 15452 15452 0 0.00% 7172 7172 0 0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/hts221/main.go 16176 16176 0 0.00% 2348 2348 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hub75/main.go 11216 11216 0 0.00% 7516 7516 0 0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/basic 12292 12292 0 0.00% 5476 5476 0 0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/basic 30520 30520 0 0.00% 38676 38676 0 0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/pyportal_boing 11252 11252 0 0.00% 7508 7508 0 0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/scroll 12384 12384 0 0.00% 5468 5468 0 0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/scroll 264192 264192 0 0.00% 47348 47348 0 0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/slideshow 11732 11732 0 0.00% 5372 5372 0 0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis3dh/main.go 14860 14860 0 0.00% 7172 7172 0 0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/lps22hb/main.go 26712 26712 0 0.00% 2312 2312 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/lsm303agr/main.go 29352 29352 0 0.00% 7420 7420 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/lsm303dlhc/main.go 13584 13584 0 0.00% 5380 5380 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/lsm6ds3/main.go 11264 11264 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mag3110/main.go 10220 10220 0 0.00% 5372 5372 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017/main.go 10664 10664 0 0.00% 5380 5380 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017-multiple/main.go 10672 10672 0 0.00% 5340 5340 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp3008/main.go 73296 73296 0 0.00% 6836 6836 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp2515/main.go 28312 28312 0 0.00% 4048 4048 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/microbitmatrix/main.go 28328 28328 0 0.00% 6092 6092 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit-v2 ./examples/microbitmatrix/main.go 8716 8716 0 0.00% 5340 5340 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mma8653/main.go 8620 8620 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mpu6050/main.go 78072 78072 0 0.00% 8048 8048 0 0.00% tinygo build -size short -o ./build/test.hex -target=p1am-100 ./examples/p1am/main.go 15096 15096 0 0.00% 6232 6232 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/pca9685/main.go 6420 6420 0 0.00% 3276 3276 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setbuffer/main.go 4804 4804 0 0.00% 2268 2268 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setpixel/main.go 13152 13152 0 0.00% 6208 6208 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/seesaw/soil-sensor 14496 14496 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 16628 16628 0 0.00% 6280 6280 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/sgp30 9092 9092 0 0.00% 7380 7380 0 0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/main.go 57896 57896 0 0.00% 3680 3680 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go 57896 57896 0 0.00% 3688 3688 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go 57896 57896 0 0.00% 3680 3680 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go 13544 13544 0 0.00% 8964 8964 0 0.00% tinygo build -size short -o ./build/test.hex -target=xiao-ble ./examples/ssd1306/ 14416 14416 0 0.00% 6160 6160 0 0.00% tinygo build -size short -o ./build/test.hex -target=xiao-rp2040 ./examples/ssd1306/ 14516 14516 0 0.00% 6160 6160 0 0.00% tinygo build -size short -o ./build/test.hex -target=thumby ./examples/ssd1306/ 6076 6076 0 0.00% 2268 2268 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go 6924 6924 0 0.00% 2268 2268 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go 6652 6652 0 0.00% 2268 2268 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go 18040 18040 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/thermistor/main.go 10844 10844 0 0.00% 5132 5132 0 0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-bluefruit ./examples/tone 11248 11248 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/tm1637/main.go 13700 13700 0 0.00% 6220 6220 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/touch/capacitive 9928 9928 0 0.00% 7380 7380 0 0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/fourwire/main.go 13396 13396 0 0.00% 7576 7576 0 0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/pyportal_touchpaint/main.go 17020 17020 0 0.00% 5340 5340 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl53l1x/main.go 15592 15592 0 0.00% 5340 5340 0 0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl6180x/main.go 25844 25844 0 0.00% 14328 14328 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840-sense ./examples/waveshare-epd/epd1in54/main.go 6536 6536 0 0.00% 2308 2308 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13/main.go 6124 6124 0 0.00% 2300 2300 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13x/main.go 6428 6428 0 0.00% 2308 2308 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd4in2/main.go 29040 29040 0 0.00% 19288 19288 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/waveshare-epd/epd2in66b/main.go 8332 8332 0 0.00% 5372 5372 0 0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/ws2812 5970 5970 0 0.00% 8432 8432 0 0.00% tinygo build -size short -o ./build/test.bin -target=m5stamp-c3 ./examples/ws2812 64096 64096 0 0.00% 6548 6548 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/is31fl3731/main.go 1757 1757 0 0.00% 600 600 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino ./examples/ws2812 1264 1264 0 0.00% 182 182 0 0.00% tinygo build -size short -o ./build/test.hex -target=digispark ./examples/ws2812 7312 7312 0 0.00% 8712 8712 0 0.00% tinygo build -size short -o ./build/test.bin -target=xiao-esp32c3 ./examples/ws2812 33164 33164 0 0.00% 5372 5372 0 0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bme280/main.go 17608 17608 0 0.00% 5324 5324 0 0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/microphone/main.go 13012 13012 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/buzzer/main.go 13512 13512 0 0.00% 5372 5372 0 0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/veml6070/main.go 8084 8084 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/simple/main.go 9996 9996 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/speed/main.go 8044 8044 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/simple/main.go 10420 10420 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/speed/main.go 7544 7544 0 0.00% 3316 3316 0 0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-f103rb ./examples/shiftregister/main.go 7136 7136 0 0.00% 2264 2264 0 0.00% tinygo build -size short -o ./build/test.hex -target=hifive1b ./examples/ssd1351/main.go 14884 14884 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis2mdl/main.go 12508 12508 0 0.00% 5356 5356 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/max72xx/main.go 78568 78568 0 0.00% 6932 6932 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/dht/main.go 38920 38920 0 0.00% 6872 6872 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8523/ 72980 72980 0 0.00% 6940 6940 0 0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/alarm/ 8428 8428 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/clkout/ 72484 72484 0 0.00% 6936 6936 0 0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/time/ 72876 72876 0 0.00% 6940 6940 0 0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/timer/ 14792 14792 0 0.00% 6184 6184 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/qmi8658c/main.go 13900 13900 0 0.00% 6168 6168 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8591/ 9952 9952 0 0.00% 5340 5340 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/ina260/main.go 13460 13460 0 0.00% 5372 5372 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/ina219/main.go 9624 9624 0 0.00% 5240 5240 0 0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-l432kc ./examples/aht20/main.go 75052 75052 0 0.00% 11368 11368 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/sdcard/console/ 63244 63244 0 0.00% 8828 8828 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/i2csoft/adt7410/ 11260 11260 0 0.00% 7380 7380 0 0.00% tinygo build -size short -o ./build/test.elf -target=wioterminal ./examples/axp192/m5stack-core2-blinky/ 11820 11820 0 0.00% 6156 6156 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/xpt2046/main.go 13472 13472 0 0.00% 4924 4924 0 0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/sx126x/lora_rxtx/ 44220 44220 0 0.00% 9452 9452 0 0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/sx127x/lora_rxtx/ 35016 35016 0 0.00% 7896 7896 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/ssd1289/main.go 14188 14188 0 0.00% 7148 7148 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/irremote/main.go 14744 14744 0 0.00% 6232 6232 0 0.00% tinygo build -size short -o ./build/test.hex -target=badger2040 ./examples/uc8151/main.go 15196 15196 0 0.00% 6676 6676 0 0.00% tinygo build -size short -o ./build/test.hex -target=badger2040 ./examples/waveshare-epd/epd2in9v2/main.go 13064 13064 0 0.00% 6228 6228 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/scd4x/main.go 9204 9204 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=circuitplay-express ./examples/makeybutton/main.go 10676 10676 0 0.00% 5356 5356 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ds18b20/main.go 123956 123956 0 0.00% 8344 8344 0 0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/lora/lorawan/atcmd/ 19144 19144 0 0.00% 7808 7808 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/as560x/main.go 12936 12936 0 0.00% 6176 6176 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu6886/main.go 9020 9020 0 0.00% 5332 5332 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ttp229/main.go 70276 70276 0 0.00% 7704 7704 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/ndir/main_ndir.go 62504 62504 0 0.00% 3776 3776 0 0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ndir/main_ndir.go 66728 66728 0 0.00% 6852 6852 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ndir/main_ndir.go 12456 12456 0 0.00% 6168 6168 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu9150/main.go 14476 14476 0 0.00% 6204 6204 0 0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/sh1106/macropad_spi 11296 11296 0 0.00% 6632 6632 0 0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/encoders/quadrature-interrupt 69964 69964 0 0.00% 7672 7672 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mcp9808/main.go 45976 45976 0 0.00% 8228 8228 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/tmc2209/main.go 14788 14788 0 0.00% 6164 6164 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/main.go 13276 13276 0 0.00% 5144 5144 0 0.00% tinygo build -size short -o ./build/test.uf2 -target=nicenano ./examples/sharpmem/main.go 63852 63852 0 0.00% 6576 6576 0 0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/max6675/main.go 15420 15420 0 0.00% 6224 6224 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/ens160/main.go 19184 19184 0 0.00% 6240 6240 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/si5351/main.go 24144 24144 0 0.00% 7536 7536 0 0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/w5500/main.go 90124 90124 0 0.00% 8024 8024 0 0.00% tinygo build -size short -o ./build/test.hex -target=challenger-rp2040 ./examples/net/ntpclient/ 375400 375400 0 0.00% 17856 17856 0 0.00% tinygo build -size short -o ./build/test.hex -target=pyportal -stack-size 8kb ./examples/net/http-get/ 122828 122828 0 0.00% 8908 8908 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 -stack-size 8kb ./examples/net/tcpclient/ 355196 355196 0 0.00% 17156 17156 0 0.00% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/websocket/dial/ 105388 105388 0 0.00% 10888 10888 0 0.00% tinygo build -size short -o ./build/test.hex -target=metro-m4-airlift -stack-size 8kb ./examples/net/socket/ 419504 419504 0 0.00% 19884 19884 0 0.00% tinygo build -size short -o ./build/test.hex -target=matrixportal-m4 -stack-size 8kb ./examples/net/webstatic/ 169532 169532 0 0.00% 10908 10908 0 0.00% tinygo build -size short -o ./build/test.hex -target=arduino-mkrwifi1010 -stack-size 8kb ./examples/net/tlsclient/ 159268 159268 0 0.00% 9880 9880 0 0.00% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/mqttclient/natiu/ 119136 119136 0 0.00% 14184 14184 0 0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webclient/ 369972 369972 0 0.00% 22996 22996 0 0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webserver/ 370932 370932 0 0.00% 22912 22912 0 0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/mqttclient/paho/ 177432 177432 0 0.00% 14996 14996 0 0.00% tinygo build -size short -o ./build/test.hex -target=elecrow-rp2040 -stack-size 8kb ./examples/net/tlsclient/ 122628 122628 0 0.00% 13004 13004 0 0.00% tinygo build -size short -o ./build/test.hex -target=elecrow-rp2350 -stack-size 8kb ./examples/net/ntpclient/ 6714348 6714348 0 0.00% 1093186 1093186 0 0.00% |
rdon-key
left a comment
There was a problem hiding this comment.
I left one comment about using the RP2040/RP2350 atomic set/clear aliases for SIE_CTRL.PULLUP_EN.
| // Detach to delay enumeration until the USB configuration (device | ||
| // identifiers, classes, ...) is complete. | ||
| func (dev *USBDevice) Attach() { | ||
| rp.USBCTRL_REGS.SIE_CTRL.SetBits(rp.USBCTRL_REGS_SIE_CTRL_PULLUP_EN) |
There was a problem hiding this comment.
RP2040/RP2350 provide atomic bitmask set/clear aliases at +0x2000 and
+0x3000 (RP2040 datasheet 2.1.2, p.18:
https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf), which
modify only the selected bits. SIE_CTRL is also touched from the ISR,
so this avoids the read-modify-write. Could we use those aliases here
for PULLUP_EN?
There was a problem hiding this comment.
Thanks for the great suggestion. I think we can definitely use it here, but it would probably be better handled in a separate PR—for example, by integrating it into implementations like SetBits() / ClearBits()/ ....
However, this suggested approach might only work for registers and not for standard RAM. If that’s the case, we might need to consider a slightly more complex approach.
Since that change would have a broad impact, it's likely better to target the dev branch right after the upcoming release, rather than squeezing it into TinyGo 0.42.
Would you be open to creating a separate PR for that?
There was a problem hiding this comment.
Understood. I'll look into a more general solution, although it may take some time.
For this PR, could we keep the change local and use the atomic aliases directly?
For example:
// USBCTRL_REGS aliases for atomic bitmask set/clear (datasheet section 2.1.2).
var (
usbSet = (*rp.USBCTRL_REGS_Type)(unsafe.Pointer(
uintptr(unsafe.Pointer(rp.USBCTRL_REGS)) + 0x2000))
usbClr = (*rp.USBCTRL_REGS_Type)(unsafe.Pointer(
uintptr(unsafe.Pointer(rp.USBCTRL_REGS)) + 0x3000))
)
func (dev *USBDevice) Attach() {
usbSet.SIE_CTRL.Set(rp.USBCTRL_REGS_SIE_CTRL_PULLUP_EN)
}
func (dev *USBDevice) Detach() {
usbClr.SIE_CTRL.Set(rp.USBCTRL_REGS_SIE_CTRL_PULLUP_EN)
}And similarly for RP2350.
There was a problem hiding this comment.
I don't think there is a need to change to a new method in this PR.
This code is a new change and has no impact on other parts. Furthermore, by using SetBits() and similar functions, the internal implementation will be updated to a better form. There is no reason to leave open the possibility of an implementation that differs from the future SetBits().
There was a problem hiding this comment.
Thanks. Understood. I still think this should use the atomic write even temporarily, but it's your call.
This PR adds
machine.USBDev.Attach()andmachine.USBDev.Detach(), exposingUSB soft-connect control (the D+ pull-up) to user code.
Motivation
The USB device is attached to the bus automatically during startup, before
user code has had a chance to finish its USB configuration (
usb.VendorID/usb.ProductID/usb.Product, additional interfaces registered throughmachine.ConfigureUSBEndpoint, ...). For composite devices such as keyboardsthis means the host may enumerate a half-configured device: identifiers and
interfaces set from
main()can arrive after the host has already read thedescriptors.
With this API, an application or library can stay detached until its
configuration is complete, so the host enumerates the finished device exactly
once:
It can also be used to force re-enumeration without replugging the cable
(Detach() → Attach()).
Prior art
Soft-connect control is a well-established primitive in embedded USB stacks,
exposed for exactly these use cases (delaying enumeration until configuration
is complete, and forcing re-enumeration from software):
cores).
driver API.
The Attach/Detach naming follows the USB specification's bus-state wording
(a device is "attached" to the bus), which is also where the SAMD register
bit name (CTRLB.DETACH) comes from — and matches the Arduino API, easing
migration for users coming from that ecosystem.
Implementation
One-register operation per chip family, following each reference manual:
The ESP32 variants are not covered: their USB is a Serial/JTAG peripheral
with no equivalent soft-connect concept. Existing behavior is unchanged —
the methods are purely additive.
Testing
(xiao-ble, xiao, wioterminal, pico, pico2).
firmware (tinygo-keyboard)
that detaches in a package init() and attaches after configuration:
the host enumerates the device once, with the complete configuration
(CDC + keyboard/mouse/raw HID interfaces and a custom ProductID).**