midi_template.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  21. static const uint8_t device_descriptor[] = {
  22. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01)
  23. };
  24. static const uint8_t config_descriptor[] = {
  25. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  26. // Standard AC Interface Descriptor
  27. 0x09,
  28. 0x04,
  29. 0x00,
  30. 0x00,
  31. 0x00,
  32. 0x01,
  33. 0x01,
  34. 0x00,
  35. 0x00,
  36. // Class-specific AC Interface Descriptor
  37. 0x09,
  38. 0x24,
  39. 0x01,
  40. 0x00,
  41. 0x01,
  42. 0x09,
  43. 0x00,
  44. 0x01,
  45. 0x01,
  46. // MIDIStreaming Interface Descriptors
  47. 0x09,
  48. 0x04,
  49. 0x01,
  50. 0x00,
  51. 0x02,
  52. 0x01,
  53. 0x03,
  54. 0x00,
  55. 0x00,
  56. // Class-Specific MS Interface Header Descriptor
  57. 0x07,
  58. 0x24,
  59. 0x01,
  60. 0x00,
  61. 0x01,
  62. WBVAL(65),
  63. // MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, 0x01),
  64. // MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, 0x02),
  65. // MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, 0x03, 0x02),
  66. // MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, 0x04, 0x01),
  67. MIDI_JACK_DESCRIPTOR_INIT(0x01),
  68. // OUT endpoint descriptor
  69. 0x09, 0x05, MIDI_OUT_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  70. 0x05, 0x25, 0x01, 0x01, 0x01,
  71. // IN endpoint descriptor
  72. 0x09, 0x05, MIDI_IN_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  73. 0x05, 0x25, 0x01, 0x01, 0x03
  74. };
  75. static const uint8_t device_quality_descriptor[] = {
  76. ///////////////////////////////////////
  77. /// device qualifier descriptor
  78. ///////////////////////////////////////
  79. 0x0a,
  80. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  81. 0x00,
  82. 0x02,
  83. 0x00,
  84. 0x00,
  85. 0x00,
  86. 0x40,
  87. 0x00,
  88. 0x00,
  89. };
  90. static const char *string_descriptors[] = {
  91. (const char[]){ 0x09, 0x04 }, /* Langid */
  92. "CherryUSB", /* Manufacturer */
  93. "CherryUSB MIDI DEMO", /* Product */
  94. "2022123456", /* Serial Number */
  95. };
  96. static const uint8_t *device_descriptor_callback(uint8_t speed)
  97. {
  98. return device_descriptor;
  99. }
  100. static const uint8_t *config_descriptor_callback(uint8_t speed)
  101. {
  102. return config_descriptor;
  103. }
  104. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  105. {
  106. return device_quality_descriptor;
  107. }
  108. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  109. {
  110. if (index > 3) {
  111. return NULL;
  112. }
  113. return string_descriptors[index];
  114. }
  115. const struct usb_descriptor midi_descriptor = {
  116. .device_descriptor_callback = device_descriptor_callback,
  117. .config_descriptor_callback = config_descriptor_callback,
  118. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  119. .string_descriptor_callback = string_descriptor_callback
  120. };
  121. #else
  122. const uint8_t midi_descriptor[] = {
  123. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01),
  124. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  125. // Standard AC Interface Descriptor
  126. 0x09,
  127. 0x04,
  128. 0x00,
  129. 0x00,
  130. 0x00,
  131. 0x01,
  132. 0x01,
  133. 0x00,
  134. 0x00,
  135. // Class-specific AC Interface Descriptor
  136. 0x09,
  137. 0x24,
  138. 0x01,
  139. 0x00,
  140. 0x01,
  141. 0x09,
  142. 0x00,
  143. 0x01,
  144. 0x01,
  145. // MIDIStreaming Interface Descriptors
  146. 0x09,
  147. 0x04,
  148. 0x01,
  149. 0x00,
  150. 0x02,
  151. 0x01,
  152. 0x03,
  153. 0x00,
  154. 0x00,
  155. // Class-Specific MS Interface Header Descriptor
  156. 0x07,
  157. 0x24,
  158. 0x01,
  159. 0x00,
  160. 0x01,
  161. WBVAL(65),
  162. // MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, 0x01),
  163. // MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, 0x02),
  164. // MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, 0x03, 0x02),
  165. // MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, 0x04, 0x01),
  166. MIDI_JACK_DESCRIPTOR_INIT(0x01),
  167. // OUT endpoint descriptor
  168. 0x09, 0x05, MIDI_OUT_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  169. 0x05, 0x25, 0x01, 0x01, 0x01,
  170. // IN endpoint descriptor
  171. 0x09, 0x05, MIDI_IN_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  172. 0x05, 0x25, 0x01, 0x01, 0x03,
  173. ///////////////////////////////////////
  174. /// string0 descriptor
  175. ///////////////////////////////////////
  176. USB_LANGID_INIT(USBD_LANGID_STRING),
  177. ///////////////////////////////////////
  178. /// string1 descriptor
  179. ///////////////////////////////////////
  180. 0x14, /* bLength */
  181. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  182. 'C', 0x00, /* wcChar0 */
  183. 'h', 0x00, /* wcChar1 */
  184. 'e', 0x00, /* wcChar2 */
  185. 'r', 0x00, /* wcChar3 */
  186. 'r', 0x00, /* wcChar4 */
  187. 'y', 0x00, /* wcChar5 */
  188. 'U', 0x00, /* wcChar6 */
  189. 'S', 0x00, /* wcChar7 */
  190. 'B', 0x00, /* wcChar8 */
  191. ///////////////////////////////////////
  192. /// string2 descriptor
  193. ///////////////////////////////////////
  194. 0x28, /* bLength */
  195. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  196. 'C', 0x00, /* wcChar0 */
  197. 'h', 0x00, /* wcChar1 */
  198. 'e', 0x00, /* wcChar2 */
  199. 'r', 0x00, /* wcChar3 */
  200. 'r', 0x00, /* wcChar4 */
  201. 'y', 0x00, /* wcChar5 */
  202. 'U', 0x00, /* wcChar6 */
  203. 'S', 0x00, /* wcChar7 */
  204. 'B', 0x00, /* wcChar8 */
  205. ' ', 0x00, /* wcChar9 */
  206. 'M', 0x00, /* wcChar10 */
  207. 'I', 0x00, /* wcChar11 */
  208. 'D', 0x00, /* wcChar12 */
  209. 'I', 0x00, /* wcChar13 */
  210. ' ', 0x00, /* wcChar14 */
  211. 'D', 0x00, /* wcChar15 */
  212. 'E', 0x00, /* wcChar16 */
  213. 'M', 0x00, /* wcChar17 */
  214. 'O', 0x00, /* wcChar18 */
  215. ///////////////////////////////////////
  216. /// string3 descriptor
  217. ///////////////////////////////////////
  218. 0x16, /* bLength */
  219. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  220. '2', 0x00, /* wcChar0 */
  221. '0', 0x00, /* wcChar1 */
  222. '2', 0x00, /* wcChar2 */
  223. '1', 0x00, /* wcChar3 */
  224. '0', 0x00, /* wcChar4 */
  225. '3', 0x00, /* wcChar5 */
  226. '1', 0x00, /* wcChar6 */
  227. '0', 0x00, /* wcChar7 */
  228. '0', 0x00, /* wcChar8 */
  229. '0', 0x00, /* wcChar9 */
  230. #ifdef CONFIG_USB_HS
  231. ///////////////////////////////////////
  232. /// device qualifier descriptor
  233. ///////////////////////////////////////
  234. 0x0a,
  235. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  236. 0x00,
  237. 0x02,
  238. 0x00,
  239. 0x00,
  240. 0x00,
  241. 0x40,
  242. 0x00,
  243. 0x00,
  244. #endif
  245. 0x00
  246. };
  247. #endif
  248. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[MIDI_EP_MPS];
  249. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[MIDI_EP_MPS];
  250. static void usbd_event_handler(uint8_t busid, uint8_t event)
  251. {
  252. switch (event) {
  253. case USBD_EVENT_RESET:
  254. break;
  255. case USBD_EVENT_CONNECTED:
  256. break;
  257. case USBD_EVENT_DISCONNECTED:
  258. break;
  259. case USBD_EVENT_RESUME:
  260. break;
  261. case USBD_EVENT_SUSPEND:
  262. break;
  263. case USBD_EVENT_CONFIGURED:
  264. usbd_ep_start_read(busid, MIDI_OUT_EP, read_buffer, MIDI_EP_MPS);
  265. break;
  266. case USBD_EVENT_SET_REMOTE_WAKEUP:
  267. break;
  268. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  269. break;
  270. default:
  271. break;
  272. }
  273. }
  274. void usbd_midi_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  275. {
  276. usbd_ep_start_read(busid, MIDI_OUT_EP, read_buffer, MIDI_EP_MPS);
  277. }
  278. void usbd_midi_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  279. {
  280. }
  281. struct usbd_interface intf0;
  282. struct usbd_interface intf1;
  283. struct usbd_endpoint midi_out_ep = {
  284. .ep_addr = MIDI_OUT_EP,
  285. .ep_cb = usbd_midi_bulk_out
  286. };
  287. struct usbd_endpoint midi_in_ep = {
  288. .ep_addr = MIDI_IN_EP,
  289. .ep_cb = usbd_midi_bulk_in
  290. };
  291. void midi_init(uint8_t busid, uintptr_t reg_base)
  292. {
  293. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  294. usbd_desc_register(busid, &midi_descriptor);
  295. #else
  296. usbd_desc_register(busid, midi_descriptor);
  297. #endif
  298. usbd_add_interface(busid, &intf0);
  299. usbd_add_interface(busid, &intf1);
  300. usbd_add_endpoint(busid, &midi_out_ep);
  301. usbd_add_endpoint(busid, &midi_in_ep);
  302. usbd_initialize(busid, reg_base, usbd_event_handler);
  303. }