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