Skip to content

Commit 4f7c64c

Browse files
rdon-keyrdon
and
rdon
authored
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>
1 parent ff2a79d commit 4f7c64c

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/machine/machine_rp2040_usb.go

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

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

174-
// last, set the device address to that requested by host
175-
// wait for transfer to complete
176-
timeout := 3000
177175
rp.USBCTRL_REGS.SIE_STATUS.Set(rp.USBCTRL_REGS_SIE_STATUS_ACK_REC)
176+
sendUSBPacket(0, []byte{}, 0)
177+
178+
// Wait for transfer to complete with a timeout.
179+
t := timer.timeElapsed()
178180
for (rp.USBCTRL_REGS.SIE_STATUS.Get() & rp.USBCTRL_REGS_SIE_STATUS_ACK_REC) == 0 {
179-
timeout--
180-
if timeout == 0 {
181-
return true
181+
if dt := timer.timeElapsed() - t; dt >= ackTimeout {
182+
return false
182183
}
183184
}
184185

186+
// Set the device address to that requested by host.
185187
rp.USBCTRL_REGS.ADDR_ENDP.Set(uint32(setup.WValueL) & rp.USBCTRL_REGS_ADDR_ENDP_ADDRESS_Msk)
186-
187188
return true
188189
}
189190

src/machine/machine_rp2350_usb.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,22 @@ func initEndpoint(ep, config uint32) {
172172
}
173173

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

177-
// last, set the device address to that requested by host
178-
// wait for transfer to complete
179-
timeout := 3000
177+
const ackTimeout = 570
180178
rp.USB.SIE_STATUS.Set(rp.USB_SIE_STATUS_ACK_REC)
179+
sendUSBPacket(0, []byte{}, 0)
180+
181+
// Wait for transfer to complete with a timeout.
182+
t := timer.timeElapsed()
181183
for (rp.USB.SIE_STATUS.Get() & rp.USB_SIE_STATUS_ACK_REC) == 0 {
182-
timeout--
183-
if timeout == 0 {
184-
return true
184+
if dt := timer.timeElapsed() - t; dt >= ackTimeout {
185+
return false
185186
}
186187
}
187188

189+
// Set the device address to that requested by host.
188190
rp.USB.ADDR_ENDP.Set(uint32(setup.WValueL) & rp.USB_ADDR_ENDP_ADDRESS_Msk)
189-
190191
return true
191192
}
192193

0 commit comments

Comments
 (0)