Skip to content

Commit 07a78c8

Browse files
committed
machine/usb: support bidirectional endpoints by dynamic registration
1 parent 7b3dc19 commit 07a78c8

5 files changed

Lines changed: 25 additions & 52 deletions

File tree

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() {
@@ -276,8 +288,11 @@ func handleStandardSetup(setup usb.Setup) bool {
276288

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

283298
usbConfiguration = setup.WValueL
@@ -358,12 +373,18 @@ func ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []usb.EndpointC
358373

359374
for _, ep := range epSettings {
360375
if ep.IsIn {
361-
endPoints[ep.Index] = uint32(ep.Type | usb.EndpointIn)
376+
endPoints = append(endPoints, usbEndpointEntry{
377+
Endpoint: uint32(ep.Index),
378+
Config: uint32(ep.Type | usb.EndpointIn),
379+
})
362380
if ep.TxHandler != nil {
363381
usbTxHandler[ep.Index] = ep.TxHandler
364382
}
365383
} else {
366-
endPoints[ep.Index] = uint32(ep.Type | usb.EndpointOut)
384+
endPoints = append(endPoints, usbEndpointEntry{
385+
Endpoint: uint32(ep.Index),
386+
Config: uint32(ep.Type | usb.EndpointOut),
387+
})
367388
if ep.RxHandler != nil {
368389
usbRxHandler[ep.Index] = func(b []byte) bool {
369390
ep.RxHandler(b)

0 commit comments

Comments
 (0)