midi_template.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usb_midi.h"
  8. #define MIDI_OUT_EP 0x02
  9. #define MIDI_IN_EP 0x81
  10. #define USBD_VID 0x0d28
  11. #define USBD_PID 0x0404
  12. #define USBD_MAX_POWER 100
  13. #define USBD_LANGID_STRING 1033
  14. #define USB_CONFIG_SIZE (9 + 9 + 9 + 9 + 7 + MIDI_SIZEOF_JACK_DESC + 9 + 5 + 9 + 5)
  15. #ifdef CONFIG_USB_HS
  16. #define MIDI_EP_MPS 512
  17. #else
  18. #define MIDI_EP_MPS 64
  19. #endif
  20. const uint8_t midi_descriptor[] = {
  21. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01),
  22. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  23. // Standard AC Interface Descriptor
  24. 0x09,
  25. 0x04,
  26. 0x00,
  27. 0x00,
  28. 0x00,
  29. 0x01,
  30. 0x01,
  31. 0x00,
  32. 0x00,
  33. // Class-specific AC Interface Descriptor
  34. 0x09,
  35. 0x24,
  36. 0x01,
  37. 0x00,
  38. 0x01,
  39. 0x09,
  40. 0x00,
  41. 0x01,
  42. 0x01,
  43. // MIDIStreaming Interface Descriptors
  44. 0x09,
  45. 0x04,
  46. 0x01,
  47. 0x00,
  48. 0x02,
  49. 0x01,
  50. 0x03,
  51. 0x00,
  52. 0x00,
  53. // Class-Specific MS Interface Header Descriptor
  54. 0x07,
  55. 0x24,
  56. 0x01,
  57. 0x00,
  58. 0x01,
  59. WBVAL(65),
  60. // MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, 0x01),
  61. // MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, 0x02),
  62. // MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, 0x03, 0x02),
  63. // MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, 0x04, 0x01),
  64. MIDI_JACK_DESCRIPTOR_INIT(0x01),
  65. // OUT endpoint descriptor
  66. 0x09, 0x05, MIDI_OUT_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  67. 0x05, 0x25, 0x01, 0x01, 0x01,
  68. // IN endpoint descriptor
  69. 0x09, 0x05, MIDI_IN_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  70. 0x05, 0x25, 0x01, 0x01, 0x03,
  71. ///////////////////////////////////////
  72. /// string0 descriptor
  73. ///////////////////////////////////////
  74. USB_LANGID_INIT(USBD_LANGID_STRING),
  75. ///////////////////////////////////////
  76. /// string1 descriptor
  77. ///////////////////////////////////////
  78. 0x14, /* bLength */
  79. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  80. 'C', 0x00, /* wcChar0 */
  81. 'h', 0x00, /* wcChar1 */
  82. 'e', 0x00, /* wcChar2 */
  83. 'r', 0x00, /* wcChar3 */
  84. 'r', 0x00, /* wcChar4 */
  85. 'y', 0x00, /* wcChar5 */
  86. 'U', 0x00, /* wcChar6 */
  87. 'S', 0x00, /* wcChar7 */
  88. 'B', 0x00, /* wcChar8 */
  89. ///////////////////////////////////////
  90. /// string2 descriptor
  91. ///////////////////////////////////////
  92. 0x28, /* bLength */
  93. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  94. 'C', 0x00, /* wcChar0 */
  95. 'h', 0x00, /* wcChar1 */
  96. 'e', 0x00, /* wcChar2 */
  97. 'r', 0x00, /* wcChar3 */
  98. 'r', 0x00, /* wcChar4 */
  99. 'y', 0x00, /* wcChar5 */
  100. 'U', 0x00, /* wcChar6 */
  101. 'S', 0x00, /* wcChar7 */
  102. 'B', 0x00, /* wcChar8 */
  103. ' ', 0x00, /* wcChar9 */
  104. 'M', 0x00, /* wcChar10 */
  105. 'I', 0x00, /* wcChar11 */
  106. 'D', 0x00, /* wcChar12 */
  107. 'I', 0x00, /* wcChar13 */
  108. ' ', 0x00, /* wcChar14 */
  109. 'D', 0x00, /* wcChar15 */
  110. 'E', 0x00, /* wcChar16 */
  111. 'M', 0x00, /* wcChar17 */
  112. 'O', 0x00, /* wcChar18 */
  113. ///////////////////////////////////////
  114. /// string3 descriptor
  115. ///////////////////////////////////////
  116. 0x16, /* bLength */
  117. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  118. '2', 0x00, /* wcChar0 */
  119. '0', 0x00, /* wcChar1 */
  120. '2', 0x00, /* wcChar2 */
  121. '1', 0x00, /* wcChar3 */
  122. '0', 0x00, /* wcChar4 */
  123. '3', 0x00, /* wcChar5 */
  124. '1', 0x00, /* wcChar6 */
  125. '0', 0x00, /* wcChar7 */
  126. '0', 0x00, /* wcChar8 */
  127. '0', 0x00, /* wcChar9 */
  128. #ifdef CONFIG_USB_HS
  129. ///////////////////////////////////////
  130. /// device qualifier descriptor
  131. ///////////////////////////////////////
  132. 0x0a,
  133. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  134. 0x00,
  135. 0x02,
  136. 0x02,
  137. 0x02,
  138. 0x01,
  139. 0x40,
  140. 0x00,
  141. 0x00,
  142. #endif
  143. 0x00
  144. };
  145. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[MIDI_EP_MPS];
  146. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[MIDI_EP_MPS];
  147. static void usbd_event_handler(uint8_t busid, uint8_t event)
  148. {
  149. switch (event) {
  150. case USBD_EVENT_RESET:
  151. break;
  152. case USBD_EVENT_CONNECTED:
  153. break;
  154. case USBD_EVENT_DISCONNECTED:
  155. break;
  156. case USBD_EVENT_RESUME:
  157. break;
  158. case USBD_EVENT_SUSPEND:
  159. break;
  160. case USBD_EVENT_CONFIGURED:
  161. usbd_ep_start_read(busid, MIDI_OUT_EP, read_buffer, MIDI_EP_MPS);
  162. break;
  163. case USBD_EVENT_SET_REMOTE_WAKEUP:
  164. break;
  165. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. void usbd_midi_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  172. {
  173. usbd_ep_start_read(busid, MIDI_OUT_EP, read_buffer, MIDI_EP_MPS);
  174. }
  175. void usbd_midi_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  176. {
  177. }
  178. struct usbd_interface intf0;
  179. struct usbd_interface intf1;
  180. struct usbd_endpoint midi_out_ep = {
  181. .ep_addr = MIDI_OUT_EP,
  182. .ep_cb = usbd_midi_bulk_out
  183. };
  184. struct usbd_endpoint midi_in_ep = {
  185. .ep_addr = MIDI_IN_EP,
  186. .ep_cb = usbd_midi_bulk_in
  187. };
  188. void midi_init(uint8_t busid, uintptr_t reg_base)
  189. {
  190. usbd_desc_register(busid, midi_descriptor);
  191. usbd_add_interface(busid, &intf0);
  192. usbd_add_interface(busid, &intf1);
  193. usbd_add_endpoint(busid, &midi_out_ep);
  194. usbd_add_endpoint(busid, &midi_in_ep);
  195. usbd_initialize(busid, reg_base, usbd_event_handler);
  196. }