Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/machine/machine_atsamd21_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func handleUSBSetAddress(setup usb.Setup) bool {
}

// SendUSBInPacket sends a packet for USB (interrupt in / bulk in).
func SendUSBInPacket(ep uint32, data []byte) bool {
func (dev *USBDevice) SendUSBInPacket(ep uint32, data []byte) bool {
sendUSBPacket(ep, data, 0)

// clear transfer complete flag
Expand Down Expand Up @@ -374,7 +374,7 @@ func sendUSBPacket(ep uint32, data []byte, maxsize uint16) {
usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits((uint32(l) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
}

func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
func (dev *USBDevice) ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
var b [cdcLineInfoSize]byte

// Wait until OUT transfer is ready.
Expand Down Expand Up @@ -417,7 +417,7 @@ func handleEndpointRx(ep uint32) []byte {
}

// AckUsbOutTransfer is called to acknowledge the completion of a USB OUT transfer.
func AckUsbOutTransfer(ep uint32) {
func (dev *USBDevice) AckUsbOutTransfer(ep uint32) {
// set byte count to zero
usbEndpointDescriptors[ep].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)

Expand All @@ -426,10 +426,9 @@ func AckUsbOutTransfer(ep uint32) {

// set ready for next data
setEPSTATUSCLR(ep, sam.USB_DEVICE_EPSTATUSCLR_BK0RDY)

}

func SendZlp() {
func (dev *USBDevice) SendZlp() {
usbEndpointDescriptors[0].DeviceDescBank[1].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
}

Expand Down Expand Up @@ -662,3 +661,23 @@ func setEPINTENSET(ep uint32, val uint8) {
return
}
}

// Set ENDPOINT_HALT/stall status on a USB IN endpoint.
func (dev *USBDevice) SetStallEPIn(ep uint32) {
setEPSTATUSSET(ep, sam.USB_DEVICE_EPSTATUSSET_STALLRQ1)
}

// Set ENDPOINT_HALT/stall status on a USB OUT endpoint.
func (dev *USBDevice) SetStallEPOut(ep uint32) {
setEPSTATUSSET(ep, sam.USB_DEVICE_EPSTATUSSET_STALLRQ0)
}

// Clear the ENDPOINT_HALT/stall on a USB IN endpoint.
func (dev *USBDevice) ClearStallEPIn(ep uint32) {
setEPSTATUSCLR(ep, sam.USB_DEVICE_EPSTATUSCLR_STALLRQ1)
}

// Clear the ENDPOINT_HALT/stall on a USB OUT endpoint.
func (dev *USBDevice) ClearStallEPOut(ep uint32) {
setEPSTATUSCLR(ep, sam.USB_DEVICE_EPSTATUSCLR_STALLRQ0)
}
28 changes: 24 additions & 4 deletions src/machine/machine_atsamd51_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func handleUSBSetAddress(setup usb.Setup) bool {
}

// SendUSBInPacket sends a packet for USB (interrupt in / bulk in).
func SendUSBInPacket(ep uint32, data []byte) bool {
func (dev *USBDevice) SendUSBInPacket(ep uint32, data []byte) bool {
sendUSBPacket(ep, data, 0)

// clear transfer complete flag
Expand Down Expand Up @@ -377,7 +377,7 @@ func sendUSBPacket(ep uint32, data []byte, maxsize uint16) {
usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits((uint32(l) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
}

func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
func (dev *USBDevice) ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
var b [cdcLineInfoSize]byte

// Wait until OUT transfer is ready.
Expand Down Expand Up @@ -420,7 +420,7 @@ func handleEndpointRx(ep uint32) []byte {
}

// AckUsbOutTransfer is called to acknowledge the completion of a USB OUT transfer.
func AckUsbOutTransfer(ep uint32) {
func (dev *USBDevice) AckUsbOutTransfer(ep uint32) {
// set byte count to zero
usbEndpointDescriptors[ep].DeviceDescBank[0].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)

Expand All @@ -431,7 +431,7 @@ func AckUsbOutTransfer(ep uint32) {
setEPSTATUSCLR(ep, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_BK0RDY)
}

func SendZlp() {
func (dev *USBDevice) SendZlp() {
usbEndpointDescriptors[0].DeviceDescBank[1].PCKSIZE.ClearBits(usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
}

Expand Down Expand Up @@ -493,3 +493,23 @@ func setEPINTENCLR(ep uint32, val uint8) {
func setEPINTENSET(ep uint32, val uint8) {
sam.USB_DEVICE.DEVICE_ENDPOINT[ep].EPINTENSET.Set(val)
}

// Set ENDPOINT_HALT/stall status on a USB IN endpoint.
func (dev *USBDevice) SetStallEPIn(ep uint32) {
setEPSTATUSSET(ep, sam.USB_DEVICE_ENDPOINT_EPSTATUSSET_STALLRQ1)
}

// Set ENDPOINT_HALT/stall status on a USB OUT endpoint.
func (dev *USBDevice) SetStallEPOut(ep uint32) {
setEPSTATUSSET(ep, sam.USB_DEVICE_ENDPOINT_EPSTATUSSET_STALLRQ0)
}

// Clear the ENDPOINT_HALT/stall on a USB IN endpoint.
func (dev *USBDevice) ClearStallEPIn(ep uint32) {
setEPSTATUSCLR(ep, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_STALLRQ1)
}

// Clear the ENDPOINT_HALT/stall on a USB OUT endpoint.
func (dev *USBDevice) ClearStallEPOut(ep uint32) {
setEPSTATUSCLR(ep, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_STALLRQ0)
}
60 changes: 53 additions & 7 deletions src/machine/machine_nrf52840_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,9 @@ func initEndpoint(ep, config uint32) {
}

// SendUSBInPacket sends a packet for USBHID (interrupt in / bulk in).
func SendUSBInPacket(ep uint32, data []byte) bool {
func (dev *USBDevice) SendUSBInPacket(ep uint32, data []byte) bool {
sendUSBPacket(ep, data, 0)

// clear transfer complete flag
nrf.USBD.INTENCLR.Set(nrf.USBD_INTENCLR_ENDEPOUT0 << 4)

return true
}

Expand Down Expand Up @@ -304,15 +301,64 @@ func handleEndpointRx(ep uint32) []byte {
}

// AckUsbOutTransfer is called to acknowledge the completion of a USB OUT transfer.
func AckUsbOutTransfer(ep uint32) {
func (dev *USBDevice) AckUsbOutTransfer(ep uint32) {
// set ready for next data
nrf.USBD.SIZE.EPOUT[ep].Set(0)
}

func SendZlp() {
func (dev *USBDevice) SendZlp() {
nrf.USBD.TASKS_EP0STATUS.Set(1)
}

// Set the USB endpoint Packet ID to DATA0 or DATA1.
// In endpoints must have bit 7 (0x80) set.
func setEPDataPID(ep uint32, dataOne bool) {
// nrf52840 DTOGGLE requires a "Select" write first (Value=Nop=0),
// then a "Set" write (Value=Data0/Data1).

// Select Endpoint (Value=Nop=0)
nrf.USBD.DTOGGLE.Set(ep)

// Now write the value
val := ep
if dataOne {
val |= nrf.USBD_DTOGGLE_VALUE_Data1 << nrf.USBD_DTOGGLE_VALUE_Pos
} else {
val |= nrf.USBD_DTOGGLE_VALUE_Data0 << nrf.USBD_DTOGGLE_VALUE_Pos
}
nrf.USBD.DTOGGLE.Set(val)
}

// Set ENDPOINT_HALT/stall status on a USB IN endpoint.
func (dev *USBDevice) SetStallEPIn(ep uint32) {
// Bit 8 is STALL, Bit 7 is IO (1 for IN), Bits 0-2 are EP number.
nrf.USBD.EPSTALL.Set((1 << 8) | (1 << 7) | (ep & 0x7))
}

// Set ENDPOINT_HALT/stall status on a USB OUT endpoint.
func (dev *USBDevice) SetStallEPOut(ep uint32) {
// Bit 8 is STALL, Bit 7 is IO (0 for OUT), Bits 0-2 are EP number.
nrf.USBD.EPSTALL.Set((1 << 8) | (0 << 7) | (ep & 0x7))
}

// Clear the ENDPOINT_HALT/stall on a USB IN endpoint.
func (dev *USBDevice) ClearStallEPIn(ep uint32) {
// Reset Data Toggle to DATA0 when unstalling.
setEPDataPID(ep|usb.EndpointIn, false)

// Bit 8 is STALL (0 for UnStall), Bit 7 is IO (1 for IN), Bits 0-2 are EP number.
nrf.USBD.EPSTALL.Set((0 << 8) | (1 << 7) | (ep & 0x7))
}

// Clear the ENDPOINT_HALT/stall on a USB OUT endpoint.
func (dev *USBDevice) ClearStallEPOut(ep uint32) {
// Reset Data Toggle to DATA0 when unstalling.
setEPDataPID(ep, false)

// Bit 8 is STALL (0 for UnStall), Bit 7 is IO (0 for OUT), Bits 0-2 are EP number.
nrf.USBD.EPSTALL.Set((0 << 8) | (0 << 7) | (ep & 0x7))
}

func sendViaEPIn(ep uint32, ptr *byte, count int) {
nrf.USBD.EPIN[ep].PTR.Set(
uint32(uintptr(unsafe.Pointer(ptr))),
Expand All @@ -336,7 +382,7 @@ func handleUSBSetAddress(setup usb.Setup) bool {
return true
}

func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
func (dev *USBDevice) ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
var b [cdcLineInfoSize]byte

nrf.USBD.TASKS_EP0RCVOUT.Set(1)
Expand Down
8 changes: 4 additions & 4 deletions src/machine/machine_rp2_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func initEndpoint(ep, config uint32) {
}

// SendUSBInPacket sends a packet for USB (interrupt in / bulk in).
func SendUSBInPacket(ep uint32, data []byte) bool {
func (dev *USBDevice) SendUSBInPacket(ep uint32, data []byte) bool {
sendUSBPacket(ep, data, 0)
return true
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func sendUSBPacket(ep uint32, data []byte, maxsize uint16) {
sendViaEPIn(ep, data, count)
}

func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
func (dev *USBDevice) ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
var b [cdcLineInfoSize]byte
ep := 0

Expand Down Expand Up @@ -129,7 +129,7 @@ func handleEndpointRx(ep uint32) []byte {
}

// AckUsbOutTransfer is called to acknowledge the completion of a USB OUT transfer.
func AckUsbOutTransfer(ep uint32) {
func (dev *USBDevice) AckUsbOutTransfer(ep uint32) {
ep = ep & 0x7F
setEPDataPID(ep, !epXdata0[ep])
}
Expand All @@ -144,7 +144,7 @@ func setEPDataPID(ep uint32, dataOne bool) {
_usbDPSRAM.EPxBufferControl[ep].Out.SetBits(usbBuf0CtrlAvail)
}

func SendZlp() {
func (dev *USBDevice) SendZlp() {
sendUSBPacket(0, []byte{}, 0)
}

Expand Down
41 changes: 39 additions & 2 deletions src/machine/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ var (
USBCDC Serialer
)

func init() {
usb.DefaultController = USBDev
}

func initUSB() {
enableUSBCDC()
USBDev.Configure(UARTConfig{})
USBDev.Enable()
}

// Using go:linkname here because there's a circular dependency between the
Expand All @@ -30,6 +33,10 @@ func initUSB() {
//go:linkname enableUSBCDC machine/usb/cdc.EnableUSBCDC
func enableUSBCDC()

func ReceiveUSBControlPacket() ([7]byte, error) {
return USBDev.ReceiveUSBControlPacket()
}

type Serialer interface {
WriteByte(c byte) error
Write(data []byte) (n int, err error)
Expand Down Expand Up @@ -285,6 +292,19 @@ func handleStandardSetup(setup usb.Setup) bool {
}
}

func (d *USBDevice) Enable() {
if d.initcomplete {
return
}
enableUSBCDC()
d.Configure(UARTConfig{})
d.initcomplete = true
}

func (d *USBDevice) IsInitEndpointComplete() bool {
return d.InitEndpointComplete
}

func EnableCDC(txHandler func(), rxHandler func([]byte), setupHandler func(usb.Setup) bool) {
if len(usbDescriptor.Device) == 0 {
usbDescriptor = descriptor.CDC
Expand Down Expand Up @@ -319,6 +339,10 @@ func EnableCDC(txHandler func(), rxHandler func([]byte), setupHandler func(usb.S
}

func ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []usb.EndpointConfig, setup []usb.SetupConfig) {
USBDev.ConfigureUSBEndpoint(desc, epSettings, setup)
}

func (d *USBDevice) ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []usb.EndpointConfig, setup []usb.SetupConfig) {
usbDescriptor = desc

for _, ep := range epSettings {
Expand Down Expand Up @@ -347,3 +371,16 @@ func ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []usb.EndpointC
usbSetupHandler[s.Index] = s.Handler
}
}

// Old usb functions kept for compatibility
func AckUsbOutTransfer(ep uint32) {
USBDev.AckUsbOutTransfer(ep)
}

func SendUSBInPacket(ep uint32, data []byte) bool {
return USBDev.SendUSBInPacket(ep, data)
}

func SendZlp() {
USBDev.SendZlp()
}
3 changes: 3 additions & 0 deletions src/machine/usb/cdc/cdc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cdc

import "machine/usb"

const (
cdcEndpointACM = 1
cdcEndpointOut = 2
Expand All @@ -12,6 +14,7 @@ func New() *USBCDC {
USB = &USBCDC{
rxBuffer: NewRxRingBuffer(),
txBuffer: NewTxRingBuffer(),
dev: usb.DefaultController,
}
}
return USB
Expand Down
Loading
Loading