video_static_yuyv_template.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "cherryusb_yuyv.h"
  9. #define MAX_PACKETS_IN_ONE_TRANSFER 1
  10. #define VIDEO_IN_EP 0x81
  11. #define VIDEO_INT_EP 0x83
  12. #ifdef CONFIG_USB_HS
  13. #define MAX_PAYLOAD_SIZE 1024 // for high speed with one transcations every one micro frame
  14. #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 1)) | (0x00 << 11))
  15. // #define MAX_PAYLOAD_SIZE 2048 // for high speed with two transcations every one micro frame
  16. // #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 2)) | (0x01 << 11))
  17. // #define MAX_PAYLOAD_SIZE 3072 // for high speed with three transcations every one micro frame
  18. // #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 3)) | (0x02 << 11))
  19. #else
  20. #define MAX_PAYLOAD_SIZE 1020
  21. #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 1)) | (0x00 << 11))
  22. #endif
  23. #define WIDTH (unsigned int)(64)
  24. #define HEIGHT (unsigned int)(48)
  25. #define CAM_FPS (30)
  26. #define INTERVAL (unsigned long)(10000000 / CAM_FPS)
  27. #define MIN_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS) //16 bit
  28. #define MAX_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS)
  29. #define MAX_FRAME_SIZE (unsigned long)(WIDTH * HEIGHT * 2)
  30. #define VS_HEADER_SIZ (unsigned int)(VIDEO_SIZEOF_VS_INPUT_HEADER_DESC(1,1) + VIDEO_SIZEOF_VS_FORMAT_UNCOMPRESSED_DESC + VIDEO_SIZEOF_VS_FRAME_UNCOMPRESSED_DESC(1))
  31. #define USB_VIDEO_DESC_SIZ (unsigned long)(9 + \
  32. VIDEO_VC_NOEP_DESCRIPTOR_LEN + \
  33. 9 + \
  34. VS_HEADER_SIZ + \
  35. 6 + \
  36. 9 + \
  37. 7)
  38. #define USBD_VID 0xffff
  39. #define USBD_PID 0xffff
  40. #define USBD_MAX_POWER 100
  41. #define USBD_LANGID_STRING 1033
  42. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  43. static const uint8_t device_descriptor[] = {
  44. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  45. };
  46. static const uint8_t config_descriptor[] = {
  47. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  48. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  49. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  50. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  51. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  52. VIDEO_VS_FORMAT_UNCOMPRESSED_DESCRIPTOR_INIT(0x01, 0x01, VIDEO_GUID_YUY2),
  53. VIDEO_VS_FRAME_UNCOMPRESSED_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, MAX_FRAME_SIZE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  54. VIDEO_VS_COLOR_MATCHING_DESCRIPTOR_INIT(),
  55. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  56. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  57. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  58. };
  59. static const uint8_t device_quality_descriptor[] = {
  60. ///////////////////////////////////////
  61. /// device qualifier descriptor
  62. ///////////////////////////////////////
  63. 0x0a,
  64. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  65. 0x00,
  66. 0x02,
  67. 0x00,
  68. 0x00,
  69. 0x00,
  70. 0x40,
  71. 0x00,
  72. 0x00,
  73. };
  74. static const char *string_descriptors[] = {
  75. (const char[]){ 0x09, 0x04 }, /* Langid */
  76. "CherryUSB", /* Manufacturer */
  77. "CherryUSB UVC DEMO", /* Product */
  78. "2022123456", /* Serial Number */
  79. };
  80. static const uint8_t *device_descriptor_callback(uint8_t speed)
  81. {
  82. return device_descriptor;
  83. }
  84. static const uint8_t *config_descriptor_callback(uint8_t speed)
  85. {
  86. return config_descriptor;
  87. }
  88. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  89. {
  90. return device_quality_descriptor;
  91. }
  92. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  93. {
  94. if (index > 3) {
  95. return NULL;
  96. }
  97. return string_descriptors[index];
  98. }
  99. const struct usb_descriptor video_descriptor = {
  100. .device_descriptor_callback = device_descriptor_callback,
  101. .config_descriptor_callback = config_descriptor_callback,
  102. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  103. .string_descriptor_callback = string_descriptor_callback
  104. };
  105. #else
  106. const uint8_t video_descriptor[] = {
  107. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  108. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  109. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  110. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  111. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  112. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  113. VIDEO_VS_FORMAT_UNCOMPRESSED_DESCRIPTOR_INIT(0x01, 0x01, VIDEO_GUID_YUY2),
  114. VIDEO_VS_FRAME_UNCOMPRESSED_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, MAX_FRAME_SIZE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  115. VIDEO_VS_COLOR_MATCHING_DESCRIPTOR_INIT(),
  116. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  117. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  118. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  119. ///////////////////////////////////////
  120. /// string0 descriptor
  121. ///////////////////////////////////////
  122. USB_LANGID_INIT(USBD_LANGID_STRING),
  123. ///////////////////////////////////////
  124. /// string1 descriptor
  125. ///////////////////////////////////////
  126. 0x14, /* bLength */
  127. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  128. 'C', 0x00, /* wcChar0 */
  129. 'h', 0x00, /* wcChar1 */
  130. 'e', 0x00, /* wcChar2 */
  131. 'r', 0x00, /* wcChar3 */
  132. 'r', 0x00, /* wcChar4 */
  133. 'y', 0x00, /* wcChar5 */
  134. 'U', 0x00, /* wcChar6 */
  135. 'S', 0x00, /* wcChar7 */
  136. 'B', 0x00, /* wcChar8 */
  137. ///////////////////////////////////////
  138. /// string2 descriptor
  139. ///////////////////////////////////////
  140. 0x26, /* bLength */
  141. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  142. 'C', 0x00, /* wcChar0 */
  143. 'h', 0x00, /* wcChar1 */
  144. 'e', 0x00, /* wcChar2 */
  145. 'r', 0x00, /* wcChar3 */
  146. 'r', 0x00, /* wcChar4 */
  147. 'y', 0x00, /* wcChar5 */
  148. 'U', 0x00, /* wcChar6 */
  149. 'S', 0x00, /* wcChar7 */
  150. 'B', 0x00, /* wcChar8 */
  151. ' ', 0x00, /* wcChar9 */
  152. 'U', 0x00, /* wcChar10 */
  153. 'V', 0x00, /* wcChar11 */
  154. 'C', 0x00, /* wcChar12 */
  155. ' ', 0x00, /* wcChar13 */
  156. 'D', 0x00, /* wcChar14 */
  157. 'E', 0x00, /* wcChar15 */
  158. 'M', 0x00, /* wcChar16 */
  159. 'O', 0x00, /* wcChar17 */
  160. ///////////////////////////////////////
  161. /// string3 descriptor
  162. ///////////////////////////////////////
  163. 0x16, /* bLength */
  164. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  165. '2', 0x00, /* wcChar0 */
  166. '0', 0x00, /* wcChar1 */
  167. '2', 0x00, /* wcChar2 */
  168. '1', 0x00, /* wcChar3 */
  169. '0', 0x00, /* wcChar4 */
  170. '3', 0x00, /* wcChar5 */
  171. '1', 0x00, /* wcChar6 */
  172. '0', 0x00, /* wcChar7 */
  173. '0', 0x00, /* wcChar8 */
  174. '0', 0x00, /* wcChar9 */
  175. #ifdef CONFIG_USB_HS
  176. ///////////////////////////////////////
  177. /// device qualifier descriptor
  178. ///////////////////////////////////////
  179. 0x0a,
  180. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  181. 0x00,
  182. 0x02,
  183. 0x00,
  184. 0x00,
  185. 0x00,
  186. 0x40,
  187. 0x00,
  188. 0x00,
  189. #endif
  190. 0x00
  191. };
  192. #endif
  193. volatile bool tx_flag = 0;
  194. volatile bool iso_tx_busy = false;
  195. static void usbd_event_handler(uint8_t busid, uint8_t event)
  196. {
  197. switch (event) {
  198. case USBD_EVENT_RESET:
  199. break;
  200. case USBD_EVENT_CONNECTED:
  201. break;
  202. case USBD_EVENT_DISCONNECTED:
  203. break;
  204. case USBD_EVENT_RESUME:
  205. break;
  206. case USBD_EVENT_SUSPEND:
  207. break;
  208. case USBD_EVENT_CONFIGURED:
  209. tx_flag = 0;
  210. iso_tx_busy = false;
  211. break;
  212. case USBD_EVENT_SET_REMOTE_WAKEUP:
  213. break;
  214. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. void usbd_video_open(uint8_t busid, uint8_t intf)
  221. {
  222. tx_flag = 1;
  223. USB_LOG_RAW("OPEN\r\n");
  224. iso_tx_busy = false;
  225. }
  226. void usbd_video_close(uint8_t busid, uint8_t intf)
  227. {
  228. USB_LOG_RAW("CLOSE\r\n");
  229. tx_flag = 0;
  230. iso_tx_busy = false;
  231. }
  232. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  233. {
  234. if (usbd_video_stream_split_transfer(busid, ep)) {
  235. /* one frame has done */
  236. iso_tx_busy = false;
  237. }
  238. }
  239. static struct usbd_endpoint video_in_ep = {
  240. .ep_cb = usbd_video_iso_callback,
  241. .ep_addr = VIDEO_IN_EP
  242. };
  243. struct usbd_interface intf0;
  244. struct usbd_interface intf1;
  245. void video_init(uint8_t busid, uintptr_t reg_base)
  246. {
  247. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  248. usbd_desc_register(busid, &video_descriptor);
  249. #else
  250. usbd_desc_register(busid, video_descriptor);
  251. #endif
  252. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  253. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  254. usbd_add_endpoint(busid, &video_in_ep);
  255. usbd_initialize(busid, reg_base, usbd_event_handler);
  256. }
  257. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE];
  258. void video_test(uint8_t busid)
  259. {
  260. memset(packet_buffer, 0, sizeof(packet_buffer));
  261. while (1) {
  262. if (tx_flag) {
  263. iso_tx_busy = true;
  264. usbd_video_stream_start_write(busid, VIDEO_IN_EP, &packet_buffer[0][0], &packet_buffer[1][0], MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE, (uint8_t *)cherryusb_yuyv, sizeof(cherryusb_yuyv));
  265. while (iso_tx_busy) {
  266. if (tx_flag == 0) {
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. }