Skip to content

Commit 6a39256

Browse files
committed
feat: add usb msc support for nrf52840
1 parent 3869f76 commit 6a39256

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/machine/machine_nrf52840_usb.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,69 @@ func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
379379

380380
return b, nil
381381
}
382+
383+
// Set the USB endpoint Packet ID to DATA0 or DATA1.
384+
// In endpoints must have bit 7 (0x80) set.
385+
func setEPDataPID(ep uint32, dataOne bool) {
386+
val := ep
387+
if dataOne {
388+
val |= USBD_DTOGGLE_VALUE_Data1 << USBD_DTOGGLE_VALUE_Pos
389+
} else {
390+
val |= USBD_DTOGGLE_VALUE_Data0 << USBD_DTOGGLE_VALUE_Pos
391+
}
392+
nrf.USBD.DTOGGLE.Set(val)
393+
}
394+
395+
// Set ENDPOINT_HALT/stall status on a USB IN endpoint.
396+
func (dev *USBDevice) SetStallEPIn(ep uint32) {
397+
if ep&0x7F == 0 {
398+
nrf.USBD.TASKS_EP0STALL.Set(1)
399+
} else if ep&0x7F < NumberOfUSBEndpoints {
400+
// Stall In Endpoint
401+
val := 0x100 | 0x80 | ep
402+
nrf.USBD.EPSTALL.Set(val)
403+
}
404+
}
405+
406+
// Set ENDPOINT_HALT/stall status on a USB OUT endpoint.
407+
func (dev *USBDevice) SetStallEPOut(ep uint32) {
408+
if ep == 0 {
409+
nrf.USBD.TASKS_EP0STALL.Set(1)
410+
} else if ep < NumberOfUSBEndpoints {
411+
// Stall Out Endpoint
412+
val := 0x100 | 0x00 | ep
413+
nrf.USBD.EPSTALL.Set(val)
414+
}
415+
}
416+
417+
// Clear the ENDPOINT_HALT/stall on a USB IN endpoint.
418+
func (dev *USBDevice) ClearStallEPIn(ep uint32) {
419+
if ep&0x7F == 0 {
420+
nrf.USBD.TASKS_EP0STALL.Set(0)
421+
} else if ep&0x7F < NumberOfUSBEndpoints {
422+
// Reset the endpoint data PID to DATA0
423+
ep |= 0x80 // Set endpoint direction bit
424+
setEPDataPID(ep, false)
425+
426+
// No-stall In Endpoint
427+
val := 0x000 | 0x80 | ep
428+
nrf.USBD.EPSTALL.Set(val)
429+
}
430+
}
431+
432+
// Clear the ENDPOINT_HALT/stall on a USB OUT endpoint.
433+
func (dev *USBDevice) ClearStallEPOut(ep uint32) {
434+
if ep == 0 {
435+
nrf.USBD.TASKS_EP0STALL.Set(0)
436+
} else if ep < NumberOfUSBEndpoints {
437+
// Reset the endpoint data PID to DATA0
438+
setEPDataPID(ep, false)
439+
440+
// No-stall Out Endpoint
441+
val := 0x000 | 0x00 | ep
442+
nrf.USBD.EPSTALL.Set(val)
443+
444+
// Write a value to the SIZE register to allow nRF to ACK/accept data
445+
nrf.USBD.SIZE.EPOUT[ep].Set(0)
446+
}
447+
}

0 commit comments

Comments
 (0)