Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/machine/machine_rp2040_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,22 @@ func initEndpoint(ep, config uint32) {
}

func handleUSBSetAddress(setup usb.Setup) bool {
sendUSBPacket(0, []byte{}, 0)
// Using 570μs timeout which is exactly the same as SAMD21.
const ackTimeout = 570

// 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)
sendUSBPacket(0, []byte{}, 0)

// 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
}

Expand Down
17 changes: 9 additions & 8 deletions src/machine/machine_rp2350_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,22 @@ func initEndpoint(ep, config uint32) {
}

func handleUSBSetAddress(setup usb.Setup) bool {
sendUSBPacket(0, []byte{}, 0)
// Using 570μs timeout which is exactly the same as SAMD21.

// last, set the device address to that requested by host
// wait for transfer to complete
timeout := 3000
const ackTimeout = 570
rp.USB.SIE_STATUS.Set(rp.USB_SIE_STATUS_ACK_REC)
sendUSBPacket(0, []byte{}, 0)

// 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
}

Expand Down