audio_speaker.c 16 KB

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