Skip to content

Commit 97cdc50

Browse files
author
rdon
committed
fix(rp2040): replace loop counter with hw timer for USB SetAddressRequest timeout
1 parent 8c54e3d commit 97cdc50

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/machine/machine_rp2040_usb.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,19 @@ func initEndpoint(ep, config uint32) {
169169
}
170170

171171
func handleUSBSetAddress(setup usb.Setup) bool {
172+
rp.USBCTRL_REGS.SIE_STATUS.Set(rp.USBCTRL_REGS_SIE_STATUS_ACK_REC)
172173
sendUSBPacket(0, []byte{}, 0)
173-
174174
// last, set the device address to that requested by host
175175
// wait for transfer to complete
176-
timeout := 3000
177-
rp.USBCTRL_REGS.SIE_STATUS.Set(rp.USBCTRL_REGS_SIE_STATUS_ACK_REC)
176+
start := rp.TIMER.TIMERAWL.Get()
178177
for (rp.USBCTRL_REGS.SIE_STATUS.Get() & rp.USBCTRL_REGS_SIE_STATUS_ACK_REC) == 0 {
179-
timeout--
180-
if timeout == 0 {
181-
return true
178+
current := rp.TIMER.TIMERAWL.Get()
179+
elapsed := current - start //Avoiding overflow in unsigned integer subtraction.
180+
if elapsed >= 570 {//exactly the same time as SAMD21.
181+
return false
182182
}
183183
}
184-
185184
rp.USBCTRL_REGS.ADDR_ENDP.Set(uint32(setup.WValueL) & rp.USBCTRL_REGS_ADDR_ENDP_ADDRESS_Msk)
186-
187185
return true
188186
}
189187

0 commit comments

Comments
 (0)