machine/samd21,samd51: remove use of binary package to avoid reflection and reduce binary size

Signed-off-by: Ron Evans <ron@hybridgroup.com>
Этот коммит содержится в:
Ron Evans 2019-09-24 17:32:17 +02:00 коммит произвёл Ayke
родитель 582457b81e
коммит c16e07469b
3 изменённых файлов: 118 добавлений и 112 удалений

Просмотреть файл

@ -8,10 +8,8 @@
package machine package machine
import ( import (
"bytes"
"device/arm" "device/arm"
"device/sam" "device/sam"
"encoding/binary"
"errors" "errors"
"unsafe" "unsafe"
) )
@ -1607,24 +1605,27 @@ func handleStandardSetup(setup usbSetup) bool {
func cdcSetup(setup usbSetup) bool { func cdcSetup(setup usbSetup) bool {
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE { if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
if setup.bRequest == usb_CDC_GET_LINE_CODING { if setup.bRequest == usb_CDC_GET_LINE_CODING {
buf := bytes.NewBuffer(make([]byte, 0, 7)) b := make([]byte, 0, 7)
binary.Write(buf, binary.LittleEndian, usbLineInfo.dwDTERate) b[0] = byte(usbLineInfo.dwDTERate)
binary.Write(buf, binary.LittleEndian, usbLineInfo.bCharFormat) b[1] = byte(usbLineInfo.dwDTERate >> 8)
binary.Write(buf, binary.LittleEndian, usbLineInfo.bParityType) b[2] = byte(usbLineInfo.dwDTERate >> 16)
binary.Write(buf, binary.LittleEndian, usbLineInfo.bDataBits) b[3] = byte(usbLineInfo.dwDTERate >> 24)
b[4] = byte(usbLineInfo.bCharFormat)
b[5] = byte(usbLineInfo.bParityType)
b[6] = byte(usbLineInfo.bDataBits)
sendUSBPacket(0, buf.Bytes()) sendUSBPacket(0, b)
return true return true
} }
} }
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE { if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
if setup.bRequest == usb_CDC_SET_LINE_CODING { if setup.bRequest == usb_CDC_SET_LINE_CODING {
buf := bytes.NewBuffer(receiveUSBControlPacket()) b := receiveUSBControlPacket()
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.dwDTERate)) usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bCharFormat)) usbLineInfo.bCharFormat = b[4]
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bParityType)) usbLineInfo.bParityType = b[5]
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bDataBits)) usbLineInfo.bDataBits = b[6]
} }
if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE { if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
@ -1814,7 +1815,7 @@ func sendConfiguration(setup usbSetup) {
sz := uint16(configDescriptorSize + cdcSize) sz := uint16(configDescriptorSize + cdcSize)
config := NewConfigDescriptor(sz, 2) config := NewConfigDescriptor(sz, 2)
buf := make([]byte, 0, sz) buf := make([]byte, 0)
buf = append(buf, config.Bytes()...) buf = append(buf, config.Bytes()...)
buf = append(buf, cdc.Bytes()...) buf = append(buf, cdc.Bytes()...)

Просмотреть файл

@ -8,10 +8,8 @@
package machine package machine
import ( import (
"bytes"
"device/arm" "device/arm"
"device/sam" "device/sam"
"encoding/binary"
"errors" "errors"
"unsafe" "unsafe"
) )
@ -1544,24 +1542,27 @@ func handleStandardSetup(setup usbSetup) bool {
func cdcSetup(setup usbSetup) bool { func cdcSetup(setup usbSetup) bool {
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE { if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
if setup.bRequest == usb_CDC_GET_LINE_CODING { if setup.bRequest == usb_CDC_GET_LINE_CODING {
buf := bytes.NewBuffer(make([]byte, 0, 7)) b := make([]byte, 7)
binary.Write(buf, binary.LittleEndian, usbLineInfo.dwDTERate) b[0] = byte(usbLineInfo.dwDTERate)
binary.Write(buf, binary.LittleEndian, usbLineInfo.bCharFormat) b[1] = byte(usbLineInfo.dwDTERate >> 8)
binary.Write(buf, binary.LittleEndian, usbLineInfo.bParityType) b[2] = byte(usbLineInfo.dwDTERate >> 16)
binary.Write(buf, binary.LittleEndian, usbLineInfo.bDataBits) b[3] = byte(usbLineInfo.dwDTERate >> 24)
b[4] = byte(usbLineInfo.bCharFormat)
b[5] = byte(usbLineInfo.bParityType)
b[6] = byte(usbLineInfo.bDataBits)
sendUSBPacket(0, buf.Bytes()) sendUSBPacket(0, b)
return true return true
} }
} }
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE { if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
if setup.bRequest == usb_CDC_SET_LINE_CODING { if setup.bRequest == usb_CDC_SET_LINE_CODING {
buf := bytes.NewBuffer(receiveUSBControlPacket()) b := receiveUSBControlPacket()
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.dwDTERate)) usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bCharFormat)) usbLineInfo.bCharFormat = b[4]
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bParityType)) usbLineInfo.bParityType = b[5]
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bDataBits)) usbLineInfo.bDataBits = b[6]
} }
if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE { if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
@ -1751,7 +1752,7 @@ func sendConfiguration(setup usbSetup) {
sz := uint16(configDescriptorSize + cdcSize) sz := uint16(configDescriptorSize + cdcSize)
config := NewConfigDescriptor(sz, 2) config := NewConfigDescriptor(sz, 2)
buf := make([]byte, 0, sz) buf := make([]byte, 0)
buf = append(buf, config.Bytes()...) buf = append(buf, config.Bytes()...)
buf = append(buf, cdc.Bytes()...) buf = append(buf, cdc.Bytes()...)

Просмотреть файл

@ -4,7 +4,6 @@ package machine
import ( import (
"bytes" "bytes"
"encoding/binary"
"errors" "errors"
"runtime/volatile" "runtime/volatile"
) )
@ -41,22 +40,26 @@ func NewDeviceDescriptor(class, subClass, proto, packetSize0 uint8, vid, pid, ve
// Bytes returns DeviceDescriptor data // Bytes returns DeviceDescriptor data
func (d DeviceDescriptor) Bytes() []byte { func (d DeviceDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, deviceDescriptorSize)) b := make([]byte, deviceDescriptorSize)
binary.Write(buf, binary.LittleEndian, d.bLength) b[0] = byte(d.bLength)
binary.Write(buf, binary.LittleEndian, d.bDescriptorType) b[1] = byte(d.bDescriptorType)
binary.Write(buf, binary.LittleEndian, d.bcdUSB) b[2] = byte(d.bcdUSB)
binary.Write(buf, binary.LittleEndian, d.bDeviceClass) b[3] = byte(d.bcdUSB >> 8)
binary.Write(buf, binary.LittleEndian, d.bDeviceSubClass) b[4] = byte(d.bDeviceClass)
binary.Write(buf, binary.LittleEndian, d.bDeviceProtocol) b[5] = byte(d.bDeviceSubClass)
binary.Write(buf, binary.LittleEndian, d.bMaxPacketSize0) b[6] = byte(d.bDeviceProtocol)
binary.Write(buf, binary.LittleEndian, d.idVendor) b[7] = byte(d.bMaxPacketSize0)
binary.Write(buf, binary.LittleEndian, d.idProduct) b[8] = byte(d.idVendor)
binary.Write(buf, binary.LittleEndian, d.bcdDevice) b[9] = byte(d.idVendor >> 8)
binary.Write(buf, binary.LittleEndian, d.iManufacturer) b[10] = byte(d.idProduct)
binary.Write(buf, binary.LittleEndian, d.iProduct) b[11] = byte(d.idProduct >> 8)
binary.Write(buf, binary.LittleEndian, d.iSerialNumber) b[12] = byte(d.bcdDevice)
binary.Write(buf, binary.LittleEndian, d.bNumConfigurations) b[13] = byte(d.bcdDevice >> 8)
return buf.Bytes() b[14] = byte(d.iManufacturer)
b[15] = byte(d.iProduct)
b[16] = byte(d.iSerialNumber)
b[17] = byte(d.bNumConfigurations)
return b
} }
const configDescriptorSize = 9 const configDescriptorSize = 9
@ -85,16 +88,17 @@ func NewConfigDescriptor(totalLength uint16, interfaces uint8) ConfigDescriptor
// Bytes returns ConfigDescriptor data. // Bytes returns ConfigDescriptor data.
func (d ConfigDescriptor) Bytes() []byte { func (d ConfigDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, configDescriptorSize)) b := make([]byte, configDescriptorSize)
binary.Write(buf, binary.LittleEndian, d.bLength) b[0] = byte(d.bLength)
binary.Write(buf, binary.LittleEndian, d.bDescriptorType) b[1] = byte(d.bDescriptorType)
binary.Write(buf, binary.LittleEndian, d.wTotalLength) b[2] = byte(d.wTotalLength)
binary.Write(buf, binary.LittleEndian, d.bNumInterfaces) b[3] = byte(d.wTotalLength >> 8)
binary.Write(buf, binary.LittleEndian, d.bConfigurationValue) b[4] = byte(d.bNumInterfaces)
binary.Write(buf, binary.LittleEndian, d.iConfiguration) b[5] = byte(d.bConfigurationValue)
binary.Write(buf, binary.LittleEndian, d.bmAttributes) b[6] = byte(d.iConfiguration)
binary.Write(buf, binary.LittleEndian, d.bMaxPower) b[7] = byte(d.bmAttributes)
return buf.Bytes() b[8] = byte(d.bMaxPower)
return b
} }
const interfaceDescriptorSize = 9 const interfaceDescriptorSize = 9
@ -124,17 +128,17 @@ func NewInterfaceDescriptor(n, numEndpoints, class, subClass, protocol uint8) In
// Bytes returns InterfaceDescriptor data. // Bytes returns InterfaceDescriptor data.
func (d InterfaceDescriptor) Bytes() []byte { func (d InterfaceDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, interfaceDescriptorSize)) b := make([]byte, interfaceDescriptorSize)
binary.Write(buf, binary.LittleEndian, d.bLength) b[0] = byte(d.bLength)
binary.Write(buf, binary.LittleEndian, d.bDescriptorType) b[1] = byte(d.bDescriptorType)
binary.Write(buf, binary.LittleEndian, d.bInterfaceNumber) b[2] = byte(d.bInterfaceNumber)
binary.Write(buf, binary.LittleEndian, d.bAlternateSetting) b[3] = byte(d.bAlternateSetting)
binary.Write(buf, binary.LittleEndian, d.bNumEndpoints) b[4] = byte(d.bNumEndpoints)
binary.Write(buf, binary.LittleEndian, d.bInterfaceClass) b[5] = byte(d.bInterfaceClass)
binary.Write(buf, binary.LittleEndian, d.bInterfaceSubClass) b[6] = byte(d.bInterfaceSubClass)
binary.Write(buf, binary.LittleEndian, d.bInterfaceProtocol) b[7] = byte(d.bInterfaceProtocol)
binary.Write(buf, binary.LittleEndian, d.iInterface) b[8] = byte(d.iInterface)
return buf.Bytes() return b
} }
const endpointDescriptorSize = 7 const endpointDescriptorSize = 7
@ -160,14 +164,15 @@ func NewEndpointDescriptor(addr, attr uint8, packetSize uint16, interval uint8)
// Bytes returns EndpointDescriptor data. // Bytes returns EndpointDescriptor data.
func (d EndpointDescriptor) Bytes() []byte { func (d EndpointDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, endpointDescriptorSize)) b := make([]byte, endpointDescriptorSize)
binary.Write(buf, binary.LittleEndian, d.bLength) b[0] = byte(d.bLength)
binary.Write(buf, binary.LittleEndian, d.bDescriptorType) b[1] = byte(d.bDescriptorType)
binary.Write(buf, binary.LittleEndian, d.bEndpointAddress) b[2] = byte(d.bEndpointAddress)
binary.Write(buf, binary.LittleEndian, d.bmAttributes) b[3] = byte(d.bmAttributes)
binary.Write(buf, binary.LittleEndian, d.wMaxPacketSize) b[4] = byte(d.wMaxPacketSize)
binary.Write(buf, binary.LittleEndian, d.bInterval) b[5] = byte(d.wMaxPacketSize >> 8)
return buf.Bytes() b[6] = byte(d.bInterval)
return b
} }
const iadDescriptorSize = 8 const iadDescriptorSize = 8
@ -197,16 +202,16 @@ func NewIADDescriptor(firstInterface, count, class, subClass, protocol uint8) IA
// Bytes returns IADDescriptor data. // Bytes returns IADDescriptor data.
func (d IADDescriptor) Bytes() []byte { func (d IADDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, iadDescriptorSize)) b := make([]byte, iadDescriptorSize)
binary.Write(buf, binary.LittleEndian, d.bLength) b[0] = byte(d.bLength)
binary.Write(buf, binary.LittleEndian, d.bDescriptorType) b[1] = byte(d.bDescriptorType)
binary.Write(buf, binary.LittleEndian, d.bFirstInterface) b[2] = byte(d.bFirstInterface)
binary.Write(buf, binary.LittleEndian, d.bInterfaceCount) b[3] = byte(d.bInterfaceCount)
binary.Write(buf, binary.LittleEndian, d.bFunctionClass) b[4] = byte(d.bFunctionClass)
binary.Write(buf, binary.LittleEndian, d.bFunctionSubClass) b[5] = byte(d.bFunctionSubClass)
binary.Write(buf, binary.LittleEndian, d.bFunctionProtocol) b[6] = byte(d.bFunctionProtocol)
binary.Write(buf, binary.LittleEndian, d.iFunction) b[7] = byte(d.iFunction)
return buf.Bytes() return b
} }
const cdcCSInterfaceDescriptorSize = 5 const cdcCSInterfaceDescriptorSize = 5
@ -227,13 +232,13 @@ func NewCDCCSInterfaceDescriptor(subtype, d0, d1 uint8) CDCCSInterfaceDescriptor
// Bytes returns CDCCSInterfaceDescriptor data. // Bytes returns CDCCSInterfaceDescriptor data.
func (d CDCCSInterfaceDescriptor) Bytes() []byte { func (d CDCCSInterfaceDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, cdcCSInterfaceDescriptorSize)) b := make([]byte, cdcCSInterfaceDescriptorSize)
binary.Write(buf, binary.LittleEndian, d.len) b[0] = byte(d.len)
binary.Write(buf, binary.LittleEndian, d.dtype) b[1] = byte(d.dtype)
binary.Write(buf, binary.LittleEndian, d.subtype) b[2] = byte(d.subtype)
binary.Write(buf, binary.LittleEndian, d.d0) b[3] = byte(d.d0)
binary.Write(buf, binary.LittleEndian, d.d1) b[4] = byte(d.d1)
return buf.Bytes() return b
} }
const cmFunctionalDescriptorSize = 5 const cmFunctionalDescriptorSize = 5
@ -254,13 +259,13 @@ func NewCMFunctionalDescriptor(subtype, d0, d1 uint8) CMFunctionalDescriptor {
// Bytes returns the CMFunctionalDescriptor data. // Bytes returns the CMFunctionalDescriptor data.
func (d CMFunctionalDescriptor) Bytes() []byte { func (d CMFunctionalDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, cmFunctionalDescriptorSize)) b := make([]byte, cmFunctionalDescriptorSize)
binary.Write(buf, binary.LittleEndian, d.bFunctionLength) b[0] = byte(d.bFunctionLength)
binary.Write(buf, binary.LittleEndian, d.bDescriptorType) b[1] = byte(d.bDescriptorType)
binary.Write(buf, binary.LittleEndian, d.bDescriptorSubtype) b[2] = byte(d.bDescriptorSubtype)
binary.Write(buf, binary.LittleEndian, d.bmCapabilities) b[3] = byte(d.bmCapabilities)
binary.Write(buf, binary.LittleEndian, d.bDataInterface) b[4] = byte(d.bDescriptorSubtype)
return buf.Bytes() return b
} }
const acmFunctionalDescriptorSize = 4 const acmFunctionalDescriptorSize = 4
@ -280,12 +285,12 @@ func NewACMFunctionalDescriptor(subtype, d0 uint8) ACMFunctionalDescriptor {
// Bytes returns the ACMFunctionalDescriptor data. // Bytes returns the ACMFunctionalDescriptor data.
func (d ACMFunctionalDescriptor) Bytes() []byte { func (d ACMFunctionalDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, acmFunctionalDescriptorSize)) b := make([]byte, acmFunctionalDescriptorSize)
binary.Write(buf, binary.LittleEndian, d.len) b[0] = byte(d.len)
binary.Write(buf, binary.LittleEndian, d.dtype) b[1] = byte(d.dtype)
binary.Write(buf, binary.LittleEndian, d.subtype) b[2] = byte(d.subtype)
binary.Write(buf, binary.LittleEndian, d.bmCapabilities) b[3] = byte(d.bmCapabilities)
return buf.Bytes() return b
} }
// CDCDescriptor is the Communication Device Class (CDC) descriptor. // CDCDescriptor is the Communication Device Class (CDC) descriptor.
@ -343,7 +348,7 @@ const cdcSize = iadDescriptorSize +
// Bytes returns CDCDescriptor data. // Bytes returns CDCDescriptor data.
func (d CDCDescriptor) Bytes() []byte { func (d CDCDescriptor) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, cdcSize)) buf := bytes.NewBuffer(make([]byte, 0))
buf.Write(d.iad.Bytes()) buf.Write(d.iad.Bytes())
buf.Write(d.cif.Bytes()) buf.Write(d.cif.Bytes())
buf.Write(d.header.Bytes()) buf.Write(d.header.Bytes())
@ -523,14 +528,13 @@ type usbSetup struct {
} }
func newUSBSetup(data []byte) usbSetup { func newUSBSetup(data []byte) usbSetup {
buf := bytes.NewBuffer(data)
u := usbSetup{} u := usbSetup{}
binary.Read(buf, binary.LittleEndian, &(u.bmRequestType)) u.bmRequestType = uint8(data[0])
binary.Read(buf, binary.LittleEndian, &(u.bRequest)) u.bRequest = uint8(data[1])
binary.Read(buf, binary.LittleEndian, &(u.wValueL)) u.wValueL = uint8(data[2])
binary.Read(buf, binary.LittleEndian, &(u.wValueH)) u.wValueH = uint8(data[3])
binary.Read(buf, binary.LittleEndian, &(u.wIndex)) u.wIndex = uint16(data[4]) | uint16(data[5]<<8)
binary.Read(buf, binary.LittleEndian, &(u.wLength)) u.wLength = uint16(data[6]) | uint16(data[7]<<8)
return u return u
} }