audio_speaker.c 16 KB

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