video_audiov1_hid_template.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_video.h"
  8. #include "usbd_audio.h"
  9. #include "usbd_hid.h"
  10. #include "cherryusb_mjpeg.h"
  11. #define MAX_PACKETS_IN_ONE_TRANSFER 1
  12. #define VIDEO_IN_EP 0x81
  13. #define VIDEO_INT_EP 0x86
  14. #ifdef CONFIG_USB_HS
  15. #define MAX_PAYLOAD_SIZE 1024 // for high speed with one transcations every one micro frame
  16. #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 1)) | (0x00 << 11))
  17. // #define MAX_PAYLOAD_SIZE 2048 // for high speed with two transcations every one micro frame
  18. // #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 2)) | (0x01 << 11))
  19. // #define MAX_PAYLOAD_SIZE 3072 // for high speed with three transcations every one micro frame
  20. // #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 3)) | (0x02 << 11))
  21. #else
  22. #define MAX_PAYLOAD_SIZE 1020
  23. #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 1)) | (0x00 << 11))
  24. #endif
  25. #define WIDTH (unsigned int)(640)
  26. #define HEIGHT (unsigned int)(480)
  27. #define CAM_FPS (30)
  28. #define INTERVAL (unsigned long)(10000000 / CAM_FPS)
  29. #define MIN_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS) //16 bit
  30. #define MAX_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS)
  31. #define MAX_FRAME_SIZE (unsigned long)(WIDTH * HEIGHT * 2)
  32. #define VS_HEADER_SIZ (unsigned int)(VIDEO_SIZEOF_VS_INPUT_HEADER_DESC(1, 1) + VIDEO_SIZEOF_VS_FORMAT_MJPEG_DESC + VIDEO_SIZEOF_VS_FRAME_MJPEG_DESC(1))
  33. #define USB_VIDEO_DESC_SIZ (unsigned long)(9 + \
  34. VIDEO_VC_NOEP_DESCRIPTOR_LEN + \
  35. 9 + \
  36. VS_HEADER_SIZ + \
  37. 9 + \
  38. 7 + \
  39. AUDIO_AC_DESCRIPTOR_INIT_LEN(2) + \
  40. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  41. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  42. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  43. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  44. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  45. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  46. AUDIO_AS_DESCRIPTOR_INIT_LEN(1) + \
  47. AUDIO_AS_DESCRIPTOR_INIT_LEN(1) + \
  48. 25)
  49. #define USBD_VID 0xffff
  50. #define USBD_PID 0xffff
  51. #define USBD_MAX_POWER 100
  52. #define USBD_LANGID_STRING 1033
  53. #ifdef CONFIG_USB_HS
  54. #define EP_INTERVAL 0x04
  55. #else
  56. #define EP_INTERVAL 0x01
  57. #endif
  58. #define AUDIO_IN_EP 0x82
  59. #define AUDIO_OUT_EP 0x03
  60. #define AUDIO_IN_FU_ID 0x02
  61. #define AUDIO_OUT_FU_ID 0x05
  62. /* AUDIO Class Config */
  63. #define AUDIO_SPEAKER_FREQ 16000U
  64. #define AUDIO_SPEAKER_FRAME_SIZE_BYTE 2u
  65. #define AUDIO_SPEAKER_RESOLUTION_BIT 16u
  66. #define AUDIO_MIC_FREQ 16000U
  67. #define AUDIO_MIC_FRAME_SIZE_BYTE 2u
  68. #define AUDIO_MIC_RESOLUTION_BIT 16u
  69. #define AUDIO_SAMPLE_FREQ(frq) (uint8_t)(frq), (uint8_t)((frq >> 8)), (uint8_t)((frq >> 16))
  70. /* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 2) */
  71. #define AUDIO_OUT_PACKET ((uint32_t)((AUDIO_SPEAKER_FREQ * AUDIO_SPEAKER_FRAME_SIZE_BYTE * 2) / 1000))
  72. /* 16bit(2 Bytes) 双声道(Mono:2) */
  73. #define AUDIO_IN_PACKET ((uint32_t)((AUDIO_MIC_FREQ * AUDIO_MIC_FRAME_SIZE_BYTE * 2) / 1000))
  74. #define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(2) + \
  75. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  76. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  77. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC + \
  78. AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC + \
  79. AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
  80. AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
  81. #define HID_INT_EP 0x84
  82. #define HID_INT_EP_SIZE 8
  83. #define HID_INT_EP_INTERVAL 10
  84. #define HID_KEYBOARD_REPORT_DESC_SIZE 63
  85. const uint8_t video_audio_hid_descriptor[] = {
  86. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  87. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x06, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  88. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  89. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  90. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  91. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  92. VIDEO_VS_FORMAT_MJPEG_DESCRIPTOR_INIT(0x01, 0x01),
  93. VIDEO_VS_FRAME_MJPEG_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, MAX_FRAME_SIZE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  94. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  95. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  96. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  97. AUDIO_AC_DESCRIPTOR_INIT(0x02, 0x03, AUDIO_AC_SIZ, 0x00, 0x03, 0x04),
  98. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003),
  99. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00, 0x00),
  100. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02),
  101. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x02, 0x0003),
  102. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00, 0x00),
  103. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x06, AUDIO_OUTTERM_SPEAKER, 0x05),
  104. AUDIO_AS_DESCRIPTOR_INIT(0x03, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET,
  105. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
  106. AUDIO_AS_DESCRIPTOR_INIT(0x04, 0x03, 0x02, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET,
  107. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ)),
  108. /************** Descriptor of Joystick Mouse interface ****************/
  109. /* 09 */
  110. 0x09, /* bLength: Interface Descriptor size */
  111. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  112. 0x05, /* bInterfaceNumber: Number of Interface */
  113. 0x00, /* bAlternateSetting: Alternate setting */
  114. 0x01, /* bNumEndpoints */
  115. 0x03, /* bInterfaceClass: HID */
  116. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  117. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  118. 0, /* iInterface: Index of string descriptor */
  119. /******************** Descriptor of Joystick Mouse HID ********************/
  120. /* 18 */
  121. 0x09, /* bLength: HID Descriptor size */
  122. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  123. 0x11, /* bcdHID: HID Class Spec release number */
  124. 0x01,
  125. 0x00, /* bCountryCode: Hardware target country */
  126. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  127. 0x22, /* bDescriptorType */
  128. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  129. 0x00,
  130. /******************** Descriptor of Mouse endpoint ********************/
  131. /* 27 */
  132. 0x07, /* bLength: Endpoint Descriptor size */
  133. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  134. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  135. 0x03, /* bmAttributes: Interrupt endpoint */
  136. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  137. 0x00,
  138. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  139. /* 34 */
  140. ///////////////////////////////////////
  141. /// string0 descriptor
  142. ///////////////////////////////////////
  143. USB_LANGID_INIT(USBD_LANGID_STRING),
  144. ///////////////////////////////////////
  145. /// string1 descriptor
  146. ///////////////////////////////////////
  147. 0x14, /* bLength */
  148. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  149. 'C', 0x00, /* wcChar0 */
  150. 'h', 0x00, /* wcChar1 */
  151. 'e', 0x00, /* wcChar2 */
  152. 'r', 0x00, /* wcChar3 */
  153. 'r', 0x00, /* wcChar4 */
  154. 'y', 0x00, /* wcChar5 */
  155. 'U', 0x00, /* wcChar6 */
  156. 'S', 0x00, /* wcChar7 */
  157. 'B', 0x00, /* wcChar8 */
  158. ///////////////////////////////////////
  159. /// string2 descriptor
  160. ///////////////////////////////////////
  161. 0x26, /* bLength */
  162. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  163. 'C', 0x00, /* wcChar0 */
  164. 'h', 0x00, /* wcChar1 */
  165. 'e', 0x00, /* wcChar2 */
  166. 'r', 0x00, /* wcChar3 */
  167. 'r', 0x00, /* wcChar4 */
  168. 'y', 0x00, /* wcChar5 */
  169. 'U', 0x00, /* wcChar6 */
  170. 'S', 0x00, /* wcChar7 */
  171. 'B', 0x00, /* wcChar8 */
  172. ' ', 0x00, /* wcChar9 */
  173. 'U', 0x00, /* wcChar10 */
  174. 'A', 0x00, /* wcChar11 */
  175. 'C', 0x00, /* wcChar12 */
  176. ' ', 0x00, /* wcChar13 */
  177. 'D', 0x00, /* wcChar14 */
  178. 'E', 0x00, /* wcChar15 */
  179. 'M', 0x00, /* wcChar16 */
  180. 'O', 0x00, /* wcChar17 */
  181. ///////////////////////////////////////
  182. /// string3 descriptor
  183. ///////////////////////////////////////
  184. 0x16, /* bLength */
  185. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  186. '2', 0x00, /* wcChar0 */
  187. '0', 0x00, /* wcChar1 */
  188. '2', 0x00, /* wcChar2 */
  189. '4', 0x00, /* wcChar3 */
  190. '0', 0x00, /* wcChar4 */
  191. '3', 0x00, /* wcChar5 */
  192. '1', 0x00, /* wcChar6 */
  193. '0', 0x00, /* wcChar7 */
  194. '0', 0x00, /* wcChar8 */
  195. '0', 0x00, /* wcChar9 */
  196. #ifdef CONFIG_USB_HS
  197. ///////////////////////////////////////
  198. /// device qualifier descriptor
  199. ///////////////////////////////////////
  200. 0x0a,
  201. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  202. 0x00,
  203. 0x02,
  204. 0x00,
  205. 0x00,
  206. 0x00,
  207. 0x40,
  208. 0x00,
  209. 0x00,
  210. #endif
  211. 0x00
  212. };
  213. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  214. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  215. 0x09, 0x06, // USAGE (Keyboard)
  216. 0xa1, 0x01, // COLLECTION (Application)
  217. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  218. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  219. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  220. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  221. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  222. 0x75, 0x01, // REPORT_SIZE (1)
  223. 0x95, 0x08, // REPORT_COUNT (8)
  224. 0x81, 0x02, // INPUT (Data,Var,Abs)
  225. 0x95, 0x01, // REPORT_COUNT (1)
  226. 0x75, 0x08, // REPORT_SIZE (8)
  227. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  228. 0x95, 0x05, // REPORT_COUNT (5)
  229. 0x75, 0x01, // REPORT_SIZE (1)
  230. 0x05, 0x08, // USAGE_PAGE (LEDs)
  231. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  232. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  233. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  234. 0x95, 0x01, // REPORT_COUNT (1)
  235. 0x75, 0x03, // REPORT_SIZE (3)
  236. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  237. 0x95, 0x06, // REPORT_COUNT (6)
  238. 0x75, 0x08, // REPORT_SIZE (8)
  239. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  240. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  241. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  242. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  243. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  244. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  245. 0xc0 // END_COLLECTION
  246. };
  247. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t audio_read_buffer[AUDIO_OUT_PACKET];
  248. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t audio_write_buffer[AUDIO_IN_PACKET];
  249. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t video_packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE];
  250. volatile bool video_tx_flag = 0;
  251. volatile bool audio_tx_flag = 0;
  252. volatile bool audio_rx_flag = 0;
  253. volatile bool video_iso_tx_busy = false;
  254. volatile bool audio_iso_tx_busy = false;
  255. #define HID_STATE_IDLE 0
  256. #define HID_STATE_BUSY 1
  257. /*!< hid state ! Data can be sent only when state is idle */
  258. static volatile uint8_t hid_state = HID_STATE_IDLE;
  259. static void usbd_event_handler(uint8_t busid, uint8_t event)
  260. {
  261. switch (event) {
  262. case USBD_EVENT_RESET:
  263. break;
  264. case USBD_EVENT_CONNECTED:
  265. break;
  266. case USBD_EVENT_DISCONNECTED:
  267. break;
  268. case USBD_EVENT_RESUME:
  269. break;
  270. case USBD_EVENT_SUSPEND:
  271. break;
  272. case USBD_EVENT_CONFIGURED:
  273. video_tx_flag = 0;
  274. audio_tx_flag = 0;
  275. audio_rx_flag = 0;
  276. video_iso_tx_busy = false;
  277. audio_iso_tx_busy = false;
  278. hid_state = HID_STATE_IDLE;
  279. break;
  280. case USBD_EVENT_SET_REMOTE_WAKEUP:
  281. break;
  282. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  283. break;
  284. default:
  285. break;
  286. }
  287. }
  288. void usbd_video_open(uint8_t busid, uint8_t intf)
  289. {
  290. if (intf == 1) {
  291. video_tx_flag = 1;
  292. USB_LOG_RAW("OPEN\r\n");
  293. video_iso_tx_busy = false;
  294. }
  295. }
  296. void usbd_video_close(uint8_t busid, uint8_t intf)
  297. {
  298. if (intf == 1) {
  299. USB_LOG_RAW("CLOSE\r\n");
  300. video_tx_flag = 0;
  301. video_iso_tx_busy = false;
  302. }
  303. }
  304. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  305. {
  306. if (usbd_video_stream_split_transfer(busid, ep)) {
  307. /* one frame has done */
  308. video_iso_tx_busy = false;
  309. }
  310. }
  311. static struct usbd_endpoint video_in_ep = {
  312. .ep_cb = usbd_video_iso_callback,
  313. .ep_addr = VIDEO_IN_EP
  314. };
  315. void usbd_audio_open(uint8_t busid, uint8_t intf)
  316. {
  317. if (intf == 3) {
  318. audio_rx_flag = 1;
  319. /* setup first out ep read transfer */
  320. usbd_ep_start_read(busid, AUDIO_OUT_EP, audio_read_buffer, AUDIO_OUT_PACKET);
  321. printf("OPEN1\r\n");
  322. } else if (intf == 4) {
  323. audio_tx_flag = 1;
  324. audio_iso_tx_busy = false;
  325. printf("OPEN2\r\n");
  326. }
  327. }
  328. void usbd_audio_close(uint8_t busid, uint8_t intf)
  329. {
  330. if (intf == 3) {
  331. audio_rx_flag = 0;
  332. printf("CLOSE1\r\n");
  333. } else if (intf == 4) {
  334. audio_tx_flag = 0;
  335. audio_iso_tx_busy = false;
  336. printf("CLOSE2\r\n");
  337. }
  338. }
  339. void usbd_audio_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  340. {
  341. USB_LOG_RAW("actual out len:%d\r\n", nbytes);
  342. usbd_ep_start_read(busid, AUDIO_OUT_EP, audio_read_buffer, AUDIO_OUT_PACKET);
  343. }
  344. void usbd_audio_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  345. {
  346. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  347. audio_iso_tx_busy = false;
  348. }
  349. static struct usbd_endpoint audio_in_ep = {
  350. .ep_cb = usbd_audio_in_callback,
  351. .ep_addr = AUDIO_IN_EP
  352. };
  353. static struct usbd_endpoint audio_out_ep = {
  354. .ep_cb = usbd_audio_out_callback,
  355. .ep_addr = AUDIO_OUT_EP
  356. };
  357. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  358. {
  359. hid_state = HID_STATE_IDLE;
  360. }
  361. static struct usbd_endpoint hid_in_ep = {
  362. .ep_cb = usbd_hid_int_callback,
  363. .ep_addr = HID_INT_EP
  364. };
  365. struct usbd_interface intf0;
  366. struct usbd_interface intf1;
  367. struct usbd_interface intf2;
  368. struct usbd_interface intf3;
  369. struct usbd_interface intf4;
  370. struct usbd_interface intf5;
  371. struct audio_entity_info audio_entity_table[] = {
  372. { .bEntityId = AUDIO_IN_FU_ID,
  373. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  374. .ep = AUDIO_IN_EP },
  375. { .bEntityId = AUDIO_OUT_FU_ID,
  376. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  377. .ep = AUDIO_OUT_EP },
  378. };
  379. void composite_init(uint8_t busid, uintptr_t reg_base)
  380. {
  381. usbd_desc_register(busid, video_audio_hid_descriptor);
  382. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  383. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  384. usbd_add_endpoint(busid, &video_in_ep);
  385. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf2, 0x0100, audio_entity_table, 2));
  386. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf3, 0x0100, audio_entity_table, 2));
  387. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf4, 0x0100, audio_entity_table, 2));
  388. usbd_add_endpoint(busid, &audio_in_ep);
  389. usbd_add_endpoint(busid, &audio_out_ep);
  390. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  391. usbd_add_endpoint(busid, &hid_in_ep);
  392. usbd_initialize(busid, reg_base, usbd_event_handler);
  393. }
  394. /* just for test, rewrite by yourself */
  395. void audio_test(uint8_t busid)
  396. {
  397. while (1) {
  398. if (audio_tx_flag) {
  399. memset(audio_write_buffer, 'a', AUDIO_IN_PACKET);
  400. audio_iso_tx_busy = true;
  401. usbd_ep_start_write(busid, AUDIO_IN_EP, audio_write_buffer, AUDIO_IN_PACKET);
  402. while (audio_iso_tx_busy) {
  403. if (audio_tx_flag == false) {
  404. break;
  405. }
  406. }
  407. }
  408. }
  409. }
  410. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t hid_write_buffer[64];
  411. void hid_keyboard_test(uint8_t busid)
  412. {
  413. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  414. if(usb_device_is_configured(busid) == false) {
  415. return;
  416. }
  417. memcpy(hid_write_buffer, sendbuffer, 8);
  418. hid_state = HID_STATE_BUSY;
  419. usbd_ep_start_write(busid, HID_INT_EP, hid_write_buffer, 8);
  420. while (hid_state == HID_STATE_BUSY) {
  421. }
  422. }
  423. void video_test(uint8_t busid)
  424. {
  425. memset(video_packet_buffer, 0, sizeof(video_packet_buffer));
  426. while (1) {
  427. if (video_tx_flag) {
  428. video_iso_tx_busy = true;
  429. usbd_video_stream_start_write(busid, VIDEO_IN_EP, &video_packet_buffer[0][0], &video_packet_buffer[1][0], MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE, (uint8_t *)cherryusb_mjpeg, sizeof(cherryusb_mjpeg));
  430. while (video_iso_tx_busy) {
  431. if (video_tx_flag == 0) {
  432. break;
  433. }
  434. }
  435. }
  436. }
  437. }