mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-13 07:23:39 +00:00
machine/atsamd21,atsamd51,nrf52840: refactor USB CDC device descriptor to reduce code duplication and heap allocations
Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
+4
-104
@@ -400,110 +400,6 @@ func sendUSBPacket(ep uint32, data []byte) {
|
||||
)
|
||||
}
|
||||
|
||||
// sendDescriptor creates and sends the various USB descriptor types that
|
||||
// can be requested by the host.
|
||||
func sendDescriptor(setup usbSetup) {
|
||||
switch setup.wValueH {
|
||||
case usb_CONFIGURATION_DESCRIPTOR_TYPE:
|
||||
sendConfiguration(setup)
|
||||
return
|
||||
case usb_DEVICE_DESCRIPTOR_TYPE:
|
||||
// composite descriptor
|
||||
dd := NewDeviceDescriptor(0xef, 0x02, 0x01, 64, usb_VID, usb_PID, 0x100, usb_IMANUFACTURER, usb_IPRODUCT, usb_ISERIAL, 1)
|
||||
l := deviceDescriptorSize
|
||||
if setup.wLength < deviceDescriptorSize {
|
||||
l = int(setup.wLength)
|
||||
}
|
||||
sendUSBPacket(0, dd.Bytes()[:l])
|
||||
return
|
||||
|
||||
case usb_STRING_DESCRIPTOR_TYPE:
|
||||
switch setup.wValueL {
|
||||
case 0:
|
||||
b := make([]byte, 4)
|
||||
b[0] = 0x04
|
||||
b[1] = 0x03
|
||||
b[2] = 0x09
|
||||
b[3] = 0x04
|
||||
sendUSBPacket(0, b)
|
||||
|
||||
case usb_IPRODUCT:
|
||||
b := strToUTF16LEDescriptor(usb_STRING_PRODUCT)
|
||||
if setup.wLength == 2 {
|
||||
sendUSBPacket(0, b[:2])
|
||||
} else {
|
||||
sendUSBPacket(0, b)
|
||||
}
|
||||
|
||||
case usb_IMANUFACTURER:
|
||||
b := strToUTF16LEDescriptor(usb_STRING_MANUFACTURER)
|
||||
if setup.wLength == 2 {
|
||||
sendUSBPacket(0, b[:2])
|
||||
} else {
|
||||
sendUSBPacket(0, b)
|
||||
}
|
||||
|
||||
case usb_ISERIAL:
|
||||
// TODO: allow returning a product serial number
|
||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// do not know how to handle this message, so return zero
|
||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||
return
|
||||
}
|
||||
|
||||
// sendConfiguration creates and sends the configuration packet to the host.
|
||||
func sendConfiguration(setup usbSetup) {
|
||||
if setup.wLength == 9 {
|
||||
sz := uint16(configDescriptorSize + cdcSize)
|
||||
config := NewConfigDescriptor(sz, 2)
|
||||
|
||||
sendUSBPacket(0, config.Bytes())
|
||||
} else {
|
||||
iad := NewIADDescriptor(0, 2, usb_CDC_COMMUNICATION_INTERFACE_CLASS, usb_CDC_ABSTRACT_CONTROL_MODEL, 0)
|
||||
|
||||
cif := NewInterfaceDescriptor(usb_CDC_ACM_INTERFACE, 1, usb_CDC_COMMUNICATION_INTERFACE_CLASS, usb_CDC_ABSTRACT_CONTROL_MODEL, 0)
|
||||
|
||||
header := NewCDCCSInterfaceDescriptor(usb_CDC_HEADER, usb_CDC_V1_10&0xFF, (usb_CDC_V1_10>>8)&0x0FF)
|
||||
|
||||
controlManagement := NewACMFunctionalDescriptor(usb_CDC_ABSTRACT_CONTROL_MANAGEMENT, 6)
|
||||
|
||||
functionalDescriptor := NewCDCCSInterfaceDescriptor(usb_CDC_UNION, usb_CDC_ACM_INTERFACE, usb_CDC_DATA_INTERFACE)
|
||||
|
||||
callManagement := NewCMFunctionalDescriptor(usb_CDC_CALL_MANAGEMENT, 1, 1)
|
||||
|
||||
cifin := NewEndpointDescriptor((usb_CDC_ENDPOINT_ACM | usbEndpointIn), usb_ENDPOINT_TYPE_INTERRUPT, 0x10, 0x10)
|
||||
|
||||
dif := NewInterfaceDescriptor(usb_CDC_DATA_INTERFACE, 2, usb_CDC_DATA_INTERFACE_CLASS, 0, 0)
|
||||
|
||||
out := NewEndpointDescriptor((usb_CDC_ENDPOINT_OUT | usbEndpointOut), usb_ENDPOINT_TYPE_BULK, usbEndpointPacketSize, 0)
|
||||
|
||||
in := NewEndpointDescriptor((usb_CDC_ENDPOINT_IN | usbEndpointIn), usb_ENDPOINT_TYPE_BULK, usbEndpointPacketSize, 0)
|
||||
|
||||
cdc := NewCDCDescriptor(iad,
|
||||
cif,
|
||||
header,
|
||||
controlManagement,
|
||||
functionalDescriptor,
|
||||
callManagement,
|
||||
cifin,
|
||||
dif,
|
||||
out,
|
||||
in)
|
||||
|
||||
sz := uint16(configDescriptorSize + cdcSize)
|
||||
config := NewConfigDescriptor(sz, 2)
|
||||
|
||||
buf := make([]byte, 0)
|
||||
buf = append(buf, config.Bytes()...)
|
||||
buf = append(buf, cdc.Bytes()...)
|
||||
sendUSBPacket(0, buf)
|
||||
}
|
||||
}
|
||||
|
||||
func (usbcdc USBCDC) handleEndpoint(ep uint32) {
|
||||
// get data
|
||||
count := int(nrf.USBD.EPOUT[ep].AMOUNT.Get())
|
||||
@@ -517,6 +413,10 @@ func (usbcdc USBCDC) handleEndpoint(ep uint32) {
|
||||
nrf.USBD.SIZE.EPOUT[ep].Set(0)
|
||||
}
|
||||
|
||||
func sendZlp() {
|
||||
nrf.USBD.TASKS_EP0STATUS.Set(1)
|
||||
}
|
||||
|
||||
func sendViaEPIn(ep uint32, ptr *byte, count int) {
|
||||
nrf.USBD.EPIN[ep].PTR.Set(
|
||||
uint32(uintptr(unsafe.Pointer(ptr))),
|
||||
|
||||
Reference in New Issue
Block a user