diff --git a/src/machine/machine_rp2040_usb.go b/src/machine/machine_rp2040_usb.go index ac7df9820..d69cd6ad5 100644 --- a/src/machine/machine_rp2040_usb.go +++ b/src/machine/machine_rp2040_usb.go @@ -169,21 +169,22 @@ func initEndpoint(ep, config uint32) { } func handleUSBSetAddress(setup usb.Setup) bool { + // Using 570μs timeout which is exactly the same as SAMD21. + const ackTimeout = 570 + + rp.USBCTRL_REGS.SIE_STATUS.Set(rp.USBCTRL_REGS_SIE_STATUS_ACK_REC) sendUSBPacket(0, []byte{}, 0) - // last, set the device address to that requested by host - // wait for transfer to complete - timeout := 3000 - rp.USBCTRL_REGS.SIE_STATUS.Set(rp.USBCTRL_REGS_SIE_STATUS_ACK_REC) + // Wait for transfer to complete with a timeout. + t := timer.timeElapsed() for (rp.USBCTRL_REGS.SIE_STATUS.Get() & rp.USBCTRL_REGS_SIE_STATUS_ACK_REC) == 0 { - timeout-- - if timeout == 0 { - return true + if dt := timer.timeElapsed() - t; dt >= ackTimeout { + return false } } + // Set the device address to that requested by host. rp.USBCTRL_REGS.ADDR_ENDP.Set(uint32(setup.WValueL) & rp.USBCTRL_REGS_ADDR_ENDP_ADDRESS_Msk) - return true } diff --git a/src/machine/machine_rp2350_usb.go b/src/machine/machine_rp2350_usb.go index 48fbbcbd0..b42ce09cc 100644 --- a/src/machine/machine_rp2350_usb.go +++ b/src/machine/machine_rp2350_usb.go @@ -172,21 +172,22 @@ func initEndpoint(ep, config uint32) { } func handleUSBSetAddress(setup usb.Setup) bool { + // Using 570μs timeout which is exactly the same as SAMD21. + + const ackTimeout = 570 + rp.USB.SIE_STATUS.Set(rp.USB_SIE_STATUS_ACK_REC) sendUSBPacket(0, []byte{}, 0) - // last, set the device address to that requested by host - // wait for transfer to complete - timeout := 3000 - rp.USB.SIE_STATUS.Set(rp.USB_SIE_STATUS_ACK_REC) + // Wait for transfer to complete with a timeout. + t := timer.timeElapsed() for (rp.USB.SIE_STATUS.Get() & rp.USB_SIE_STATUS_ACK_REC) == 0 { - timeout-- - if timeout == 0 { - return true + if dt := timer.timeElapsed() - t; dt >= ackTimeout { + return false } } + // Set the device address to that requested by host. rp.USB.ADDR_ENDP.Set(uint32(setup.WValueL) & rp.USB_ADDR_ENDP_ADDRESS_Msk) - return true }