video_static_yuyv_template.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 VIDEO_IN_EP 0x81
  10. #define VIDEO_INT_EP 0x83
  11. #ifdef CONFIG_USB_HS
  12. #define MAX_PAYLOAD_SIZE 1024 // for high speed with one transcations every one micro frame
  13. #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 1)) | (0x00 << 11))
  14. // #define MAX_PAYLOAD_SIZE 2048 // for high speed with two transcations every one micro frame
  15. // #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 2)) | (0x01 << 11))
  16. // #define MAX_PAYLOAD_SIZE 3072 // for high speed with three transcations every one micro frame
  17. // #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 3)) | (0x02 << 11))
  18. #else
  19. #define MAX_PAYLOAD_SIZE 1020
  20. #define VIDEO_PACKET_SIZE (unsigned int)(((MAX_PAYLOAD_SIZE / 1)) | (0x00 << 11))
  21. #endif
  22. #define WIDTH (unsigned int)(64)
  23. #define HEIGHT (unsigned int)(48)
  24. #define CAM_FPS (30)
  25. #define INTERVAL (unsigned long)(10000000 / CAM_FPS)
  26. #define MIN_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS) //16 bit
  27. #define MAX_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS)
  28. #define MAX_FRAME_SIZE (unsigned long)(WIDTH * HEIGHT * 2)
  29. #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))
  30. #define USB_VIDEO_DESC_SIZ (unsigned long)(9 + \
  31. VIDEO_VC_NOEP_DESCRIPTOR_LEN + \
  32. 9 + \
  33. VS_HEADER_SIZ + \
  34. 6 + \
  35. 9 + \
  36. 7)
  37. #define USBD_VID 0xffff
  38. #define USBD_PID 0xffff
  39. #define USBD_MAX_POWER 100
  40. #define USBD_LANGID_STRING 1033
  41. static const uint8_t device_descriptor[] = {
  42. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  43. };
  44. static const uint8_t config_descriptor[] = {
  45. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  46. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  47. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  48. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  49. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  50. VIDEO_VS_FORMAT_UNCOMPRESSED_DESCRIPTOR_INIT(0x01, 0x01, VIDEO_GUID_YUY2),
  51. VIDEO_VS_FRAME_UNCOMPRESSED_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, MAX_FRAME_SIZE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  52. VIDEO_VS_COLOR_MATCHING_DESCRIPTOR_INIT(),
  53. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  54. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  55. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  56. };
  57. static const uint8_t device_quality_descriptor[] = {
  58. ///////////////////////////////////////
  59. /// device qualifier descriptor
  60. ///////////////////////////////////////
  61. 0x0a,
  62. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  63. 0x00,
  64. 0x02,
  65. 0x00,
  66. 0x00,
  67. 0x00,
  68. 0x40,
  69. 0x00,
  70. 0x00,
  71. };
  72. static const char *string_descriptors[] = {
  73. (const char[]){ 0x09, 0x04 }, /* Langid */
  74. "CherryUSB", /* Manufacturer */
  75. "CherryUSB UVC DEMO", /* Product */
  76. "2022123456", /* Serial Number */
  77. };
  78. static const uint8_t *device_descriptor_callback(uint8_t speed)
  79. {
  80. return device_descriptor;
  81. }
  82. static const uint8_t *config_descriptor_callback(uint8_t speed)
  83. {
  84. return config_descriptor;
  85. }
  86. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  87. {
  88. return device_quality_descriptor;
  89. }
  90. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  91. {
  92. if (index > 3) {
  93. return NULL;
  94. }
  95. return string_descriptors[index];
  96. }
  97. const struct usb_descriptor video_descriptor = {
  98. .device_descriptor_callback = device_descriptor_callback,
  99. .config_descriptor_callback = config_descriptor_callback,
  100. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  101. .string_descriptor_callback = string_descriptor_callback
  102. };
  103. volatile bool tx_flag = 0;
  104. volatile bool iso_tx_busy = false;
  105. static void usbd_event_handler(uint8_t busid, uint8_t event)
  106. {
  107. switch (event) {
  108. case USBD_EVENT_RESET:
  109. break;
  110. case USBD_EVENT_CONNECTED:
  111. break;
  112. case USBD_EVENT_DISCONNECTED:
  113. break;
  114. case USBD_EVENT_RESUME:
  115. break;
  116. case USBD_EVENT_SUSPEND:
  117. break;
  118. case USBD_EVENT_CONFIGURED:
  119. tx_flag = 0;
  120. iso_tx_busy = false;
  121. break;
  122. case USBD_EVENT_SET_REMOTE_WAKEUP:
  123. break;
  124. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. void usbd_video_open(uint8_t busid, uint8_t intf)
  131. {
  132. tx_flag = 1;
  133. USB_LOG_RAW("OPEN\r\n");
  134. iso_tx_busy = false;
  135. }
  136. void usbd_video_close(uint8_t busid, uint8_t intf)
  137. {
  138. USB_LOG_RAW("CLOSE\r\n");
  139. tx_flag = 0;
  140. iso_tx_busy = false;
  141. }
  142. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  143. {
  144. if (nbytes == 0) {
  145. return;
  146. }
  147. if (usbd_video_stream_split_transfer(busid, ep)) {
  148. /* one frame has done */
  149. iso_tx_busy = false;
  150. }
  151. }
  152. static struct usbd_endpoint video_in_ep = {
  153. .ep_cb = usbd_video_iso_callback,
  154. .ep_addr = VIDEO_IN_EP
  155. };
  156. struct usbd_interface intf0;
  157. struct usbd_interface intf1;
  158. void video_init(uint8_t busid, uintptr_t reg_base)
  159. {
  160. usbd_desc_register(busid, &video_descriptor);
  161. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  162. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  163. usbd_add_endpoint(busid, &video_in_ep);
  164. usbd_initialize(busid, reg_base, usbd_event_handler);
  165. }
  166. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE];
  167. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024];
  168. void video_test(uint8_t busid)
  169. {
  170. memset(packet_buffer, 0, sizeof(packet_buffer));
  171. while (1) {
  172. if (tx_flag) {
  173. iso_tx_busy = true;
  174. memcpy(frame_buffer, cherryusb_yuyv, sizeof(cherryusb_yuyv)); // cherryusb_yuyv is a static yuyv frame buffer, so we need copy it to frame_buffer
  175. usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_yuyv), false);
  176. while (iso_tx_busy) {
  177. if (tx_flag == 0) {
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. }