Skip to content

Commit 7f5fee0

Browse files
committed
fix(nrf52840): add missing flow control functions
1 parent 96b63a1 commit 7f5fee0

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/machine/machine_nrf52840_usb.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,9 @@ func initEndpoint(ep, config uint32) {
258258
func (dev *USBDevice) SendUSBInPacket(ep uint32, data []byte) bool {
259259
sendUSBPacket(ep, data, 0)
260260

261-
// clear transfer complete flag
262-
nrf.USBD.INTENCLR.Set(nrf.USBD_INTENCLR_ENDEPOUT0 << 4)
263-
264261
return true
265262
}
266263

267-
func SendUSBInPacket(ep uint32, data []byte) bool {
268-
return USBDev.SendUSBInPacket(ep, data)
269-
}
270-
271264
// Prevent file size increases: https://github.com/tinygo-org/tinygo/pull/998
272265
//
273266
//go:noinline
@@ -313,16 +306,27 @@ func (dev *USBDevice) AckUsbOutTransfer(ep uint32) {
313306
nrf.USBD.SIZE.EPOUT[ep].Set(0)
314307
}
315308

316-
func AckUsbOutTransfer(ep uint32) {
317-
USBDev.AckUsbOutTransfer(ep)
318-
}
319-
320309
func (dev *USBDevice) SendZlp() {
321310
nrf.USBD.TASKS_EP0STATUS.Set(1)
322311
}
323312

324-
func SendZlp() {
325-
USBDev.SendZlp()
313+
// Set the USB endpoint Packet ID to DATA0 or DATA1.
314+
// In endpoints must have bit 7 (0x80) set.
315+
func setEPDataPID(ep uint32, dataOne bool) {
316+
// nrf52840 DTOGGLE requires a "Select" write first (Value=Nop=0),
317+
// then a "Set" write (Value=Data0/Data1).
318+
319+
// Select Endpoint (Value=Nop=0)
320+
nrf.USBD.DTOGGLE.Set(ep)
321+
322+
// Now write the value
323+
val := ep
324+
if dataOne {
325+
val |= nrf.USBD_DTOGGLE_VALUE_Data1 << nrf.USBD_DTOGGLE_VALUE_Pos
326+
} else {
327+
val |= nrf.USBD_DTOGGLE_VALUE_Data0 << nrf.USBD_DTOGGLE_VALUE_Pos
328+
}
329+
nrf.USBD.DTOGGLE.Set(val)
326330
}
327331

328332
// Set ENDPOINT_HALT/stall status on a USB IN endpoint.
@@ -339,12 +343,23 @@ func (dev *USBDevice) SetStallEPOut(ep uint32) {
339343

340344
// Clear the ENDPOINT_HALT/stall on a USB IN endpoint.
341345
func (dev *USBDevice) ClearStallEPIn(ep uint32) {
346+
// Disable Endpoint
347+
nrf.USBD.EPINEN.ClearBits(1 << ep)
348+
342349
// Bit 8 is STALL (0 for UnStall), Bit 7 is IO (1 for IN), Bits 0-2 are EP number.
343350
nrf.USBD.EPSTALL.Set((0 << 8) | (1 << 7) | (ep & 0x7))
351+
352+
// Reset Data Toggle to DATA0 *after* unstalling.
353+
setEPDataPID(ep|usb.EndpointIn, false)
354+
355+
// Re-enable Endpoint
356+
nrf.USBD.EPINEN.SetBits(1 << ep)
344357
}
345358

346359
// Clear the ENDPOINT_HALT/stall on a USB OUT endpoint.
347360
func (dev *USBDevice) ClearStallEPOut(ep uint32) {
361+
// Reset Data Toggle to DATA0.
362+
setEPDataPID(ep, false)
348363
// Bit 8 is STALL (0 for UnStall), Bit 7 is IO (0 for OUT), Bits 0-2 are EP number.
349364
nrf.USBD.EPSTALL.Set((0 << 8) | (0 << 7) | (ep & 0x7))
350365
}

0 commit comments

Comments
 (0)