video_audiov1_hid_template.c 19 KB

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