@@ -258,9 +258,6 @@ func initEndpoint(ep, config uint32) {
258258func (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.
317333func (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.
329345func (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.
335354func (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