video_static_yuyv_template.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. const uint8_t video_descriptor[] = {
  43. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  44. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  45. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  46. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  47. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  48. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  49. VIDEO_VS_FORMAT_UNCOMPRESSED_DESCRIPTOR_INIT(0x01, 0x01, VIDEO_GUID_YUY2),
  50. VIDEO_VS_FRAME_UNCOMPRESSED_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, MAX_FRAME_SIZE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  51. VIDEO_VS_COLOR_MATCHING_DESCRIPTOR_INIT(),
  52. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  53. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  54. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  55. ///////////////////////////////////////
  56. /// string0 descriptor
  57. ///////////////////////////////////////
  58. USB_LANGID_INIT(USBD_LANGID_STRING),
  59. ///////////////////////////////////////
  60. /// string1 descriptor
  61. ///////////////////////////////////////
  62. 0x14, /* bLength */
  63. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  64. 'C', 0x00, /* wcChar0 */
  65. 'h', 0x00, /* wcChar1 */
  66. 'e', 0x00, /* wcChar2 */
  67. 'r', 0x00, /* wcChar3 */
  68. 'r', 0x00, /* wcChar4 */
  69. 'y', 0x00, /* wcChar5 */
  70. 'U', 0x00, /* wcChar6 */
  71. 'S', 0x00, /* wcChar7 */
  72. 'B', 0x00, /* wcChar8 */
  73. ///////////////////////////////////////
  74. /// string2 descriptor
  75. ///////////////////////////////////////
  76. 0x26, /* bLength */
  77. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  78. 'C', 0x00, /* wcChar0 */
  79. 'h', 0x00, /* wcChar1 */
  80. 'e', 0x00, /* wcChar2 */
  81. 'r', 0x00, /* wcChar3 */
  82. 'r', 0x00, /* wcChar4 */
  83. 'y', 0x00, /* wcChar5 */
  84. 'U', 0x00, /* wcChar6 */
  85. 'S', 0x00, /* wcChar7 */
  86. 'B', 0x00, /* wcChar8 */
  87. ' ', 0x00, /* wcChar9 */
  88. 'U', 0x00, /* wcChar10 */
  89. 'V', 0x00, /* wcChar11 */
  90. 'C', 0x00, /* wcChar12 */
  91. ' ', 0x00, /* wcChar13 */
  92. 'D', 0x00, /* wcChar14 */
  93. 'E', 0x00, /* wcChar15 */
  94. 'M', 0x00, /* wcChar16 */
  95. 'O', 0x00, /* wcChar17 */
  96. ///////////////////////////////////////
  97. /// string3 descriptor
  98. ///////////////////////////////////////
  99. 0x16, /* bLength */
  100. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  101. '2', 0x00, /* wcChar0 */
  102. '0', 0x00, /* wcChar1 */
  103. '2', 0x00, /* wcChar2 */
  104. '1', 0x00, /* wcChar3 */
  105. '0', 0x00, /* wcChar4 */
  106. '3', 0x00, /* wcChar5 */
  107. '1', 0x00, /* wcChar6 */
  108. '0', 0x00, /* wcChar7 */
  109. '0', 0x00, /* wcChar8 */
  110. '0', 0x00, /* wcChar9 */
  111. #ifdef CONFIG_USB_HS
  112. ///////////////////////////////////////
  113. /// device qualifier descriptor
  114. ///////////////////////////////////////
  115. 0x0a,
  116. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  117. 0x00,
  118. 0x02,
  119. 0x00,
  120. 0x00,
  121. 0x00,
  122. 0x40,
  123. 0x00,
  124. 0x00,
  125. #endif
  126. 0x00
  127. };
  128. volatile bool tx_flag = 0;
  129. volatile bool iso_tx_busy = false;
  130. static void usbd_event_handler(uint8_t busid, uint8_t event)
  131. {
  132. switch (event) {
  133. case USBD_EVENT_RESET:
  134. break;
  135. case USBD_EVENT_CONNECTED:
  136. break;
  137. case USBD_EVENT_DISCONNECTED:
  138. break;
  139. case USBD_EVENT_RESUME:
  140. break;
  141. case USBD_EVENT_SUSPEND:
  142. break;
  143. case USBD_EVENT_CONFIGURED:
  144. tx_flag = 0;
  145. iso_tx_busy = false;
  146. break;
  147. case USBD_EVENT_SET_REMOTE_WAKEUP:
  148. break;
  149. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. void usbd_video_open(uint8_t busid, uint8_t intf)
  156. {
  157. tx_flag = 1;
  158. USB_LOG_RAW("OPEN\r\n");
  159. iso_tx_busy = false;
  160. }
  161. void usbd_video_close(uint8_t busid, uint8_t intf)
  162. {
  163. USB_LOG_RAW("CLOSE\r\n");
  164. tx_flag = 0;
  165. iso_tx_busy = false;
  166. }
  167. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  168. {
  169. if (usbd_video_stream_split_transfer(busid, ep)) {
  170. /* one frame has done */
  171. iso_tx_busy = false;
  172. }
  173. }
  174. static struct usbd_endpoint video_in_ep = {
  175. .ep_cb = usbd_video_iso_callback,
  176. .ep_addr = VIDEO_IN_EP
  177. };
  178. struct usbd_interface intf0;
  179. struct usbd_interface intf1;
  180. void video_init(uint8_t busid, uintptr_t reg_base)
  181. {
  182. usbd_desc_register(busid, video_descriptor);
  183. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  184. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  185. usbd_add_endpoint(busid, &video_in_ep);
  186. usbd_initialize(busid, reg_base, usbd_event_handler);
  187. }
  188. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][MAX_PACKETS_IN_ONE_TRANSFER * MAX_PAYLOAD_SIZE];
  189. void video_test(uint8_t busid)
  190. {
  191. memset(packet_buffer, 0, sizeof(packet_buffer));
  192. while (1) {
  193. if (tx_flag) {
  194. iso_tx_busy = true;
  195. 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));
  196. while (iso_tx_busy) {
  197. if (tx_flag == 0) {
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. }