usb_midi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _USB_MIDI_H_
  16. #define _USB_MIDI_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**\addtogroup USB_MIDI USB MIDI class
  21. * \brief Generic USB MIDI class definitions
  22. * \details This module based on
  23. * + [Universal Serial Bus Device Class Definition for MIDI Devices](https://www.usb.org/sites/default/files/midi10.pdf)
  24. * @{ */
  25. /**\name USB MIDI Class codes
  26. * @{ */
  27. #define USB_CLASS_MIDI 0x01U /**<\brief MIDI Device class */
  28. #define USB_MIDI_SUBCLASS_CONTROL 0x01U /**<\brief Data Interface class */
  29. /** @} */
  30. #define USB_MIDI_SET_CUR 0x01U /**<\brief Set current setting attribute.*/
  31. #define USB_MIDI_SET_MIN 0x02U /**<\brief Set minimum setting attribute.*/
  32. #define USB_MIDI_SET_MAX 0x03U /**<\brief Set maximum setting attribute.*/
  33. #define USB_MIDI_SET_RES 0x04U /**<\brief Set resolution attribute.*/
  34. #define USB_MIDI_SET_MEM 0x05U /**<\brief Set memory space attribute.*/
  35. #define USB_MIDI_GET_CUR 0x81U /**<\brief Get current setting attribute.*/
  36. #define USB_MIDI_GET_MIN 0x82U /**<\brief Get minimum setting attribute.*/
  37. #define USB_MIDI_GET_MAX 0x83U /**<\brief Get maximum setting attribute.*/
  38. #define USB_MIDI_GET_RES 0x84U /**<\brief Get resolution attribute.*/
  39. #define USB_MIDI_GET_MEM 0x85U /**<\brief Upload a parameter block from a
  40. * particular Entity of the USB-MIDI function.*/
  41. #define USB_MIDI_GET_STAT 0xFFU /**<\brief Retrieve status information from an
  42. * Entity within the USB-MIDI function.*/
  43. /**\name USB MIDI descriptor types
  44. * @{ */
  45. #define USB_DTYPE_MIDI_UNDEFINED 0x00U /**<\brief */
  46. #define USB_DTYPE_MIDI_HEADER 0x01U /**<\see \ref usb_midi_header_desc */
  47. #define USB_DTYPE_MIDI_IN_JACK 0x02U /**<\see \ref usb_midi_jack_in_desc */
  48. #define USB_DTYPE_MIDI_OUT_JACK 0x03U /**<\see \ref usb_midi_jack_out_desc */
  49. #define USB_DTYPE_MIDI_ELEMENT 0x04U /**<\see \ref usb_midi_element_desc */
  50. #define USB_DTYPE_MIDI_MS_GENERAL 0x01U /**<\brief */
  51. /** @} */
  52. /**\name USB MIDI Jack types
  53. * @{ */
  54. #define USB_MIDI_JACK_UNDEFINED 0x00U /**<\brief Declares undefined Jack */
  55. #define USB_MIDI_JACK_EMBEDDED 0x01U /**<\brief Declares embedded Jack */
  56. #define USB_MIDI_JACK_EXTERNL 0x02U /**<\brief Declares external Jack */
  57. /** @} */
  58. /**\name USB MIDI Element capabilities
  59. * @{ */
  60. #define USB_MIDI_ELEMENT_CUSTOM 0x0001U /**<\brief The Element has unique, undefined features.*/
  61. #define USB_MIDI_ELEMENT_CLOCK 0x0002U /**<\brief MIDI CLOCK messages are supported.*/
  62. #define USB_MIDI_ELEMENT_TIMEDODE 0x0004U /**<\brief Synchronization features are supported.*/
  63. #define USB_MIDI_ELEMENT_MMC 0x0008U /**<\brief MMC messages are supported.*/
  64. #define USB_MIDI_ELEMENT_GM1 0x0010U /**<\brief General MIDI System Level 1 compatibility as
  65. * defined by the MIDI Manufacturers Association.*/
  66. #define USB_MIDI_ELEMENT_GM2 0x0020U /**<\brief General MIDI System Level 2 compatibility as
  67. * defined by the MIDI Manufacturers Association.*/
  68. #define USB_MIDI_ELEMENT_GS 0x0040U /**<\brief GS Format compatibility as defined by Roland Corporation.*/
  69. #define USB_MIDI_ELEMENT_XG 0x0080U /**<\brief XG compatibility as defined by Yamaha Corporation.*/
  70. #define USB_MIDI_ELEMENT_EFX 0x0100U /**<\brief The Element provides an audio effects processor controlled by USB.*/
  71. #define USB_MIDI_ELEMENT_PATCH 0x0200U /**<\brief The Element provides an internal MIDI Patcher or Router.*/
  72. #define USB_MIDI_ELEMENT_DLS1 0x0400U /**<\brief Downloadable Sounds Standard Level 1 compatibility as
  73. * defined by the MIDI Manufacturers Association.*/
  74. #define USB_MIDI_ELEMENT_DLS2 0x0800U /**<\brief Downloadable Sounds Standard Level 1 compatibility as
  75. * defined by the MIDI Manufacturers Association.*/
  76. /** @} */
  77. /**\name USB MIDI Code index numbers
  78. * @{ */
  79. #define USB_MIDI_CIN_MISC 0x00U /**<\brief Miscellaneous function codes. Reserved for future extensions.*/
  80. #define USB_MIDI_CIN_CABLE_EVT 0x01U /**<\brief Cable events. Reserved for future expansion.*/
  81. #define USB_MIDI_CIN_2B_MSG 0x02U /**<\brief Two-byte System Common messages like MTC, SongSelect, etc.*/
  82. #define USB_MIDI_CIN_3B_MSG 0x03U /**<\brief Three-byte System Common messages like SPP, etc.*/
  83. #define USB_MIDI_CIN_SYSEX 0x04U /**<\brief SysEx starts or continues.*/
  84. #define USB_MIDI_CIN_1B_MSG 0x05U /**<\brief Single-byte System Common Message.*/
  85. #define USB_MIDI_CIN_SYSEX_END1 0x05U /**<\brief SysEx ends with following single byte.*/
  86. #define USB_MIDI_CIN_SYSEX_END2 0x06U /**<\brief SysEx ends with following two bytes.*/
  87. #define USB_MIDI_CIN_SYSEX_END3 0x07U /**<\brief SysEx ends with following three bytes.*/
  88. #define USB_MIDI_CIN_NOTE_OFF 0x08U /**<\brief Note-off.*/
  89. #define USB_MIDI_CIN_NOTE_ON 0x09U /**<\brief Note-on.*/
  90. #define USB_MIDI_CIN_PKEY_PRESS 0x0AU /**<\brief Poly-Key press.*/
  91. #define USB_MIDI_CIN_CTRL_CHANGE 0x0BU /**<\brief Control change.*/
  92. #define USB_MIDI_CIN_PRGM_CHANGE 0x0CU /**<\brief Program change.*/
  93. #define USB_MIDI_CIN_CHNL_PRESS 0x0DU /**<\brief Channel pressure.*/
  94. #define USB_MIDI_CIN_PBEND_CHANGE 0x0EU /**<\brief PitchBend change.*/
  95. #define USB_MIDI_CIN_SINGLE_BYTE 0x0FU /**<\brief Single byte.*/
  96. /** @} */
  97. /**\brief Class-Specific MS Interface Header Descriptor
  98. * \details Header Functional Descriptor marks the beginning of the concatenated set of functional
  99. * descriptors for the interface. */
  100. struct usb_midi_header_desc {
  101. uint8_t bLength; /**<\brief Size of this descriptor in bytes.*/
  102. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  103. uint8_t bDescriptorSubType; /**<\brief MS_HEADER descriptor subtype.*/
  104. uint16_t bcdMSC; /**<\brief MIDIStreaming SubClass Specification Release Number.*/
  105. uint16_t wTotalLength; /**<\brief Total number of bytes returned for the class-specific
  106. * MIDIStreaming interface descriptor.*/
  107. } __attribute__ ((packed));
  108. /**\brief MIDI IN Jack Descriptor */
  109. struct usb_midi_jack_in_desc {
  110. uint8_t bLength; /**<\brief Size of this descriptor in bytes.*/
  111. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  112. uint8_t bDescriptorSubType; /**<\brief MIDI_IN_JACK descriptor subtype.*/
  113. uint8_t bJackType; /**<\brief EMBEDDED or EXTERNAL.*/
  114. uint8_t bJackID; /**<\brief Constant uniquely identifying the MIDI IN Jack within
  115. * the USB-MIDI function.*/
  116. uint8_t iJack; /**<\brief Index, describing the MIDI IN Jack.*/
  117. } __attribute__ ((packed));
  118. struct usb_midi_source_pin {
  119. uint8_t bSourceID;
  120. uint8_t bSourcePin;
  121. } __attribute__ ((packed));
  122. /**\brief MIDI OUT Jack Descriptor */
  123. struct usb_midi_jack_out_desc {
  124. uint8_t bLength; /**<\brief Size of this descriptor in bytes.*/
  125. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  126. uint8_t bDescriptorSubType; /**<\brief MIDI_OUT_JACK descriptor subtype.*/
  127. uint8_t bJackType; /**<\brief EMBEDDED or EXTERNAL.*/
  128. uint8_t bJackID; /**<\brief Constant uniquely identifying the MIDI OUT Jack within
  129. * the USB-MIDI function.*/
  130. uint8_t bNrInputPins; /**<\brief Number of Input Pins of this MIDI OUT Jack */
  131. uint8_t baSourceID;
  132. uint8_t baSourcePin;
  133. uint8_t iJack; /**<\brief Index of a string descriptor, describing the MIDI OUT Jack.*/
  134. } __attribute__ ((packed));
  135. struct usb_midi_element_desc {
  136. uint8_t bLength; /**<\brief Size of this descriptor in bytes.*/
  137. uint8_t bDescriptorType; /**<\brief CS_INTERFACE descriptor type.*/
  138. uint8_t bDescriptorSubType; /**<\brief ELEMENT descriptor subtype.*/
  139. uint8_t bElementID;
  140. uint8_t bNrInputPins;
  141. uint8_t baSourceID;
  142. uint8_t baSourcePin;
  143. uint8_t bNrOutputPins;
  144. uint8_t bInTerminalLink;
  145. uint8_t bOutTerminalLink;
  146. uint8_t bElCapsSize;
  147. uint16_t bmElementCaps;
  148. uint8_t iElement;
  149. } __attribute__ ((packed));
  150. struct usb_midi_std_ep_descriptor {
  151. uint8_t bLength; /**<\brief Size of the descriptor, in bytes. */
  152. uint8_t bDescriptorType; /**<\brief Endpoint descriptor.*/
  153. uint8_t bEndpointAddress; /**<\brief Logical address of the endpoint within the device for
  154. * the current configuration, including direction mask. */
  155. uint8_t bmAttributes; /**<\brief Endpoint attributes, \ref USB_ENDPOINT_DEF. */
  156. uint16_t wMaxPacketSize; /**<\brief Size of the endpoint bank, in bytes. This indicates the
  157. * maximum packet size that the endpoint can receive at a time. */
  158. uint8_t bInterval; /**<\brief Polling interval in milliseconds for the endpoint.
  159. * Must be reset to 0.*/
  160. uint8_t bRefresh; /**<\brief Reset to 0.*/
  161. uint8_t bSyncAddress; /**<\brief The address o f the endpoint used to communicate
  162. * synchronization information if required by this endpoint.
  163. * Reset to zero.*/
  164. } __attribute__((packed));
  165. struct usb_midi_class_ep_descriptor {
  166. uint8_t bLength; /**<\brief Size of the descriptor, in bytes.*/
  167. uint8_t bDescriptorType; /**<\brief CS_ENDPOINT descriptor.*/
  168. uint8_t bDescriptorSubType; /**<\brief USB_DTYPE_MIDI_EP_GENERAL subtype.*/
  169. uint8_t bNumEmbMIDIJack; /**<\brief Number of Embedded MIDI Jacks.*/
  170. uint8_t baAssocJackID[]; /**<\brief Embedded jacks ID's */
  171. } __attribute__((packed));
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. #endif /* _USB_MIDI_H_ */