midi_template.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 AUDIO_AC_SIZ AUDIO_SIZEOF_AC_HEADER_DESC(1)
  15. #define AUDIO_MS_SIZ (7 + MIDI_SIZEOF_JACK_DESC + 9 + 5 + 9 + 5)
  16. #define USB_CONFIG_SIZE (unsigned long)(9 + \
  17. AUDIO_AC_DESCRIPTOR_LEN(1) + \
  18. MIDI_STANDARD_DESCRIPTOR_LEN + \
  19. AUDIO_MS_SIZ)
  20. #ifdef CONFIG_USB_HS
  21. #define MIDI_EP_MPS 512
  22. #else
  23. #define MIDI_EP_MPS 64
  24. #endif
  25. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  26. static const uint8_t device_descriptor[] = {
  27. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01)
  28. };
  29. static const uint8_t config_descriptor[] = {
  30. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  31. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
  32. MIDI_STANDARD_DESCRIPTOR_INIT(0x01, 0x02),
  33. MIDI_CS_HEADER_DESCRIPTOR_INIT(AUDIO_MS_SIZ),
  34. MIDI_JACK_DESCRIPTOR_INIT(0x01),
  35. // OUT endpoint descriptor
  36. 0x09, 0x05, MIDI_OUT_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  37. 0x05, 0x25, 0x01, 0x01, 0x01,
  38. // IN endpoint descriptor
  39. 0x09, 0x05, MIDI_IN_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  40. 0x05, 0x25, 0x01, 0x01, 0x03
  41. };
  42. static const uint8_t device_quality_descriptor[] = {
  43. ///////////////////////////////////////
  44. /// device qualifier descriptor
  45. ///////////////////////////////////////
  46. 0x0a,
  47. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  48. 0x00,
  49. 0x02,
  50. 0x00,
  51. 0x00,
  52. 0x00,
  53. 0x40,
  54. 0x00,
  55. 0x00,
  56. };
  57. static const char *string_descriptors[] = {
  58. (const char[]){ 0x09, 0x04 }, /* Langid */
  59. "CherryUSB", /* Manufacturer */
  60. "CherryUSB MIDI DEMO", /* Product */
  61. "2022123456", /* Serial Number */
  62. };
  63. static const uint8_t *device_descriptor_callback(uint8_t speed)
  64. {
  65. return device_descriptor;
  66. }
  67. static const uint8_t *config_descriptor_callback(uint8_t speed)
  68. {
  69. return config_descriptor;
  70. }
  71. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  72. {
  73. return device_quality_descriptor;
  74. }
  75. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  76. {
  77. if (index > 3) {
  78. return NULL;
  79. }
  80. return string_descriptors[index];
  81. }
  82. const struct usb_descriptor midi_descriptor = {
  83. .device_descriptor_callback = device_descriptor_callback,
  84. .config_descriptor_callback = config_descriptor_callback,
  85. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  86. .string_descriptor_callback = string_descriptor_callback
  87. };
  88. #else
  89. const uint8_t midi_descriptor[] = {
  90. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01),
  91. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  92. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
  93. MIDI_STANDARD_DESCRIPTOR_INIT(0x01, 0x02),
  94. MIDI_CS_HEADER_DESCRIPTOR_INIT(AUDIO_MS_SIZ),
  95. MIDI_JACK_DESCRIPTOR_INIT(0x01),
  96. // OUT endpoint descriptor
  97. 0x09, 0x05, MIDI_OUT_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  98. 0x05, 0x25, 0x01, 0x01, 0x01,
  99. // IN endpoint descriptor
  100. 0x09, 0x05, MIDI_IN_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  101. 0x05, 0x25, 0x01, 0x01, 0x03
  102. ///////////////////////////////////////
  103. /// string0 descriptor
  104. ///////////////////////////////////////
  105. USB_LANGID_INIT(USBD_LANGID_STRING),
  106. ///////////////////////////////////////
  107. /// string1 descriptor
  108. ///////////////////////////////////////
  109. 0x14, /* bLength */
  110. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  111. 'C', 0x00, /* wcChar0 */
  112. 'h', 0x00, /* wcChar1 */
  113. 'e', 0x00, /* wcChar2 */
  114. 'r', 0x00, /* wcChar3 */
  115. 'r', 0x00, /* wcChar4 */
  116. 'y', 0x00, /* wcChar5 */
  117. 'U', 0x00, /* wcChar6 */
  118. 'S', 0x00, /* wcChar7 */
  119. 'B', 0x00, /* wcChar8 */
  120. ///////////////////////////////////////
  121. /// string2 descriptor
  122. ///////////////////////////////////////
  123. 0x28, /* bLength */
  124. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  125. 'C', 0x00, /* wcChar0 */
  126. 'h', 0x00, /* wcChar1 */
  127. 'e', 0x00, /* wcChar2 */
  128. 'r', 0x00, /* wcChar3 */
  129. 'r', 0x00, /* wcChar4 */
  130. 'y', 0x00, /* wcChar5 */
  131. 'U', 0x00, /* wcChar6 */
  132. 'S', 0x00, /* wcChar7 */
  133. 'B', 0x00, /* wcChar8 */
  134. ' ', 0x00, /* wcChar9 */
  135. 'M', 0x00, /* wcChar10 */
  136. 'I', 0x00, /* wcChar11 */
  137. 'D', 0x00, /* wcChar12 */
  138. 'I', 0x00, /* wcChar13 */
  139. ' ', 0x00, /* wcChar14 */
  140. 'D', 0x00, /* wcChar15 */
  141. 'E', 0x00, /* wcChar16 */
  142. 'M', 0x00, /* wcChar17 */
  143. 'O', 0x00, /* wcChar18 */
  144. ///////////////////////////////////////
  145. /// string3 descriptor
  146. ///////////////////////////////////////
  147. 0x16, /* bLength */
  148. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  149. '2', 0x00, /* wcChar0 */
  150. '0', 0x00, /* wcChar1 */
  151. '2', 0x00, /* wcChar2 */
  152. '1', 0x00, /* wcChar3 */
  153. '0', 0x00, /* wcChar4 */
  154. '3', 0x00, /* wcChar5 */
  155. '1', 0x00, /* wcChar6 */
  156. '0', 0x00, /* wcChar7 */
  157. '0', 0x00, /* wcChar8 */
  158. '0', 0x00, /* wcChar9 */
  159. #ifdef CONFIG_USB_HS
  160. ///////////////////////////////////////
  161. /// device qualifier descriptor
  162. ///////////////////////////////////////
  163. 0x0a,
  164. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  165. 0x00,
  166. 0x02,
  167. 0x00,
  168. 0x00,
  169. 0x00,
  170. 0x40,
  171. 0x00,
  172. 0x00,
  173. #endif
  174. 0x00
  175. };
  176. #endif
  177. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[MIDI_EP_MPS];
  178. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[MIDI_EP_MPS];
  179. static void usbd_event_handler(uint8_t busid, uint8_t event)
  180. {
  181. switch (event) {
  182. case USBD_EVENT_RESET:
  183. break;
  184. case USBD_EVENT_CONNECTED:
  185. break;
  186. case USBD_EVENT_DISCONNECTED:
  187. break;
  188. case USBD_EVENT_RESUME:
  189. break;
  190. case USBD_EVENT_SUSPEND:
  191. break;
  192. case USBD_EVENT_CONFIGURED:
  193. usbd_ep_start_read(busid, MIDI_OUT_EP, read_buffer, MIDI_EP_MPS);
  194. break;
  195. case USBD_EVENT_SET_REMOTE_WAKEUP:
  196. break;
  197. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  198. break;
  199. default:
  200. break;
  201. }
  202. }
  203. void usbd_midi_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  204. {
  205. usbd_ep_start_read(busid, MIDI_OUT_EP, read_buffer, MIDI_EP_MPS);
  206. }
  207. void usbd_midi_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  208. {
  209. }
  210. struct usbd_interface intf0;
  211. struct usbd_interface intf1;
  212. struct usbd_endpoint midi_out_ep = {
  213. .ep_addr = MIDI_OUT_EP,
  214. .ep_cb = usbd_midi_bulk_out
  215. };
  216. struct usbd_endpoint midi_in_ep = {
  217. .ep_addr = MIDI_IN_EP,
  218. .ep_cb = usbd_midi_bulk_in
  219. };
  220. void midi_init(uint8_t busid, uintptr_t reg_base)
  221. {
  222. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  223. usbd_desc_register(busid, &midi_descriptor);
  224. #else
  225. usbd_desc_register(busid, midi_descriptor);
  226. #endif
  227. usbd_add_interface(busid, &intf0);
  228. usbd_add_interface(busid, &intf1);
  229. usbd_add_endpoint(busid, &midi_out_ep);
  230. usbd_add_endpoint(busid, &midi_in_ep);
  231. usbd_initialize(busid, reg_base, usbd_event_handler);
  232. }