From 4f7c64cb24853027e28fe0777c3bd0974297389a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=83=BC=E3=82=8B=E3=81=A9=E3=82=93?= <82225965+rdon-key@users.noreply.github.com> Date: Fri, 14 Mar 2025 00:39:43 +0900 Subject: [PATCH] =?UTF-8?q?fix(rp2040):=20replace=20loop=20counter=20with?= =?UTF-8?q?=20hw=20timer=20for=20USB=20SetAddressReq=E2=80=A6=20(#4796)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(rp2040/rp2350): replace loop counter with hw timer for USB SetAddressRequest timeout. * fix code format. --------- Co-authored-by: rdon --- src/machine/machine_rp2040_usb.go | 17 +++++++++-------- src/machine/machine_rp2350_usb.go | 17 +++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) 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 }