fix(rp2040): replace loop counter with hw timer for USB SetAddressReq… (#4796)

* fix(rp2040/rp2350): replace loop counter with hw timer for USB SetAddressRequest timeout.

* fix code format.

---------

Co-authored-by: rdon <you@example.com>
This commit is contained in:
あーるどん
2025-03-14 00:39:43 +09:00
committed by GitHub
parent ff2a79de7c
commit 4f7c64cb24
2 changed files with 18 additions and 16 deletions
+9 -8
View File
@@ -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
}
+9 -8
View File
@@ -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
}