video_audiov1_hid_template.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  86. static const uint8_t device_descriptor[] = {
  87. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  88. };
  89. static const uint8_t config_descriptor[] = {
  90. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x06, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  91. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  92. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  93. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  94. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  95. VIDEO_VS_FORMAT_MJPEG_DESCRIPTOR_INIT(0x01, 0x01),
  96. VIDEO_VS_FRAME_MJPEG_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, MAX_FRAME_SIZE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  97. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  98. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  99. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  100. AUDIO_AC_DESCRIPTOR_INIT(0x02, 0x03, AUDIO_AC_SIZ, 0x00, 0x03, 0x04),
  101. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003),
  102. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00, 0x00),
  103. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02),
  104. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x02, 0x0003),
  105. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00, 0x00),
  106. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x06, AUDIO_OUTTERM_SPEAKER, 0x05),
  107. AUDIO_AS_DESCRIPTOR_INIT(0x03, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET,
  108. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
  109. AUDIO_AS_DESCRIPTOR_INIT(0x04, 0x03, 0x02, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET,
  110. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ)),
  111. /************** Descriptor of Joystick Mouse interface ****************/
  112. /* 09 */
  113. 0x09, /* bLength: Interface Descriptor size */
  114. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  115. 0x05, /* bInterfaceNumber: Number of Interface */
  116. 0x00, /* bAlternateSetting: Alternate setting */
  117. 0x01, /* bNumEndpoints */
  118. 0x03, /* bInterfaceClass: HID */
  119. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  120. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  121. 0, /* iInterface: Index of string descriptor */
  122. /******************** Descriptor of Joystick Mouse HID ********************/
  123. /* 18 */
  124. 0x09, /* bLength: HID Descriptor size */
  125. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  126. 0x11, /* bcdHID: HID Class Spec release number */
  127. 0x01,
  128. 0x00, /* bCountryCode: Hardware target country */
  129. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  130. 0x22, /* bDescriptorType */
  131. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  132. 0x00,
  133. /******************** Descriptor of Mouse endpoint ********************/
  134. /* 27 */
  135. 0x07, /* bLength: Endpoint Descriptor size */
  136. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  137. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  138. 0x03, /* bmAttributes: Interrupt endpoint */
  139. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  140. 0x00,
  141. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  142. /* 34 */
  143. };
  144. static const uint8_t device_quality_descriptor[] = {
  145. ///////////////////////////////////////
  146. /// device qualifier descriptor
  147. ///////////////////////////////////////
  148. 0x0a,
  149. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  150. 0x00,
  151. 0x02,
  152. 0x00,
  153. 0x00,
  154. 0x00,
  155. 0x40,
  156. 0x00,
  157. 0x00,
  158. };
  159. static const char *string_descriptors[] = {
  160. (const char[]){ 0x09, 0x04 }, /* Langid */
  161. "CherryUSB", /* Manufacturer */
  162. "CherryUSB UVC UAC HID DEMO", /* Product */
  163. "2022123456", /* Serial Number */
  164. };
  165. static const uint8_t *device_descriptor_callback(uint8_t speed)
  166. {
  167. return device_descriptor;
  168. }
  169. static const uint8_t *config_descriptor_callback(uint8_t speed)
  170. {
  171. return config_descriptor;
  172. }
  173. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  174. {
  175. return device_quality_descriptor;
  176. }
  177. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  178. {
  179. if (index > 3) {
  180. return NULL;
  181. }
  182. return string_descriptors[index];
  183. }
  184. const struct usb_descriptor video_audio_hid_descriptor = {
  185. .device_descriptor_callback = device_descriptor_callback,
  186. .config_descriptor_callback = config_descriptor_callback,
  187. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  188. .string_descriptor_callback = string_descriptor_callback
  189. };
  190. #else
  191. const uint8_t video_audio_hid_descriptor[] = {
  192. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  193. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x06, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  194. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  195. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  196. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  197. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  198. VIDEO_VS_FORMAT_MJPEG_DESCRIPTOR_INIT(0x01, 0x01),
  199. VIDEO_VS_FRAME_MJPEG_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, MAX_FRAME_SIZE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  200. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  201. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  202. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  203. AUDIO_AC_DESCRIPTOR_INIT(0x02, 0x03, AUDIO_AC_SIZ, 0x00, 0x03, 0x04),
  204. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003),
  205. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00, 0x00),
  206. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02),
  207. AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x02, 0x0003),
  208. AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00, 0x00),
  209. AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x06, AUDIO_OUTTERM_SPEAKER, 0x05),
  210. AUDIO_AS_DESCRIPTOR_INIT(0x03, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET,
  211. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
  212. AUDIO_AS_DESCRIPTOR_INIT(0x04, 0x03, 0x02, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET,
  213. EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ)),
  214. /************** Descriptor of Joystick Mouse interface ****************/
  215. /* 09 */
  216. 0x09, /* bLength: Interface Descriptor size */
  217. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
  218. 0x05, /* bInterfaceNumber: Number of Interface */
  219. 0x00, /* bAlternateSetting: Alternate setting */
  220. 0x01, /* bNumEndpoints */
  221. 0x03, /* bInterfaceClass: HID */
  222. 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  223. 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  224. 0, /* iInterface: Index of string descriptor */
  225. /******************** Descriptor of Joystick Mouse HID ********************/
  226. /* 18 */
  227. 0x09, /* bLength: HID Descriptor size */
  228. HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */
  229. 0x11, /* bcdHID: HID Class Spec release number */
  230. 0x01,
  231. 0x00, /* bCountryCode: Hardware target country */
  232. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  233. 0x22, /* bDescriptorType */
  234. HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
  235. 0x00,
  236. /******************** Descriptor of Mouse endpoint ********************/
  237. /* 27 */
  238. 0x07, /* bLength: Endpoint Descriptor size */
  239. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */
  240. HID_INT_EP, /* bEndpointAddress: Endpoint Address (IN) */
  241. 0x03, /* bmAttributes: Interrupt endpoint */
  242. HID_INT_EP_SIZE, /* wMaxPacketSize: 4 Byte max */
  243. 0x00,
  244. HID_INT_EP_INTERVAL, /* bInterval: Polling Interval */
  245. /* 34 */
  246. ///////////////////////////////////////
  247. /// string0 descriptor
  248. ///////////////////////////////////////
  249. USB_LANGID_INIT(USBD_LANGID_STRING),
  250. ///////////////////////////////////////
  251. /// string1 descriptor
  252. ///////////////////////////////////////
  253. 0x14, /* bLength */
  254. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  255. 'C', 0x00, /* wcChar0 */
  256. 'h', 0x00, /* wcChar1 */
  257. 'e', 0x00, /* wcChar2 */
  258. 'r', 0x00, /* wcChar3 */
  259. 'r', 0x00, /* wcChar4 */
  260. 'y', 0x00, /* wcChar5 */
  261. 'U', 0x00, /* wcChar6 */
  262. 'S', 0x00, /* wcChar7 */
  263. 'B', 0x00, /* wcChar8 */
  264. ///////////////////////////////////////
  265. /// string2 descriptor
  266. ///////////////////////////////////////
  267. 0x26, /* bLength */
  268. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  269. 'C', 0x00, /* wcChar0 */
  270. 'h', 0x00, /* wcChar1 */
  271. 'e', 0x00, /* wcChar2 */
  272. 'r', 0x00, /* wcChar3 */
  273. 'r', 0x00, /* wcChar4 */
  274. 'y', 0x00, /* wcChar5 */
  275. 'U', 0x00, /* wcChar6 */
  276. 'S', 0x00, /* wcChar7 */
  277. 'B', 0x00, /* wcChar8 */
  278. ' ', 0x00, /* wcChar9 */
  279. 'U', 0x00, /* wcChar10 */
  280. 'A', 0x00, /* wcChar11 */
  281. 'C', 0x00, /* wcChar12 */
  282. ' ', 0x00, /* wcChar13 */
  283. 'D', 0x00, /* wcChar14 */
  284. 'E', 0x00, /* wcChar15 */
  285. 'M', 0x00, /* wcChar16 */
  286. 'O', 0x00, /* wcChar17 */
  287. ///////////////////////////////////////
  288. /// string3 descriptor
  289. ///////////////////////////////////////
  290. 0x16, /* bLength */
  291. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  292. '2', 0x00, /* wcChar0 */
  293. '0', 0x00, /* wcChar1 */
  294. '2', 0x00, /* wcChar2 */
  295. '4', 0x00, /* wcChar3 */
  296. '0', 0x00, /* wcChar4 */
  297. '3', 0x00, /* wcChar5 */
  298. '1', 0x00, /* wcChar6 */
  299. '0', 0x00, /* wcChar7 */
  300. '0', 0x00, /* wcChar8 */
  301. '0', 0x00, /* wcChar9 */
  302. #ifdef CONFIG_USB_HS
  303. ///////////////////////////////////////
  304. /// device qualifier descriptor
  305. ///////////////////////////////////////
  306. 0x0a,
  307. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  308. 0x00,
  309. 0x02,
  310. 0x00,
  311. 0x00,
  312. 0x00,
  313. 0x40,
  314. 0x00,
  315. 0x00,
  316. #endif
  317. 0x00
  318. };
  319. #endif
  320. static const uint8_t hid_keyboard_report_desc[HID_KEYBOARD_REPORT_DESC_SIZE] = {
  321. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  322. 0x09, 0x06, // USAGE (Keyboard)
  323. 0xa1, 0x01, // COLLECTION (Application)
  324. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  325. 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
  326. 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
  327. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  328. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  329. 0x75, 0x01, // REPORT_SIZE (1)
  330. 0x95, 0x08, // REPORT_COUNT (8)
  331. 0x81, 0x02, // INPUT (Data,Var,Abs)
  332. 0x95, 0x01, // REPORT_COUNT (1)
  333. 0x75, 0x08, // REPORT_SIZE (8)
  334. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  335. 0x95, 0x05, // REPORT_COUNT (5)
  336. 0x75, 0x01, // REPORT_SIZE (1)
  337. 0x05, 0x08, // USAGE_PAGE (LEDs)
  338. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  339. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  340. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  341. 0x95, 0x01, // REPORT_COUNT (1)
  342. 0x75, 0x03, // REPORT_SIZE (3)
  343. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  344. 0x95, 0x06, // REPORT_COUNT (6)
  345. 0x75, 0x08, // REPORT_SIZE (8)
  346. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  347. 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
  348. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  349. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  350. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  351. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  352. 0xc0 // END_COLLECTION
  353. };
  354. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t audio_read_buffer[AUDIO_OUT_PACKET];
  355. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t audio_write_buffer[AUDIO_IN_PACKET];
  356. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t video_packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE];
  357. volatile bool video_tx_flag = 0;
  358. volatile bool audio_tx_flag = 0;
  359. volatile bool audio_rx_flag = 0;
  360. volatile bool video_iso_tx_busy = false;
  361. volatile bool audio_iso_tx_busy = false;
  362. #define HID_STATE_IDLE 0
  363. #define HID_STATE_BUSY 1
  364. /*!< hid state ! Data can be sent only when state is idle */
  365. static volatile uint8_t hid_state = HID_STATE_IDLE;
  366. static void usbd_event_handler(uint8_t busid, uint8_t event)
  367. {
  368. switch (event) {
  369. case USBD_EVENT_RESET:
  370. break;
  371. case USBD_EVENT_CONNECTED:
  372. break;
  373. case USBD_EVENT_DISCONNECTED:
  374. break;
  375. case USBD_EVENT_RESUME:
  376. break;
  377. case USBD_EVENT_SUSPEND:
  378. break;
  379. case USBD_EVENT_CONFIGURED:
  380. video_tx_flag = 0;
  381. audio_tx_flag = 0;
  382. audio_rx_flag = 0;
  383. video_iso_tx_busy = false;
  384. audio_iso_tx_busy = false;
  385. hid_state = HID_STATE_IDLE;
  386. break;
  387. case USBD_EVENT_SET_REMOTE_WAKEUP:
  388. break;
  389. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  390. break;
  391. default:
  392. break;
  393. }
  394. }
  395. void usbd_video_open(uint8_t busid, uint8_t intf)
  396. {
  397. if (intf == 1) {
  398. video_tx_flag = 1;
  399. USB_LOG_RAW("OPEN\r\n");
  400. video_iso_tx_busy = false;
  401. }
  402. }
  403. void usbd_video_close(uint8_t busid, uint8_t intf)
  404. {
  405. if (intf == 1) {
  406. USB_LOG_RAW("CLOSE\r\n");
  407. video_tx_flag = 0;
  408. video_iso_tx_busy = false;
  409. }
  410. }
  411. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  412. {
  413. if (usbd_video_stream_split_transfer(busid, ep)) {
  414. /* one frame has done */
  415. video_iso_tx_busy = false;
  416. }
  417. }
  418. static struct usbd_endpoint video_in_ep = {
  419. .ep_cb = usbd_video_iso_callback,
  420. .ep_addr = VIDEO_IN_EP
  421. };
  422. void usbd_audio_open(uint8_t busid, uint8_t intf)
  423. {
  424. if (intf == 3) {
  425. audio_rx_flag = 1;
  426. /* setup first out ep read transfer */
  427. usbd_ep_start_read(busid, AUDIO_OUT_EP, audio_read_buffer, AUDIO_OUT_PACKET);
  428. printf("OPEN1\r\n");
  429. } else if (intf == 4) {
  430. audio_tx_flag = 1;
  431. audio_iso_tx_busy = false;
  432. printf("OPEN2\r\n");
  433. }
  434. }
  435. void usbd_audio_close(uint8_t busid, uint8_t intf)
  436. {
  437. if (intf == 3) {
  438. audio_rx_flag = 0;
  439. printf("CLOSE1\r\n");
  440. } else if (intf == 4) {
  441. audio_tx_flag = 0;
  442. audio_iso_tx_busy = false;
  443. printf("CLOSE2\r\n");
  444. }
  445. }
  446. void usbd_audio_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  447. {
  448. USB_LOG_RAW("actual out len:%d\r\n", nbytes);
  449. usbd_ep_start_read(busid, AUDIO_OUT_EP, audio_read_buffer, AUDIO_OUT_PACKET);
  450. }
  451. void usbd_audio_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  452. {
  453. USB_LOG_RAW("actual in len:%d\r\n", nbytes);
  454. audio_iso_tx_busy = false;
  455. }
  456. static struct usbd_endpoint audio_in_ep = {
  457. .ep_cb = usbd_audio_in_callback,
  458. .ep_addr = AUDIO_IN_EP
  459. };
  460. static struct usbd_endpoint audio_out_ep = {
  461. .ep_cb = usbd_audio_out_callback,
  462. .ep_addr = AUDIO_OUT_EP
  463. };
  464. void usbd_hid_int_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  465. {
  466. hid_state = HID_STATE_IDLE;
  467. }
  468. static struct usbd_endpoint hid_in_ep = {
  469. .ep_cb = usbd_hid_int_callback,
  470. .ep_addr = HID_INT_EP
  471. };
  472. struct usbd_interface intf0;
  473. struct usbd_interface intf1;
  474. struct usbd_interface intf2;
  475. struct usbd_interface intf3;
  476. struct usbd_interface intf4;
  477. struct usbd_interface intf5;
  478. struct audio_entity_info audio_entity_table[] = {
  479. { .bEntityId = AUDIO_IN_FU_ID,
  480. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  481. .ep = AUDIO_IN_EP },
  482. { .bEntityId = AUDIO_OUT_FU_ID,
  483. .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
  484. .ep = AUDIO_OUT_EP },
  485. };
  486. void composite_init(uint8_t busid, uintptr_t reg_base)
  487. {
  488. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  489. usbd_desc_register(busid, &video_audio_hid_descriptor);
  490. #else
  491. usbd_desc_register(busid, video_audio_hid_descriptor);
  492. #endif
  493. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  494. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  495. usbd_add_endpoint(busid, &video_in_ep);
  496. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf2, 0x0100, audio_entity_table, 2));
  497. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf3, 0x0100, audio_entity_table, 2));
  498. usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf4, 0x0100, audio_entity_table, 2));
  499. usbd_add_endpoint(busid, &audio_in_ep);
  500. usbd_add_endpoint(busid, &audio_out_ep);
  501. usbd_add_interface(busid, usbd_hid_init_intf(busid, &intf0, hid_keyboard_report_desc, HID_KEYBOARD_REPORT_DESC_SIZE));
  502. usbd_add_endpoint(busid, &hid_in_ep);
  503. usbd_initialize(busid, reg_base, usbd_event_handler);
  504. }
  505. /* just for test, rewrite by yourself */
  506. void audio_test(uint8_t busid)
  507. {
  508. while (1) {
  509. if (audio_tx_flag) {
  510. memset(audio_write_buffer, 'a', AUDIO_IN_PACKET);
  511. audio_iso_tx_busy = true;
  512. usbd_ep_start_write(busid, AUDIO_IN_EP, audio_write_buffer, AUDIO_IN_PACKET);
  513. while (audio_iso_tx_busy) {
  514. if (audio_tx_flag == false) {
  515. break;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t hid_write_buffer[64];
  522. void hid_keyboard_test(uint8_t busid)
  523. {
  524. const uint8_t sendbuffer[8] = { 0x00, 0x00, HID_KBD_USAGE_A, 0x00, 0x00, 0x00, 0x00, 0x00 };
  525. if(usb_device_is_configured(busid) == false) {
  526. return;
  527. }
  528. memcpy(hid_write_buffer, sendbuffer, 8);
  529. hid_state = HID_STATE_BUSY;
  530. usbd_ep_start_write(busid, HID_INT_EP, hid_write_buffer, 8);
  531. while (hid_state == HID_STATE_BUSY) {
  532. }
  533. }
  534. void video_test(uint8_t busid)
  535. {
  536. memset(video_packet_buffer, 0, sizeof(video_packet_buffer));
  537. while (1) {
  538. if (video_tx_flag) {
  539. video_iso_tx_busy = true;
  540. 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));
  541. while (video_iso_tx_busy) {
  542. if (video_tx_flag == 0) {
  543. break;
  544. }
  545. }
  546. }
  547. }
  548. }