mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 15:03:41 +00:00
machine/usb: allow USB Endpoint settings to be changed externally
This commit is contained in:
+43
-38
@@ -263,47 +263,52 @@ func EnableCDC(txHandler func(), rxHandler func([]byte), setupHandler func(usb.S
|
||||
usbDescriptor = descriptor.CDC
|
||||
}
|
||||
// Initialization of endpoints is required even for non-CDC
|
||||
endPoints[usb.CDC_ENDPOINT_ACM] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn)
|
||||
endPoints[usb.CDC_ENDPOINT_OUT] = (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut)
|
||||
endPoints[usb.CDC_ENDPOINT_IN] = (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn)
|
||||
usbRxHandler[usb.CDC_ENDPOINT_OUT] = rxHandler
|
||||
usbTxHandler[usb.CDC_ENDPOINT_IN] = txHandler
|
||||
usbSetupHandler[usb.CDC_ACM_INTERFACE] = setupHandler // 0x02 (Communications and CDC Control)
|
||||
usbSetupHandler[usb.CDC_DATA_INTERFACE] = nil // 0x0A (CDC-Data)
|
||||
ConfigureUSBEndpoint(usbDescriptor,
|
||||
[]usb.EndpointConfig{
|
||||
{
|
||||
No: usb.CDC_ENDPOINT_ACM,
|
||||
IsIn: true,
|
||||
Type: usb.ENDPOINT_TYPE_INTERRUPT,
|
||||
},
|
||||
{
|
||||
No: usb.CDC_ENDPOINT_OUT,
|
||||
IsIn: false,
|
||||
Type: usb.ENDPOINT_TYPE_BULK,
|
||||
RxHandler: rxHandler,
|
||||
},
|
||||
{
|
||||
No: usb.CDC_ENDPOINT_IN,
|
||||
IsIn: true,
|
||||
Type: usb.ENDPOINT_TYPE_BULK,
|
||||
TxHandler: txHandler,
|
||||
},
|
||||
},
|
||||
[]usb.SetupConfig{
|
||||
{
|
||||
No: usb.CDC_ACM_INTERFACE,
|
||||
Handler: setupHandler,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// EnableHID enables HID. This function must be executed from the init().
|
||||
func EnableHID(txHandler func(), rxHandler func([]byte), setupHandler func(usb.Setup) bool) {
|
||||
usbDescriptor = descriptor.CDCHID
|
||||
endPoints[usb.HID_ENDPOINT_IN] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn)
|
||||
usbTxHandler[usb.HID_ENDPOINT_IN] = txHandler
|
||||
usbSetupHandler[usb.HID_INTERFACE] = setupHandler // 0x03 (HID - Human Interface Device)
|
||||
}
|
||||
func ConfigureUSBEndpoint(desc descriptor.Descriptor, epSettings []usb.EndpointConfig, setup []usb.SetupConfig) {
|
||||
usbDescriptor = desc
|
||||
|
||||
// EnableMIDI enables MIDI. This function must be executed from the init().
|
||||
func EnableMIDI(txHandler func(), rxHandler func([]byte), setupHandler func(usb.Setup) bool) {
|
||||
usbDescriptor = descriptor.CDCMIDI
|
||||
endPoints[usb.MIDI_ENDPOINT_OUT] = (usb.ENDPOINT_TYPE_BULK | usb.EndpointOut)
|
||||
endPoints[usb.MIDI_ENDPOINT_IN] = (usb.ENDPOINT_TYPE_BULK | usb.EndpointIn)
|
||||
usbRxHandler[usb.MIDI_ENDPOINT_OUT] = rxHandler
|
||||
usbTxHandler[usb.MIDI_ENDPOINT_IN] = txHandler
|
||||
}
|
||||
|
||||
// EnableJoystick enables HID. This function must be executed from the init().
|
||||
func EnableJoystick(txHandler func(), rxHandler func([]byte), setupHandler func(usb.Setup) bool, hidDesc []byte) {
|
||||
class, err := descriptor.FindClassHIDType(descriptor.CDCJoystick.Configuration, descriptor.ClassHIDJoystick.Bytes())
|
||||
if err != nil {
|
||||
// TODO: some way to notify about error
|
||||
return
|
||||
for _, ep := range epSettings {
|
||||
if ep.IsIn {
|
||||
endPoints[ep.No] = uint32(ep.Type | usb.EndpointIn)
|
||||
if ep.TxHandler != nil {
|
||||
usbTxHandler[ep.No] = ep.TxHandler
|
||||
}
|
||||
} else {
|
||||
endPoints[ep.No] = uint32(ep.Type | usb.EndpointOut)
|
||||
if ep.RxHandler != nil {
|
||||
usbRxHandler[ep.No] = ep.RxHandler
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class.ClassLength(uint16(len(hidDesc)))
|
||||
descriptor.CDCJoystick.HID[2] = hidDesc
|
||||
|
||||
usbDescriptor = descriptor.CDCJoystick
|
||||
endPoints[usb.HID_ENDPOINT_OUT] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointOut)
|
||||
usbRxHandler[usb.HID_ENDPOINT_OUT] = rxHandler
|
||||
endPoints[usb.HID_ENDPOINT_IN] = (usb.ENDPOINT_TYPE_INTERRUPT | usb.EndpointIn)
|
||||
usbTxHandler[usb.HID_ENDPOINT_IN] = txHandler
|
||||
usbSetupHandler[usb.HID_INTERFACE] = setupHandler // 0x03 (HID - Human Interface Device)
|
||||
for _, s := range setup {
|
||||
usbSetupHandler[s.No] = s.Handler
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user