video_static_h264_template.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_h264.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)(640)
  24. #define HEIGHT (unsigned int)(480)
  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_H264_DESC + VIDEO_SIZEOF_VS_FRAME_H264_DESC(1))
  31. #define USB_VIDEO_DESC_SIZ (unsigned long)(9 + \
  32. VIDEO_VC_NOEP_DESCRIPTOR_LEN + \
  33. 9 + \
  34. VS_HEADER_SIZ + \
  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. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  42. static const uint8_t device_descriptor[] = {
  43. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  44. };
  45. static const uint8_t config_descriptor[] = {
  46. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  47. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  48. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  49. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  50. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  51. VIDEO_VS_FORMAT_H264_DESCRIPTOR_INIT(0x01, 0x01),
  52. VIDEO_VS_FRAME_H264_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  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. #else
  104. const uint8_t video_descriptor[] = {
  105. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
  106. USB_CONFIG_DESCRIPTOR_INIT(USB_VIDEO_DESC_SIZ, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  107. //VIDEO_VC_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  108. VIDEO_VC_NOEP_DESCRIPTOR_INIT(0x00, VIDEO_INT_EP, 0x0100, VIDEO_VC_TERMINAL_LEN, 48000000, 0x02),
  109. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x00, 0x00),
  110. VIDEO_VS_INPUT_HEADER_DESCRIPTOR_INIT(0x01, VS_HEADER_SIZ, VIDEO_IN_EP, 0x00),
  111. VIDEO_VS_FORMAT_H264_DESCRIPTOR_INIT(0x01, 0x01),
  112. VIDEO_VS_FRAME_H264_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  113. VIDEO_VS_DESCRIPTOR_INIT(0x01, 0x01, 0x01),
  114. /* 1.2.2.2 Standard VideoStream Isochronous Video Data Endpoint Descriptor */
  115. USB_ENDPOINT_DESCRIPTOR_INIT(VIDEO_IN_EP, 0x05, VIDEO_PACKET_SIZE, 0x01),
  116. ///////////////////////////////////////
  117. /// string0 descriptor
  118. ///////////////////////////////////////
  119. USB_LANGID_INIT(USBD_LANGID_STRING),
  120. ///////////////////////////////////////
  121. /// string1 descriptor
  122. ///////////////////////////////////////
  123. 0x14, /* bLength */
  124. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  125. 'C', 0x00, /* wcChar0 */
  126. 'h', 0x00, /* wcChar1 */
  127. 'e', 0x00, /* wcChar2 */
  128. 'r', 0x00, /* wcChar3 */
  129. 'r', 0x00, /* wcChar4 */
  130. 'y', 0x00, /* wcChar5 */
  131. 'U', 0x00, /* wcChar6 */
  132. 'S', 0x00, /* wcChar7 */
  133. 'B', 0x00, /* wcChar8 */
  134. ///////////////////////////////////////
  135. /// string2 descriptor
  136. ///////////////////////////////////////
  137. 0x26, /* bLength */
  138. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  139. 'C', 0x00, /* wcChar0 */
  140. 'h', 0x00, /* wcChar1 */
  141. 'e', 0x00, /* wcChar2 */
  142. 'r', 0x00, /* wcChar3 */
  143. 'r', 0x00, /* wcChar4 */
  144. 'y', 0x00, /* wcChar5 */
  145. 'U', 0x00, /* wcChar6 */
  146. 'S', 0x00, /* wcChar7 */
  147. 'B', 0x00, /* wcChar8 */
  148. ' ', 0x00, /* wcChar9 */
  149. 'U', 0x00, /* wcChar10 */
  150. 'V', 0x00, /* wcChar11 */
  151. 'C', 0x00, /* wcChar12 */
  152. ' ', 0x00, /* wcChar13 */
  153. 'D', 0x00, /* wcChar14 */
  154. 'E', 0x00, /* wcChar15 */
  155. 'M', 0x00, /* wcChar16 */
  156. 'O', 0x00, /* wcChar17 */
  157. ///////////////////////////////////////
  158. /// string3 descriptor
  159. ///////////////////////////////////////
  160. 0x16, /* bLength */
  161. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  162. '2', 0x00, /* wcChar0 */
  163. '0', 0x00, /* wcChar1 */
  164. '2', 0x00, /* wcChar2 */
  165. '1', 0x00, /* wcChar3 */
  166. '0', 0x00, /* wcChar4 */
  167. '3', 0x00, /* wcChar5 */
  168. '1', 0x00, /* wcChar6 */
  169. '0', 0x00, /* wcChar7 */
  170. '0', 0x00, /* wcChar8 */
  171. '0', 0x00, /* wcChar9 */
  172. #ifdef CONFIG_USB_HS
  173. ///////////////////////////////////////
  174. /// device qualifier descriptor
  175. ///////////////////////////////////////
  176. 0x0a,
  177. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  178. 0x00,
  179. 0x02,
  180. 0x00,
  181. 0x00,
  182. 0x00,
  183. 0x40,
  184. 0x00,
  185. 0x00,
  186. #endif
  187. 0x00
  188. };
  189. #endif
  190. volatile bool tx_flag = 0;
  191. volatile bool iso_tx_busy = false;
  192. static void usbd_event_handler(uint8_t busid, uint8_t event)
  193. {
  194. switch (event) {
  195. case USBD_EVENT_RESET:
  196. break;
  197. case USBD_EVENT_CONNECTED:
  198. break;
  199. case USBD_EVENT_DISCONNECTED:
  200. break;
  201. case USBD_EVENT_RESUME:
  202. break;
  203. case USBD_EVENT_SUSPEND:
  204. break;
  205. case USBD_EVENT_CONFIGURED:
  206. tx_flag = 0;
  207. iso_tx_busy = false;
  208. break;
  209. case USBD_EVENT_SET_REMOTE_WAKEUP:
  210. break;
  211. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. void usbd_video_open(uint8_t busid, uint8_t intf)
  218. {
  219. tx_flag = 1;
  220. USB_LOG_RAW("OPEN\r\n");
  221. iso_tx_busy = false;
  222. }
  223. void usbd_video_close(uint8_t busid, uint8_t intf)
  224. {
  225. USB_LOG_RAW("CLOSE\r\n");
  226. tx_flag = 0;
  227. iso_tx_busy = false;
  228. }
  229. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  230. {
  231. if (usbd_video_stream_split_transfer(busid, ep)) {
  232. /* one frame has done */
  233. iso_tx_busy = false;
  234. }
  235. }
  236. static struct usbd_endpoint video_in_ep = {
  237. .ep_cb = usbd_video_iso_callback,
  238. .ep_addr = VIDEO_IN_EP
  239. };
  240. struct usbd_interface intf0;
  241. struct usbd_interface intf1;
  242. void video_init(uint8_t busid, uintptr_t reg_base)
  243. {
  244. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  245. usbd_desc_register(busid, &video_descriptor);
  246. #else
  247. usbd_desc_register(busid, video_descriptor);
  248. #endif
  249. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  250. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  251. usbd_add_endpoint(busid, &video_in_ep);
  252. usbd_initialize(busid, reg_base, usbd_event_handler);
  253. }
  254. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[2][40 * 1024];
  255. void video_test(uint8_t busid)
  256. {
  257. uint32_t out_len;
  258. uint32_t packets;
  259. (void)packets;
  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_h264, sizeof(cherryusb_h264));
  265. while (iso_tx_busy) {
  266. if (tx_flag == 0) {
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. }