machine/usbcdc: remove remaining heap allocations for USB CDC implementations

Signed-off-by: deadprogram <ron@hybridgroup.com>
Этот коммит содержится в:
deadprogram 2021-04-20 10:48:43 +02:00 коммит произвёл Ron Evans
родитель 321488dcfe
коммит b661882391
4 изменённых файлов: 42 добавлений и 20 удалений

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

@ -2298,7 +2298,7 @@ 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 {
b := make([]byte, 7) var b [cdcLineInfoSize]byte
b[0] = byte(usbLineInfo.dwDTERate) b[0] = byte(usbLineInfo.dwDTERate)
b[1] = byte(usbLineInfo.dwDTERate >> 8) b[1] = byte(usbLineInfo.dwDTERate >> 8)
b[2] = byte(usbLineInfo.dwDTERate >> 16) b[2] = byte(usbLineInfo.dwDTERate >> 16)
@ -2307,14 +2307,18 @@ func cdcSetup(setup usbSetup) bool {
b[5] = byte(usbLineInfo.bParityType) b[5] = byte(usbLineInfo.bParityType)
b[6] = byte(usbLineInfo.bDataBits) b[6] = byte(usbLineInfo.bDataBits)
sendUSBPacket(0, b) 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 {
b := receiveUSBControlPacket() b, err := receiveUSBControlPacket()
if err != nil {
return false
}
usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
usbLineInfo.bCharFormat = b[4] usbLineInfo.bCharFormat = b[4]
usbLineInfo.bParityType = b[5] usbLineInfo.bParityType = b[5]
@ -2361,7 +2365,9 @@ func sendUSBPacket(ep uint32, data []byte) {
usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits(uint32((len(data) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)) usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits(uint32((len(data) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos))
} }
func receiveUSBControlPacket() []byte { func receiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
var b [cdcLineInfoSize]byte
// address // address
usbEndpointDescriptors[0].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[0])))) usbEndpointDescriptors[0].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[0]))))
@ -2376,7 +2382,7 @@ func receiveUSBControlPacket() []byte {
for (getEPSTATUS(0) & sam.USB_DEVICE_EPSTATUS_BK0RDY) == 0 { for (getEPSTATUS(0) & sam.USB_DEVICE_EPSTATUS_BK0RDY) == 0 {
timeout-- timeout--
if timeout == 0 { if timeout == 0 {
return []byte{} return b, errUSBCDCReadTimeout
} }
} }
@ -2385,7 +2391,7 @@ func receiveUSBControlPacket() []byte {
for (getEPINTFLAG(0) & sam.USB_DEVICE_EPINTFLAG_TRCPT0) == 0 { for (getEPINTFLAG(0) & sam.USB_DEVICE_EPINTFLAG_TRCPT0) == 0 {
timeout-- timeout--
if timeout == 0 { if timeout == 0 {
return []byte{} return b, errUSBCDCReadTimeout
} }
} }
@ -2393,10 +2399,13 @@ func receiveUSBControlPacket() []byte {
bytesread := uint32((usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.Get() >> bytesread := uint32((usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.Get() >>
usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask)
data := make([]byte, bytesread) if bytesread != cdcLineInfoSize {
copy(data, udd_ep_out_cache_buffer[0][:]) return b, errUSBCDCBytesRead
}
return data copy(b[:7], udd_ep_out_cache_buffer[0][:7])
return b, nil
} }
func handleEndpoint(ep uint32) { func handleEndpoint(ep uint32) {

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

@ -2426,7 +2426,7 @@ 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 {
b := make([]byte, 7) var b [cdcLineInfoSize]byte
b[0] = byte(usbLineInfo.dwDTERate) b[0] = byte(usbLineInfo.dwDTERate)
b[1] = byte(usbLineInfo.dwDTERate >> 8) b[1] = byte(usbLineInfo.dwDTERate >> 8)
b[2] = byte(usbLineInfo.dwDTERate >> 16) b[2] = byte(usbLineInfo.dwDTERate >> 16)
@ -2435,14 +2435,18 @@ func cdcSetup(setup usbSetup) bool {
b[5] = byte(usbLineInfo.bParityType) b[5] = byte(usbLineInfo.bParityType)
b[6] = byte(usbLineInfo.bDataBits) b[6] = byte(usbLineInfo.bDataBits)
sendUSBPacket(0, b) 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 {
b := receiveUSBControlPacket() b, err := receiveUSBControlPacket()
if err != nil {
return false
}
usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 usbLineInfo.dwDTERate = uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
usbLineInfo.bCharFormat = b[4] usbLineInfo.bCharFormat = b[4]
usbLineInfo.bParityType = b[5] usbLineInfo.bParityType = b[5]
@ -2489,7 +2493,9 @@ func sendUSBPacket(ep uint32, data []byte) {
usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits(uint32((len(data) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)) usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits(uint32((len(data) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos))
} }
func receiveUSBControlPacket() []byte { func receiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
var b [cdcLineInfoSize]byte
// address // address
usbEndpointDescriptors[0].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[0])))) usbEndpointDescriptors[0].DeviceDescBank[0].ADDR.Set(uint32(uintptr(unsafe.Pointer(&udd_ep_out_cache_buffer[0]))))
@ -2504,7 +2510,7 @@ func receiveUSBControlPacket() []byte {
for (getEPSTATUS(0) & sam.USB_DEVICE_ENDPOINT_EPSTATUS_BK0RDY) == 0 { for (getEPSTATUS(0) & sam.USB_DEVICE_ENDPOINT_EPSTATUS_BK0RDY) == 0 {
timeout-- timeout--
if timeout == 0 { if timeout == 0 {
return []byte{} return b, errUSBCDCReadTimeout
} }
} }
@ -2513,7 +2519,7 @@ func receiveUSBControlPacket() []byte {
for (getEPINTFLAG(0) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) == 0 { for (getEPINTFLAG(0) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) == 0 {
timeout-- timeout--
if timeout == 0 { if timeout == 0 {
return []byte{} return b, errUSBCDCReadTimeout
} }
} }
@ -2521,10 +2527,13 @@ func receiveUSBControlPacket() []byte {
bytesread := uint32((usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.Get() >> bytesread := uint32((usbEndpointDescriptors[0].DeviceDescBank[0].PCKSIZE.Get() >>
usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask)
data := make([]byte, bytesread) if bytesread != cdcLineInfoSize {
copy(data, udd_ep_out_cache_buffer[0][:]) return b, errUSBCDCBytesRead
}
return data copy(b[:7], udd_ep_out_cache_buffer[0][:7])
return b, nil
} }
func handleEndpoint(ep uint32) { func handleEndpoint(ep uint32) {

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

@ -445,7 +445,7 @@ 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 {
b := make([]byte, 7) var b [cdcLineInfoSize]byte
b[0] = byte(usbLineInfo.dwDTERate) b[0] = byte(usbLineInfo.dwDTERate)
b[1] = byte(usbLineInfo.dwDTERate >> 8) b[1] = byte(usbLineInfo.dwDTERate >> 8)
b[2] = byte(usbLineInfo.dwDTERate >> 16) b[2] = byte(usbLineInfo.dwDTERate >> 16)
@ -454,7 +454,7 @@ func cdcSetup(setup usbSetup) bool {
b[5] = byte(usbLineInfo.bParityType) b[5] = byte(usbLineInfo.bParityType)
b[6] = byte(usbLineInfo.bDataBits) b[6] = byte(usbLineInfo.bDataBits)
sendUSBPacket(0, b) sendUSBPacket(0, b[:])
return true return true
} }
} }

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

@ -12,6 +12,8 @@ const deviceDescriptorSize = 18
var ( var (
errUSBCDCBufferEmpty = errors.New("USB-CDC buffer empty") errUSBCDCBufferEmpty = errors.New("USB-CDC buffer empty")
errUSBCDCWriteByteTimeout = errors.New("USB-CDC write byte timeout") errUSBCDCWriteByteTimeout = errors.New("USB-CDC write byte timeout")
errUSBCDCReadTimeout = errors.New("USB-CDC read timeout")
errUSBCDCBytesRead = errors.New("USB-CDC invalid number of bytes read")
) )
// DeviceDescriptor implements the USB standard device descriptor. // DeviceDescriptor implements the USB standard device descriptor.
@ -405,6 +407,8 @@ type MSCDescriptor struct {
out EndpointDescriptor out EndpointDescriptor
} }
const cdcLineInfoSize = 7
type cdcLineInfo struct { type cdcLineInfo struct {
dwDTERate uint32 dwDTERate uint32
bCharFormat uint8 bCharFormat uint8