在STM32 USB HID工程上修改为USB MIDI方法

在原是STM32F103 USB HID的工程基础上改为USB MIDI设备

 

主要内容修改配置描述符 USBD_CUSTOM_HID_CfgDesc,

注意MIDI属于音频类,需要配置为Audio类,并且需要添加控制端点。

 

__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_CfgDesc[USB_CUSTOM_HID_CONFIG_DESC_SIZ] __ALIGN_END =

{

0x09, /* bLength: Configuration Descriptor size */

USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */

USB_CUSTOM_HID_CONFIG_DESC_SIZ,

/* wTotalLength: Bytes returned */

0x00,

0x02,//1         /*bNumInterfaces: 2 interface*/

0x01,         /*bConfigurationValue: Configuration value*/

0x00,         /*iConfiguration: Index of string descriptor describing

the configuration*/

0xC0,         /*bmAttributes: bus powered */

0x32,         /*MaxPower 100 mA: this current is used for detecting Vbus*/

  

/******************* Standard AC Interface Descriptor *********************/

/* 09 */

0x09,         /*bLength: Interface Descriptor size*/

USB_DESC_TYPE_INTERFACE, /*bDescriptorType: Interface descriptor type*/

0x00,         /*bInterfaceNumber: Number of Interface*/

0x00,         /*bAlternateSetting: Alternate setting*/

0x00,         /*bNumEndpoints*/

0x01,         /*bInterfaceClass: Audio*/

0x01,         /*bInterfaceSubClass : Audio Control*/

0,            /*nInterfaceProtocol*/

0,            /*iInterface: Index of string descriptor*/

 

/**************** Class-specific AC Interface Descriptor ******************/

/* 18 */

0x09,         /*bLength: Interface Descriptor size*/

0x24,         /*bDescriptorType: Class-specific interface descriptor type*/

0x01,         /*bDescriptorSubType: Header*/

0x00,         /*bcdADC: Revision of class specification - 1.0*/

0x01,

0x09,         /*wTotalLength: Total size of class specific discriptor*/

0x00,

0x01,         /*bInCollection: Number of streaming interfaces*/

0x01,         /*baInterfaceNr : MIDIStreaming interface 1 belongs to

                                this AudioControl interface*/

 

/******************* Standard MS Interface Descriptor *********************/

/* 27 */

0x09,         /*bLength: Interface Descriptor size*/

USB_DESC_TYPE_INTERFACE, /*bDescriptorType: Interface descriptor type*/

0x01,         /*bInterfaceNumber: Number of Interface*/

0x00,         /*bAlternateSetting: Alternate setting*/

0x02,         /*bNumEndpoints*/

0x01,         /*bInterfaceClass: Audio*/

0x03,         /*bInterfaceSubClass : MIDI Streaming*/

0,            /*nInterfaceProtocol*/

0,            /*iInterface: Index of string descriptor*/

/**************** Class-specific MS Interface Descriptor ******************/

/* 36 */

0x07,         /*bLength: Interface Descriptor size*/

0x24,         /*bDescriptorType: Class-specific interface descriptor type*/

0x01,         /*bDescriptorSubType: MS Header*/

0x00,         /*bcdADC: Revision of class specification*/

0x01,

0x41,         /*wTotalLength: Total size of class specific discriptor*/

0x00,

/******************* MIDI IN Jack Descriptor (Embedded) *******************/

/* 43 */

0x06,         /*bLength: Size of this descriptor*/

0x24,         /*bDescriptorType: Class-specific interface descriptor type*/

0x02,         /*bDescriptorSubType: MIDI IN Jack*/

0x01,         /*bJackType: Embedded*/

0x01,         /*bJackID: ID of this Jack*/

0x00,         /*iJack*/

/******************* MIDI IN Jack Descriptor (External) *******************/

/* 49 */

0x06,         /*bLength: Size of this descriptor*/

0x24,         /*bDescriptorType: Class-specific interface descriptor type*/

0x02,         /*bDescriptorSubType: MIDI IN Jack*/

0x02,         /*bJackType: External*/

0x02,         /*bJackID: ID of this Jack*/

0x00,         /*iJack*/

/******************* MIDI OUT Jack Descriptor (Embedded) ******************/

/* 55 */

0x09,         /*bLength: Size of this descriptor*/

0x24,         /*bDescriptorType: Class-specific interface descriptor type*/

0x03,         /*bDescriptorSubType: MIDI OUT Jack*/

0x01,         /*bJackType: Embedded*/

0x03,         /*bJackID: ID of this Jack*/

0x01,         /*bNrInputPins: Number of Input Pins of this Jack*/

0x02,         /*BaSourceID: ID of the Entry to which this Pin is connected*/

0x01,         /*BaSourceID: Output Pin number of the Entry to

                            which this Input Pin is connected*/

0x00,         /*iJack*/

/******************* MIDI OUT Jack Descriptor (External) ******************/

/* 64 */

0x09,         /*bLength: Size of this descriptor*/

0x24,         /*bDescriptorType: Class-specific interface descriptor type*/

0x03,         /*bDescriptorSubType: MIDI OUT Jack*/

0x02,         /*bJackType: External*/

0x04,         /*bJackID: ID of this Jack*/

0x01,         /*bNrInputPins: Number of Input Pins of this Jack*/

0x01,         /*BaSourceID: ID of the Entry to which this Pin is connected*/

0x01,         /*BaSourceID: Output Pin number of the Entry to

                            which this Input Pin is connected*/

0x00,         /*iJack*/

/****************** Standard Bulk OUT Endpoint Descriptor *****************/

/* 73 */

0x09,         /*bLength: Size of this descriptor*/

USB_DESC_TYPE_ENDPOINT, /*bDescriptorType: Endpoint descriptor type*/

0x01,         /*bEndpointAddress: OUT Endpoint 1*/

0x02,         /*bmAttributes: Bulk, not shared.*/

0x40,         /*wMaxPacketSize 64*/

0x00,

0x00,         /*bInterval*/

0x00,         /*bRefresh*/

0x00,         /*bSynchAddress*/

/************* Class-specific MS Bulk OUT Endpoint Descriptor *************/

/* 82 */

0x05,         /*bLength: Size of this descriptor*/

0x25,         /*bDescriptorType: Class-specific endpoint descriptor type*/

0x01,         /*bDescriptorSubType: MS General*/

0x01,         /*bNumEmbMIDIJack: Number of embedded MIDI IN Jack*/

0x01,         /*BaAssocJackID: ID of the Embedded MIDI IN Jack*/

/****************** Standard Bulk IN Endpoint Descriptor *****************/

/* 87 */

0x09,         /*bLength: Size of this descriptor*/

USB_DESC_TYPE_ENDPOINT, /*bDescriptorType: Endpoint descriptor type*/

0x81,         /*bEndpointAddress: IN Endpoint 1*/

0x02,         /*bmAttributes: Bulk, not shared.*/

0x40,         /*wMaxPacketSize 64*/

0x00,

0x00,         /*bInterval*/

0x00,         /*bRefresh*/

0x00,         /*bSynchAddress*/

/************* Class-specific MS Bulk OUT Endpoint Descriptor *************/

/* 96 */

0x05,         /*bLength: Size of this descriptor*/

0x25,         /*bDescriptorType: Class-specific endpoint descriptor type*/

0x01,         /*bDescriptorSubType: MS General*/

0x01,         /*bNumEmbMIDIJack: Number of embedded MIDI OUT Jack*/

0x03,         /*BaAssocJackID: ID of the Embedded MIDI OUT Jack*/

/* 101 */

} ;

 

修改配置描述符长度

在STM32 USB HID工程上修改为USB MIDI方法

在STM32 USB HID工程上修改为USB MIDI方法

去除原来添加的收发回环代码,

不然容易导致上位机也开了收发回环,结果只要有一帧数据,两边就传得很嗨,停不下来。

在STM32 USB HID工程上修改为USB MIDI方法

重新封装发送函数

在STM32 USB HID工程上修改为USB MIDI方法

定时发送

在STM32 USB HID工程上修改为USB MIDI方法

烧录

成功识别到MIDI设备

在STM32 USB HID工程上修改为USB MIDI方法

使用MIDI-OX打开MIDI设备,数据收发测试OK

在STM32 USB HID工程上修改为USB MIDI方法

参考midi10.pdf(Universal Serial Bus Device Class Definition for MIDI Devices)

正确发送MIDI数据有规定格式。

 

完善发送格式,MIDI-OX软件上可收到正确PC码

在STM32 USB HID工程上修改为USB MIDI方法

 

 

参考链接:

https://www.kechuang.org/t/81321