mirror of
https://github.com/UberGuidoZ/Flipper.git
synced 2026-09-18 20:01:29 +00:00
Added xMasterX API v11.x source
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
/** @defgroup usb_audio_defines USB Audio Type Definitions
|
||||
|
||||
@brief <b>Defined Constants and Types for the USB Audio Type Definitions</b>
|
||||
|
||||
@ingroup USB_defines
|
||||
|
||||
@version 1.0.0
|
||||
|
||||
@author @htmlonly © @endhtmlonly 2014
|
||||
Daniel Thompson <daniel@redfelineninja.org.uk>
|
||||
Seb Holzapfel <schnommus@gmail.com>
|
||||
|
||||
@date 19 April 2014
|
||||
|
||||
LGPL License Terms @ref lgpl_license
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
|
||||
* Copyright (C) 2018 Seb Holzapfel <schnommus@gmail.com>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**@{*/
|
||||
|
||||
#ifndef LIBOPENCM3_USB_AUDIO_H
|
||||
#define LIBOPENCM3_USB_AUDIO_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
* Definitions from the USB_AUDIO_ or usb_audio_ namespace come from:
|
||||
* "Universal Serial Bus Class Definitions for Audio Devices, Revision 1.0"
|
||||
*/
|
||||
|
||||
/* Table A-1: Audio Interface Class Code */
|
||||
#define USB_CLASS_AUDIO 0x01
|
||||
|
||||
/* Table A-2: Audio Interface Subclass Codes */
|
||||
#define USB_AUDIO_SUBCLASS_UNDEFINED 0x00
|
||||
#define USB_AUDIO_SUBCLASS_CONTROL 0x01
|
||||
#define USB_AUDIO_SUBCLASS_AUDIOSTREAMING 0x02
|
||||
#define USB_AUDIO_SUBCLASS_MIDISTREAMING 0x03
|
||||
|
||||
/* Table A-4: Audio Class-specific Descriptor Types */
|
||||
#define USB_AUDIO_DT_CS_UNDEFINED 0x20
|
||||
#define USB_AUDIO_DT_CS_DEVICE 0x21
|
||||
#define USB_AUDIO_DT_CS_CONFIGURATION 0x22
|
||||
#define USB_AUDIO_DT_CS_STRING 0x23
|
||||
#define USB_AUDIO_DT_CS_INTERFACE 0x24
|
||||
#define USB_AUDIO_DT_CS_ENDPOINT 0x25
|
||||
|
||||
/* Table A-5: Audio Class-Specific AC Interface Descriptor Subtypes */
|
||||
#define USB_AUDIO_TYPE_AC_DESCRIPTOR_UNDEFINED 0x00
|
||||
#define USB_AUDIO_TYPE_HEADER 0x01
|
||||
#define USB_AUDIO_TYPE_INPUT_TERMINAL 0x02
|
||||
#define USB_AUDIO_TYPE_OUTPUT_TERMINAL 0x03
|
||||
#define USB_AUDIO_TYPE_MIXER_UNIT 0x04
|
||||
#define USB_AUDIO_TYPE_SELECTOR_UNIT 0x05
|
||||
#define USB_AUDIO_TYPE_FEATURE_UNIT 0x06
|
||||
#define USB_AUDIO_TYPE_PROCESSING_UNIT 0x07
|
||||
#define USB_AUDIO_TYPE_EXTENSION_UNIT 0x08
|
||||
|
||||
/* Table 4-2: Class-Specific AC Interface Header Descriptor (head) */
|
||||
struct usb_audio_header_descriptor_head {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint16_t bcdADC;
|
||||
uint16_t wTotalLength;
|
||||
uint8_t bInCollection;
|
||||
/* ... */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-2: Class-Specific AC Interface Header Descriptor (body) */
|
||||
struct usb_audio_header_descriptor_body {
|
||||
/* ... */
|
||||
uint8_t baInterfaceNr;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-3: Input Terminal Descriptor */
|
||||
struct usb_audio_input_terminal_descriptor {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bTerminalID;
|
||||
uint16_t wTerminalType;
|
||||
uint8_t bAssocTerminal;
|
||||
uint8_t bNrChannels;
|
||||
uint16_t wChannelConfig;
|
||||
uint8_t iChannelNames;
|
||||
uint8_t iTerminal;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-3: Output Terminal Descriptor */
|
||||
struct usb_audio_output_terminal_descriptor {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bTerminalID;
|
||||
uint16_t wTerminalType;
|
||||
uint8_t bAssocTerminal;
|
||||
uint8_t bSourceID;
|
||||
uint8_t iTerminal;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-7: Feature Unit Descriptor (head) */
|
||||
struct usb_audio_feature_unit_descriptor_head {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bUnitID;
|
||||
uint8_t bSourceID;
|
||||
uint8_t bControlSize;
|
||||
uint16_t bmaControlMaster; /* device can assume 16-bit, given highest
|
||||
* defined bit in spec is bit #9.
|
||||
* (it is thus required bControlSize=2) */
|
||||
/* ... */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-7: Feature Unit Descriptor (body) */
|
||||
struct usb_audio_feature_unit_descriptor_body {
|
||||
/* ... */
|
||||
uint16_t bmaControl;
|
||||
/* ... */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-7: Feature Unit Descriptor (tail) */
|
||||
struct usb_audio_feature_unit_descriptor_tail {
|
||||
/* ... */
|
||||
uint8_t iFeature;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-7: Feature Unit Descriptor (2-channel)
|
||||
*
|
||||
* This structure is a convenience covering the (common) case where
|
||||
* there are 2 channels associated with the feature unit
|
||||
*/
|
||||
struct usb_audio_feature_unit_descriptor_2ch {
|
||||
struct usb_audio_feature_unit_descriptor_head head;
|
||||
struct usb_audio_feature_unit_descriptor_body channel_control[2];
|
||||
struct usb_audio_feature_unit_descriptor_tail tail;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-19: Class-Specific AS Interface Descriptor */
|
||||
struct usb_audio_stream_interface_descriptor {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bTerminalLink;
|
||||
uint8_t bDelay;
|
||||
uint16_t wFormatTag;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-20: Standard AS Isochronous Audio Data Endpoint Descriptor */
|
||||
struct usb_audio_stream_endpoint_descriptor {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bEndpointAddress;
|
||||
uint8_t bmAttributes;
|
||||
uint16_t wMaxPacketSize;
|
||||
uint8_t bInterval;
|
||||
uint8_t bRefresh;
|
||||
uint8_t bSynchAddress;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 4-21: Class-Specific AS Isochronous Audio Data Endpoint Descriptor */
|
||||
struct usb_audio_stream_audio_endpoint_descriptor {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bmAttributes;
|
||||
uint8_t bLockDelayUnits;
|
||||
uint16_t wLockDelay;
|
||||
} __attribute__((packed));
|
||||
|
||||
/*
|
||||
* Definitions from the USB_AUDIO_FORMAT_ or usb_audio_format_ namespace come from:
|
||||
* "Universal Serial Bus Device Class Definition for Audio Data Formats, Revision 1.0"
|
||||
*/
|
||||
|
||||
/* Table 2-1: Type I Format Type Descriptor (head) */
|
||||
struct usb_audio_format_type1_descriptor_head {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bFormatType;
|
||||
uint8_t bNrChannels;
|
||||
uint8_t bSubFrameSize;
|
||||
uint8_t bBitResolution;
|
||||
uint8_t bSamFreqType;
|
||||
/* ... */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 2-2: Continuous Sampling Frequency */
|
||||
struct usb_audio_format_continuous_sampling_frequency {
|
||||
/* ... */
|
||||
uint32_t tLowerSamFreq : 24;
|
||||
uint32_t tUpperSamFreq : 24;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 2-3: Discrete Number of Sampling Frequencies */
|
||||
struct usb_audio_format_discrete_sampling_frequency {
|
||||
/* ... */
|
||||
uint32_t tSamFreq : 24;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 2-1: Type I Format Type Descriptor (1 sampling frequency)
|
||||
*
|
||||
* This structure is a convenience covering the (common) case where
|
||||
* only 1 discrete sampling frequency is used
|
||||
*/
|
||||
struct usb_audio_format_type1_descriptor_1freq {
|
||||
struct usb_audio_format_type1_descriptor_head head;
|
||||
struct usb_audio_format_discrete_sampling_frequency freqs[1];
|
||||
} __attribute__((packed));
|
||||
|
||||
#endif
|
||||
|
||||
/**@}*/
|
||||
@@ -0,0 +1,190 @@
|
||||
/** @defgroup usb_audio_defines USB MIDI Type Definitions
|
||||
|
||||
@brief <b>Defined Constants and Types for the USB MIDI Type Definitions</b>
|
||||
|
||||
@ingroup USB_defines
|
||||
|
||||
@version 1.0.0
|
||||
|
||||
@author @htmlonly © @endhtmlonly 2014
|
||||
Daniel Thompson <daniel@redfelineninja.org.uk>
|
||||
|
||||
@date 19 April 2014
|
||||
|
||||
LGPL License Terms @ref lgpl_license
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
* Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
|
||||
*
|
||||
* This library is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**@{*/
|
||||
|
||||
#ifndef LIBOPENCM3_USB_MIDI_H
|
||||
#define LIBOPENCM3_USB_MIDI_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
* Definitions from the USB_MIDI_ or usb_midi_ namespace come from:
|
||||
* "Universal Serial Bus Class Definitions for MIDI Devices, Revision 1.0"
|
||||
*/
|
||||
|
||||
/* Appendix A.1: MS Class-Specific Interface Descriptor Subtypes */
|
||||
#define USB_MIDI_SUBTYPE_MS_DESCRIPTOR_UNDEFINED 0x00
|
||||
#define USB_MIDI_SUBTYPE_MS_HEADER 0x01
|
||||
#define USB_MIDI_SUBTYPE_MIDI_IN_JACK 0x02
|
||||
#define USB_MIDI_SUBTYPE_MIDI_OUT_JACK 0x03
|
||||
#define USB_MIDI_SUBTYPE_MIDI_ELEMENT 0x04
|
||||
|
||||
/* Appendix A.2: MS Class-Specific Endpoint Descriptor Subtypes */
|
||||
#define USB_MIDI_SUBTYPE_DESCRIPTOR_UNDEFINED 0x00
|
||||
#define USB_MIDI_SUBTYPE_MS_GENERAL 0x01
|
||||
|
||||
/* Appendix A.3: MS MIDI IN and OUT Jack types */
|
||||
#define USB_MIDI_JACK_TYPE_UNDEFINED 0x00
|
||||
#define USB_MIDI_JACK_TYPE_EMBEDDED 0x01
|
||||
#define USB_MIDI_JACK_TYPE_EXTERNAL 0x02
|
||||
|
||||
/* Appendix A.5.1 Endpoint Control Selectors */
|
||||
#define USB_MIDI_EP_CONTROL_UNDEFINED 0x00
|
||||
#define USB_MIDI_ASSOCIATION_CONTROL 0x01
|
||||
|
||||
/* Table 6-2: Class-Specific MS Interface Header Descriptor */
|
||||
struct usb_midi_header_descriptor {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint16_t bcdMSC;
|
||||
uint16_t wTotalLength;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6-3: MIDI IN Jack Descriptor */
|
||||
struct usb_midi_in_jack_descriptor {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bJackType;
|
||||
uint8_t bJackID;
|
||||
uint8_t iJack;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6-4: MIDI OUT Jack Descriptor (head) */
|
||||
struct usb_midi_out_jack_descriptor_head {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bJackType;
|
||||
uint8_t bJackID;
|
||||
uint8_t bNrInputPins;
|
||||
/* ... */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6.4: MIDI OUT Jack Descriptor (body) */
|
||||
struct usb_midi_out_jack_descriptor_body {
|
||||
/* ... */
|
||||
uint8_t baSourceID;
|
||||
uint8_t baSourcePin;
|
||||
/* ... */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6.4: MIDI OUT Jack Descriptor (tail) */
|
||||
struct usb_midi_out_jack_descriptor_tail {
|
||||
/* ... */
|
||||
uint8_t iJack;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6.4: MIDI OUT Jack Descriptor (single)
|
||||
*
|
||||
* This structure is a convenience covering the (normal) case where
|
||||
* there is only one input pin.
|
||||
*/
|
||||
struct usb_midi_out_jack_descriptor {
|
||||
struct usb_midi_out_jack_descriptor_head head;
|
||||
struct usb_midi_out_jack_descriptor_body source[1];
|
||||
struct usb_midi_out_jack_descriptor_tail tail;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6-5: MIDI Element Descriptor (head) */
|
||||
struct usb_midi_element_descriptor_head {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bElementID;
|
||||
uint8_t bNrInputPins;
|
||||
/* ... */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6-5: MIDI Element Descriptor (body) */
|
||||
struct usb_midi_element_descriptor_body {
|
||||
/* ... */
|
||||
uint8_t baSourceID;
|
||||
uint8_t baSourcePin;
|
||||
/* ... */
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6-5: MIDI Element Descriptor (tail) */
|
||||
struct usb_midi_element_descriptor_tail {
|
||||
/* ... */
|
||||
uint8_t bNrOutputPins;
|
||||
uint8_t bInTerminalLink;
|
||||
uint8_t bOutTerminalLink;
|
||||
uint8_t bElCapsSize;
|
||||
uint16_t bmElementCaps; /* host cannot assume this is 16-bit but device
|
||||
can (since highest defined bitmap value in
|
||||
v1.0 is bit 11) */
|
||||
uint8_t iElement;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6-5: MIDI Element Descriptor (single)
|
||||
*
|
||||
* This structure is a convenience covering the (common) case where
|
||||
* there is only one input pin.
|
||||
*/
|
||||
struct usb_midi_element_descriptor {
|
||||
struct usb_midi_element_descriptor_head head;
|
||||
struct usb_midi_element_descriptor_body source[1];
|
||||
struct usb_midi_element_descriptor_tail tail;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6-7: Class-specific MS Bulk Data Endpoint Descriptor (head) */
|
||||
struct usb_midi_endpoint_descriptor_head {
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint8_t bDescriptorSubType;
|
||||
uint8_t bNumEmbMIDIJack;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6-7: Class-specific MS Bulk Data Endpoint Descriptor (body) */
|
||||
struct usb_midi_endpoint_descriptor_body {
|
||||
uint8_t baAssocJackID;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* Table 6.7: Class-specific MS Bulk Data Endpoint Descriptor (single)
|
||||
*
|
||||
* This structure is a convenience covering the (normal) case where
|
||||
* there is only one input pin.
|
||||
*/
|
||||
struct usb_midi_endpoint_descriptor {
|
||||
struct usb_midi_endpoint_descriptor_head head;
|
||||
struct usb_midi_endpoint_descriptor_body jack[1];
|
||||
} __attribute__((packed));
|
||||
|
||||
#endif
|
||||
|
||||
/**@}*/
|
||||
@@ -0,0 +1,428 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal_console.h>
|
||||
#include <usb.h>
|
||||
#include <usb_std.h>
|
||||
|
||||
#include "usb_midi_driver.h"
|
||||
#include "cm3_usb_audio.h"
|
||||
#include "cm3_usb_midi.h"
|
||||
|
||||
// Appendix B. "Example: Simple MIDI Adapter" from "Universal Serial Bus Device Class Definition for MIDI Devices", Revision 1.0
|
||||
|
||||
#define USB_VID 0x6666
|
||||
#define USB_PID 0x5119
|
||||
|
||||
#define USB_EP0_SIZE 8
|
||||
|
||||
#define USB_MIDI_EP_SIZE 64
|
||||
#define USB_MIDI_EP_IN 0x81
|
||||
#define USB_MIDI_EP_OUT 0x01
|
||||
|
||||
#define EP_CFG_DECONFIGURE 0
|
||||
#define EP_CFG_CONFIGURE 1
|
||||
|
||||
enum {
|
||||
USB_STR_ZERO,
|
||||
USB_STR_MANUFACTURER,
|
||||
USB_STR_PRODUCT,
|
||||
USB_STR_SERIAL_NUMBER,
|
||||
};
|
||||
|
||||
/*
|
||||
B.1 Device Descriptor
|
||||
*/
|
||||
static const struct usb_device_descriptor device_descriptor = {
|
||||
.bLength = sizeof(struct usb_device_descriptor),
|
||||
.bDescriptorType = USB_DTYPE_DEVICE,
|
||||
.bcdUSB = VERSION_BCD(2, 0, 0), // was 0x0110, 1.10 - current revision of USBspecification.
|
||||
.bDeviceClass = USB_CLASS_PER_INTERFACE,
|
||||
.bDeviceSubClass = USB_SUBCLASS_NONE,
|
||||
.bDeviceProtocol = USB_PROTO_NONE,
|
||||
.bMaxPacketSize0 = USB_EP0_SIZE,
|
||||
.idVendor = USB_VID,
|
||||
.idProduct = USB_PID,
|
||||
.bcdDevice = VERSION_BCD(1, 0, 0),
|
||||
.iManufacturer = USB_STR_MANUFACTURER,
|
||||
.iProduct = USB_STR_PRODUCT,
|
||||
.iSerialNumber = USB_STR_SERIAL_NUMBER,
|
||||
.bNumConfigurations = 1,
|
||||
};
|
||||
|
||||
struct usb_audio_header_descriptor {
|
||||
struct usb_audio_header_descriptor_head head;
|
||||
struct usb_audio_header_descriptor_body body;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct usb_midi_jacks_descriptor {
|
||||
struct usb_midi_header_descriptor header;
|
||||
struct usb_midi_in_jack_descriptor in_embedded;
|
||||
struct usb_midi_in_jack_descriptor in_external;
|
||||
struct usb_midi_out_jack_descriptor out_embedded;
|
||||
struct usb_midi_out_jack_descriptor out_external;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct MidiConfigDescriptor {
|
||||
/*
|
||||
B.2 Configuration Descriptor
|
||||
*/
|
||||
struct usb_config_descriptor config;
|
||||
|
||||
/*
|
||||
B.3 AudioControl Interface Descriptors
|
||||
|
||||
The AudioControl interface describes the device structure (audio function topology)
|
||||
and is used to manipulate the Audio Controls. This device has no audio function incorporated.
|
||||
However, the AudioControl interface is mandatory and therefore both the standard AC interface
|
||||
descriptor and the classspecific AC interface descriptor must be present.
|
||||
The class-specific AC interface descriptor only contains the header descriptor.
|
||||
*/
|
||||
// B.3.1 Standard AC Interface Descriptor
|
||||
struct usb_interface_descriptor audio_control_iface;
|
||||
// B.3.2 Class-specific AC Interface Descriptor
|
||||
struct usb_audio_header_descriptor audio_control_header;
|
||||
|
||||
/*
|
||||
B.4 MIDIStreaming Interface Descriptors
|
||||
*/
|
||||
// B.4.1 Standard MS Interface Descriptor
|
||||
struct usb_interface_descriptor midi_streaming_iface;
|
||||
// B.4.2 Class-specific MS Interface Descriptor
|
||||
// B.4.3 MIDI IN Jack Descriptor
|
||||
// B.4.4 MIDI OUT Jack Descriptor
|
||||
struct usb_midi_jacks_descriptor midi_jacks;
|
||||
|
||||
/*
|
||||
B.5 Bulk OUT Endpoint Descriptors
|
||||
*/
|
||||
// B.5.1 Standard Bulk OUT Endpoint Descriptor
|
||||
struct usb_endpoint_descriptor bulk_out;
|
||||
// B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor
|
||||
struct usb_midi_endpoint_descriptor midi_bulk_out;
|
||||
|
||||
/*
|
||||
B.6 Bulk IN Endpoint Descriptors
|
||||
*/
|
||||
// B.6.1 Standard Bulk IN Endpoint Descriptor
|
||||
struct usb_endpoint_descriptor bulk_in;
|
||||
// B.6.2 Class-specific MS Bulk IN Endpoint Descriptor
|
||||
struct usb_midi_endpoint_descriptor midi_bulk_in;
|
||||
} __attribute__((packed));
|
||||
|
||||
static const struct MidiConfigDescriptor config_descriptor = {
|
||||
.config =
|
||||
{
|
||||
.bLength = sizeof(struct usb_config_descriptor),
|
||||
.bDescriptorType = USB_DTYPE_CONFIGURATION,
|
||||
.wTotalLength = sizeof(struct MidiConfigDescriptor),
|
||||
.bNumInterfaces = 2, /* control and data */
|
||||
.bConfigurationValue = 1,
|
||||
.iConfiguration = 0,
|
||||
.bmAttributes = USB_CFG_ATTR_RESERVED,
|
||||
.bMaxPower = USB_CFG_POWER_MA(100),
|
||||
},
|
||||
.audio_control_iface =
|
||||
{
|
||||
.bLength = sizeof(struct usb_interface_descriptor),
|
||||
.bDescriptorType = USB_DTYPE_INTERFACE,
|
||||
.bInterfaceNumber = 0,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 0,
|
||||
.bInterfaceClass = USB_CLASS_AUDIO,
|
||||
.bInterfaceSubClass = USB_AUDIO_SUBCLASS_CONTROL,
|
||||
.bInterfaceProtocol = USB_PROTO_NONE,
|
||||
.iInterface = 0,
|
||||
},
|
||||
.audio_control_header =
|
||||
{
|
||||
.head =
|
||||
{
|
||||
.bLength = sizeof(struct usb_audio_header_descriptor),
|
||||
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_AUDIO_TYPE_HEADER,
|
||||
.bcdADC = VERSION_BCD(1, 0, 0),
|
||||
.wTotalLength = sizeof(struct usb_audio_header_descriptor),
|
||||
.bInCollection = 1,
|
||||
},
|
||||
.body =
|
||||
{
|
||||
.baInterfaceNr = 1,
|
||||
},
|
||||
},
|
||||
.midi_streaming_iface =
|
||||
{
|
||||
.bLength = sizeof(struct usb_interface_descriptor),
|
||||
.bDescriptorType = USB_DTYPE_INTERFACE,
|
||||
.bInterfaceNumber = 1,
|
||||
.bAlternateSetting = 0,
|
||||
.bNumEndpoints = 2,
|
||||
.bInterfaceClass = USB_CLASS_AUDIO,
|
||||
.bInterfaceSubClass = USB_AUDIO_SUBCLASS_MIDISTREAMING,
|
||||
.bInterfaceProtocol = USB_PROTO_NONE,
|
||||
.iInterface = 0,
|
||||
},
|
||||
.midi_jacks =
|
||||
{
|
||||
.header =
|
||||
{
|
||||
.bLength = sizeof(struct usb_midi_header_descriptor),
|
||||
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MS_HEADER,
|
||||
.bcdMSC = VERSION_BCD(1, 0, 0),
|
||||
.wTotalLength = sizeof(struct usb_midi_jacks_descriptor),
|
||||
},
|
||||
.in_embedded =
|
||||
{
|
||||
.bLength = sizeof(struct usb_midi_in_jack_descriptor),
|
||||
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MIDI_IN_JACK,
|
||||
.bJackType = USB_MIDI_JACK_TYPE_EMBEDDED,
|
||||
.bJackID = 0x01,
|
||||
.iJack = 0x00,
|
||||
},
|
||||
.in_external =
|
||||
{
|
||||
.bLength = sizeof(struct usb_midi_in_jack_descriptor),
|
||||
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MIDI_IN_JACK,
|
||||
.bJackType = USB_MIDI_JACK_TYPE_EXTERNAL,
|
||||
.bJackID = 0x02,
|
||||
.iJack = 0x00,
|
||||
},
|
||||
.out_embedded =
|
||||
{
|
||||
.head =
|
||||
{
|
||||
.bLength = sizeof(struct usb_midi_out_jack_descriptor),
|
||||
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MIDI_OUT_JACK,
|
||||
.bJackType = USB_MIDI_JACK_TYPE_EMBEDDED,
|
||||
.bJackID = 0x03,
|
||||
.bNrInputPins = 1,
|
||||
},
|
||||
.source[0] =
|
||||
{
|
||||
.baSourceID = 0x02,
|
||||
.baSourcePin = 0x01,
|
||||
},
|
||||
.tail =
|
||||
{
|
||||
.iJack = 0x00,
|
||||
},
|
||||
},
|
||||
.out_external =
|
||||
{
|
||||
.head =
|
||||
{
|
||||
.bLength = sizeof(struct usb_midi_out_jack_descriptor),
|
||||
.bDescriptorType = USB_AUDIO_DT_CS_INTERFACE,
|
||||
.bDescriptorSubtype = USB_MIDI_SUBTYPE_MIDI_OUT_JACK,
|
||||
.bJackType = USB_MIDI_JACK_TYPE_EXTERNAL,
|
||||
.bJackID = 0x04,
|
||||
.bNrInputPins = 1,
|
||||
},
|
||||
.source[0] =
|
||||
{
|
||||
.baSourceID = 0x01,
|
||||
.baSourcePin = 0x01,
|
||||
},
|
||||
.tail =
|
||||
{
|
||||
.iJack = 0x00,
|
||||
},
|
||||
},
|
||||
},
|
||||
.bulk_out =
|
||||
{
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DTYPE_ENDPOINT,
|
||||
.bEndpointAddress = USB_MIDI_EP_OUT,
|
||||
.bmAttributes = USB_EPTYPE_BULK,
|
||||
.wMaxPacketSize = USB_MIDI_EP_SIZE,
|
||||
.bInterval = 0,
|
||||
},
|
||||
.midi_bulk_out =
|
||||
{
|
||||
.head =
|
||||
{
|
||||
.bLength = sizeof(struct usb_midi_endpoint_descriptor),
|
||||
.bDescriptorType = USB_AUDIO_DT_CS_ENDPOINT,
|
||||
.bDescriptorSubType = USB_MIDI_SUBTYPE_MS_GENERAL,
|
||||
.bNumEmbMIDIJack = 1,
|
||||
},
|
||||
.jack[0] =
|
||||
{
|
||||
.baAssocJackID = 0x01,
|
||||
},
|
||||
},
|
||||
.bulk_in =
|
||||
{
|
||||
.bLength = sizeof(struct usb_endpoint_descriptor),
|
||||
.bDescriptorType = USB_DTYPE_ENDPOINT,
|
||||
.bEndpointAddress = USB_MIDI_EP_IN,
|
||||
.bmAttributes = USB_EPTYPE_BULK,
|
||||
.wMaxPacketSize = USB_MIDI_EP_SIZE,
|
||||
.bInterval = 0,
|
||||
},
|
||||
.midi_bulk_in =
|
||||
{
|
||||
.head =
|
||||
{
|
||||
.bLength = sizeof(struct usb_midi_endpoint_descriptor),
|
||||
.bDescriptorType = USB_AUDIO_DT_CS_ENDPOINT,
|
||||
.bDescriptorSubType = USB_MIDI_SUBTYPE_MS_GENERAL,
|
||||
.bNumEmbMIDIJack = 1,
|
||||
},
|
||||
.jack[0] =
|
||||
{
|
||||
.baAssocJackID = 0x03,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static const struct usb_string_descriptor dev_manufacturer_string =
|
||||
USB_STRING_DESC("Flipper Devices Inc.");
|
||||
|
||||
static const struct usb_string_descriptor dev_product_string =
|
||||
USB_STRING_DESC("Flipper MIDI Device");
|
||||
|
||||
static const struct usb_string_descriptor dev_serial_number_string =
|
||||
USB_STRING_DESC("Serial Number");
|
||||
|
||||
static void midi_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx);
|
||||
static void midi_deinit(usbd_device* dev);
|
||||
static void midi_on_wakeup(usbd_device* dev);
|
||||
static void midi_on_suspend(usbd_device* dev);
|
||||
static usbd_respond midi_ep_config(usbd_device* dev, uint8_t cfg);
|
||||
static usbd_respond midi_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
|
||||
|
||||
FuriHalUsbInterface midi_usb_interface = {
|
||||
.init = midi_init,
|
||||
.deinit = midi_deinit,
|
||||
.wakeup = midi_on_wakeup,
|
||||
.suspend = midi_on_suspend,
|
||||
.dev_descr = (struct usb_device_descriptor*)&device_descriptor,
|
||||
.cfg_descr = (void*)&config_descriptor,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
usbd_device* dev;
|
||||
MidiRxCallback rx_callback;
|
||||
void* context;
|
||||
FuriSemaphore* semaphore_tx;
|
||||
bool connected;
|
||||
} MidiUsb;
|
||||
|
||||
static MidiUsb midi_usb;
|
||||
|
||||
void midi_usb_set_context(void* context) {
|
||||
midi_usb.context = context;
|
||||
}
|
||||
|
||||
void midi_usb_set_rx_callback(MidiRxCallback callback) {
|
||||
midi_usb.rx_callback = callback;
|
||||
}
|
||||
|
||||
size_t midi_usb_rx(uint8_t* buffer, size_t size) {
|
||||
size_t len = usbd_ep_read(midi_usb.dev, USB_MIDI_EP_OUT, buffer, size);
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t midi_usb_tx(uint8_t* buffer, uint8_t size) {
|
||||
if((midi_usb.semaphore_tx == NULL) || (midi_usb.connected == false)) return 0;
|
||||
|
||||
furi_check(furi_semaphore_acquire(midi_usb.semaphore_tx, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
if(midi_usb.connected) {
|
||||
int32_t len = usbd_ep_write(midi_usb.dev, USB_MIDI_EP_IN, buffer, size);
|
||||
return len;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void midi_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
|
||||
UNUSED(intf);
|
||||
UNUSED(ctx);
|
||||
|
||||
midi_usb_interface.str_manuf_descr = (void*)&dev_manufacturer_string;
|
||||
midi_usb_interface.str_prod_descr = (void*)&dev_product_string;
|
||||
midi_usb_interface.str_serial_descr = (void*)&dev_serial_number_string;
|
||||
midi_usb_interface.dev_descr->idVendor = USB_VID;
|
||||
midi_usb_interface.dev_descr->idProduct = USB_PID;
|
||||
|
||||
midi_usb.dev = dev;
|
||||
if(midi_usb.semaphore_tx == NULL) midi_usb.semaphore_tx = furi_semaphore_alloc(1, 1);
|
||||
|
||||
usbd_reg_config(dev, midi_ep_config);
|
||||
usbd_reg_control(dev, midi_control);
|
||||
|
||||
usbd_connect(dev, true);
|
||||
}
|
||||
|
||||
static void midi_deinit(usbd_device* dev) {
|
||||
midi_usb.connected = false;
|
||||
midi_usb.dev = NULL;
|
||||
furi_semaphore_free(midi_usb.semaphore_tx);
|
||||
|
||||
usbd_reg_config(dev, NULL);
|
||||
usbd_reg_control(dev, NULL);
|
||||
}
|
||||
|
||||
static void midi_on_wakeup(usbd_device* dev) {
|
||||
UNUSED(dev);
|
||||
if(!midi_usb.connected) {
|
||||
midi_usb.connected = true;
|
||||
}
|
||||
}
|
||||
|
||||
static void midi_on_suspend(usbd_device* dev) {
|
||||
UNUSED(dev);
|
||||
if(midi_usb.connected) {
|
||||
midi_usb.connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void midi_tx_rx(usbd_device* dev, uint8_t event, uint8_t ep) {
|
||||
UNUSED(dev);
|
||||
UNUSED(ep);
|
||||
|
||||
switch(event) {
|
||||
case usbd_evt_eptx:
|
||||
furi_semaphore_release(midi_usb.semaphore_tx);
|
||||
break;
|
||||
case usbd_evt_eprx:
|
||||
if(midi_usb.rx_callback != NULL) {
|
||||
midi_usb.rx_callback(midi_usb.context);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static usbd_respond midi_ep_config(usbd_device* dev, uint8_t cfg) {
|
||||
switch(cfg) {
|
||||
case EP_CFG_DECONFIGURE:
|
||||
usbd_ep_deconfig(dev, USB_MIDI_EP_OUT);
|
||||
usbd_ep_deconfig(dev, USB_MIDI_EP_IN);
|
||||
usbd_reg_endpoint(dev, USB_MIDI_EP_OUT, NULL);
|
||||
usbd_reg_endpoint(dev, USB_MIDI_EP_IN, NULL);
|
||||
return usbd_ack;
|
||||
case EP_CFG_CONFIGURE:
|
||||
usbd_ep_config(dev, USB_MIDI_EP_OUT, USB_EPTYPE_BULK, USB_MIDI_EP_SIZE);
|
||||
usbd_ep_config(dev, USB_MIDI_EP_IN, USB_EPTYPE_BULK, USB_MIDI_EP_SIZE);
|
||||
usbd_reg_endpoint(dev, USB_MIDI_EP_OUT, midi_tx_rx);
|
||||
usbd_reg_endpoint(dev, USB_MIDI_EP_IN, midi_tx_rx);
|
||||
return usbd_ack;
|
||||
default:
|
||||
return usbd_fail;
|
||||
}
|
||||
}
|
||||
|
||||
static usbd_respond midi_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
|
||||
UNUSED(dev);
|
||||
UNUSED(req);
|
||||
UNUSED(callback);
|
||||
|
||||
return usbd_fail;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <furi_hal_usb.h>
|
||||
|
||||
extern FuriHalUsbInterface midi_usb_interface;
|
||||
|
||||
typedef void (*MidiRxCallback)(void* context);
|
||||
|
||||
void midi_usb_set_context(void* context);
|
||||
|
||||
void midi_usb_set_rx_callback(MidiRxCallback callback);
|
||||
|
||||
size_t midi_usb_rx(uint8_t* buffer, size_t size);
|
||||
|
||||
size_t midi_usb_tx(uint8_t* buffer, uint8_t size);
|
||||
Reference in New Issue
Block a user