midi_template.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. static const uint8_t device_descriptor[] = {
  26. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01)
  27. };
  28. static const uint8_t config_descriptor[] = {
  29. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  30. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
  31. MIDI_STANDARD_DESCRIPTOR_INIT(0x01, 0x02),
  32. MIDI_CS_HEADER_DESCRIPTOR_INIT(AUDIO_MS_SIZ),
  33. MIDI_JACK_DESCRIPTOR_INIT(0x01),
  34. // OUT endpoint descriptor
  35. 0x09, 0x05, MIDI_OUT_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  36. 0x05, 0x25, 0x01, 0x01, 0x01,
  37. // IN endpoint descriptor
  38. 0x09, 0x05, MIDI_IN_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
  39. 0x05, 0x25, 0x01, 0x01, 0x03
  40. };
  41. static const uint8_t device_quality_descriptor[] = {
  42. ///////////////////////////////////////
  43. /// device qualifier descriptor
  44. ///////////////////////////////////////
  45. 0x0a,
  46. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  47. 0x00,
  48. 0x02,
  49. 0x00,
  50. 0x00,
  51. 0x00,
  52. 0x40,
  53. 0x00,
  54. 0x00,
  55. };
  56. static const char *string_descriptors[] = {
  57. (const char[]){ 0x09, 0x04 }, /* Langid */
  58. "CherryUSB", /* Manufacturer */
  59. "CherryUSB MIDI DEMO", /* Product */
  60. "2022123456", /* Serial Number */
  61. };
  62. static const uint8_t *device_descriptor_callback(uint8_t speed)
  63. {
  64. return device_descriptor;
  65. }
  66. static const uint8_t *config_descriptor_callback(uint8_t speed)
  67. {
  68. return config_descriptor;
  69. }
  70. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  71. {
  72. return device_quality_descriptor;
  73. }
  74. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  75. {
  76. if (index > 3) {
  77. return NULL;
  78. }
  79. return string_descriptors[index];
  80. }
  81. const struct usb_descriptor midi_descriptor = {
  82. .device_descriptor_callback = device_descriptor_callback,
  83. .config_descriptor_callback = config_descriptor_callback,
  84. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  85. .string_descriptor_callback = string_descriptor_callback
  86. };
  87. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[MIDI_EP_MPS];
  88. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[MIDI_EP_MPS];
  89. static void usbd_event_handler(uint8_t busid, uint8_t event)
  90. {
  91. switch (event) {
  92. case USBD_EVENT_RESET:
  93. break;
  94. case USBD_EVENT_CONNECTED:
  95. break;
  96. case USBD_EVENT_DISCONNECTED:
  97. break;
  98. case USBD_EVENT_RESUME:
  99. break;
  100. case USBD_EVENT_SUSPEND:
  101. break;
  102. case USBD_EVENT_CONFIGURED:
  103. usbd_ep_start_read(busid, MIDI_OUT_EP, read_buffer, MIDI_EP_MPS);
  104. break;
  105. case USBD_EVENT_SET_REMOTE_WAKEUP:
  106. break;
  107. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. void usbd_midi_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  114. {
  115. usbd_ep_start_read(busid, MIDI_OUT_EP, read_buffer, MIDI_EP_MPS);
  116. }
  117. void usbd_midi_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  118. {
  119. }
  120. struct usbd_interface intf0;
  121. struct usbd_interface intf1;
  122. struct usbd_endpoint midi_out_ep = {
  123. .ep_addr = MIDI_OUT_EP,
  124. .ep_cb = usbd_midi_bulk_out
  125. };
  126. struct usbd_endpoint midi_in_ep = {
  127. .ep_addr = MIDI_IN_EP,
  128. .ep_cb = usbd_midi_bulk_in
  129. };
  130. void midi_init(uint8_t busid, uintptr_t reg_base)
  131. {
  132. usbd_desc_register(busid, &midi_descriptor);
  133. usbd_add_interface(busid, &intf0);
  134. usbd_add_interface(busid, &intf1);
  135. usbd_add_endpoint(busid, &midi_out_ep);
  136. usbd_add_endpoint(busid, &midi_in_ep);
  137. usbd_initialize(busid, reg_base, usbd_event_handler);
  138. }