Skip to content

Commit c16e074

Browse files
deadprogramaykevl
authored andcommitted
machine/samd21,samd51: remove use of binary package to avoid reflection and reduce binary size
Signed-off-by: Ron Evans <ron@hybridgroup.com>
1 parent 582457b commit c16e074

File tree

3 files changed

+118
-112
lines changed

3 files changed

+118
-112
lines changed

src/machine/machine_atsamd21.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
package machine
99

1010
import (
11-
"bytes"
1211
"device/arm"
1312
"device/sam"
14-
"encoding/binary"
1513
"errors"
1614
"unsafe"
1715
)
@@ -1607,24 +1605,27 @@ func handleStandardSetup(setup usbSetup) bool {
16071605
func cdcSetup(setup usbSetup) bool {
16081606
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
16091607
if setup.bRequest == usb_CDC_GET_LINE_CODING {
1610-
buf := bytes.NewBuffer(make([]byte, 0, 7))
1611-
binary.Write(buf, binary.LittleEndian, usbLineInfo.dwDTERate)
1612-
binary.Write(buf, binary.LittleEndian, usbLineInfo.bCharFormat)
1613-
binary.Write(buf, binary.LittleEndian, usbLineInfo.bParityType)
1614-
binary.Write(buf, binary.LittleEndian, usbLineInfo.bDataBits)
1608+
b := make([]byte, 0, 7)
1609+
b[0] = byte(usbLineInfo.dwDTERate)
1610+
b[1] = byte(usbLineInfo.dwDTERate >> 8)
1611+
b[2] = byte(usbLineInfo.dwDTERate >> 16)
1612+
b[3] = byte(usbLineInfo.dwDTERate >> 24)
1613+
b[4] = byte(usbLineInfo.bCharFormat)
1614+
b[5] = byte(usbLineInfo.bParityType)
1615+
b[6] = byte(usbLineInfo.bDataBits)
16151616

1616-
sendUSBPacket(0, buf.Bytes())
1617+
sendUSBPacket(0, b)
16171618
return true
16181619
}
16191620
}
16201621

16211622
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
16221623
if setup.bRequest == usb_CDC_SET_LINE_CODING {
1623-
buf := bytes.NewBuffer(receiveUSBControlPacket())
1624-
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.dwDTERate))
1625-
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bCharFormat))
1626-
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bParityType))
1627-
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bDataBits))
1624+
b := receiveUSBControlPacket()
1625+
usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1626+
usbLineInfo.bCharFormat = b[4]
1627+
usbLineInfo.bParityType = b[5]
1628+
usbLineInfo.bDataBits = b[6]
16281629
}
16291630

16301631
if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
@@ -1814,7 +1815,7 @@ func sendConfiguration(setup usbSetup) {
18141815
sz := uint16(configDescriptorSize + cdcSize)
18151816
config := NewConfigDescriptor(sz, 2)
18161817

1817-
buf := make([]byte, 0, sz)
1818+
buf := make([]byte, 0)
18181819
buf = append(buf, config.Bytes()...)
18191820
buf = append(buf, cdc.Bytes()...)
18201821

src/machine/machine_atsamd51.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
package machine
99

1010
import (
11-
"bytes"
1211
"device/arm"
1312
"device/sam"
14-
"encoding/binary"
1513
"errors"
1614
"unsafe"
1715
)
@@ -1544,24 +1542,27 @@ func handleStandardSetup(setup usbSetup) bool {
15441542
func cdcSetup(setup usbSetup) bool {
15451543
if setup.bmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
15461544
if setup.bRequest == usb_CDC_GET_LINE_CODING {
1547-
buf := bytes.NewBuffer(make([]byte, 0, 7))
1548-
binary.Write(buf, binary.LittleEndian, usbLineInfo.dwDTERate)
1549-
binary.Write(buf, binary.LittleEndian, usbLineInfo.bCharFormat)
1550-
binary.Write(buf, binary.LittleEndian, usbLineInfo.bParityType)
1551-
binary.Write(buf, binary.LittleEndian, usbLineInfo.bDataBits)
1545+
b := make([]byte, 7)
1546+
b[0] = byte(usbLineInfo.dwDTERate)
1547+
b[1] = byte(usbLineInfo.dwDTERate >> 8)
1548+
b[2] = byte(usbLineInfo.dwDTERate >> 16)
1549+
b[3] = byte(usbLineInfo.dwDTERate >> 24)
1550+
b[4] = byte(usbLineInfo.bCharFormat)
1551+
b[5] = byte(usbLineInfo.bParityType)
1552+
b[6] = byte(usbLineInfo.bDataBits)
15521553

1553-
sendUSBPacket(0, buf.Bytes())
1554+
sendUSBPacket(0, b)
15541555
return true
15551556
}
15561557
}
15571558

15581559
if setup.bmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
15591560
if setup.bRequest == usb_CDC_SET_LINE_CODING {
1560-
buf := bytes.NewBuffer(receiveUSBControlPacket())
1561-
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.dwDTERate))
1562-
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bCharFormat))
1563-
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bParityType))
1564-
binary.Read(buf, binary.LittleEndian, &(usbLineInfo.bDataBits))
1561+
b := receiveUSBControlPacket()
1562+
usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1563+
usbLineInfo.bCharFormat = b[4]
1564+
usbLineInfo.bParityType = b[5]
1565+
usbLineInfo.bDataBits = b[6]
15651566
}
15661567

15671568
if setup.bRequest == usb_CDC_SET_CONTROL_LINE_STATE {
@@ -1751,7 +1752,7 @@ func sendConfiguration(setup usbSetup) {
17511752
sz := uint16(configDescriptorSize + cdcSize)
17521753
config := NewConfigDescriptor(sz, 2)
17531754

1754-
buf := make([]byte, 0, sz)
1755+
buf := make([]byte, 0)
17551756
buf = append(buf, config.Bytes()...)
17561757
buf = append(buf, cdc.Bytes()...)
17571758

src/machine/usb.go

Lines changed: 88 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package machine
44

55
import (
66
"bytes"
7-
"encoding/binary"
87
"errors"
98
"runtime/volatile"
109
)
@@ -41,22 +40,26 @@ func NewDeviceDescriptor(class, subClass, proto, packetSize0 uint8, vid, pid, ve
4140

4241
// Bytes returns DeviceDescriptor data
4342
func (d DeviceDescriptor) Bytes() []byte {
44-
buf := bytes.NewBuffer(make([]byte, 0, deviceDescriptorSize))
45-
binary.Write(buf, binary.LittleEndian, d.bLength)
46-
binary.Write(buf, binary.LittleEndian, d.bDescriptorType)
47-
binary.Write(buf, binary.LittleEndian, d.bcdUSB)
48-
binary.Write(buf, binary.LittleEndian, d.bDeviceClass)
49-
binary.Write(buf, binary.LittleEndian, d.bDeviceSubClass)
50-
binary.Write(buf, binary.LittleEndian, d.bDeviceProtocol)
51-
binary.Write(buf, binary.LittleEndian, d.bMaxPacketSize0)
52-
binary.Write(buf, binary.LittleEndian, d.idVendor)
53-
binary.Write(buf, binary.LittleEndian, d.idProduct)
54-
binary.Write(buf, binary.LittleEndian, d.bcdDevice)
55-
binary.Write(buf, binary.LittleEndian, d.iManufacturer)
56-
binary.Write(buf, binary.LittleEndian, d.iProduct)
57-
binary.Write(buf, binary.LittleEndian, d.iSerialNumber)
58-
binary.Write(buf, binary.LittleEndian, d.bNumConfigurations)
59-
return buf.Bytes()
43+
b := make([]byte, deviceDescriptorSize)
44+
b[0] = byte(d.bLength)
45+
b[1] = byte(d.bDescriptorType)
46+
b[2] = byte(d.bcdUSB)
47+
b[3] = byte(d.bcdUSB >> 8)
48+
b[4] = byte(d.bDeviceClass)
49+
b[5] = byte(d.bDeviceSubClass)
50+
b[6] = byte(d.bDeviceProtocol)
51+
b[7] = byte(d.bMaxPacketSize0)
52+
b[8] = byte(d.idVendor)
53+
b[9] = byte(d.idVendor >> 8)
54+
b[10] = byte(d.idProduct)
55+
b[11] = byte(d.idProduct >> 8)
56+
b[12] = byte(d.bcdDevice)
57+
b[13] = byte(d.bcdDevice >> 8)
58+
b[14] = byte(d.iManufacturer)
59+
b[15] = byte(d.iProduct)
60+
b[16] = byte(d.iSerialNumber)
61+
b[17] = byte(d.bNumConfigurations)
62+
return b
6063
}
6164

6265
const configDescriptorSize = 9
@@ -85,16 +88,17 @@ func NewConfigDescriptor(totalLength uint16, interfaces uint8) ConfigDescriptor
8588

8689
// Bytes returns ConfigDescriptor data.
8790
func (d ConfigDescriptor) Bytes() []byte {
88-
buf := bytes.NewBuffer(make([]byte, 0, configDescriptorSize))
89-
binary.Write(buf, binary.LittleEndian, d.bLength)
90-
binary.Write(buf, binary.LittleEndian, d.bDescriptorType)
91-
binary.Write(buf, binary.LittleEndian, d.wTotalLength)
92-
binary.Write(buf, binary.LittleEndian, d.bNumInterfaces)
93-
binary.Write(buf, binary.LittleEndian, d.bConfigurationValue)
94-
binary.Write(buf, binary.LittleEndian, d.iConfiguration)
95-
binary.Write(buf, binary.LittleEndian, d.bmAttributes)
96-
binary.Write(buf, binary.LittleEndian, d.bMaxPower)
97-
return buf.Bytes()
91+
b := make([]byte, configDescriptorSize)
92+
b[0] = byte(d.bLength)
93+
b[1] = byte(d.bDescriptorType)
94+
b[2] = byte(d.wTotalLength)
95+
b[3] = byte(d.wTotalLength >> 8)
96+
b[4] = byte(d.bNumInterfaces)
97+
b[5] = byte(d.bConfigurationValue)
98+
b[6] = byte(d.iConfiguration)
99+
b[7] = byte(d.bmAttributes)
100+
b[8] = byte(d.bMaxPower)
101+
return b
98102
}
99103

100104
const interfaceDescriptorSize = 9
@@ -124,17 +128,17 @@ func NewInterfaceDescriptor(n, numEndpoints, class, subClass, protocol uint8) In
124128

125129
// Bytes returns InterfaceDescriptor data.
126130
func (d InterfaceDescriptor) Bytes() []byte {
127-
buf := bytes.NewBuffer(make([]byte, 0, interfaceDescriptorSize))
128-
binary.Write(buf, binary.LittleEndian, d.bLength)
129-
binary.Write(buf, binary.LittleEndian, d.bDescriptorType)
130-
binary.Write(buf, binary.LittleEndian, d.bInterfaceNumber)
131-
binary.Write(buf, binary.LittleEndian, d.bAlternateSetting)
132-
binary.Write(buf, binary.LittleEndian, d.bNumEndpoints)
133-
binary.Write(buf, binary.LittleEndian, d.bInterfaceClass)
134-
binary.Write(buf, binary.LittleEndian, d.bInterfaceSubClass)
135-
binary.Write(buf, binary.LittleEndian, d.bInterfaceProtocol)
136-
binary.Write(buf, binary.LittleEndian, d.iInterface)
137-
return buf.Bytes()
131+
b := make([]byte, interfaceDescriptorSize)
132+
b[0] = byte(d.bLength)
133+
b[1] = byte(d.bDescriptorType)
134+
b[2] = byte(d.bInterfaceNumber)
135+
b[3] = byte(d.bAlternateSetting)
136+
b[4] = byte(d.bNumEndpoints)
137+
b[5] = byte(d.bInterfaceClass)
138+
b[6] = byte(d.bInterfaceSubClass)
139+
b[7] = byte(d.bInterfaceProtocol)
140+
b[8] = byte(d.iInterface)
141+
return b
138142
}
139143

140144
const endpointDescriptorSize = 7
@@ -160,14 +164,15 @@ func NewEndpointDescriptor(addr, attr uint8, packetSize uint16, interval uint8)
160164

161165
// Bytes returns EndpointDescriptor data.
162166
func (d EndpointDescriptor) Bytes() []byte {
163-
buf := bytes.NewBuffer(make([]byte, 0, endpointDescriptorSize))
164-
binary.Write(buf, binary.LittleEndian, d.bLength)
165-
binary.Write(buf, binary.LittleEndian, d.bDescriptorType)
166-
binary.Write(buf, binary.LittleEndian, d.bEndpointAddress)
167-
binary.Write(buf, binary.LittleEndian, d.bmAttributes)
168-
binary.Write(buf, binary.LittleEndian, d.wMaxPacketSize)
169-
binary.Write(buf, binary.LittleEndian, d.bInterval)
170-
return buf.Bytes()
167+
b := make([]byte, endpointDescriptorSize)
168+
b[0] = byte(d.bLength)
169+
b[1] = byte(d.bDescriptorType)
170+
b[2] = byte(d.bEndpointAddress)
171+
b[3] = byte(d.bmAttributes)
172+
b[4] = byte(d.wMaxPacketSize)
173+
b[5] = byte(d.wMaxPacketSize >> 8)
174+
b[6] = byte(d.bInterval)
175+
return b
171176
}
172177

173178
const iadDescriptorSize = 8
@@ -197,16 +202,16 @@ func NewIADDescriptor(firstInterface, count, class, subClass, protocol uint8) IA
197202

198203
// Bytes returns IADDescriptor data.
199204
func (d IADDescriptor) Bytes() []byte {
200-
buf := bytes.NewBuffer(make([]byte, 0, iadDescriptorSize))
201-
binary.Write(buf, binary.LittleEndian, d.bLength)
202-
binary.Write(buf, binary.LittleEndian, d.bDescriptorType)
203-
binary.Write(buf, binary.LittleEndian, d.bFirstInterface)
204-
binary.Write(buf, binary.LittleEndian, d.bInterfaceCount)
205-
binary.Write(buf, binary.LittleEndian, d.bFunctionClass)
206-
binary.Write(buf, binary.LittleEndian, d.bFunctionSubClass)
207-
binary.Write(buf, binary.LittleEndian, d.bFunctionProtocol)
208-
binary.Write(buf, binary.LittleEndian, d.iFunction)
209-
return buf.Bytes()
205+
b := make([]byte, iadDescriptorSize)
206+
b[0] = byte(d.bLength)
207+
b[1] = byte(d.bDescriptorType)
208+
b[2] = byte(d.bFirstInterface)
209+
b[3] = byte(d.bInterfaceCount)
210+
b[4] = byte(d.bFunctionClass)
211+
b[5] = byte(d.bFunctionSubClass)
212+
b[6] = byte(d.bFunctionProtocol)
213+
b[7] = byte(d.iFunction)
214+
return b
210215
}
211216

212217
const cdcCSInterfaceDescriptorSize = 5
@@ -227,13 +232,13 @@ func NewCDCCSInterfaceDescriptor(subtype, d0, d1 uint8) CDCCSInterfaceDescriptor
227232

228233
// Bytes returns CDCCSInterfaceDescriptor data.
229234
func (d CDCCSInterfaceDescriptor) Bytes() []byte {
230-
buf := bytes.NewBuffer(make([]byte, 0, cdcCSInterfaceDescriptorSize))
231-
binary.Write(buf, binary.LittleEndian, d.len)
232-
binary.Write(buf, binary.LittleEndian, d.dtype)
233-
binary.Write(buf, binary.LittleEndian, d.subtype)
234-
binary.Write(buf, binary.LittleEndian, d.d0)
235-
binary.Write(buf, binary.LittleEndian, d.d1)
236-
return buf.Bytes()
235+
b := make([]byte, cdcCSInterfaceDescriptorSize)
236+
b[0] = byte(d.len)
237+
b[1] = byte(d.dtype)
238+
b[2] = byte(d.subtype)
239+
b[3] = byte(d.d0)
240+
b[4] = byte(d.d1)
241+
return b
237242
}
238243

239244
const cmFunctionalDescriptorSize = 5
@@ -254,13 +259,13 @@ func NewCMFunctionalDescriptor(subtype, d0, d1 uint8) CMFunctionalDescriptor {
254259

255260
// Bytes returns the CMFunctionalDescriptor data.
256261
func (d CMFunctionalDescriptor) Bytes() []byte {
257-
buf := bytes.NewBuffer(make([]byte, 0, cmFunctionalDescriptorSize))
258-
binary.Write(buf, binary.LittleEndian, d.bFunctionLength)
259-
binary.Write(buf, binary.LittleEndian, d.bDescriptorType)
260-
binary.Write(buf, binary.LittleEndian, d.bDescriptorSubtype)
261-
binary.Write(buf, binary.LittleEndian, d.bmCapabilities)
262-
binary.Write(buf, binary.LittleEndian, d.bDataInterface)
263-
return buf.Bytes()
262+
b := make([]byte, cmFunctionalDescriptorSize)
263+
b[0] = byte(d.bFunctionLength)
264+
b[1] = byte(d.bDescriptorType)
265+
b[2] = byte(d.bDescriptorSubtype)
266+
b[3] = byte(d.bmCapabilities)
267+
b[4] = byte(d.bDescriptorSubtype)
268+
return b
264269
}
265270

266271
const acmFunctionalDescriptorSize = 4
@@ -280,12 +285,12 @@ func NewACMFunctionalDescriptor(subtype, d0 uint8) ACMFunctionalDescriptor {
280285

281286
// Bytes returns the ACMFunctionalDescriptor data.
282287
func (d ACMFunctionalDescriptor) Bytes() []byte {
283-
buf := bytes.NewBuffer(make([]byte, 0, acmFunctionalDescriptorSize))
284-
binary.Write(buf, binary.LittleEndian, d.len)
285-
binary.Write(buf, binary.LittleEndian, d.dtype)
286-
binary.Write(buf, binary.LittleEndian, d.subtype)
287-
binary.Write(buf, binary.LittleEndian, d.bmCapabilities)
288-
return buf.Bytes()
288+
b := make([]byte, acmFunctionalDescriptorSize)
289+
b[0] = byte(d.len)
290+
b[1] = byte(d.dtype)
291+
b[2] = byte(d.subtype)
292+
b[3] = byte(d.bmCapabilities)
293+
return b
289294
}
290295

291296
// CDCDescriptor is the Communication Device Class (CDC) descriptor.
@@ -343,7 +348,7 @@ const cdcSize = iadDescriptorSize +
343348

344349
// Bytes returns CDCDescriptor data.
345350
func (d CDCDescriptor) Bytes() []byte {
346-
buf := bytes.NewBuffer(make([]byte, 0, cdcSize))
351+
buf := bytes.NewBuffer(make([]byte, 0))
347352
buf.Write(d.iad.Bytes())
348353
buf.Write(d.cif.Bytes())
349354
buf.Write(d.header.Bytes())
@@ -523,14 +528,13 @@ type usbSetup struct {
523528
}
524529

525530
func newUSBSetup(data []byte) usbSetup {
526-
buf := bytes.NewBuffer(data)
527531
u := usbSetup{}
528-
binary.Read(buf, binary.LittleEndian, &(u.bmRequestType))
529-
binary.Read(buf, binary.LittleEndian, &(u.bRequest))
530-
binary.Read(buf, binary.LittleEndian, &(u.wValueL))
531-
binary.Read(buf, binary.LittleEndian, &(u.wValueH))
532-
binary.Read(buf, binary.LittleEndian, &(u.wIndex))
533-
binary.Read(buf, binary.LittleEndian, &(u.wLength))
532+
u.bmRequestType = uint8(data[0])
533+
u.bRequest = uint8(data[1])
534+
u.wValueL = uint8(data[2])
535+
u.wValueH = uint8(data[3])
536+
u.wIndex = uint16(data[4]) | uint16(data[5]<<8)
537+
u.wLength = uint16(data[6]) | uint16(data[7]<<8)
534538
return u
535539
}
536540

0 commit comments

Comments
 (0)