1
0

audio_mic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-09-07 flybreak the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #include "drivers/usb_device.h"
  13. #include "uaudioreg.h"
  14. #define DBG_TAG "usbd.audio.mic"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #define RECORD_SAMPLERATE 16000
  18. #define RECORD_CHANNEL 1
  19. #define RESOLUTION_BITS 16
  20. #define RESOLUTION_BYTE (RESOLUTION_BITS / 8)
  21. #define RECORD_PER_MS_SZ ((RECORD_SAMPLERATE * RECORD_CHANNEL * RESOLUTION_BYTE) / 1000)
  22. #define RECORD_BUFFER_SZ (RECORD_PER_MS_SZ * 20) /* 20ms */
  23. #if defined(RT_USBD_MIC_DEVICE_NAME)
  24. #define MIC_DEVICE_NAME RT_USBD_MIC_DEVICE_NAME
  25. #else
  26. #define MIC_DEVICE_NAME "mic0"
  27. #endif
  28. #define EVENT_RECORD_START (1 << 0)
  29. #define EVENT_RECORD_STOP (1 << 1)
  30. #define EVENT_RECORD_DATA (1 << 2)
  31. #define MIC_INTF_STR_INDEX 8
  32. /*
  33. * uac mic descriptor define
  34. */
  35. #define UAC_CS_INTERFACE 0x24
  36. #define UAC_CS_ENDPOINT 0x25
  37. #define UAC_MAX_PACKET_SIZE 64
  38. #define UAC_EP_MAX_PACKET_SIZE 32
  39. #define UAC_CHANNEL_NUM RECORD_CHANNEL
  40. struct uac_ac_descriptor
  41. {
  42. #ifdef RT_USB_DEVICE_COMPOSITE
  43. struct uiad_descriptor iad_desc;
  44. #endif
  45. struct uinterface_descriptor intf_desc;
  46. struct usb_audio_control_descriptor hdr_desc;
  47. struct usb_audio_input_terminal it_desc;
  48. struct usb_audio_output_terminal ot_desc;
  49. #if UAC_USE_FEATURE_UNIT
  50. struct usb_audio_feature_unit feature_unit_desc;
  51. #endif
  52. };
  53. struct uac_as_descriptor
  54. {
  55. struct uinterface_descriptor intf_desc;
  56. struct usb_audio_streaming_interface_descriptor hdr_desc;
  57. struct usb_audio_streaming_type1_descriptor format_type_desc;
  58. struct uendpoint_descriptor ep_desc;
  59. struct usb_audio_streaming_endpoint_descriptor as_ep_desc;
  60. };
  61. /*
  62. * uac mic device type
  63. */
  64. struct uac_audio_mic
  65. {
  66. rt_device_t dev;
  67. rt_event_t event;
  68. rt_uint8_t open_count;
  69. rt_uint8_t *buffer;
  70. rt_uint32_t buffer_index;
  71. uep_t ep;
  72. };
  73. static struct uac_audio_mic mic;
  74. ALIGN(4)
  75. static struct udevice_descriptor dev_desc =
  76. {
  77. USB_DESC_LENGTH_DEVICE, //bLength;
  78. USB_DESC_TYPE_DEVICE, //type;
  79. USB_BCD_VERSION, //bcdUSB;
  80. USB_CLASS_DEVICE, //bDeviceClass;
  81. 0x00, //bDeviceSubClass;
  82. 0x00, //bDeviceProtocol;
  83. UAC_MAX_PACKET_SIZE, //bMaxPacketSize0;
  84. _VENDOR_ID, //idVendor;
  85. _PRODUCT_ID, //idProduct;
  86. USB_BCD_DEVICE, //bcdDevice;
  87. USB_STRING_MANU_INDEX, //iManufacturer;
  88. USB_STRING_PRODUCT_INDEX, //iProduct;
  89. USB_STRING_SERIAL_INDEX, //iSerialNumber;Unused.
  90. USB_DYNAMIC, //bNumConfigurations;
  91. };
  92. //FS and HS needed
  93. ALIGN(4)
  94. static struct usb_qualifier_descriptor dev_qualifier =
  95. {
  96. sizeof(dev_qualifier), //bLength
  97. USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
  98. 0x0200, //bcdUSB
  99. USB_CLASS_AUDIO, //bDeviceClass
  100. 0x00, //bDeviceSubClass
  101. 0x00, //bDeviceProtocol
  102. 64, //bMaxPacketSize0
  103. 0x01, //bNumConfigurations
  104. 0,
  105. };
  106. ALIGN(4)
  107. const static char *_ustring[] =
  108. {
  109. "Language",
  110. "RT-Thread Team.",
  111. "RT-Thread Audio Microphone",
  112. "32021919830108",
  113. "Configuration",
  114. "Interface",
  115. };
  116. ALIGN(4)
  117. static struct uac_ac_descriptor ac_desc =
  118. {
  119. #ifdef RT_USB_DEVICE_COMPOSITE
  120. /* Interface Association Descriptor */
  121. {
  122. USB_DESC_LENGTH_IAD,
  123. USB_DESC_TYPE_IAD,
  124. USB_DYNAMIC,
  125. 0x02,
  126. USB_CLASS_AUDIO,
  127. USB_SUBCLASS_AUDIOSTREAMING,
  128. 0x00,
  129. 0x00,
  130. },
  131. #endif
  132. /* Interface Descriptor */
  133. {
  134. USB_DESC_LENGTH_INTERFACE,
  135. USB_DESC_TYPE_INTERFACE,
  136. USB_DYNAMIC,
  137. 0x00,
  138. 0x00,
  139. USB_CLASS_AUDIO,
  140. USB_SUBCLASS_AUDIOCONTROL,
  141. 0x00,
  142. #ifdef RT_USB_DEVICE_COMPOSITE
  143. MIC_INTF_STR_INDEX,
  144. #else
  145. 0x00,
  146. #endif
  147. },
  148. /* Header Descriptor */
  149. {
  150. sizeof(struct usb_audio_control_descriptor),
  151. UAC_CS_INTERFACE,
  152. UDESCSUB_AC_HEADER,
  153. 0x0100, /* Version: 1.00 */
  154. 0x001E, /* Total length: 30 */
  155. 0x01, /* Total number of interfaces: 1 */
  156. {0x01}, /* Interface number: 1 */
  157. },
  158. /* Input Terminal Descriptor */
  159. {
  160. sizeof(struct usb_audio_input_terminal),
  161. UAC_CS_INTERFACE,
  162. UDESCSUB_AC_INPUT,
  163. 0x01, /* Terminal ID: 1 */
  164. 0x0201, /* Terminal Type: Microphone (0x0201) */
  165. 0x00, /* Assoc Terminal: 0 */
  166. 0x01, /* Number Channels: 1 */
  167. 0x0000, /* Channel Config: 0x0000 */
  168. 0x00, /* Channel Names: 0 */
  169. 0x00, /* Terminal: 0 */
  170. },
  171. /* Output Terminal Descriptor */
  172. {
  173. sizeof(struct usb_audio_output_terminal),
  174. UAC_CS_INTERFACE,
  175. UDESCSUB_AC_OUTPUT,
  176. 0x02, /* Terminal ID: 2 */
  177. 0x0101, /* Terminal Type: USB Streaming (0x0101) */
  178. 0x00, /* Assoc Terminal: 0 */
  179. 0x01, /* Source ID: 1 */
  180. 0x00, /* Terminal: 0 */
  181. },
  182. #if UAC_USE_FEATURE_UNIT
  183. /* Feature unit Descriptor */
  184. {
  185. sizeof(struct usb_audio_feature_unit),
  186. UAC_CS_INTERFACE,
  187. UDESCSUB_AC_FEATURE,
  188. 0x02,
  189. 0x01,
  190. 0x01,
  191. 0x00,
  192. 0x01,
  193. },
  194. #endif
  195. };
  196. ALIGN(4)
  197. static struct uinterface_descriptor as_desc0 =
  198. {
  199. USB_DESC_LENGTH_INTERFACE,
  200. USB_DESC_TYPE_INTERFACE,
  201. USB_DYNAMIC,
  202. 0x00,
  203. 0x00,
  204. USB_CLASS_AUDIO,
  205. USB_SUBCLASS_AUDIOSTREAMING,
  206. 0x00,
  207. 0x00,
  208. };
  209. ALIGN(4)
  210. static struct uac_as_descriptor as_desc =
  211. {
  212. /* Interface Descriptor */
  213. {
  214. USB_DESC_LENGTH_INTERFACE,
  215. USB_DESC_TYPE_INTERFACE,
  216. USB_DYNAMIC,
  217. 0x01,
  218. 0x01,
  219. USB_CLASS_AUDIO,
  220. USB_SUBCLASS_AUDIOSTREAMING,
  221. 0x00,
  222. 0x00,
  223. },
  224. /* General AS Descriptor */
  225. {
  226. sizeof(struct usb_audio_streaming_interface_descriptor),
  227. UAC_CS_INTERFACE,
  228. AS_GENERAL,
  229. 0x02, /* Terminal ID: 2 */
  230. 0x01, /* Interface delay in frames: 1 */
  231. UA_FMT_PCM,
  232. },
  233. /* Format type i Descriptor */
  234. {
  235. sizeof(struct usb_audio_streaming_type1_descriptor),
  236. UAC_CS_INTERFACE,
  237. FORMAT_TYPE,
  238. FORMAT_TYPE_I,
  239. UAC_CHANNEL_NUM,
  240. 2, /* Subframe Size: 2 */
  241. RESOLUTION_BITS,
  242. 0x01, /* Samples Frequence Type: 1 */
  243. {0}, /* Samples Frequence */
  244. },
  245. /* Endpoint Descriptor */
  246. {
  247. USB_DESC_LENGTH_ENDPOINT,
  248. USB_DESC_TYPE_ENDPOINT,
  249. USB_DYNAMIC | USB_DIR_IN,
  250. USB_EP_ATTR_ISOC,
  251. UAC_EP_MAX_PACKET_SIZE,
  252. 0x01,
  253. },
  254. /* AS Endpoint Descriptor */
  255. {
  256. sizeof(struct usb_audio_streaming_endpoint_descriptor),
  257. UAC_CS_ENDPOINT,
  258. AS_GENERAL,
  259. },
  260. };
  261. void mic_entry(void *parameter)
  262. {
  263. struct rt_audio_caps caps = {0};
  264. rt_uint32_t e, index;
  265. mic.buffer = rt_malloc(RECORD_BUFFER_SZ);
  266. if (mic.buffer == RT_NULL)
  267. {
  268. LOG_E("malloc failed");
  269. goto __exit;
  270. }
  271. mic.dev = rt_device_find(MIC_DEVICE_NAME);
  272. if (mic.dev == RT_NULL)
  273. {
  274. LOG_E("can't find device:%s", MIC_DEVICE_NAME);
  275. goto __exit;
  276. }
  277. while (1)
  278. {
  279. if (rt_event_recv(mic.event, EVENT_RECORD_START | EVENT_RECORD_STOP,
  280. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  281. 1000, &e) != RT_EOK)
  282. {
  283. continue;
  284. }
  285. if (mic.open_count == 0)
  286. {
  287. continue;
  288. }
  289. LOG_D("record start");
  290. rt_device_open(mic.dev, RT_DEVICE_OFLAG_RDONLY);
  291. caps.main_type = AUDIO_TYPE_INPUT;
  292. caps.sub_type = AUDIO_DSP_PARAM;
  293. caps.udata.config.samplerate = RECORD_SAMPLERATE;
  294. caps.udata.config.channels = RECORD_CHANNEL;
  295. caps.udata.config.samplebits = RESOLUTION_BITS;
  296. rt_device_control(mic.dev, AUDIO_CTL_CONFIGURE, &caps);
  297. while (1)
  298. {
  299. if (rt_event_recv(mic.event, EVENT_RECORD_DATA | EVENT_RECORD_STOP,
  300. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  301. 1000, &e) != RT_EOK)
  302. {
  303. if (mic.open_count > 0)
  304. continue;
  305. else
  306. break;
  307. }
  308. if (e & EVENT_RECORD_DATA)
  309. {
  310. index = (mic.buffer_index >= RECORD_BUFFER_SZ / 2) ? 0 : (RECORD_BUFFER_SZ / 2);
  311. rt_device_read(mic.dev, 0, mic.buffer + index, RECORD_BUFFER_SZ / 2);
  312. }
  313. else if (e & EVENT_RECORD_STOP)
  314. {
  315. break;
  316. }
  317. }
  318. LOG_D("record stop");
  319. rt_device_close(mic.dev);
  320. }
  321. __exit:
  322. if (mic.buffer)
  323. rt_free(mic.buffer);
  324. }
  325. static rt_err_t _record_start(ufunction_t func)
  326. {
  327. mic.ep->request.buffer = RT_NULL;
  328. mic.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
  329. mic.ep->request.req_type = UIO_REQUEST_WRITE;
  330. rt_usbd_io_request(func->device, mic.ep, &mic.ep->request);
  331. mic.open_count ++;
  332. rt_event_send(mic.event, EVENT_RECORD_START);
  333. return 0;
  334. }
  335. static rt_err_t _record_stop(ufunction_t func)
  336. {
  337. mic.open_count --;
  338. rt_event_send(mic.event, EVENT_RECORD_STOP);
  339. return 0;
  340. }
  341. static rt_err_t _ep_data_in_handler(ufunction_t func, rt_size_t size)
  342. {
  343. RT_ASSERT(func != RT_NULL);
  344. LOG_D("_ep_data_in_handler");
  345. mic.ep->request.buffer = mic.buffer + mic.buffer_index;
  346. mic.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
  347. mic.ep->request.req_type = UIO_REQUEST_WRITE;
  348. rt_usbd_io_request(func->device, mic.ep, &mic.ep->request);
  349. mic.buffer_index += UAC_EP_MAX_PACKET_SIZE;
  350. if (mic.buffer_index >= RECORD_BUFFER_SZ)
  351. {
  352. mic.buffer_index = 0;
  353. rt_event_send(mic.event, EVENT_RECORD_DATA);
  354. }
  355. else if (mic.buffer_index == RECORD_BUFFER_SZ / 2)
  356. {
  357. rt_event_send(mic.event, EVENT_RECORD_DATA);
  358. }
  359. return RT_EOK;
  360. }
  361. static rt_err_t _interface_as_handler(ufunction_t func, ureq_t setup)
  362. {
  363. RT_ASSERT(func != RT_NULL);
  364. RT_ASSERT(func->device != RT_NULL);
  365. RT_ASSERT(setup != RT_NULL);
  366. LOG_D("_interface_as_handler");
  367. if ((setup->request_type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
  368. {
  369. switch (setup->bRequest)
  370. {
  371. case USB_REQ_GET_INTERFACE:
  372. break;
  373. case USB_REQ_SET_INTERFACE:
  374. LOG_D("set interface handler");
  375. if (setup->wValue == 1)
  376. {
  377. _record_start(func);
  378. }
  379. else if (setup->wValue == 0)
  380. {
  381. _record_stop(func);
  382. }
  383. break;
  384. default:
  385. LOG_D("unknown uac request 0x%x", setup->bRequest);
  386. return -RT_ERROR;
  387. }
  388. }
  389. return RT_EOK;
  390. }
  391. static rt_err_t _function_enable(ufunction_t func)
  392. {
  393. RT_ASSERT(func != RT_NULL);
  394. LOG_D("uac function enable");
  395. return RT_EOK;
  396. }
  397. static rt_err_t _function_disable(ufunction_t func)
  398. {
  399. RT_ASSERT(func != RT_NULL);
  400. LOG_D("uac function disable");
  401. _record_stop(func);
  402. return RT_EOK;
  403. }
  404. static struct ufunction_ops ops =
  405. {
  406. _function_enable,
  407. _function_disable,
  408. RT_NULL,
  409. };
  410. /**
  411. * This function will configure uac descriptor.
  412. *
  413. * @param comm the communication interface number.
  414. * @param data the data interface number.
  415. *
  416. * @return RT_EOK on successful.
  417. */
  418. static rt_err_t _uac_descriptor_config(struct uac_ac_descriptor *ac,
  419. rt_uint8_t cintf_nr, struct uac_as_descriptor *as, rt_uint8_t sintf_nr)
  420. {
  421. ac->hdr_desc.baInterfaceNr[0] = sintf_nr;
  422. #ifdef RT_USB_DEVICE_COMPOSITE
  423. ac->iad_desc.bFirstInterface = cintf_nr;
  424. #endif
  425. return RT_EOK;
  426. }
  427. static rt_err_t _uac_samplerate_config(struct uac_as_descriptor *as, rt_uint32_t samplerate)
  428. {
  429. as->format_type_desc.tSamFreq[0 * 3 + 2] = samplerate >> 16 & 0xff;
  430. as->format_type_desc.tSamFreq[0 * 3 + 1] = samplerate >> 8 & 0xff;
  431. as->format_type_desc.tSamFreq[0 * 3 + 0] = samplerate & 0xff;
  432. return RT_EOK;
  433. }
  434. /**
  435. * This function will create a uac function instance.
  436. *
  437. * @param device the usb device object.
  438. *
  439. * @return RT_EOK on successful.
  440. */
  441. ufunction_t rt_usbd_function_uac_mic_create(udevice_t device)
  442. {
  443. ufunction_t func;
  444. uintf_t intf_ac, intf_as;
  445. ualtsetting_t setting_as0;
  446. ualtsetting_t setting_ac, setting_as;
  447. struct uac_as_descriptor *as_desc_t;
  448. /* parameter check */
  449. RT_ASSERT(device != RT_NULL);
  450. #ifdef RT_USB_DEVICE_COMPOSITE
  451. rt_usbd_device_set_interface_string(device, MIC_INTF_STR_INDEX, _ustring[2]);
  452. #else
  453. /* set usb device string description */
  454. rt_usbd_device_set_string(device, _ustring);
  455. #endif
  456. /* create a uac function */
  457. func = rt_usbd_function_new(device, &dev_desc, &ops);
  458. //not support HS
  459. //rt_usbd_device_set_qualifier(device, &dev_qualifier);
  460. /* create interface */
  461. intf_ac = rt_usbd_interface_new(device, RT_NULL);
  462. intf_as = rt_usbd_interface_new(device, _interface_as_handler);
  463. /* create alternate setting */
  464. setting_ac = rt_usbd_altsetting_new(sizeof(struct uac_ac_descriptor));
  465. setting_as0 = rt_usbd_altsetting_new(sizeof(struct uinterface_descriptor));
  466. setting_as = rt_usbd_altsetting_new(sizeof(struct uac_as_descriptor));
  467. /* config desc in alternate setting */
  468. rt_usbd_altsetting_config_descriptor(setting_ac, &ac_desc,
  469. (rt_off_t) & ((struct uac_ac_descriptor *)0)->intf_desc);
  470. rt_usbd_altsetting_config_descriptor(setting_as0, &as_desc0, 0);
  471. rt_usbd_altsetting_config_descriptor(setting_as, &as_desc,
  472. (rt_off_t) & ((struct uac_as_descriptor *)0)->intf_desc);
  473. /* configure the uac interface descriptor */
  474. _uac_descriptor_config(setting_ac->desc, intf_ac->intf_num, setting_as->desc, intf_as->intf_num);
  475. _uac_samplerate_config(setting_as->desc, RECORD_SAMPLERATE);
  476. /* create endpoint */
  477. as_desc_t = (struct uac_as_descriptor *)setting_as->desc;
  478. mic.ep = rt_usbd_endpoint_new(&as_desc_t->ep_desc, _ep_data_in_handler);
  479. /* add the endpoint to the alternate setting */
  480. rt_usbd_altsetting_add_endpoint(setting_as, mic.ep);
  481. /* add the alternate setting to the interface, then set default setting of the interface */
  482. rt_usbd_interface_add_altsetting(intf_ac, setting_ac);
  483. rt_usbd_set_altsetting(intf_ac, 0);
  484. rt_usbd_interface_add_altsetting(intf_as, setting_as0);
  485. rt_usbd_interface_add_altsetting(intf_as, setting_as);
  486. rt_usbd_set_altsetting(intf_as, 0);
  487. /* add the interface to the uac function */
  488. rt_usbd_function_add_interface(func, intf_ac);
  489. rt_usbd_function_add_interface(func, intf_as);
  490. return func;
  491. }
  492. int audio_mic_init(void)
  493. {
  494. rt_thread_t mic_tid;
  495. mic.event = rt_event_create("mic_event", RT_IPC_FLAG_FIFO);
  496. mic_tid = rt_thread_create("mic_thread",
  497. mic_entry, RT_NULL,
  498. 1024,
  499. 5, 10);
  500. if (mic_tid != RT_NULL)
  501. rt_thread_startup(mic_tid);
  502. return RT_EOK;
  503. }
  504. INIT_COMPONENT_EXPORT(audio_mic_init);
  505. /*
  506. * register uac class
  507. */
  508. struct udclass uac_class =
  509. {
  510. .rt_usbd_function_create = rt_usbd_function_uac_mic_create
  511. };
  512. int rt_usbd_uac_mic_class_register(void)
  513. {
  514. rt_usbd_class_register(&uac_class);
  515. return 0;
  516. }
  517. INIT_PREV_EXPORT(rt_usbd_uac_mic_class_register);