audio_speaker.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. #define AUFMT_MAX_FREQUENCIES 1
  14. #include "uaudioreg.h"
  15. #define DBG_TAG "usbd.audio.speaker"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #define AUDIO_SAMPLERATE 16000
  19. #define AUDIO_CHANNEL 1
  20. #define RESOLUTION_BITS 16
  21. #define RESOLUTION_BYTE (RESOLUTION_BITS / 8)
  22. #define AUDIO_PER_MS_SZ ((AUDIO_SAMPLERATE * AUDIO_CHANNEL * RESOLUTION_BYTE) / 1000)
  23. #define AUDIO_BUFFER_SZ (AUDIO_PER_MS_SZ * 20) /* 20ms */
  24. #if defined(RT_USBD_SPEAKER_DEVICE_NAME)
  25. #define SPEAKER_DEVICE_NAME RT_USBD_SPEAKER_DEVICE_NAME
  26. #else
  27. #define SPEAKER_DEVICE_NAME "sound0"
  28. #endif
  29. #define EVENT_AUDIO_START (1 << 0)
  30. #define EVENT_AUDIO_STOP (1 << 1)
  31. #define EVENT_AUDIO_DATA (1 << 2)
  32. #define SPK_INTF_STR_INDEX 9
  33. /*
  34. * uac speaker descriptor define
  35. */
  36. #define UAC_CS_INTERFACE 0x24
  37. #define UAC_CS_ENDPOINT 0x25
  38. #define UAC_MAX_PACKET_SIZE 64
  39. #define UAC_EP_MAX_PACKET_SIZE 32
  40. #define UAC_CHANNEL_NUM AUDIO_CHANNEL
  41. struct uac_ac_descriptor
  42. {
  43. #ifdef RT_USB_DEVICE_COMPOSITE
  44. struct uiad_descriptor iad_desc;
  45. #endif
  46. struct uinterface_descriptor intf_desc;
  47. struct usb_audio_control_descriptor hdr_desc;
  48. struct usb_audio_input_terminal it_desc;
  49. struct usb_audio_output_terminal ot_desc;
  50. #if UAC_USE_FEATURE_UNIT
  51. struct usb_audio_feature_unit feature_unit_desc;
  52. #endif
  53. };
  54. struct uac_as_descriptor
  55. {
  56. struct uinterface_descriptor intf_desc;
  57. struct usb_audio_streaming_interface_descriptor hdr_desc;
  58. struct usb_audio_streaming_type1_descriptor format_type_desc;
  59. struct uendpoint_descriptor ep_desc;
  60. struct usb_audio_streaming_endpoint_descriptor as_ep_desc;
  61. };
  62. /*
  63. * uac speaker device type
  64. */
  65. struct uac_audio_speaker
  66. {
  67. rt_device_t dev;
  68. rt_event_t event;
  69. rt_uint8_t open_count;
  70. rt_uint8_t *buffer;
  71. rt_uint32_t buffer_index;
  72. uep_t ep;
  73. };
  74. static struct uac_audio_speaker speaker;
  75. ALIGN(4)
  76. static struct udevice_descriptor dev_desc =
  77. {
  78. USB_DESC_LENGTH_DEVICE, //bLength;
  79. USB_DESC_TYPE_DEVICE, //type;
  80. USB_BCD_VERSION, //bcdUSB;
  81. USB_CLASS_DEVICE, //bDeviceClass;
  82. 0x00, //bDeviceSubClass;
  83. 0x00, //bDeviceProtocol;
  84. UAC_MAX_PACKET_SIZE, //bMaxPacketSize0;
  85. _VENDOR_ID, //idVendor;
  86. _PRODUCT_ID, //idProduct;
  87. USB_BCD_DEVICE, //bcdDevice;
  88. USB_STRING_MANU_INDEX, //iManufacturer;
  89. USB_STRING_PRODUCT_INDEX, //iProduct;
  90. USB_STRING_SERIAL_INDEX, //iSerialNumber;Unused.
  91. USB_DYNAMIC, //bNumConfigurations;
  92. };
  93. //FS and HS needed
  94. ALIGN(4)
  95. static struct usb_qualifier_descriptor dev_qualifier =
  96. {
  97. sizeof(dev_qualifier), //bLength
  98. USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
  99. 0x0200, //bcdUSB
  100. USB_CLASS_AUDIO, //bDeviceClass
  101. 0x00, //bDeviceSubClass
  102. 0x00, //bDeviceProtocol
  103. 64, //bMaxPacketSize0
  104. 0x01, //bNumConfigurations
  105. 0,
  106. };
  107. ALIGN(4)
  108. const static char *_ustring[] =
  109. {
  110. "Language",
  111. "RT-Thread Team.",
  112. "RT-Thread Audio Speaker",
  113. "32021919830108",
  114. "Configuration",
  115. "Interface",
  116. };
  117. ALIGN(4)
  118. static struct uac_ac_descriptor ac_desc =
  119. {
  120. #ifdef RT_USB_DEVICE_COMPOSITE
  121. /* Interface Association Descriptor */
  122. {
  123. USB_DESC_LENGTH_IAD,
  124. USB_DESC_TYPE_IAD,
  125. USB_DYNAMIC,
  126. 0x02,
  127. USB_CLASS_AUDIO,
  128. USB_SUBCLASS_AUDIOSTREAMING,
  129. 0x00,
  130. 0x00,
  131. },
  132. #endif
  133. /* Interface Descriptor */
  134. {
  135. USB_DESC_LENGTH_INTERFACE,
  136. USB_DESC_TYPE_INTERFACE,
  137. USB_DYNAMIC,
  138. 0x00,
  139. 0x00,
  140. USB_CLASS_AUDIO,
  141. USB_SUBCLASS_AUDIOCONTROL,
  142. 0x00,
  143. #ifdef RT_USB_DEVICE_COMPOSITE
  144. SPK_INTF_STR_INDEX,
  145. #else
  146. 0x00,
  147. #endif
  148. },
  149. /* Header Descriptor */
  150. {
  151. sizeof(struct usb_audio_control_descriptor),
  152. UAC_CS_INTERFACE,
  153. UDESCSUB_AC_HEADER,
  154. 0x0100, /* Version: 1.00 */
  155. 0x0027, /* Total length: 39 */
  156. 0x01, /* Total number of interfaces: 1 */
  157. {0x01}, /* Interface number: 1 */
  158. },
  159. /* Input Terminal Descriptor */
  160. {
  161. sizeof(struct usb_audio_input_terminal),
  162. UAC_CS_INTERFACE,
  163. UDESCSUB_AC_INPUT,
  164. 0x01, /* Terminal ID: 1 */
  165. 0x0101, /* Terminal Type: USB Streaming (0x0101) */
  166. 0x00, /* Assoc Terminal: 0 */
  167. 0x01, /* Number Channels: 1 */
  168. 0x0000, /* Channel Config: 0x0000 */
  169. 0x00, /* Channel Names: 0 */
  170. 0x00, /* Terminal: 0 */
  171. },
  172. /* Output Terminal Descriptor */
  173. {
  174. sizeof(struct usb_audio_output_terminal),
  175. UAC_CS_INTERFACE,
  176. UDESCSUB_AC_OUTPUT,
  177. 0x02, /* Terminal ID: 2 */
  178. 0x0302, /* Terminal Type: Headphones (0x0302) */
  179. 0x00, /* Assoc Terminal: 0 */
  180. 0x01, /* Source ID: 1 */
  181. 0x00, /* Terminal: 0 */
  182. },
  183. #if UAC_USE_FEATURE_UNIT
  184. /* Feature unit Descriptor */
  185. {
  186. UAC_DT_FEATURE_UNIT_SIZE(UAC_CH_NUM),
  187. UAC_CS_INTERFACE,
  188. UAC_FEATURE_UNIT,
  189. 0x02,
  190. 0x0101,
  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. 0x01, /* Terminal ID: 1 */
  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_OUT,
  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 speaker_entry(void *parameter)
  262. {
  263. struct rt_audio_caps caps = {0};
  264. rt_uint32_t e, index;
  265. speaker.buffer = rt_malloc(AUDIO_BUFFER_SZ);
  266. if (speaker.buffer == RT_NULL)
  267. {
  268. LOG_E("malloc failed");
  269. goto __exit;
  270. }
  271. speaker.dev = rt_device_find(SPEAKER_DEVICE_NAME);
  272. if (speaker.dev == RT_NULL)
  273. {
  274. LOG_E("can't find device:%s", SPEAKER_DEVICE_NAME);
  275. goto __exit;
  276. }
  277. while (1)
  278. {
  279. if (rt_event_recv(speaker.event, EVENT_AUDIO_START | EVENT_AUDIO_STOP,
  280. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  281. 1000, &e) != RT_EOK)
  282. {
  283. continue;
  284. }
  285. if (speaker.open_count == 0)
  286. {
  287. continue;
  288. }
  289. LOG_D("play start");
  290. rt_device_open(speaker.dev, RT_DEVICE_OFLAG_WRONLY);
  291. caps.main_type = AUDIO_TYPE_OUTPUT;
  292. caps.sub_type = AUDIO_DSP_PARAM;
  293. caps.udata.config.samplerate = AUDIO_SAMPLERATE;
  294. caps.udata.config.channels = AUDIO_CHANNEL;
  295. caps.udata.config.samplebits = RESOLUTION_BITS;
  296. rt_device_control(speaker.dev, AUDIO_CTL_CONFIGURE, &caps);
  297. while (1)
  298. {
  299. if (rt_event_recv(speaker.event, EVENT_AUDIO_DATA | EVENT_AUDIO_STOP,
  300. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  301. 1000, &e) != RT_EOK)
  302. {
  303. if (speaker.open_count > 0)
  304. continue;
  305. else
  306. break;
  307. }
  308. if (e & EVENT_AUDIO_DATA)
  309. {
  310. index = (speaker.buffer_index >= AUDIO_BUFFER_SZ / 2) ? 0 : (AUDIO_BUFFER_SZ / 2);
  311. rt_device_write(speaker.dev, 0, speaker.buffer + index, AUDIO_BUFFER_SZ / 2);
  312. }
  313. else if (e & EVENT_AUDIO_STOP)
  314. {
  315. break;
  316. }
  317. }
  318. LOG_D("play stop");
  319. rt_device_close(speaker.dev);
  320. }
  321. __exit:
  322. if (speaker.buffer)
  323. rt_free(speaker.buffer);
  324. }
  325. static rt_err_t _audio_start(ufunction_t func)
  326. {
  327. speaker.ep->request.buffer = speaker.buffer;
  328. speaker.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
  329. speaker.ep->request.req_type = UIO_REQUEST_READ_FULL;
  330. rt_usbd_io_request(func->device, speaker.ep, &speaker.ep->request);
  331. speaker.open_count ++;
  332. rt_event_send(speaker.event, EVENT_AUDIO_START);
  333. return 0;
  334. }
  335. static rt_err_t _audio_stop(ufunction_t func)
  336. {
  337. speaker.open_count --;
  338. rt_event_send(speaker.event, EVENT_AUDIO_STOP);
  339. return 0;
  340. }
  341. static rt_err_t _ep_data_handler(ufunction_t func, rt_size_t size)
  342. {
  343. RT_ASSERT(func != RT_NULL);
  344. LOG_D("_ep_data_handler");
  345. speaker.ep->request.buffer = speaker.buffer + speaker.buffer_index;
  346. speaker.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
  347. speaker.ep->request.req_type = UIO_REQUEST_READ_FULL;
  348. rt_usbd_io_request(func->device, speaker.ep, &speaker.ep->request);
  349. speaker.buffer_index += UAC_EP_MAX_PACKET_SIZE;
  350. if (speaker.buffer_index >= AUDIO_BUFFER_SZ)
  351. {
  352. speaker.buffer_index = 0;
  353. rt_event_send(speaker.event, EVENT_AUDIO_DATA);
  354. }
  355. else if (speaker.buffer_index == AUDIO_BUFFER_SZ / 2)
  356. {
  357. rt_event_send(speaker.event, EVENT_AUDIO_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. _audio_start(func);
  378. }
  379. else if (setup->wValue == 0)
  380. {
  381. _audio_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. _audio_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_speaker_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, SPK_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, AUDIO_SAMPLERATE);
  476. /* create endpoint */
  477. as_desc_t = (struct uac_as_descriptor *)setting_as->desc;
  478. speaker.ep = rt_usbd_endpoint_new(&as_desc_t->ep_desc, _ep_data_handler);
  479. /* add the endpoint to the alternate setting */
  480. rt_usbd_altsetting_add_endpoint(setting_as, speaker.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_speaker_init(void)
  493. {
  494. rt_thread_t speaker_tid;
  495. speaker.event = rt_event_create("speaker_event", RT_IPC_FLAG_FIFO);
  496. speaker_tid = rt_thread_create("speaker_thread",
  497. speaker_entry, RT_NULL,
  498. 1024,
  499. 5, 10);
  500. if (speaker_tid != RT_NULL)
  501. rt_thread_startup(speaker_tid);
  502. return RT_EOK;
  503. }
  504. INIT_COMPONENT_EXPORT(audio_speaker_init);
  505. /*
  506. * register uac class
  507. */
  508. static struct udclass uac_speaker_class =
  509. {
  510. .rt_usbd_function_create = rt_usbd_function_uac_speaker_create
  511. };
  512. int rt_usbd_uac_speaker_class_register(void)
  513. {
  514. rt_usbd_class_register(&uac_speaker_class);
  515. return 0;
  516. }
  517. INIT_PREV_EXPORT(rt_usbd_uac_speaker_class_register);