video_static_yuyv_template.c 8.9 KB

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