audio_v2_mic_multichan_template.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_CLOCK_ID 0x01
  19. #define AUDIO_IN_FU_ID 0x03
  20. #define AUDIO_FREQ 48000
  21. #define HALF_WORD_BYTES 2 //2 half word (one channel)
  22. #define SAMPLE_BITS 16 //16 bit per channel
  23. #define BMCONTROL (AUDIO_V2_FU_CONTROL_MUTE | AUDIO_V2_FU_CONTROL_VOLUME)
  24. #define IN_CHANNEL_NUM 2
  25. #if IN_CHANNEL_NUM == 1
  26. #define INPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  27. #define INPUT_CH_ENABLE 0x00000000
  28. #elif IN_CHANNEL_NUM == 2
  29. #define INPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  30. #define INPUT_CH_ENABLE 0x00000003
  31. #elif IN_CHANNEL_NUM == 3
  32. #define INPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  33. #define INPUT_CH_ENABLE 0x00000007
  34. #elif IN_CHANNEL_NUM == 4
  35. #define INPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  36. #define INPUT_CH_ENABLE 0x0000000f
  37. #elif IN_CHANNEL_NUM == 5
  38. #define INPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  39. #define INPUT_CH_ENABLE 0x0000001f
  40. #elif IN_CHANNEL_NUM == 6
  41. #define INPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  42. #define INPUT_CH_ENABLE 0x0000003F
  43. #elif IN_CHANNEL_NUM == 7
  44. #define INPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  45. #define INPUT_CH_ENABLE 0x0000007f
  46. #elif IN_CHANNEL_NUM == 8
  47. #define INPUT_CTRL DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL), DBVAL(BMCONTROL)
  48. #define INPUT_CH_ENABLE 0x000000ff
  49. #endif
  50. #define AUDIO_IN_PACKET ((uint32_t)((AUDIO_FREQ * HALF_WORD_BYTES * IN_CHANNEL_NUM) / 1000))
  51. #define USB_AUDIO_CONFIG_DESC_SIZ (9 + \
  52. AUDIO_V2_AC_DESCRIPTOR_INIT_LEN + \
  53. AUDIO_V2_SIZEOF_AC_CLOCK_SOURCE_DESC + \
  54. AUDIO_V2_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  55. AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM) + \
  56. AUDIO_V2_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  57. AUDIO_V2_AS_DESCRIPTOR_INIT_LEN)
  58. #define AUDIO_AC_SIZ (AUDIO_V2_SIZEOF_AC_HEADER_DESC + \
  59. AUDIO_V2_SIZEOF_AC_CLOCK_SOURCE_DESC + \
  60. AUDIO_V2_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  61. AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM) + \
  62. AUDIO_V2_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
  63. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  64. static const uint8_t device_descriptor[] = {
  65. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01)
  66. };
  67. static const uint8_t config_descriptor[] = {
  68. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  69. AUDIO_V2_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, AUDIO_CATEGORY_MICROPHONE, 0x00, 0x00),
  70. AUDIO_V2_AC_CLOCK_SOURCE_DESCRIPTOR_INIT(0x01, 0x03, 0x03),
  71. AUDIO_V2_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x02, AUDIO_INTERM_MIC, 0x01, IN_CHANNEL_NUM, INPUT_CH_ENABLE, 0x0000),
  72. AUDIO_V2_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x03, 0x02, INPUT_CTRL),
  73. AUDIO_V2_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x03, 0x01, 0x0000),
  74. AUDIO_V2_AS_DESCRIPTOR_INIT(0x01, 0x04, IN_CHANNEL_NUM, INPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_IN_EP, 0x05, (AUDIO_IN_PACKET + 4), EP_INTERVAL)
  75. };
  76. static const uint8_t device_quality_descriptor[] = {
  77. ///////////////////////////////////////
  78. /// device qualifier descriptor
  79. ///////////////////////////////////////
  80. 0x0a,
  81. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  82. 0x00,
  83. 0x02,
  84. 0x00,
  85. 0x00,
  86. 0x00,
  87. 0x40,
  88. 0x00,
  89. 0x00,
  90. };
  91. static const char *string_descriptors[] = {
  92. (const char[]){ 0x09, 0x04 }, /* Langid */
  93. "CherryUSB", /* Manufacturer */
  94. "CherryUSB UAC DEMO", /* Product */
  95. "2022123456", /* Serial Number */
  96. };
  97. static const uint8_t *device_descriptor_callback(uint8_t speed)
  98. {
  99. return device_descriptor;
  100. }
  101. static const uint8_t *config_descriptor_callback(uint8_t speed)
  102. {
  103. return config_descriptor;
  104. }
  105. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  106. {
  107. return device_quality_descriptor;
  108. }
  109. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  110. {
  111. if (index > 3) {
  112. return NULL;
  113. }
  114. return string_descriptors[index];
  115. }
  116. const struct usb_descriptor audio_v2_descriptor = {
  117. .device_descriptor_callback = device_descriptor_callback,
  118. .config_descriptor_callback = config_descriptor_callback,
  119. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  120. .string_descriptor_callback = string_descriptor_callback
  121. };
  122. #else
  123. const uint8_t audio_v2_descriptor[] = {
  124. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0001, 0x01),
  125. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  126. AUDIO_V2_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, AUDIO_CATEGORY_MICROPHONE, 0x00, 0x00),
  127. AUDIO_V2_AC_CLOCK_SOURCE_DESCRIPTOR_INIT(0x01, 0x03, 0x03),
  128. AUDIO_V2_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x02, AUDIO_INTERM_MIC, 0x01, IN_CHANNEL_NUM, INPUT_CH_ENABLE, 0x0000),
  129. AUDIO_V2_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x03, 0x02, INPUT_CTRL),
  130. AUDIO_V2_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x03, 0x01, 0x0000),
  131. AUDIO_V2_AS_DESCRIPTOR_INIT(0x01, 0x04, IN_CHANNEL_NUM, INPUT_CH_ENABLE, HALF_WORD_BYTES, SAMPLE_BITS, AUDIO_IN_EP, 0x05, (AUDIO_IN_PACKET + 4), EP_INTERVAL),
  132. ///////////////////////////////////////
  133. /// string0 descriptor
  134. ///////////////////////////////////////
  135. USB_LANGID_INIT(USBD_LANGID_STRING),
  136. ///////////////////////////////////////
  137. /// string1 descriptor
  138. ///////////////////////////////////////
  139. 0x14, /* bLength */
  140. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  141. 'C', 0x00, /* wcChar0 */
  142. 'h', 0x00, /* wcChar1 */
  143. 'e', 0x00, /* wcChar2 */
  144. 'r', 0x00, /* wcChar3 */
  145. 'r', 0x00, /* wcChar4 */
  146. 'y', 0x00, /* wcChar5 */
  147. 'U', 0x00, /* wcChar6 */
  148. 'S', 0x00, /* wcChar7 */
  149. 'B', 0x00, /* wcChar8 */
  150. ///////////////////////////////////////
  151. /// string2 descriptor
  152. ///////////////////////////////////////
  153. 0x26, /* bLength */
  154. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  155. 'C', 0x00, /* wcChar0 */
  156. 'h', 0x00, /* wcChar1 */
  157. 'e', 0x00, /* wcChar2 */
  158. 'r', 0x00, /* wcChar3 */
  159. 'r', 0x00, /* wcChar4 */
  160. 'y', 0x00, /* wcChar5 */
  161. 'U', 0x00, /* wcChar6 */
  162. 'S', 0x00, /* wcChar7 */
  163. 'B', 0x00, /* wcChar8 */
  164. ' ', 0x00, /* wcChar9 */
  165. 'U', 0x00, /* wcChar10 */
  166. 'A', 0x00, /* wcChar11 */
  167. 'C', 0x00, /* wcChar12 */
  168. ' ', 0x00, /* wcChar13 */
  169. 'D', 0x00, /* wcChar14 */
  170. 'E', 0x00, /* wcChar15 */
  171. 'M', 0x00, /* wcChar16 */
  172. 'O', 0x00, /* wcChar17 */
  173. ///////////////////////////////////////
  174. /// string3 descriptor
  175. ///////////////////////////////////////
  176. 0x16, /* bLength */
  177. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  178. '2', 0x00, /* wcChar0 */
  179. '0', 0x00, /* wcChar1 */
  180. '2', 0x00, /* wcChar2 */
  181. '1', 0x00, /* wcChar3 */
  182. '0', 0x00, /* wcChar4 */
  183. '3', 0x00, /* wcChar5 */
  184. '1', 0x00, /* wcChar6 */
  185. '0', 0x00, /* wcChar7 */
  186. '0', 0x00, /* wcChar8 */
  187. '4', 0x00, /* wcChar9 */
  188. #ifdef CONFIG_USB_HS
  189. ///////////////////////////////////////
  190. /// device qualifier descriptor
  191. ///////////////////////////////////////
  192. 0x0a,
  193. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  194. 0x00,
  195. 0x02,
  196. 0x00,
  197. 0x00,
  198. 0x00,
  199. 0x40,
  200. 0x00,
  201. 0x00,
  202. #endif
  203. 0x00
  204. };
  205. #endif
  206. static const uint8_t mic_default_sampling_freq_table[] = {
  207. AUDIO_SAMPLE_FREQ_NUM(1),
  208. AUDIO_SAMPLE_FREQ_4B(16000),
  209. AUDIO_SAMPLE_FREQ_4B(16000),
  210. AUDIO_SAMPLE_FREQ_4B(0x00)
  211. };
  212. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[AUDIO_IN_PACKET];
  213. volatile bool tx_flag = 0;
  214. volatile bool ep_tx_busy_flag = false;
  215. static void usbd_event_handler(uint8_t busid, uint8_t event)
  216. {
  217. switch (event) {
  218. case USBD_EVENT_RESET:
  219. break;
  220. case USBD_EVENT_CONNECTED:
  221. break;
  222. case USBD_EVENT_DISCONNECTED:
  223. break;
  224. case USBD_EVENT_RESUME:
  225. break;
  226. case USBD_EVENT_SUSPEND:
  227. break;
  228. case USBD_EVENT_CONFIGURED:
  229. break;
  230. case USBD_EVENT_SET_REMOTE_WAKEUP:
  231. break;
  232. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. void usbd_audio_open(uint8_t busid, uint8_t intf)
  239. {
  240. tx_flag = 1;
  241. USB_LOG_RAW("OPEN\r\n");
  242. }
  243. void usbd_audio_close(uint8_t busid, uint8_t intf)
  244. {
  245. USB_LOG_RAW("CLOSE\r\n");
  246. tx_flag = 0;
  247. }
  248. void usbd_audio_get_sampling_freq_table(uint8_t busid, uint8_t ep, uint8_t **sampling_freq_table)
  249. {
  250. if (ep == AUDIO_IN_EP) {
  251. *sampling_freq_table = (uint8_t *)mic_default_sampling_freq_table;
  252. }
  253. }
  254. void usbd_audio_iso_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  255. {
  256. ep_tx_busy_flag = false;
  257. }
  258. static struct usbd_endpoint audio_in_ep = {
  259. .ep_cb = usbd_audio_iso_in_callback,
  260. .ep_addr = AUDIO_IN_EP
  261. };
  262. struct usbd_interface intf0;
  263. struct usbd_interface intf1;
  264. struct audio_entity_info audio_entity_table[] = {
  265. { .bEntityId = AUDIO_IN_CLOCK_ID,
  266. .bDescriptorSubtype = AUDIO_CONTROL_CLOCK_SOURCE,
  267. .ep = AUDIO_IN_EP },
  268. { .bEntityId = AUDIO_IN_FU_ID,
  269. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  270. .ep = AUDIO_IN_EP },
  271. };
  272. void audio_v2_init(uint8_t busid, uintptr_t reg_base)
  273. {
  274. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  275. usbd_desc_register(busid, &audio_v2_descriptor);
  276. #else
  277. usbd_desc_register(busid, audio_v2_descriptor);
  278. #endif
  279. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0200, audio_entity_table, 2));
  280. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0200, audio_entity_table, 2));
  281. usbd_add_endpoint(busid, &audio_in_ep);
  282. usbd_initialize(busid, reg_base, usbd_event_handler);
  283. }
  284. void audio_v2_test(uint8_t busid)
  285. {
  286. if (tx_flag) {
  287. memset(write_buffer, 'a', AUDIO_IN_PACKET);
  288. ep_tx_busy_flag = true;
  289. usbd_ep_start_write(busid, AUDIO_IN_EP, write_buffer, AUDIO_IN_PACKET);
  290. while (ep_tx_busy_flag) {
  291. if (tx_flag == false) {
  292. break;
  293. }
  294. }
  295. }
  296. }