audio_v1_mic_speaker_multichan_template.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 USING_FEEDBACK 0
  9. #define USBD_VID 0xffff
  10. #define USBD_PID 0xffff
  11. #define USBD_MAX_POWER 100
  12. #define USBD_LANGID_STRING 1033
  13. #ifdef CONFIG_USB_HS
  14. #define EP_INTERVAL 0x04
  15. #define FEEDBACK_ENDP_PACKET_SIZE 0x04
  16. #else
  17. #define EP_INTERVAL 0x01
  18. #define FEEDBACK_ENDP_PACKET_SIZE 0x03
  19. #endif
  20. #define AUDIO_IN_EP 0x81
  21. #define AUDIO_OUT_EP 0x02
  22. #define AUDIO_OUT_FEEDBACK_EP 0x83
  23. #define AUDIO_IN_FU_ID 0x02
  24. #define AUDIO_OUT_FU_ID 0x05
  25. /* AUDIO Class Config */
  26. #define AUDIO_SPEAKER_FREQ 16000U
  27. #define AUDIO_SPEAKER_FRAME_SIZE_BYTE 2u
  28. #define AUDIO_SPEAKER_RESOLUTION_BIT 16u
  29. #define AUDIO_MIC_FREQ 16000U
  30. #define AUDIO_MIC_FRAME_SIZE_BYTE 2u
  31. #define AUDIO_MIC_RESOLUTION_BIT 16u
  32. #define AUDIO_SAMPLE_FREQ(frq) (uint8_t)(frq), (uint8_t)((frq >> 8)), (uint8_t)((frq >> 16))
  33. /* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 2) */
  34. #define AUDIO_OUT_PACKET ((uint32_t)((AUDIO_SPEAKER_FREQ * AUDIO_SPEAKER_FRAME_SIZE_BYTE * 2) / 1000))
  35. /* 16bit(2 Bytes) 双声道(Mono:2) */
  36. #define AUDIO_IN_PACKET ((uint32_t)((AUDIO_MIC_FREQ * AUDIO_MIC_FRAME_SIZE_BYTE * 2) / 1000))
  37. #if USING_FEEDBACK == 0
  38. #define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 + \
  39. AUDIO_AC_DESCRIPTOR_INIT_LEN(2) + \
  40. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  41. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  42. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  43. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  44. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  45. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  46. AUDIO_AS_DESCRIPTOR_INIT_LEN(1) + \
  47. AUDIO_AS_DESCRIPTOR_INIT_LEN(1))
  48. #else
  49. #define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 + \
  50. AUDIO_AC_DESCRIPTOR_INIT_LEN(2) + \
  51. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  52. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  53. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  54. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  55. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  56. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  57. AUDIO_AS_DESCRIPTOR_INIT_LEN(1) + \
  58. AUDIO_AS_FEEDBACK_DESCRIPTOR_INIT_LEN(1))
  59. #endif
  60. #define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(2) + \
  61. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  62. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  63. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  64. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  65. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  66. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
  67. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  68. static const uint8_t device_descriptor[] = {
  69. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  70. };
  71. static const uint8_t config_descriptor[] = {
  72. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x03, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  73. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x03, AUDIO_AC_SIZ, 0x00, 0x01, 0x02),
  74. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003),
  75. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00, 0x00),
  76. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02),
  77. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x02, 0x0003),
  78. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00, 0x00),
  79. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x06, AUDIO_OUTTERM_SPEAKER, 0x05),
  80. #if USING_FEEDBACK == 0
  81. AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET,
  82. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
  83. #else
  84. AUDIO_AS_FEEDBACK_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, AUDIO_OUT_PACKET,
  85. EP_INTERVAL, AUDIO_OUT_FEEDBACK_EP, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
  86. #endif
  87. AUDIO_AS_DESCRIPTOR_INIT(0x02, 0x03, 0x02, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET,
  88. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ))
  89. };
  90. static const uint8_t device_quality_descriptor[] = {
  91. ///////////////////////////////////////
  92. /// device qualifier descriptor
  93. ///////////////////////////////////////
  94. 0x0a,
  95. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  96. 0x00,
  97. 0x02,
  98. 0x00,
  99. 0x00,
  100. 0x00,
  101. 0x40,
  102. 0x00,
  103. 0x00,
  104. };
  105. static const char *string_descriptors[] = {
  106. (const char[]){ 0x09, 0x04 }, /* Langid */
  107. "CherryUSB", /* Manufacturer */
  108. "CherryUSB UAC DEMO", /* Product */
  109. "2022123456", /* Serial Number */
  110. };
  111. static const uint8_t *device_descriptor_callback(uint8_t speed)
  112. {
  113. return device_descriptor;
  114. }
  115. static const uint8_t *config_descriptor_callback(uint8_t speed)
  116. {
  117. return config_descriptor;
  118. }
  119. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  120. {
  121. return device_quality_descriptor;
  122. }
  123. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  124. {
  125. if (index > 3) {
  126. return NULL;
  127. }
  128. return string_descriptors[index];
  129. }
  130. const struct usb_descriptor audio_v1_descriptor = {
  131. .device_descriptor_callback = device_descriptor_callback,
  132. .config_descriptor_callback = config_descriptor_callback,
  133. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  134. .string_descriptor_callback = string_descriptor_callback
  135. };
  136. #else
  137. const uint8_t audio_v1_descriptor[] = {
  138. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  139. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x03, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  140. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x03, AUDIO_AC_SIZ, 0x00, 0x01, 0x02),
  141. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003),
  142. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00, 0x00),
  143. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02),
  144. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x02, 0x0003),
  145. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00, 0x00),
  146. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x06, AUDIO_OUTTERM_SPEAKER, 0x05),
  147. #if USING_FEEDBACK == 0
  148. AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET,
  149. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
  150. #else
  151. AUDIO_AS_FEEDBACK_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, AUDIO_OUT_PACKET,
  152. EP_INTERVAL, AUDIO_OUT_FEEDBACK_EP, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
  153. #endif
  154. AUDIO_AS_DESCRIPTOR_INIT(0x02, 0x03, 0x02, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET,
  155. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ)),
  156. ///////////////////////////////////////
  157. /// string0 descriptor
  158. ///////////////////////////////////////
  159. USB_LANGID_INIT(USBD_LANGID_STRING),
  160. ///////////////////////////////////////
  161. /// string1 descriptor
  162. ///////////////////////////////////////
  163. 0x14, /* bLength */
  164. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  165. 'C', 0x00, /* wcChar0 */
  166. 'h', 0x00, /* wcChar1 */
  167. 'e', 0x00, /* wcChar2 */
  168. 'r', 0x00, /* wcChar3 */
  169. 'r', 0x00, /* wcChar4 */
  170. 'y', 0x00, /* wcChar5 */
  171. 'U', 0x00, /* wcChar6 */
  172. 'S', 0x00, /* wcChar7 */
  173. 'B', 0x00, /* wcChar8 */
  174. ///////////////////////////////////////
  175. /// string2 descriptor
  176. ///////////////////////////////////////
  177. 0x26, /* bLength */
  178. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  179. 'C', 0x00, /* wcChar0 */
  180. 'h', 0x00, /* wcChar1 */
  181. 'e', 0x00, /* wcChar2 */
  182. 'r', 0x00, /* wcChar3 */
  183. 'r', 0x00, /* wcChar4 */
  184. 'y', 0x00, /* wcChar5 */
  185. 'U', 0x00, /* wcChar6 */
  186. 'S', 0x00, /* wcChar7 */
  187. 'B', 0x00, /* wcChar8 */
  188. ' ', 0x00, /* wcChar9 */
  189. 'U', 0x00, /* wcChar10 */
  190. 'A', 0x00, /* wcChar11 */
  191. 'C', 0x00, /* wcChar12 */
  192. ' ', 0x00, /* wcChar13 */
  193. 'D', 0x00, /* wcChar14 */
  194. 'E', 0x00, /* wcChar15 */
  195. 'M', 0x00, /* wcChar16 */
  196. 'O', 0x00, /* wcChar17 */
  197. ///////////////////////////////////////
  198. /// string3 descriptor
  199. ///////////////////////////////////////
  200. 0x16, /* bLength */
  201. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  202. '2', 0x00, /* wcChar0 */
  203. '0', 0x00, /* wcChar1 */
  204. '2', 0x00, /* wcChar2 */
  205. '2', 0x00, /* wcChar3 */
  206. '1', 0x00, /* wcChar4 */
  207. '2', 0x00, /* wcChar5 */
  208. '3', 0x00, /* wcChar6 */
  209. '4', 0x00, /* wcChar7 */
  210. '5', 0x00, /* wcChar8 */
  211. #if USING_FEEDBACK == 0
  212. '1', 0x00, /* wcChar9 */
  213. #else
  214. '2', 0x00, /* wcChar9 */
  215. #endif
  216. #ifdef CONFIG_USB_HS
  217. ///////////////////////////////////////
  218. /// device qualifier descriptor
  219. ///////////////////////////////////////
  220. 0x0a,
  221. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  222. 0x00,
  223. 0x02,
  224. 0x00,
  225. 0x00,
  226. 0x00,
  227. 0x40,
  228. 0x00,
  229. 0x00,
  230. #endif
  231. 0x00
  232. };
  233. #endif
  234. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[AUDIO_OUT_PACKET];
  235. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[AUDIO_IN_PACKET];
  236. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t s_speaker_feedback_buffer[4];
  237. volatile bool tx_flag = 0;
  238. volatile bool rx_flag = 0;
  239. volatile bool ep_tx_busy_flag = false;
  240. static void usbd_event_handler(uint8_t busid, uint8_t event)
  241. {
  242. switch (event) {
  243. case USBD_EVENT_RESET:
  244. break;
  245. case USBD_EVENT_CONNECTED:
  246. break;
  247. case USBD_EVENT_DISCONNECTED:
  248. break;
  249. case USBD_EVENT_RESUME:
  250. break;
  251. case USBD_EVENT_SUSPEND:
  252. break;
  253. case USBD_EVENT_CONFIGURED:
  254. break;
  255. case USBD_EVENT_SET_REMOTE_WAKEUP:
  256. break;
  257. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. void usbd_audio_open(uint8_t busid, uint8_t intf)
  264. {
  265. if (intf == 1) {
  266. rx_flag = 1;
  267. /* setup first out ep read transfer */
  268. usbd_ep_start_read(busid, AUDIO_OUT_EP, read_buffer, AUDIO_OUT_PACKET);
  269. uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_FS(AUDIO_SPEAKER_FREQ);
  270. AUDIO_FEEDBACK_TO_BUF_FS(s_speaker_feedback_buffer, feedback_value); /* uac1 can only use 10.14 */
  271. usbd_ep_start_write(busid, AUDIO_OUT_FEEDBACK_EP, s_speaker_feedback_buffer, FEEDBACK_ENDP_PACKET_SIZE);
  272. printf("OPEN1\r\n");
  273. } else {
  274. tx_flag = 1;
  275. ep_tx_busy_flag = false;
  276. printf("OPEN2\r\n");
  277. }
  278. }
  279. void usbd_audio_close(uint8_t busid, uint8_t intf)
  280. {
  281. if (intf == 1) {
  282. rx_flag = 0;
  283. printf("CLOSE1\r\n");
  284. } else {
  285. tx_flag = 0;
  286. ep_tx_busy_flag = false;
  287. printf("CLOSE2\r\n");
  288. }
  289. }
  290. void usbd_audio_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  291. {
  292. USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
  293. usbd_ep_start_read(busid, AUDIO_OUT_EP, read_buffer, AUDIO_OUT_PACKET);
  294. }
  295. void usbd_audio_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  296. {
  297. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  298. ep_tx_busy_flag = false;
  299. }
  300. #if USING_FEEDBACK == 1
  301. void usbd_audio_iso_out_feedback_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  302. {
  303. USB_LOG_RAW("actual feedback len:%d\r\n", (unsigned int)nbytes);
  304. uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_FS(AUDIO_SPEAKER_FREQ);
  305. AUDIO_FEEDBACK_TO_BUF_FS(s_speaker_feedback_buffer, feedback_value);
  306. usbd_ep_start_write(busid, AUDIO_OUT_FEEDBACK_EP, s_speaker_feedback_buffer, FEEDBACK_ENDP_PACKET_SIZE);
  307. }
  308. #endif
  309. static struct usbd_endpoint audio_in_ep = {
  310. .ep_cb = usbd_audio_in_callback,
  311. .ep_addr = AUDIO_IN_EP
  312. };
  313. static struct usbd_endpoint audio_out_ep = {
  314. .ep_cb = usbd_audio_out_callback,
  315. .ep_addr = AUDIO_OUT_EP
  316. };
  317. #if USING_FEEDBACK == 1
  318. static struct usbd_endpoint audio_out_feedback_ep = {
  319. .ep_cb = usbd_audio_iso_out_feedback_callback,
  320. .ep_addr = AUDIO_OUT_FEEDBACK_EP
  321. };
  322. #endif
  323. struct usbd_interface intf0;
  324. struct usbd_interface intf1;
  325. struct usbd_interface intf2;
  326. struct audio_entity_info audio_entity_table[] = {
  327. { .bEntityId = AUDIO_IN_FU_ID,
  328. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  329. .ep = AUDIO_IN_EP },
  330. { .bEntityId = AUDIO_OUT_FU_ID,
  331. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  332. .ep = AUDIO_OUT_EP },
  333. };
  334. void audio_v1_init(uint8_t busid, uintptr_t reg_base)
  335. {
  336. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  337. usbd_desc_register(busid, &audio_v1_descriptor);
  338. #else
  339. usbd_desc_register(busid, audio_v1_descriptor);
  340. #endif
  341. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0100, audio_entity_table, 2));
  342. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0100, audio_entity_table, 2));
  343. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf2, 0x0100, audio_entity_table, 2));
  344. usbd_add_endpoint(busid, &audio_in_ep);
  345. usbd_add_endpoint(busid, &audio_out_ep);
  346. #if USING_FEEDBACK == 1
  347. usbd_add_endpoint(busid, &audio_out_feedback_ep);
  348. #endif
  349. usbd_initialize(busid, reg_base, usbd_event_handler);
  350. }
  351. void audio_v1_test(uint8_t busid)
  352. {
  353. if (tx_flag) {
  354. memset(write_buffer, 'a', AUDIO_IN_PACKET);
  355. ep_tx_busy_flag = true;
  356. usbd_ep_start_write(busid, AUDIO_IN_EP, write_buffer, AUDIO_IN_PACKET);
  357. while (ep_tx_busy_flag) {
  358. if (tx_flag == false) {
  359. break;
  360. }
  361. }
  362. }
  363. }