audio_v1_mic_multichan_template.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #define AUDIO_MIC_FREQ 16000U
  20. #define AUDIO_MIC_FRAME_SIZE_BYTE 2u
  21. #define AUDIO_MIC_RESOLUTION_BIT 16u
  22. #define IN_CHANNEL_NUM 2
  23. #if IN_CHANNEL_NUM == 1
  24. #define INPUT_CTRL 0x03, 0x03
  25. #define INPUT_CH_ENABLE 0x0001
  26. #elif IN_CHANNEL_NUM == 2
  27. #define INPUT_CTRL 0x03, 0x03, 0x03
  28. #define INPUT_CH_ENABLE 0x0003
  29. #elif IN_CHANNEL_NUM == 3
  30. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03
  31. #define INPUT_CH_ENABLE 0x0007
  32. #elif IN_CHANNEL_NUM == 4
  33. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03
  34. #define INPUT_CH_ENABLE 0x000f
  35. #elif IN_CHANNEL_NUM == 5
  36. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  37. #define INPUT_CH_ENABLE 0x001f
  38. #elif IN_CHANNEL_NUM == 6
  39. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  40. #define INPUT_CH_ENABLE 0x003F
  41. #elif IN_CHANNEL_NUM == 7
  42. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  43. #define INPUT_CH_ENABLE 0x007f
  44. #elif IN_CHANNEL_NUM == 8
  45. #define INPUT_CTRL 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  46. #define INPUT_CH_ENABLE 0x00ff
  47. #endif
  48. /* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 1) */
  49. /* 16bit(2 Bytes) 单声道(Mono:1) */
  50. #define AUDIO_IN_PACKET ((uint32_t)((AUDIO_MIC_FREQ * 2 * IN_CHANNEL_NUM) / 1000))
  51. #define USB_CONFIG_SIZE (unsigned long)(9 + \
  52. AUDIO_AC_DESCRIPTOR_LEN(1) + \
  53. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  54. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
  55. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  56. AUDIO_AS_DESCRIPTOR_LEN(1))
  57. #define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(1) + \
  58. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  59. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(IN_CHANNEL_NUM, 1) + \
  60. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
  61. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  62. static const uint8_t device_descriptor[] = {
  63. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  64. };
  65. static const uint8_t config_descriptor[] = {
  66. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  67. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
  68. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
  69. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
  70. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, AUDIO_IN_FU_ID),
  71. AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x03, IN_CHANNEL_NUM, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ))
  72. };
  73. static const uint8_t device_quality_descriptor[] = {
  74. ///////////////////////////////////////
  75. /// device qualifier descriptor
  76. ///////////////////////////////////////
  77. 0x0a,
  78. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  79. 0x00,
  80. 0x02,
  81. 0x00,
  82. 0x00,
  83. 0x00,
  84. 0x40,
  85. 0x00,
  86. 0x00,
  87. };
  88. static const char *string_descriptors[] = {
  89. (const char[]){ 0x09, 0x04 }, /* Langid */
  90. "CherryUSB", /* Manufacturer */
  91. "CherryUSB UAC DEMO", /* Product */
  92. "2022123456", /* Serial Number */
  93. };
  94. static const uint8_t *device_descriptor_callback(uint8_t speed)
  95. {
  96. return device_descriptor;
  97. }
  98. static const uint8_t *config_descriptor_callback(uint8_t speed)
  99. {
  100. return config_descriptor;
  101. }
  102. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  103. {
  104. return device_quality_descriptor;
  105. }
  106. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  107. {
  108. if (index > 3) {
  109. return NULL;
  110. }
  111. return string_descriptors[index];
  112. }
  113. const struct usb_descriptor audio_v1_descriptor = {
  114. .device_descriptor_callback = device_descriptor_callback,
  115. .config_descriptor_callback = config_descriptor_callback,
  116. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  117. .string_descriptor_callback = string_descriptor_callback
  118. };
  119. #else
  120. const uint8_t audio_v1_descriptor[] = {
  121. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  122. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  123. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
  124. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
  125. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
  126. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, AUDIO_IN_FU_ID),
  127. AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x03, IN_CHANNEL_NUM, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ)),
  128. ///////////////////////////////////////
  129. /// string0 descriptor
  130. ///////////////////////////////////////
  131. USB_LANGID_INIT(USBD_LANGID_STRING),
  132. ///////////////////////////////////////
  133. /// string1 descriptor
  134. ///////////////////////////////////////
  135. 0x14, /* bLength */
  136. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  137. 'C', 0x00, /* wcChar0 */
  138. 'h', 0x00, /* wcChar1 */
  139. 'e', 0x00, /* wcChar2 */
  140. 'r', 0x00, /* wcChar3 */
  141. 'r', 0x00, /* wcChar4 */
  142. 'y', 0x00, /* wcChar5 */
  143. 'U', 0x00, /* wcChar6 */
  144. 'S', 0x00, /* wcChar7 */
  145. 'B', 0x00, /* wcChar8 */
  146. ///////////////////////////////////////
  147. /// string2 descriptor
  148. ///////////////////////////////////////
  149. 0x26, /* bLength */
  150. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  151. 'C', 0x00, /* wcChar0 */
  152. 'h', 0x00, /* wcChar1 */
  153. 'e', 0x00, /* wcChar2 */
  154. 'r', 0x00, /* wcChar3 */
  155. 'r', 0x00, /* wcChar4 */
  156. 'y', 0x00, /* wcChar5 */
  157. 'U', 0x00, /* wcChar6 */
  158. 'S', 0x00, /* wcChar7 */
  159. 'B', 0x00, /* wcChar8 */
  160. ' ', 0x00, /* wcChar9 */
  161. 'U', 0x00, /* wcChar10 */
  162. 'A', 0x00, /* wcChar11 */
  163. 'C', 0x00, /* wcChar12 */
  164. ' ', 0x00, /* wcChar13 */
  165. 'D', 0x00, /* wcChar14 */
  166. 'E', 0x00, /* wcChar15 */
  167. 'M', 0x00, /* wcChar16 */
  168. 'O', 0x00, /* wcChar17 */
  169. ///////////////////////////////////////
  170. /// string3 descriptor
  171. ///////////////////////////////////////
  172. 0x16, /* bLength */
  173. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  174. '2', 0x00, /* wcChar0 */
  175. '0', 0x00, /* wcChar1 */
  176. '2', 0x00, /* wcChar2 */
  177. '2', 0x00, /* wcChar3 */
  178. '1', 0x00, /* wcChar4 */
  179. '2', 0x00, /* wcChar5 */
  180. '3', 0x00, /* wcChar6 */
  181. '4', 0x00, /* wcChar7 */
  182. '5', 0x00, /* wcChar8 */
  183. '0' + IN_CHANNEL_NUM, 0x00, /* wcChar9 */
  184. #ifdef CONFIG_USB_HS
  185. ///////////////////////////////////////
  186. /// device qualifier descriptor
  187. ///////////////////////////////////////
  188. 0x0a,
  189. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  190. 0x00,
  191. 0x02,
  192. 0x00,
  193. 0x00,
  194. 0x00,
  195. 0x40,
  196. 0x00,
  197. 0x00,
  198. #endif
  199. 0x00
  200. };
  201. #endif
  202. volatile bool tx_flag = 0;
  203. volatile bool ep_tx_busy_flag = false;
  204. volatile uint32_t s_mic_sample_rate;
  205. static void usbd_event_handler(uint8_t busid, uint8_t event)
  206. {
  207. switch (event) {
  208. case USBD_EVENT_RESET:
  209. break;
  210. case USBD_EVENT_CONNECTED:
  211. break;
  212. case USBD_EVENT_DISCONNECTED:
  213. break;
  214. case USBD_EVENT_RESUME:
  215. break;
  216. case USBD_EVENT_SUSPEND:
  217. break;
  218. case USBD_EVENT_CONFIGURED:
  219. break;
  220. case USBD_EVENT_SET_REMOTE_WAKEUP:
  221. break;
  222. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. void usbd_audio_open(uint8_t busid, uint8_t intf)
  229. {
  230. tx_flag = 1;
  231. ep_tx_busy_flag = false;
  232. USB_LOG_RAW("OPEN\r\n");
  233. }
  234. void usbd_audio_close(uint8_t busid, uint8_t intf)
  235. {
  236. USB_LOG_RAW("CLOSE\r\n");
  237. ep_tx_busy_flag = false;
  238. tx_flag = 0;
  239. }
  240. void usbd_audio_set_sampling_freq(uint8_t busid, uint8_t ep, uint32_t sampling_freq)
  241. {
  242. if (ep == AUDIO_IN_EP) {
  243. s_mic_sample_rate = sampling_freq;
  244. }
  245. }
  246. uint32_t usbd_audio_get_sampling_freq(uint8_t busid, uint8_t ep)
  247. {
  248. (void)busid;
  249. uint32_t freq = 0;
  250. if (ep == AUDIO_IN_EP) {
  251. freq = s_mic_sample_rate;
  252. }
  253. return freq;
  254. }
  255. void usbd_audio_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  256. {
  257. USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
  258. ep_tx_busy_flag = false;
  259. }
  260. static struct usbd_endpoint audio_in_ep = {
  261. .ep_cb = usbd_audio_iso_callback,
  262. .ep_addr = AUDIO_IN_EP
  263. };
  264. struct usbd_interface intf0;
  265. struct usbd_interface intf1;
  266. struct audio_entity_info audio_entity_table[] = {
  267. { .bEntityId = AUDIO_IN_FU_ID,
  268. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  269. .ep = AUDIO_IN_EP },
  270. };
  271. // In windows, audio driver cannot remove auto, so when you modify any descriptor information, please modify string descriptors too.
  272. void audio_v1_init(uint8_t busid, uintptr_t reg_base)
  273. {
  274. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  275. usbd_desc_register(busid, &audio_v1_descriptor);
  276. #else
  277. usbd_desc_register(busid, audio_v1_descriptor);
  278. #endif
  279. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0100, audio_entity_table, 1));
  280. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0100, audio_entity_table, 1));
  281. usbd_add_endpoint(busid, &audio_in_ep);
  282. usbd_initialize(busid, reg_base, usbd_event_handler);
  283. }
  284. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[AUDIO_IN_PACKET];
  285. void audio_test(uint8_t busid)
  286. {
  287. while (1) {
  288. if (tx_flag) {
  289. memset(write_buffer, 'a', AUDIO_IN_PACKET);
  290. ep_tx_busy_flag = true;
  291. usbd_ep_start_write(busid, AUDIO_IN_EP, write_buffer, AUDIO_IN_PACKET);
  292. while (ep_tx_busy_flag) {
  293. if (tx_flag == false) {
  294. break;
  295. }
  296. }
  297. }
  298. }
  299. }