audio_speaker.c 16 KB

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