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
39 changes: 35 additions & 4 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 All @@ -348,6 +348,10 @@ func SendUSBInPacket(ep uint32, data []byte) bool {
return true
}

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

// Prevent file size increases: https://github.com/tinygo-org/tinygo/pull/998
//
//go:noinline
Expand All @@ -374,7 +378,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 +421,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,13 +430,20 @@ func AckUsbOutTransfer(ep uint32) {

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

func AckUsbOutTransfer(ep uint32) {
USBDev.AckUsbOutTransfer(ep)
}

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

func SendZlp() {
USBDev.SendZlp()
}

func epPacketSize(size uint16) uint32 {
switch size {
case 8:
Expand Down Expand Up @@ -662,3 +673,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)
}
40 changes: 36 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 All @@ -351,6 +351,10 @@ func SendUSBInPacket(ep uint32, data []byte) bool {
return true
}

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

// Prevent file size increases: https://github.com/tinygo-org/tinygo/pull/998
//
//go:noinline
Expand All @@ -377,7 +381,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 +424,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,10 +435,18 @@ func AckUsbOutTransfer(ep uint32) {
setEPSTATUSCLR(ep, sam.USB_DEVICE_ENDPOINT_EPSTATUSCLR_BK0RDY)
}

func SendZlp() {
func AckUsbOutTransfer(ep uint32) {
USBDev.AckUsbOutTransfer(ep)
}

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

func SendZlp() {
USBDev.SendZlp()
}

func epPacketSize(size uint16) uint32 {
switch size {
case 8:
Expand Down Expand Up @@ -493,3 +505,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)
}
44 changes: 40 additions & 4 deletions src/machine/machine_nrf52840_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ 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
Expand All @@ -264,6 +264,10 @@ func SendUSBInPacket(ep uint32, data []byte) bool {
return true
}

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

// Prevent file size increases: https://github.com/tinygo-org/tinygo/pull/998
//
//go:noinline
Expand Down Expand Up @@ -304,15 +308,47 @@ 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 AckUsbOutTransfer(ep uint32) {
USBDev.AckUsbOutTransfer(ep)
}

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

func SendZlp() {
USBDev.SendZlp()
}

// 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) {
// 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) {
// 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 +372,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
20 changes: 16 additions & 4 deletions src/machine/machine_rp2_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ 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
}

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

// Prevent file size increases: https://github.com/tinygo-org/tinygo/pull/998
//
//go:noinline
Expand All @@ -100,7 +104,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,11 +133,15 @@ 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])
}

func AckUsbOutTransfer(ep uint32) {
USBDev.AckUsbOutTransfer(ep)
}

// Set the USB endpoint Packet ID to DATA0 or DATA1.
func setEPDataPID(ep uint32, dataOne bool) {
epXdata0[ep] = dataOne
Expand All @@ -144,10 +152,14 @@ func setEPDataPID(ep uint32, dataOne bool) {
_usbDPSRAM.EPxBufferControl[ep].Out.SetBits(usbBuf0CtrlAvail)
}

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

func SendZlp() {
USBDev.SendZlp()
}

func sendViaEPIn(ep uint32, data []byte, count int) {
// Prepare buffer control register value
val := uint32(count) | usbBuf0CtrlAvail
Expand Down
28 changes: 26 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
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