video_static_mjpeg_template.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_mjpeg.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)(640)
  23. #define HEIGHT (unsigned int)(480)
  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_MJPEG_DESC + VIDEO_SIZEOF_VS_FRAME_MJPEG_DESC(1))
  30. #define USB_VIDEO_DESC_SIZ (unsigned long)(9 + \
  31. VIDEO_VC_NOEP_DESCRIPTOR_LEN + \
  32. 9 + \
  33. VS_HEADER_SIZ + \
  34. 9 + \
  35. 7)
  36. #define USBD_VID 0xffff
  37. #define USBD_PID 0xffff
  38. #define USBD_MAX_POWER 100
  39. #define USBD_LANGID_STRING 1033
  40. static const uint8_t device_descriptor[] = {
  41. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
  42. };
  43. static const uint8_t config_descriptor[] = {
  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_MJPEG_DESCRIPTOR_INIT(0x01, 0x01),
  50. VIDEO_VS_FRAME_MJPEG_DESCRIPTOR_INIT(0x01, WIDTH, HEIGHT, MIN_BIT_RATE, MAX_BIT_RATE, MAX_FRAME_SIZE, DBVAL(INTERVAL), 0x01, DBVAL(INTERVAL)),
  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. static const uint8_t device_quality_descriptor[] = {
  56. ///////////////////////////////////////
  57. /// device qualifier descriptor
  58. ///////////////////////////////////////
  59. 0x0a,
  60. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  61. 0x00,
  62. 0x02,
  63. 0x00,
  64. 0x00,
  65. 0x00,
  66. 0x40,
  67. 0x00,
  68. 0x00,
  69. };
  70. static const char *string_descriptors[] = {
  71. (const char[]){ 0x09, 0x04 }, /* Langid */
  72. "CherryUSB", /* Manufacturer */
  73. "CherryUSB UVC DEMO", /* Product */
  74. "2022123456", /* Serial Number */
  75. };
  76. static const uint8_t *device_descriptor_callback(uint8_t speed)
  77. {
  78. return device_descriptor;
  79. }
  80. static const uint8_t *config_descriptor_callback(uint8_t speed)
  81. {
  82. return config_descriptor;
  83. }
  84. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  85. {
  86. return device_quality_descriptor;
  87. }
  88. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  89. {
  90. if (index > 3) {
  91. return NULL;
  92. }
  93. return string_descriptors[index];
  94. }
  95. const struct usb_descriptor video_descriptor = {
  96. .device_descriptor_callback = device_descriptor_callback,
  97. .config_descriptor_callback = config_descriptor_callback,
  98. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  99. .string_descriptor_callback = string_descriptor_callback
  100. };
  101. volatile bool tx_flag = 0;
  102. volatile bool iso_tx_busy = false;
  103. static void usbd_event_handler(uint8_t busid, uint8_t event)
  104. {
  105. switch (event) {
  106. case USBD_EVENT_RESET:
  107. break;
  108. case USBD_EVENT_CONNECTED:
  109. break;
  110. case USBD_EVENT_DISCONNECTED:
  111. break;
  112. case USBD_EVENT_RESUME:
  113. break;
  114. case USBD_EVENT_SUSPEND:
  115. break;
  116. case USBD_EVENT_CONFIGURED:
  117. tx_flag = 0;
  118. iso_tx_busy = false;
  119. break;
  120. case USBD_EVENT_SET_REMOTE_WAKEUP:
  121. break;
  122. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. void usbd_video_open(uint8_t busid, uint8_t intf)
  129. {
  130. tx_flag = 1;
  131. USB_LOG_RAW("OPEN\r\n");
  132. iso_tx_busy = false;
  133. }
  134. void usbd_video_close(uint8_t busid, uint8_t intf)
  135. {
  136. USB_LOG_RAW("CLOSE\r\n");
  137. tx_flag = 0;
  138. iso_tx_busy = false;
  139. }
  140. void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
  141. {
  142. if (nbytes == 0) {
  143. return;
  144. }
  145. if (usbd_video_stream_split_transfer(busid, ep)) {
  146. /* one frame has done */
  147. iso_tx_busy = false;
  148. }
  149. }
  150. static struct usbd_endpoint video_in_ep = {
  151. .ep_cb = usbd_video_iso_callback,
  152. .ep_addr = VIDEO_IN_EP
  153. };
  154. struct usbd_interface intf0;
  155. struct usbd_interface intf1;
  156. void video_init(uint8_t busid, uintptr_t reg_base)
  157. {
  158. usbd_desc_register(busid, &video_descriptor);
  159. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf0, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  160. usbd_add_interface(busid, usbd_video_init_intf(busid, &intf1, INTERVAL, MAX_FRAME_SIZE, MAX_PAYLOAD_SIZE));
  161. usbd_add_endpoint(busid, &video_in_ep);
  162. usbd_initialize(busid, reg_base, usbd_event_handler);
  163. }
  164. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE];
  165. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t frame_buffer[32 * 1024];
  166. void video_test(uint8_t busid)
  167. {
  168. memset(packet_buffer, 0, sizeof(packet_buffer));
  169. while (1) {
  170. if (tx_flag) {
  171. iso_tx_busy = true;
  172. memcpy(frame_buffer, cherryusb_mjpeg, sizeof(cherryusb_mjpeg)); // cherryusb_mjpeg is a static MJPEG frame buffer, so we need copy it to frame_buffer
  173. usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)frame_buffer, sizeof(cherryusb_mjpeg), false);
  174. while (iso_tx_busy) {
  175. if (tx_flag == 0) {
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. }