Skip to content

Commit 64281df

Browse files
committed
refactor(cdc): integrate cdc package with usb.Controller
1 parent 7929007 commit 64281df

File tree

10 files changed

+47
-18
lines changed

10 files changed

+47
-18
lines changed

src/machine/machine_atsamd21_usb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func sendUSBPacket(ep uint32, data []byte, maxsize uint16) {
378378
usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits((uint32(l) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
379379
}
380380

381-
func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
381+
func (dev *USBDevice) ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
382382
var b [cdcLineInfoSize]byte
383383

384384
// Wait until OUT transfer is ready.

src/machine/machine_atsamd51_usb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func sendUSBPacket(ep uint32, data []byte, maxsize uint16) {
381381
usbEndpointDescriptors[ep].DeviceDescBank[1].PCKSIZE.SetBits((uint32(l) & usb_DEVICE_PCKSIZE_BYTE_COUNT_Mask) << usb_DEVICE_PCKSIZE_BYTE_COUNT_Pos)
382382
}
383383

384-
func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
384+
func (dev *USBDevice) ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
385385
var b [cdcLineInfoSize]byte
386386

387387
// Wait until OUT transfer is ready.

src/machine/machine_nrf52840_usb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func handleUSBSetAddress(setup usb.Setup) bool {
372372
return true
373373
}
374374

375-
func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
375+
func (dev *USBDevice) ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
376376
var b [cdcLineInfoSize]byte
377377

378378
nrf.USBD.TASKS_EP0RCVOUT.Set(1)

src/machine/machine_rp2_usb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func sendUSBPacket(ep uint32, data []byte, maxsize uint16) {
104104
sendViaEPIn(ep, data, count)
105105
}
106106

107-
func ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
107+
func (dev *USBDevice) ReceiveUSBControlPacket() ([cdcLineInfoSize]byte, error) {
108108
var b [cdcLineInfoSize]byte
109109
ep := 0
110110

src/machine/usb.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@ var (
1919
USBCDC Serialer
2020
)
2121

22-
func initUSB() {
23-
enableUSBCDC()
24-
USBDev.Configure(UARTConfig{})
25-
22+
func init() {
2623
usb.DefaultController = USBDev
2724
}
2825

26+
func initUSB() {
27+
USBDev.Enable()
28+
}
29+
2930
// Using go:linkname here because there's a circular dependency between the
3031
// machine package and the machine/usb/cdc package.
3132
//
3233
//go:linkname enableUSBCDC machine/usb/cdc.EnableUSBCDC
3334
func enableUSBCDC()
3435

36+
func ReceiveUSBControlPacket() ([7]byte, error) {
37+
return USBDev.ReceiveUSBControlPacket()
38+
}
39+
3540
type Serialer interface {
3641
WriteByte(c byte) error
3742
Write(data []byte) (n int, err error)
@@ -287,6 +292,15 @@ func handleStandardSetup(setup usb.Setup) bool {
287292
}
288293
}
289294

295+
func (d *USBDevice) Enable() {
296+
if d.initcomplete {
297+
return
298+
}
299+
enableUSBCDC()
300+
d.Configure(UARTConfig{})
301+
d.initcomplete = true
302+
}
303+
290304
func (d *USBDevice) IsInitEndpointComplete() bool {
291305
return d.InitEndpointComplete
292306
}

src/machine/usb/cdc/cdc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cdc
22

3+
import "machine/usb"
4+
35
const (
46
cdcEndpointACM = 1
57
cdcEndpointOut = 2
@@ -12,6 +14,7 @@ func New() *USBCDC {
1214
USB = &USBCDC{
1315
rxBuffer: NewRxRingBuffer(),
1416
txBuffer: NewTxRingBuffer(),
17+
dev: usb.DefaultController,
1518
}
1619
}
1720
return USB

src/machine/usb/cdc/usbcdc.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type USBCDC struct {
7070
rxBuffer *rxRingBuffer
7171
txBuffer *txRingBuffer
7272
waitTxc bool
73+
dev usb.Controller
7374
}
7475

7576
var (
@@ -88,7 +89,7 @@ func (usbcdc *USBCDC) Configure(config machine.UARTConfig) error {
8889
func (usbcdc *USBCDC) Flush() {
8990
mask := interrupt.Disable()
9091
if b, ok := usbcdc.txBuffer.Get(); ok {
91-
machine.SendUSBInPacket(cdcEndpointIn, b)
92+
usbcdc.dev.SendUSBInPacket(cdcEndpointIn, b)
9293
} else {
9394
usbcdc.waitTxc = false
9495
}
@@ -123,15 +124,15 @@ func (usbcdc *USBCDC) RTS() bool {
123124
return (usbLineInfo.lineState & usb_CDC_LINESTATE_RTS) > 0
124125
}
125126

126-
func cdcCallbackRx(b []byte) {
127+
func (usbcdc *USBCDC) Rx(b []byte) {
127128
for i := range b {
128-
USB.Receive(b[i])
129+
usbcdc.Receive(b[i])
129130
}
130131
}
131132

132133
var cdcSetupBuff [cdcLineInfoSize]byte
133134

134-
func cdcSetup(setup usb.Setup) bool {
135+
func (usbcdc *USBCDC) Setup(setup usb.Setup) bool {
135136
if setup.BmRequestType == usb_REQUEST_DEVICETOHOST_CLASS_INTERFACE {
136137
if setup.BRequest == usb_CDC_GET_LINE_CODING {
137138
cdcSetupBuff[0] = byte(usbLineInfo.dwDTERate)
@@ -142,14 +143,14 @@ func cdcSetup(setup usb.Setup) bool {
142143
cdcSetupBuff[5] = byte(usbLineInfo.bParityType)
143144
cdcSetupBuff[6] = byte(usbLineInfo.bDataBits)
144145

145-
machine.SendUSBInPacket(0, cdcSetupBuff[:])
146+
usbcdc.dev.SendUSBInPacket(0, cdcSetupBuff[:])
146147
return true
147148
}
148149
}
149150

150151
if setup.BmRequestType == usb_REQUEST_HOSTTODEVICE_CLASS_INTERFACE {
151152
if setup.BRequest == usb_CDC_SET_LINE_CODING {
152-
b, err := machine.ReceiveUSBControlPacket()
153+
b, err := usbcdc.dev.ReceiveUSBControlPacket()
153154
if err != nil {
154155
return false
155156
}
@@ -171,21 +172,22 @@ func cdcSetup(setup usb.Setup) bool {
171172
} else {
172173
// TODO: cancel any reset
173174
}
174-
machine.SendZlp()
175+
usbcdc.dev.SendZlp()
175176
}
176177

177178
if setup.BRequest == usb_CDC_SEND_BREAK {
178179
// TODO: something with this value?
179180
// breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
180181
// return false;
181-
machine.SendZlp()
182+
usbcdc.dev.SendZlp()
182183
}
183184
return true
184185
}
185186
return false
186187
}
187188

188189
func EnableUSBCDC() {
189-
machine.USBCDC = New()
190-
machine.EnableCDC(USB.Flush, cdcCallbackRx, cdcSetup)
190+
c := New()
191+
machine.USBCDC = c
192+
machine.EnableCDC(c.Flush, c.Rx, c.Setup)
191193
}

src/machine/usb/device.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
// Controller abstracts the USB interactions to allow for testing without hardware.
88
type Controller interface {
9+
Enable()
910
ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []EndpointConfig, setup []SetupConfig)
1011
SendUSBInPacket(ep uint32, data []byte) bool
1112
AckUsbOutTransfer(ep uint32)
@@ -15,6 +16,7 @@ type Controller interface {
1516
SetStallEPOut(ep uint32)
1617
ClearStallEPIn(ep uint32)
1718
ClearStallEPOut(ep uint32)
19+
ReceiveUSBControlPacket() ([7]byte, error)
1820
}
1921

2022
var DefaultController Controller

src/machine/usb/msc/mock_usb_device.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ func (m *MockUSBDevice) ClearStallEPIn(ep uint32) {
5454
func (m *MockUSBDevice) ClearStallEPOut(ep uint32) {
5555
m.StallOut = false
5656
}
57+
58+
func (m *MockUSBDevice) Enable() {
59+
}
60+
61+
func (m *MockUSBDevice) ReceiveUSBControlPacket() ([7]byte, error) {
62+
return [7]byte{}, nil
63+
}

src/machine/usb/msc/msc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func newMSC(dev BlockDevice, usbCtrl usb.Controller) *msc {
8080
maxPacketSize: uint32(maxPacketSize),
8181
usb: usbCtrl,
8282
}
83+
m.usb.Enable()
8384
m.RegisterBlockDevice(dev)
8485

8586
// Set default inquiry data fields

0 commit comments

Comments
 (0)