Skip to content

Commit ca584de

Browse files
authored
machine/usb: support bidirectional endpoints by dynamic registration (#5447)
* machine/usb: support bidirectional endpoints by dynamic registration
1 parent 594be6d commit ca584de

6 files changed

Lines changed: 26 additions & 53 deletions

File tree

builder/sizes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestBinarySize(t *testing.T) {
4444
// microcontrollers
4545
{"hifive1b", "examples/echo", 3817, 299, 0, 2252},
4646
{"microbit", "examples/serial", 2820, 356, 8, 2248},
47-
{"wioterminal", "examples/pininterrupt", 7330, 1550, 120, 7248},
47+
{"wioterminal", "examples/pininterrupt", 7930, 1650, 132, 7472},
4848

4949
// TODO: also check wasm. Right now this is difficult, because
5050
// wasm binaries are run through wasm-opt and therefore the

src/machine/machine_atsamd21_usb.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ const (
2323
NumberOfUSBEndpoints = 8
2424
)
2525

26-
var (
27-
endPoints = []uint32{
28-
usb.CONTROL_ENDPOINT: usb.ENDPOINT_TYPE_CONTROL,
29-
usb.CDC_ENDPOINT_ACM: (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn),
30-
usb.CDC_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut),
31-
usb.CDC_ENDPOINT_IN: (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn),
32-
usb.HID_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt In
33-
usb.HID_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt Out
34-
usb.MIDI_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Bulk In
35-
usb.MIDI_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Bulk Out
36-
}
37-
)
38-
3926
// Configure the USB peripheral. The config is here for compatibility with the UART interface.
4027
func (dev *USBDevice) Configure(config UARTConfig) {
4128
if dev.initcomplete {

src/machine/machine_atsamd51_usb.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ const (
2323
NumberOfUSBEndpoints = 8
2424
)
2525

26-
var (
27-
endPoints = []uint32{
28-
usb.CONTROL_ENDPOINT: usb.ENDPOINT_TYPE_CONTROL,
29-
usb.CDC_ENDPOINT_ACM: (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn),
30-
usb.CDC_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut),
31-
usb.CDC_ENDPOINT_IN: (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn),
32-
usb.HID_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt In
33-
usb.HID_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt Out
34-
usb.MIDI_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Bulk In
35-
usb.MIDI_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Bulk Out
36-
}
37-
)
38-
3926
// Configure the USB peripheral. The config is here for compatibility with the UART interface.
4027
func (dev *USBDevice) Configure(config UARTConfig) {
4128
if dev.initcomplete {

src/machine/machine_nrf52840_usb.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ var (
2222
epinen uint32
2323
epouten uint32
2424
easyDMABusy volatile.Register8
25-
26-
endPoints = []uint32{
27-
usb.CONTROL_ENDPOINT: usb.ENDPOINT_TYPE_CONTROL,
28-
usb.CDC_ENDPOINT_ACM: (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn),
29-
usb.CDC_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut),
30-
usb.CDC_ENDPOINT_IN: (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn),
31-
usb.HID_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt In
32-
usb.HID_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt Out
33-
usb.MIDI_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Bulk In
34-
usb.MIDI_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Bulk Out
35-
}
3625
)
3726

3827
// enterCriticalSection is used to protect access to easyDMA - only one thing

src/machine/machine_rp2_usb.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ var (
1616
data []byte
1717
pid uint32
1818
}
19-
20-
endPoints = []uint32{
21-
usb.CONTROL_ENDPOINT: usb.ENDPOINT_TYPE_CONTROL,
22-
usb.CDC_ENDPOINT_ACM: (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn),
23-
usb.CDC_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut),
24-
usb.CDC_ENDPOINT_IN: (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn),
25-
usb.HID_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt In
26-
usb.HID_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Interrupt Out
27-
usb.MIDI_ENDPOINT_IN: (usb.ENDPOINT_TYPE_DISABLE), // Bulk In
28-
usb.MIDI_ENDPOINT_OUT: (usb.ENDPOINT_TYPE_DISABLE), // Bulk Out
29-
}
3019
)
3120

3221
func initEndpoint(ep, config uint32) {

src/machine/usb.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@ type USBDevice struct {
1414
InitEndpointComplete bool
1515
}
1616

17+
type usbEndpointEntry struct {
18+
Endpoint uint32
19+
Config uint32
20+
}
21+
1722
var (
1823
USBDev = &USBDevice{}
1924
USBCDC Serialer
25+
26+
endPoints = []usbEndpointEntry{
27+
{
28+
Endpoint: usb.CONTROL_ENDPOINT,
29+
Config: usb.ENDPOINT_TYPE_CONTROL,
30+
},
31+
}
2032
)
2133

2234
func initUSB() {
@@ -277,8 +289,11 @@ func handleStandardSetup(setup usb.Setup) bool {
277289

278290
case usb.SET_CONFIGURATION:
279291
if setup.BmRequestType&usb.REQUEST_RECIPIENT == usb.REQUEST_DEVICE {
280-
for i := 1; i < len(endPoints); i++ {
281-
initEndpoint(uint32(i), endPoints[i])
292+
for _, entry := range endPoints {
293+
if entry.Endpoint == usb.CONTROL_ENDPOINT {
294+
continue
295+
}
296+
initEndpoint(uint32(entry.Endpoint), entry.Config)
282297
}
283298

284299
usbConfiguration = setup.WValueL
@@ -359,12 +374,18 @@ func ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []usb.EndpointC
359374

360375
for _, ep := range epSettings {
361376
if ep.IsIn {
362-
endPoints[ep.Index] = uint32(ep.Type | usb.EndpointIn)
377+
endPoints = append(endPoints, usbEndpointEntry{
378+
Endpoint: uint32(ep.Index),
379+
Config: uint32(ep.Type | usb.EndpointIn),
380+
})
363381
if ep.TxHandler != nil {
364382
usbTxHandler[ep.Index] = ep.TxHandler
365383
}
366384
} else {
367-
endPoints[ep.Index] = uint32(ep.Type | usb.EndpointOut)
385+
endPoints = append(endPoints, usbEndpointEntry{
386+
Endpoint: uint32(ep.Index),
387+
Config: uint32(ep.Type | usb.EndpointOut),
388+
})
368389
if ep.RxHandler != nil {
369390
usbRxHandler[ep.Index] = func(b []byte) bool {
370391
ep.RxHandler(b)

0 commit comments

Comments
 (0)