audio_v1_mic_multichan_template.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  61. static const uint8_t device_descriptor[] = {
  62. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  63. };
  64. static const uint8_t config_descriptor[] = {
  65. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  66. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
  67. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
  68. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
  69. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, AUDIO_IN_FU_ID),
  70. 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))
  71. };
  72. static const uint8_t device_quality_descriptor[] = {
  73. ///////////////////////////////////////
  74. /// device qualifier descriptor
  75. ///////////////////////////////////////
  76. 0x0a,
  77. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  78. 0x00,
  79. 0x02,
  80. 0x00,
  81. 0x00,
  82. 0x00,
  83. 0x40,
  84. 0x00,
  85. 0x00,
  86. };
  87. static const char *string_descriptors[] = {
  88. (const char[]){ 0x09, 0x04 }, /* Langid */
  89. "CherryUSB", /* Manufacturer */
  90. "CherryUSB UAC DEMO", /* Product */
  91. "2022123456", /* Serial Number */
  92. };
  93. static const uint8_t *device_descriptor_callback(uint8_t speed)
  94. {
  95. return device_descriptor;
  96. }
  97. static const uint8_t *config_descriptor_callback(uint8_t speed)
  98. {
  99. return config_descriptor;
  100. }
  101. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  102. {
  103. return device_quality_descriptor;
  104. }
  105. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  106. {
  107. if (index > 3) {
  108. return NULL;
  109. }
  110. return string_descriptors[index];
  111. }
  112. const struct usb_descriptor audio_v1_descriptor = {
  113. .device_descriptor_callback = device_descriptor_callback,
  114. .config_descriptor_callback = config_descriptor_callback,
  115. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  116. .string_descriptor_callback = string_descriptor_callback
  117. };
  118. #else
  119. const uint8_t audio_v1_descriptor[] = {
  120. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  121. USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  122. AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x02, AUDIO_AC_SIZ, 0x00, 0x01),
  123. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, IN_CHANNEL_NUM, INPUT_CH_ENABLE),
  124. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(AUDIO_IN_FU_ID, 0x01, 0x01, INPUT_CTRL),
  125. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, AUDIO_IN_FU_ID),
  126. 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)),
  127. ///////////////////////////////////////
  128. /// string0 descriptor
  129. ///////////////////////////////////////
  130. USB_LANGID_INIT(USBD_LANGID_STRING),
  131. ///////////////////////////////////////
  132. /// string1 descriptor
  133. ///////////////////////////////////////
  134. 0x14, /* bLength */
  135. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  136. 'C', 0x00, /* wcChar0 */
  137. 'h', 0x00, /* wcChar1 */
  138. 'e', 0x00, /* wcChar2 */
  139. 'r', 0x00, /* wcChar3 */
  140. 'r', 0x00, /* wcChar4 */
  141. 'y', 0x00, /* wcChar5 */
  142. 'U', 0x00, /* wcChar6 */
  143. 'S', 0x00, /* wcChar7 */
  144. 'B', 0x00, /* wcChar8 */
  145. ///////////////////////////////////////
  146. /// string2 descriptor
  147. ///////////////////////////////////////
  148. 0x26, /* bLength */
  149. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  150. 'C', 0x00, /* wcChar0 */
  151. 'h', 0x00, /* wcChar1 */
  152. 'e', 0x00, /* wcChar2 */
  153. 'r', 0x00, /* wcChar3 */
  154. 'r', 0x00, /* wcChar4 */
  155. 'y', 0x00, /* wcChar5 */
  156. 'U', 0x00, /* wcChar6 */
  157. 'S', 0x00, /* wcChar7 */
  158. 'B', 0x00, /* wcChar8 */
  159. ' ', 0x00, /* wcChar9 */
  160. 'U', 0x00, /* wcChar10 */
  161. 'A', 0x00, /* wcChar11 */
  162. 'C', 0x00, /* wcChar12 */
  163. ' ', 0x00, /* wcChar13 */
  164. 'D', 0x00, /* wcChar14 */
  165. 'E', 0x00, /* wcChar15 */
  166. 'M', 0x00, /* wcChar16 */
  167. 'O', 0x00, /* wcChar17 */
  168. ///////////////////////////////////////
  169. /// string3 descriptor
  170. ///////////////////////////////////////
  171. 0x16, /* bLength */
  172. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  173. '2', 0x00, /* wcChar0 */
  174. '0', 0x00, /* wcChar1 */
  175. '2', 0x00, /* wcChar2 */
  176. '2', 0x00, /* wcChar3 */
  177. '1', 0x00, /* wcChar4 */
  178. '2', 0x00, /* wcChar5 */
  179. '3', 0x00, /* wcChar6 */
  180. '4', 0x00, /* wcChar7 */
  181. '5', 0x00, /* wcChar8 */
  182. '0' + IN_CHANNEL_NUM, 0x00, /* wcChar9 */
  183. #ifdef CONFIG_USB_HS
  184. ///////////////////////////////////////
  185. /// device qualifier descriptor
  186. ///////////////////////////////////////
  187. 0x0a,
  188. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  189. 0x00,
  190. 0x02,
  191. 0x00,
  192. 0x00,
  193. 0x00,
  194. 0x40,
  195. 0x00,
  196. 0x00,
  197. #endif
  198. 0x00
  199. };
  200. #endif
  201. volatile bool tx_flag = 0;
  202. volatile bool ep_tx_busy_flag = false;
  203. static void usbd_event_handler(uint8_t busid, uint8_t event)
  204. {
  205. switch (event) {
  206. case USBD_EVENT_RESET:
  207. break;
  208. case USBD_EVENT_CONNECTED:
  209. break;
  210. case USBD_EVENT_DISCONNECTED:
  211. break;
  212. case USBD_EVENT_RESUME:
  213. break;
  214. case USBD_EVENT_SUSPEND:
  215. break;
  216. case USBD_EVENT_CONFIGURED:
  217. break;
  218. case USBD_EVENT_SET_REMOTE_WAKEUP:
  219. break;
  220. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. void usbd_audio_open(uint8_t busid, uint8_t intf)
  227. {
  228. tx_flag = 1;
  229. ep_tx_busy_flag = false;
  230. USB_LOG_RAW("OPEN\r\n");
  231. }
  232. void usbd_audio_close(uint8_t busid, uint8_t intf)
  233. {
  234. USB_LOG_RAW("CLOSE\r\n");
  235. ep_tx_busy_flag = false;
  236. tx_flag = 0;
  237. }
  238. void usbd_audio_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  239. {
  240. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  241. ep_tx_busy_flag = false;
  242. }
  243. static struct usbd_endpoint audio_in_ep = {
  244. .ep_cb = usbd_audio_iso_callback,
  245. .ep_addr = AUDIO_IN_EP
  246. };
  247. struct usbd_interface intf0;
  248. struct usbd_interface intf1;
  249. struct audio_entity_info audio_entity_table[] = {
  250. { .bEntityId = AUDIO_IN_FU_ID,
  251. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  252. .ep = AUDIO_IN_EP },
  253. };
  254. void audio_v1_init(uint8_t busid, uintptr_t reg_base)
  255. {
  256. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  257. usbd_desc_register(busid, &audio_v1_descriptor);
  258. #else
  259. usbd_desc_register(busid, audio_v1_descriptor);
  260. #endif
  261. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0100, audio_entity_table, 1));
  262. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0100, audio_entity_table, 1));
  263. usbd_add_endpoint(busid, &audio_in_ep);
  264. usbd_initialize(busid, reg_base, usbd_event_handler);
  265. }
  266. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[AUDIO_IN_PACKET];
  267. void audio_test(uint8_t busid)
  268. {
  269. while (1) {
  270. if (tx_flag) {
  271. memset(write_buffer, 'a', AUDIO_IN_PACKET);
  272. ep_tx_busy_flag = true;
  273. usbd_ep_start_write(busid, AUDIO_IN_EP, write_buffer, AUDIO_IN_PACKET);
  274. while (ep_tx_busy_flag) {
  275. if (tx_flag == false) {
  276. break;
  277. }
  278. }
  279. }
  280. }
  281. }