audio_v1_mic_multichan_template.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_audio.h"
  8. #define USBD_VID 0xffff
  9. #define USBD_PID 0xffff
  10. #define USBD_MAX_POWER 100
  11. #define USBD_LANGID_STRING 1033
  12. #ifdef CONFIG_USB_HS
  13. #define EP_INTERVAL 0x04
  14. #else
  15. #define EP_INTERVAL 0x01
  16. #endif
  17. #define AUDIO_IN_EP 0x81
  18. #define AUDIO_IN_FU_ID 0x02
  19. /* AUDIO Class Config */
  20. #define AUDIO_FREQ 16000U
  21. #define IN_CHANNEL_NUM 1
  22. #if IN_CHANNEL_NUM == 1
  23. #define INPUT_CTRL 0x03, 0x03
  24. #define INPUT_CH_ENABLE 0x0000
  25. #elif IN_CHANNEL_NUM == 2
  26. #define INPUT_CTRL 0x03, 0x03, 0x03
  27. #define INPUT_CH_ENABLE 0x0003
  28. #elif IN_CHANNEL_NUM == 3
  29. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03
  30. #define INPUT_CH_ENABLE 0x0007
  31. #elif IN_CHANNEL_NUM == 4
  32. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03
  33. #define INPUT_CH_ENABLE 0x000f
  34. #elif IN_CHANNEL_NUM == 5
  35. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  36. #define INPUT_CH_ENABLE 0x001f
  37. #elif IN_CHANNEL_NUM == 6
  38. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  39. #define INPUT_CH_ENABLE 0x003F
  40. #elif IN_CHANNEL_NUM == 7
  41. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  42. #define INPUT_CH_ENABLE 0x007f
  43. #elif IN_CHANNEL_NUM == 8
  44. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  45. #define INPUT_CH_ENABLE 0x00ff
  46. #endif
  47. /* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 1) */
  48. /* 16bit(2 Bytes) 单声道(Mono:1) */
  49. #define AUDIO_IN_PACKET ((uint32_t)((AUDIO_FREQ * 2 * IN_CHANNEL_NUM) / 1000))
  50. #define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 + \
  51. AUDIO_AC_DESCRIPTOR_INIT_LEN(1) + \
  52. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  53. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
  54. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  55. AUDIO_AS_DESCRIPTOR_INIT_LEN(1))
  56. #define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(1) + \
  57. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  58. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
  59. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
  60. const uint8_t audio_v1_descriptor[] = {
  61. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  62. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  63. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
  64. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
  65. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
  66. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, AUDIO_IN_FU_ID),
  67. AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x03, IN_CHANNEL_NUM, 2, 16, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_FREQ)),
  68. ///////////////////////////////////////
  69. /// string0 descriptor
  70. ///////////////////////////////////////
  71. USB_LANGID_INIT(USBD_LANGID_STRING),
  72. ///////////////////////////////////////
  73. /// string1 descriptor
  74. ///////////////////////////////////////
  75. 0x14, /* bLength */
  76. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  77. 'C', 0x00, /* wcChar0 */
  78. 'h', 0x00, /* wcChar1 */
  79. 'e', 0x00, /* wcChar2 */
  80. 'r', 0x00, /* wcChar3 */
  81. 'r', 0x00, /* wcChar4 */
  82. 'y', 0x00, /* wcChar5 */
  83. 'U', 0x00, /* wcChar6 */
  84. 'S', 0x00, /* wcChar7 */
  85. 'B', 0x00, /* wcChar8 */
  86. ///////////////////////////////////////
  87. /// string2 descriptor
  88. ///////////////////////////////////////
  89. 0x26, /* bLength */
  90. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  91. 'C', 0x00, /* wcChar0 */
  92. 'h', 0x00, /* wcChar1 */
  93. 'e', 0x00, /* wcChar2 */
  94. 'r', 0x00, /* wcChar3 */
  95. 'r', 0x00, /* wcChar4 */
  96. 'y', 0x00, /* wcChar5 */
  97. 'U', 0x00, /* wcChar6 */
  98. 'S', 0x00, /* wcChar7 */
  99. 'B', 0x00, /* wcChar8 */
  100. ' ', 0x00, /* wcChar9 */
  101. 'U', 0x00, /* wcChar10 */
  102. 'A', 0x00, /* wcChar11 */
  103. 'C', 0x00, /* wcChar12 */
  104. ' ', 0x00, /* wcChar13 */
  105. 'D', 0x00, /* wcChar14 */
  106. 'E', 0x00, /* wcChar15 */
  107. 'M', 0x00, /* wcChar16 */
  108. 'O', 0x00, /* wcChar17 */
  109. ///////////////////////////////////////
  110. /// string3 descriptor
  111. ///////////////////////////////////////
  112. 0x16, /* bLength */
  113. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  114. '2', 0x00, /* wcChar0 */
  115. '0', 0x00, /* wcChar1 */
  116. '2', 0x00, /* wcChar2 */
  117. '1', 0x00, /* wcChar3 */
  118. '0', 0x00, /* wcChar4 */
  119. '3', 0x00, /* wcChar5 */
  120. '1', 0x00, /* wcChar6 */
  121. '0', 0x00, /* wcChar7 */
  122. '0', 0x00, /* wcChar8 */
  123. '0' + IN_CHANNEL_NUM, 0x00, /* wcChar9 */
  124. #ifdef CONFIG_USB_HS
  125. ///////////////////////////////////////
  126. /// device qualifier descriptor
  127. ///////////////////////////////////////
  128. 0x0a,
  129. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  130. 0x00,
  131. 0x02,
  132. 0x00,
  133. 0x00,
  134. 0x00,
  135. 0x40,
  136. 0x01,
  137. 0x00,
  138. #endif
  139. 0x00
  140. };
  141. volatile bool tx_flag = 0;
  142. volatile bool ep_tx_busy_flag = false;
  143. static void usbd_event_handler(uint8_t busid, uint8_t event)
  144. {
  145. switch (event) {
  146. case USBD_EVENT_RESET:
  147. break;
  148. case USBD_EVENT_CONNECTED:
  149. break;
  150. case USBD_EVENT_DISCONNECTED:
  151. break;
  152. case USBD_EVENT_RESUME:
  153. break;
  154. case USBD_EVENT_SUSPEND:
  155. break;
  156. case USBD_EVENT_CONFIGURED:
  157. break;
  158. case USBD_EVENT_SET_REMOTE_WAKEUP:
  159. break;
  160. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. void usbd_audio_open(uint8_t busid, uint8_t intf)
  167. {
  168. tx_flag = 1;
  169. ep_tx_busy_flag = false;
  170. USB_LOG_RAW("OPEN\r\n");
  171. }
  172. void usbd_audio_close(uint8_t busid, uint8_t intf)
  173. {
  174. USB_LOG_RAW("CLOSE\r\n");
  175. ep_tx_busy_flag = false;
  176. tx_flag = 0;
  177. }
  178. void usbd_audio_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  179. {
  180. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  181. ep_tx_busy_flag = false;
  182. }
  183. static struct usbd_endpoint audio_in_ep = {
  184. .ep_cb = usbd_audio_iso_callback,
  185. .ep_addr = AUDIO_IN_EP
  186. };
  187. struct usbd_interface intf0;
  188. struct usbd_interface intf1;
  189. struct audio_entity_info audio_entity_table[] = {
  190. { .bEntityId = AUDIO_IN_FU_ID,
  191. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  192. .ep = AUDIO_IN_EP },
  193. };
  194. void audio_v1_init(uint8_t busid, uint32_t reg_base)
  195. {
  196. usbd_desc_register(busid, audio_v1_descriptor);
  197. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0100, audio_entity_table, 1));
  198. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0100, audio_entity_table, 1));
  199. usbd_add_endpoint(busid, &audio_in_ep);
  200. usbd_initialize(busid, reg_base, usbd_event_handler);
  201. }
  202. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[AUDIO_IN_PACKET];
  203. void audio_test(uint8_t busid)
  204. {
  205. while (1) {
  206. if (tx_flag) {
  207. memset(write_buffer, 'a', AUDIO_IN_PACKET);
  208. ep_tx_busy_flag = true;
  209. usbd_ep_start_write(busid, AUDIO_IN_EP, write_buffer, AUDIO_IN_PACKET);
  210. while (ep_tx_busy_flag) {
  211. if (tx_flag == false) {
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. }