audio_v2_speaker_multichan_template.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_OUT_EP 0x01
  21. #define AUDIO_OUT_FEEDBACK_EP 0x82
  22. #define AUDIO_OUT_CLOCK_ID 0x01
  23. #define AUDIO_OUT_FU_ID 0x03
  24. #define AUDIO_FREQ 48000
  25. #define HALF_WORD_BYTES 2 //2 half word (one channel)
  26. #define SAMPLE_BITS 16 //16 bit per channel
  27. #define BMCONTROL (AUDIO_V2_FU_CONTROL_MUTE | AUDIO_V2_FU_CONTROL_VOLUME)
  28. #define OUT_CHANNEL_NUM 2
  29. #if OUT_CHANNEL_NUM == 1
  30. #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  31. #define OUTPUT_CH_ENABLE 0x00000000
  32. #elif OUT_CHANNEL_NUM == 2
  33. #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  34. #define OUTPUT_CH_ENABLE 0x00000003
  35. #elif OUT_CHANNEL_NUM == 3
  36. #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  37. #define OUTPUT_CH_ENABLE 0x00000007
  38. #elif OUT_CHANNEL_NUM == 4
  39. #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  40. #define OUTPUT_CH_ENABLE 0x0000000f
  41. #elif OUT_CHANNEL_NUM == 5
  42. #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  43. #define OUTPUT_CH_ENABLE 0x0000001f
  44. #elif OUT_CHANNEL_NUM == 6
  45. #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  46. #define OUTPUT_CH_ENABLE 0x0000003F
  47. #elif OUT_CHANNEL_NUM == 7
  48. #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  49. #define OUTPUT_CH_ENABLE 0x0000007f
  50. #elif OUT_CHANNEL_NUM == 8
  51. #define OUTPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  52. #define OUTPUT_CH_ENABLE 0x000000ff
  53. #endif
  54. #define AUDIO_OUT_PACKET ((uint32_t)((AUDIO_FREQ * HALF_WORD_BYTES * OUT_CHANNEL_NUM) / 1000))
  55. #if USING_FEEDBACK == 0
  56. #define USB_AUDIO_CONFIG_DESC_SIZ (9 + \
  57. AUDIO_V2_AC_DESCRIPTOR_INIT_LEN + \
  58. AUDIO_V2_SIZEOF_AC_CLOCK_SOURCE_DESC + \
  59. AUDIO_V2_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  60. AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(OUT_CHANNEL_NUM) + \
  61. AUDIO_V2_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  62. AUDIO_V2_AS_DESCRIPTOR_INIT_LEN)
  63. #else
  64. #define USB_AUDIO_CONFIG_DESC_SIZ (9 + \
  65. AUDIO_V2_AC_DESCRIPTOR_INIT_LEN + \
  66. AUDIO_V2_SIZEOF_AC_CLOCK_SOURCE_DESC + \
  67. AUDIO_V2_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  68. AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(OUT_CHANNEL_NUM) + \
  69. AUDIO_V2_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  70. AUDIO_V2_AS_FEEDBACK_DESCRIPTOR_INIT_LEN)
  71. #endif
  72. #define AUDIO_AC_SIZ (AUDIO_V2_SIZEOF_AC_HEADER_DESC + \
  73. AUDIO_V2_SIZEOF_AC_CLOCK_SOURCE_DESC + \
  74. AUDIO_V2_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  75. AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(OUT_CHANNEL_NUM) + \
  76. AUDIO_V2_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
  77. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  78. static const uint8_t device_descriptor[] = {
  79. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01)
  80. };
  81. static const uint8_t config_descriptor[] = {
  82. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  83. AUDIO_V2_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, AUDIO_CATEGORY_SPEAKER, 0x00, 0x00),
  84. AUDIO_V2_AC_CLOCK_SOURCE_DESCRIPTOR_INIT(AUDIO_OUT_CLOCK_ID, 0x03, 0x03),
  85. AUDIO_V2_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x02, AUDIO_TERMINAL_STREAMING, 0x01, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, 0x0000),
  86. AUDIO_V2_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_OUT_FU_ID, 0x02, OUTPUT_CTRL),
  87. AUDIO_V2_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_OUTTERM_SPEAKER, 0x03, 0x01, 0x0000),
  88. #if USING_FEEDBACK == 0
  89. AUDIO_V2_AS_DESCRIPTOR_INIT(0x01, 0x02, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET, EP_INTERVAL),
  90. #else
  91. AUDIO_V2_AS_FEEDBACK_DESCRIPTOR_INIT(0x01, 0x02, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_OUT_EP, AUDIO_OUT_PACKET, EP_INTERVAL, AUDIO_OUT_FEEDBACK_EP),
  92. #endif
  93. };
  94. static const uint8_t device_quality_descriptor[] = {
  95. ///////////////////////////////////////
  96. /// device qualifier descriptor
  97. ///////////////////////////////////////
  98. 0x0a,
  99. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  100. 0x00,
  101. 0x02,
  102. 0x00,
  103. 0x00,
  104. 0x00,
  105. 0x40,
  106. 0x00,
  107. 0x00,
  108. };
  109. static const char *string_descriptors[] = {
  110. (const char[]){ 0x09, 0x04 }, /* Langid */
  111. "CherryUSB", /* Manufacturer */
  112. "CherryUSB UAC DEMO", /* Product */
  113. "2022123456", /* Serial Number */
  114. };
  115. static const uint8_t *device_descriptor_callback(uint8_t speed)
  116. {
  117. return device_descriptor;
  118. }
  119. static const uint8_t *config_descriptor_callback(uint8_t speed)
  120. {
  121. return config_descriptor;
  122. }
  123. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  124. {
  125. return device_quality_descriptor;
  126. }
  127. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  128. {
  129. if (index > 3) {
  130. return NULL;
  131. }
  132. return string_descriptors[index];
  133. }
  134. const struct usb_descriptor audio_v2_descriptor = {
  135. .device_descriptor_callback = device_descriptor_callback,
  136. .config_descriptor_callback = config_descriptor_callback,
  137. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  138. .string_descriptor_callback = string_descriptor_callback
  139. };
  140. #else
  141. const uint8_t audio_v2_descriptor[] = {
  142. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01),
  143. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  144. AUDIO_V2_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, AUDIO_CATEGORY_SPEAKER, 0x00, 0x00),
  145. AUDIO_V2_AC_CLOCK_SOURCE_DESCRIPTOR_INIT(AUDIO_OUT_CLOCK_ID, 0x03, 0x03),
  146. AUDIO_V2_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x02, AUDIO_TERMINAL_STREAMING, 0x01, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, 0x0000),
  147. AUDIO_V2_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_OUT_FU_ID, 0x02, OUTPUT_CTRL),
  148. AUDIO_V2_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_OUTTERM_SPEAKER, 0x03, 0x01, 0x0000),
  149. #if USING_FEEDBACK == 0
  150. AUDIO_V2_AS_DESCRIPTOR_INIT(0x01, 0x02, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET, EP_INTERVAL),
  151. #else
  152. AUDIO_V2_AS_FEEDBACK_DESCRIPTOR_INIT(0x01, 0x02, OUT_CHANNEL_NUM, OUTPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_OUT_EP, AUDIO_OUT_PACKET, EP_INTERVAL, AUDIO_OUT_FEEDBACK_EP),
  153. #endif
  154. ///////////////////////////////////////
  155. /// string0 descriptor
  156. ///////////////////////////////////////
  157. USB_LANGID_INIT(USBD_LANGID_STRING),
  158. ///////////////////////////////////////
  159. /// string1 descriptor
  160. ///////////////////////////////////////
  161. 0x14, /* bLength */
  162. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  163. 'C', 0x00, /* wcChar0 */
  164. 'h', 0x00, /* wcChar1 */
  165. 'e', 0x00, /* wcChar2 */
  166. 'r', 0x00, /* wcChar3 */
  167. 'r', 0x00, /* wcChar4 */
  168. 'y', 0x00, /* wcChar5 */
  169. 'U', 0x00, /* wcChar6 */
  170. 'S', 0x00, /* wcChar7 */
  171. 'B', 0x00, /* wcChar8 */
  172. ///////////////////////////////////////
  173. /// string2 descriptor
  174. ///////////////////////////////////////
  175. 0x26, /* bLength */
  176. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  177. 'C', 0x00, /* wcChar0 */
  178. 'h', 0x00, /* wcChar1 */
  179. 'e', 0x00, /* wcChar2 */
  180. 'r', 0x00, /* wcChar3 */
  181. 'r', 0x00, /* wcChar4 */
  182. 'y', 0x00, /* wcChar5 */
  183. 'U', 0x00, /* wcChar6 */
  184. 'S', 0x00, /* wcChar7 */
  185. 'B', 0x00, /* wcChar8 */
  186. ' ', 0x00, /* wcChar9 */
  187. 'U', 0x00, /* wcChar10 */
  188. 'A', 0x00, /* wcChar11 */
  189. 'C', 0x00, /* wcChar12 */
  190. ' ', 0x00, /* wcChar13 */
  191. 'D', 0x00, /* wcChar14 */
  192. 'E', 0x00, /* wcChar15 */
  193. 'M', 0x00, /* wcChar16 */
  194. 'O', 0x00, /* wcChar17 */
  195. ///////////////////////////////////////
  196. /// string3 descriptor
  197. ///////////////////////////////////////
  198. 0x16, /* bLength */
  199. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  200. '2', 0x00, /* wcChar0 */
  201. '0', 0x00, /* wcChar1 */
  202. '2', 0x00, /* wcChar2 */
  203. '1', 0x00, /* wcChar3 */
  204. '0', 0x00, /* wcChar4 */
  205. '3', 0x00, /* wcChar5 */
  206. '1', 0x00, /* wcChar6 */
  207. '0', 0x00, /* wcChar7 */
  208. '0', 0x00, /* wcChar8 */
  209. #if USING_FEEDBACK == 0
  210. '3', 0x00, /* wcChar9 */
  211. #else
  212. '4', 0x00, /* wcChar9 */
  213. #endif
  214. #ifdef CONFIG_USB_HS
  215. ///////////////////////////////////////
  216. /// device qualifier descriptor
  217. ///////////////////////////////////////
  218. 0x0a,
  219. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  220. 0x00,
  221. 0x02,
  222. 0x00,
  223. 0x00,
  224. 0x00,
  225. 0x40,
  226. 0x00,
  227. 0x00,
  228. #endif
  229. 0x00
  230. };
  231. #endif
  232. static const uint8_t default_sampling_freq_table[] = {
  233. AUDIO_SAMPLE_FREQ_NUM(5),
  234. AUDIO_SAMPLE_FREQ_4B(8000),
  235. AUDIO_SAMPLE_FREQ_4B(8000),
  236. AUDIO_SAMPLE_FREQ_4B(0x00),
  237. AUDIO_SAMPLE_FREQ_4B(16000),
  238. AUDIO_SAMPLE_FREQ_4B(16000),
  239. AUDIO_SAMPLE_FREQ_4B(0x00),
  240. AUDIO_SAMPLE_FREQ_4B(32000),
  241. AUDIO_SAMPLE_FREQ_4B(32000),
  242. AUDIO_SAMPLE_FREQ_4B(0x00),
  243. AUDIO_SAMPLE_FREQ_4B(48000),
  244. AUDIO_SAMPLE_FREQ_4B(48000),
  245. AUDIO_SAMPLE_FREQ_4B(0x00),
  246. AUDIO_SAMPLE_FREQ_4B(96000),
  247. AUDIO_SAMPLE_FREQ_4B(96000),
  248. AUDIO_SAMPLE_FREQ_4B(0x00),
  249. };
  250. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[AUDIO_OUT_PACKET];
  251. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t s_speaker_feedback_buffer[4];
  252. volatile bool rx_flag = 0;
  253. static void usbd_event_handler(uint8_t busid, uint8_t event)
  254. {
  255. switch (event) {
  256. case USBD_EVENT_RESET:
  257. break;
  258. case USBD_EVENT_CONNECTED:
  259. break;
  260. case USBD_EVENT_DISCONNECTED:
  261. break;
  262. case USBD_EVENT_RESUME:
  263. break;
  264. case USBD_EVENT_SUSPEND:
  265. break;
  266. case USBD_EVENT_CONFIGURED:
  267. break;
  268. case USBD_EVENT_SET_REMOTE_WAKEUP:
  269. break;
  270. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  271. break;
  272. default:
  273. break;
  274. }
  275. }
  276. void usbd_audio_open(uint8_t busid, uint8_t intf)
  277. {
  278. rx_flag = 1;
  279. /* setup first out ep read transfer */
  280. usbd_ep_start_read(busid, AUDIO_OUT_EP, read_buffer, AUDIO_OUT_PACKET);
  281. #ifdef CONFIG_USB_HS
  282. uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_HS(AUDIO_FREQ);
  283. AUDIO_FEEDBACK_TO_BUF_HS(s_speaker_feedback_buffer, feedback_value);
  284. #else
  285. uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_FS(AUDIO_FREQ);
  286. AUDIO_FEEDBACK_TO_BUF_FS(s_speaker_feedback_buffer, feedback_value);
  287. #endif
  288. usbd_ep_start_write(busid, AUDIO_OUT_FEEDBACK_EP, s_speaker_feedback_buffer, FEEDBACK_ENDP_PACKET_SIZE);
  289. USB_LOG_RAW("OPEN\r\n");
  290. }
  291. void usbd_audio_close(uint8_t busid, uint8_t intf)
  292. {
  293. USB_LOG_RAW("CLOSE\r\n");
  294. rx_flag = 0;
  295. }
  296. void usbd_audio_get_sampling_freq_table(uint8_t busid, uint8_t ep, uint8_t **sampling_freq_table)
  297. {
  298. if (ep == AUDIO_OUT_EP) {
  299. *sampling_freq_table = (uint8_t *)default_sampling_freq_table;
  300. }
  301. }
  302. void usbd_audio_iso_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  303. {
  304. USB_LOG_RAW("actual out len:%d\r\n", nbytes);
  305. usbd_ep_start_read(busid, AUDIO_OUT_EP, read_buffer, AUDIO_OUT_PACKET);
  306. }
  307. #if USING_FEEDBACK == 1
  308. void usbd_audio_iso_out_feedback_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  309. {
  310. USB_LOG_RAW("actual feedback len:%d\r\n", nbytes);
  311. #ifdef CONFIG_USB_HS
  312. uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_HS(AUDIO_FREQ);
  313. AUDIO_FEEDBACK_TO_BUF_HS(s_speaker_feedback_buffer, feedback_value);
  314. #else
  315. uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_FS(AUDIO_FREQ);
  316. AUDIO_FEEDBACK_TO_BUF_FS(s_speaker_feedback_buffer, feedback_value);
  317. #endif
  318. usbd_ep_start_write(busid, AUDIO_OUT_FEEDBACK_EP, s_speaker_feedback_buffer, FEEDBACK_ENDP_PACKET_SIZE);
  319. }
  320. #endif
  321. static struct usbd_endpoint audio_out_ep = {
  322. .ep_cb = usbd_audio_iso_out_callback,
  323. .ep_addr = AUDIO_OUT_EP
  324. };
  325. #if USING_FEEDBACK == 1
  326. static struct usbd_endpoint audio_out_feedback_ep = {
  327. .ep_cb = usbd_audio_iso_out_feedback_callback,
  328. .ep_addr = AUDIO_OUT_FEEDBACK_EP
  329. };
  330. #endif
  331. struct usbd_interface intf0;
  332. struct usbd_interface intf1;
  333. struct audio_entity_info audio_entity_table[] = {
  334. { .bEntityId = AUDIO_OUT_CLOCK_ID,
  335. .bDescriptorSubtype = AUDIO_CONTROL_CLOCK_SOURCE,
  336. .ep = AUDIO_OUT_EP },
  337. { .bEntityId = AUDIO_OUT_FU_ID,
  338. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  339. .ep = AUDIO_OUT_EP },
  340. };
  341. void audio_v2_init(uint8_t busid, uintptr_t reg_base)
  342. {
  343. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  344. usbd_desc_register(busid, &audio_v2_descriptor);
  345. #else
  346. usbd_desc_register(busid, audio_v2_descriptor);
  347. #endif
  348. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0200, audio_entity_table, 2));
  349. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0200, audio_entity_table, 2));
  350. usbd_add_endpoint(busid, &audio_out_ep);
  351. #if USING_FEEDBACK == 1
  352. usbd_add_endpoint(busid, &audio_out_feedback_ep);
  353. #endif
  354. usbd_initialize(busid, reg_base, usbd_event_handler);
  355. }
  356. void audio_v2_test(uint8_t busid)
  357. {
  358. if (rx_flag) {
  359. }
  360. }