Skip to content

Commit 3e62c8e

Browse files
committed
fix(nrf52840): add missing data toggle functions
1 parent 6245593 commit 3e62c8e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/machine/machine_nrf52840_usb.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,6 @@ 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

@@ -313,6 +310,25 @@ func (dev *USBDevice) SendZlp() {
313310
nrf.USBD.TASKS_EP0STATUS.Set(1)
314311
}
315312

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)
330+
}
331+
316332
// Set ENDPOINT_HALT/stall status on a USB IN endpoint.
317333
func (dev *USBDevice) SetStallEPIn(ep uint32) {
318334
// Bit 8 is STALL, Bit 7 is IO (1 for IN), Bits 0-2 are EP number.
@@ -327,12 +343,18 @@ func (dev *USBDevice) SetStallEPOut(ep uint32) {
327343

328344
// Clear the ENDPOINT_HALT/stall on a USB IN endpoint.
329345
func (dev *USBDevice) ClearStallEPIn(ep uint32) {
346+
// Reset Data Toggle to DATA0 when unstalling.
347+
setEPDataPID(ep|usb.EndpointIn, false)
348+
330349
// Bit 8 is STALL (0 for UnStall), Bit 7 is IO (1 for IN), Bits 0-2 are EP number.
331350
nrf.USBD.EPSTALL.Set((0 << 8) | (1 << 7) | (ep & 0x7))
332351
}
333352

334353
// Clear the ENDPOINT_HALT/stall on a USB OUT endpoint.
335354
func (dev *USBDevice) ClearStallEPOut(ep uint32) {
355+
// Reset Data Toggle to DATA0 when unstalling.
356+
setEPDataPID(ep, false)
357+
336358
// Bit 8 is STALL (0 for UnStall), Bit 7 is IO (0 for OUT), Bits 0-2 are EP number.
337359
nrf.USBD.EPSTALL.Set((0 << 8) | (0 << 7) | (ep & 0x7))
338360
}

0 commit comments

Comments
 (0)