[原创] 【ST多款开发板返场测评】STM32F767 Nucleo-144 实现UVC设备-枚举与开关
<div class='showpostmsg'><h1><b>前言</b></h1><p >USB设备的开发第一步是准备描述符,可以参考规格书和搜索样例准备。</p>
<p >我们这里就来准备UVC设备的描述符, 完成枚举过程调试,枚举成功也就成功了一半了。</p>
<p > </p>
<h1 ><b>添加代码</b></h1>
<ol start="12" style="list-style-type:lower-alpha">
<livalue="50">添加源码</li>
</ol>
<p > </p>
<p > </p>
<ol start="12" style="list-style-type:lower-alpha">
<livalue="50">配置头文件包含路径</li>
</ol>
<p > </p>
<ol start="12" style="list-style-type:lower-alpha">
<livalue="50">Main.h中</li>
</ol>
<p >#include "usbd_video.h"</p>
<p >#include "usbd_video_if_template.h"</p>
<ol start="12" style="list-style-type:lower-alpha">
<livalue="50">Main.c中</li>
</ol>
<p > USBD_RegisterClass(&USBD_Device, USBD_VIDEO_CLASS);</p>
<p >USBD_VIDEO_RegisterInterface(&USBD_Device, &USBD_VIDEO_fops_FS);</p>
<p > </p>
<ol start="12" style="list-style-type:lower-alpha">
<livalue="50">usbd_video_if_template.h中</li>
</ol>
<p >const uint8_t image[] = {0x00};</p>
<p >const uint8_t *tImagesList[] = {image};</p>
<p >uint16_t tImagesSizes[] = {IMAGE_SIZE};</p>
<p >移动到usbd_video_if_template.c中</p>
<p > </p>
<ol start="12" style="list-style-type:lower-alpha">
<livalue="50">usbd_core.c中USBD_LL_SetupStage添加调试输出</li>
</ol>
<p > do_debug(DEBUG_TAG_SETUP, DEBUG_LEVEL_INFO, "%#02x %#02x %#02x %#02x %#02x", pdev->request.bmRequest,pdev->request.bRequest,pdev->request.wValue,pdev->request.wIndex,pdev->request.wLength);</p>
<ol start="12" style="list-style-type:lower-alpha">
<livalue="50">USBD_malloc使用了malloc堆管理,堆设置大一些</li>
</ol>
<p > </p>
<h1 ><b>描述符</b></h1>
<p >Usbd_desc.c中</p>
<h2 ><b>设备描述符</b></h2>
<p >如下</p>
<p >重点注意<b>bDeviceClass</b><b>,</b><b>bDeviceSubClass</b><b>,</b><b>bDeviceProtocol </b>着三个值</p>
<p >参考<a href="https://mp.weixin.qq.com/s/qPdHOjLlZRoAKCdj1_8ZGA"><u>https://mp.weixin.qq.com/s/qPdHOjLlZRoAKCdj1_8ZGA</u></a></p>
<p > </p>
<p >/* USB Standard Device Descriptor */</p>
<p >#if defined ( __ICCARM__ ) /*!< IAR Compiler */</p>
<p > #pragma data_alignment=4 </p>
<p >#endif</p>
<p >__ALIGN_BEGIN uint8_t USBD_DeviceDesc __ALIGN_END = {</p>
<p > 0x12, /* bLength */</p>
<p > USB_DESC_TYPE_DEVICE, /* bDescriptorType */</p>
<p > 0x00, /* bcdUSB */</p>
<p > 0x02,</p>
<p ><b> 0xEF, /* bDeviceClass */</b></p>
<p ><b> 0x02, /* bDeviceSubClass */</b></p>
<p ><b> 0x01, /* bDeviceProtocol */</b></p>
<p > USB_MAX_EP0_SIZE, /* bMaxPacketSize */</p>
<p > LOBYTE(USBD_VID), /* idVendor */</p>
<p > HIBYTE(USBD_VID), /* idVendor */</p>
<p > LOBYTE(USBD_PID), /* idVendor */</p>
<p > HIBYTE(USBD_PID), /* idVendor */</p>
<p > 0x00, /* bcdDevice rel. 2.00 */</p>
<p > 0x02,</p>
<p > USBD_IDX_MFC_STR, /* Index of manufacturer string */</p>
<p > USBD_IDX_PRODUCT_STR, /* Index of product string */</p>
<p > USBD_IDX_SERIAL_STR, /* Index of serial number string */</p>
<p > USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */</p>
<p >}; /* USB_DeviceDescriptor */</p>
<h2 ><b>字符串描述符</b></h2>
<p >修改对应的字符串</p>
<p >#define USBD_MANUFACTURER_STRING "MyCompany"</p>
<p >#define USBD_PRODUCT_HS_STRING "UVCCamera"</p>
<p >#define USBD_PRODUCT_FS_STRING "UVCCamera"</p>
<p >#define USBD_CONFIGURATION_HS_STRING "UVC Config"</p>
<p >#define USBD_INTERFACE_HS_STRING "UVC Interface"</p>
<p >#define USBD_CONFIGURATION_FS_STRING "UVC Config"</p>
<p >#define USBD_INTERFACE_FS_STRING "UVC Interface"</p>
<p > </p>
<h2 ><b>配置描述符</b></h2>
<p >usbd_video.c中</p>
<p >USBD_VIDEO_CfgDesc</p>
<p >详见后面UsbTreeView解析</p>
<h1 ><b>测试</b></h1>
<p >运行看到打印的枚举过程如下</p>
<p > </p>
<p >设备管理器看到枚举的设备</p>
<p > </p>
<p >使用potplayer可以看到设备</p>
<p > </p>
<p > </p>
<p >打开关闭设备</p>
<p > </p>
<p > </p>
<p >UsbTreeView查看描述符解析</p>
<p > </p>
<p > </p>
<pre>
<code class="language-cpp"> ---------------------- Device Descriptor ----------------------
bLength : 0x12 (18 bytes)
bDescriptorType : 0x01 (Device Descriptor)
bcdUSB : 0x200 (USB Version 2.0) -> but device is Full-Speed only
bDeviceClass : 0xEF (Miscellaneous)
bDeviceSubClass : 0x02
bDeviceProtocol : 0x01 (IAD - Interface Association Descriptor)
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0x0483 (STMicroelectronics)
idProduct : 0x5710
bcdDevice : 0x0200
iManufacturer : 0x01 (String Descriptor 1)
Language 0x0409 : "MyCompany"
iProduct : 0x02 (String Descriptor 2)
Language 0x0409 : "UVCCamera"
iSerialNumber : 0x03 (String Descriptor 3)
Language 0x0409 : "FFFFFFFEFFFF"
bNumConfigurations : 0x01 (1 Configuration)
Data (HexDump) : 12 01 00 02 EF 02 01 40 83 04 10 57 00 02 01 02 .......@...W....
03 01 ..
------------------ Configuration Descriptor -------------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x02 (Configuration Descriptor)
wTotalLength : 0x0088 (136 bytes)
bNumInterfaces : 0x02 (2 Interfaces)
bConfigurationValue : 0x01 (Configuration 1)
iConfiguration : 0x00 (No String Descriptor)
bmAttributes : 0xC0
D7: Reserved, set 1 : 0x01
D6: Self Powered : 0x01 (yes)
D5: Remote Wakeup : 0x00 (no)
D4..0: Reserved, set 0 : 0x00
MaxPower : 0x32 (100 mA)
Data (HexDump) : 09 02 88 00 02 01 00 C0 32 08 0B 00 02 0E 03 00 ........2.......
00 09 04 00 00 00 0E 01 00 00 0D 24 01 10 01 1E ...........$....
00 00 6C DC 02 01 01 08 24 02 01 00 02 00 00 09 ..l.....$.......
24 03 02 01 01 00 01 00 09 04 01 00 00 0E 02 00 $...............
00 0E 24 01 01 37 00 81 00 02 00 00 00 01 00 0B ..$..7..........
24 06 01 01 01 01 00 00 00 00 1E 24 07 01 02 90 $..........$....
01 F0 00 00 60 EA 00 00 60 EA 00 00 B8 0B 00 40 ....`...`......@
42 0F 00 01 40 42 0F 00 09 04 01 01 01 0E 02 00 B...@B..........
00 07 05 81 05 00 01 01 ........
------------------- IAD Descriptor --------------------
bLength : 0x08 (8 bytes)
bDescriptorType : 0x0B (Interface Association Descriptor)
bFirstInterface : 0x00 (Interface 0)
bInterfaceCount : 0x02 (2 Interfaces)
bFunctionClass : 0x0E (Video)
bFunctionSubClass : 0x03 (Video Interface Collection)
bFunctionProtocol : 0x00 (PC_PROTOCOL_UNDEFINED)
iFunction : 0x00 (No String Descriptor)
Data (HexDump) : 08 0B 00 02 0E 03 00 00 ........
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x00 (Interface 0)
bAlternateSetting : 0x00
bNumEndpoints : 0x00 (Default Control Pipe only)
bInterfaceClass : 0x0E (Video)
bInterfaceSubClass : 0x01 (Video Control)
bInterfaceProtocol : 0x00
iInterface : 0x00 (No String Descriptor)
Data (HexDump) : 09 04 00 00 00 0E 01 00 00 .........
------- Video Control Interface Header Descriptor -----
bLength : 0x0D (13 bytes)
bDescriptorType : 0x24 (Video Control Interface)
bDescriptorSubtype : 0x01 (Video Control Header)
bcdUVC : 0x0110 (UVC Version 1.10)
wTotalLength : 0x001E (30 bytes)
dwClockFreq : 0x02DC6C00 (48 MHz)
bInCollection : 0x01 (1 VideoStreaming interface)
baInterfaceNr : 0x01 (Interface 1)
Data (HexDump) : 0D 24 01 10 01 1E 00 00 6C DC 02 01 01 .$......l....
-------- Video Control Input Terminal Descriptor ------
bLength : 0x08 (8 bytes)
bDescriptorType : 0x24 (Video Control Interface)
bDescriptorSubtype : 0x02 (Input Terminal)
bTerminalID : 0x01 (1)
wTerminalType : 0x0200 (ITT_VENDOR_SPECIFIC)
bAssocTerminal : 0x00 (Not associated with an Output Terminal)
iTerminal : 0x00 (No String Descriptor)
Data (HexDump) : 08 24 02 01 00 02 00 00 .$......
------- Video Control Output Terminal Descriptor ------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x24 (Video Control Interface)
bDescriptorSubtype : 0x03 (Output Terminal)
bTerminalID : 0x02 (2)
wTerminalType : 0x0101 (TT_STREAMING)
bAssocTerminal : 0x00 (Not associated with an Input Terminal)
bSourceID : 0x01 (1)
iTerminal : 0x00 (No String Descriptor)
Data (HexDump) : 09 24 03 02 01 01 00 01 00 .$.......
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x01 (Interface 1)
bAlternateSetting : 0x00
bNumEndpoints : 0x00 (Default Control Pipe only)
bInterfaceClass : 0x0E (Video)
bInterfaceSubClass : 0x02 (Video Streaming)
bInterfaceProtocol : 0x00
iInterface : 0x00 (No String Descriptor)
Data (HexDump) : 09 04 01 00 00 0E 02 00 00 .........
---- VC-Specific VS Video Input Header Descriptor -----
bLength : 0x0E (14 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x01 (Input Header)
bNumFormats : 0x01
wTotalLength : 0x0037 (55 bytes)
bEndpointAddress : 0x81 (Direction=IN EndpointID=1)
bmInfo : 0x00 (Dynamic Format Change not supported)
bTerminalLink : 0x02 (Output Terminal ID 2)
bStillCaptureMethod : 0x00 (No Still Capture)
nbTriggerSupport : 0x00 (Hardware Triggering not supported)
bTriggerUsage : 0x00 (Host will initiate still image capture)
bControlSize : 0x01 (1 bytes each)
Video Payload Format 1 : 0x00
D0 : 0 no - Key Frame Rate
D1 : 0 no - P Frame Rate
D2 : 0 no - Compression Quality
D3 : 0 no - Compression Window Size
D4 : 0 no - Generate Key Frame
D5 : 0 no - Update Frame Segment
D6 : 0 no - Reserved
D7 : 0 no - Reserved
Data (HexDump) : 0E 24 01 01 37 00 81 00 02 00 00 00 01 00 .$..7.........
----- Video Streaming MJPEG Format Type Descriptor ----
bLength : 0x0B (11 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x06 (Format MJPEG)
bFormatIndex : 0x01 (1)
bNumFrameDescriptors : 0x01 (1)
bmFlags : 0x01 (Sample size is fixed)
bDefaultFrameIndex : 0x01 (1)
bAspectRatioX : 0x00
bAspectRatioY : 0x00
bmInterlaceFlags : 0x00
D0 IL stream or variable: 0 (no)
D1 Fields per frame : 0 (2 fields)
D2 Field 1 first : 0 (no)
D3 Reserved : 0
D4..5 Field pattern : 0 (Field 1 only)
D6..7 Display Mode : 0 (Bob only)
bCopyProtect : 0x00 (No restrictions)
*!*ERROR: no Color Matching Descriptor for this format
Data (HexDump) : 0B 24 06 01 01 01 01 00 00 00 00 .$.........
----- Video Streaming MJPEG Frame Type Descriptor -----
---> This is the Default (optimum) Frame index
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x07 (MJPEG Frame Type)
bFrameIndex : 0x01
bmCapabilities : 0x02
wWidth : 0x0190 (400)
wHeight : 0x00F0 (240)
dwMinBitRate : 0x00EA6000 (15360000 bps -> 1.920 MB/s)
dwMaxBitRate : 0x00EA6000 (15360000 bps -> 1.920 MB/s)
dwMaxVideoFrameBufferSize: 0x000BB800 (768000 bytes)
dwDefaultFrameInterval : 0x000F4240 (100.0000 ms -> 10.0000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval : 0x000F4240 (100.0000 ms -> 10.0000 fps)
Data (HexDump) : 1E 24 07 01 02 90 01 F0 00 00 60 EA 00 00 60 EA .$........`...`.
00 00 B8 0B 00 40 42 0F 00 01 40 42 0F 00 .....@B...@B..
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x01 (Interface 1)
bAlternateSetting : 0x01
bNumEndpoints : 0x01 (1 Endpoint)
bInterfaceClass : 0x0E (Video)
bInterfaceSubClass : 0x02 (Video Streaming)
bInterfaceProtocol : 0x00
iInterface : 0x00 (No String Descriptor)
Data (HexDump) : 09 04 01 01 01 0E 02 00 00 .........
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x81 (Direction=IN EndpointID=1)
bmAttributes : 0x05 (TransferType=Isochronous SyncType=Asynchronous EndpointType=Data)
wMaxPacketSize : 0x0100 (256 bytes)
bInterval : 0x01 (1 ms)
Data (HexDump) : 07 05 81 05 00 01 01 .......
----------------- Device Qualifier Descriptor -----------------
Error : ERROR_GEN_FAILURE (because the device is Full-Speed only)
-------------------- String Descriptors -------------------
------ String Descriptor 0 ------
bLength : 0x04 (4 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language ID : 0x0409 (English - United States)
Data (HexDump) : 04 03 09 04 ....
------ String Descriptor 1 ------
bLength : 0x14 (20 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "MyCompany"
Data (HexDump) : 14 03 4D 00 79 00 43 00 6F 00 6D 00 70 00 61 00 ..M.y.C.o.m.p.a.
6E 00 79 00 n.y.
------ String Descriptor 2 ------
bLength : 0x14 (20 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "UVCCamera"
Data (HexDump) : 14 03 55 00 56 00 43 00 43 00 61 00 6D 00 65 00 ..U.V.C.C.a.m.e.
72 00 61 00 r.a.
------ String Descriptor 3 ------
bLength : 0x1A (26 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "FFFFFFFEFFFF"
Data (HexDump) : 1A 03 46 00 46 00 46 00 46 00 46 00 46 00 46 00 ..F.F.F.F.F.F.F.
45 00 46 00 46 00 46 00 46 00 E.F.F.F.F.
------ String Descriptor 4 ------
bLength : 0x16 (22 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "UVC Config"
Data (HexDump) : 16 03 55 00 56 00 43 00 20 00 43 00 6F 00 6E 00 ..U.V.C. .C.o.n.
66 00 69 00 67 00 f.i.g.
------ String Descriptor 5 ------
bLength : 0x1C (28 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "UVC Interface"
Data (HexDump) : 1C 03 55 00 56 00 43 00 20 00 49 00 6E 00 74 00 ..U.V.C. .I.n.t.
65 00 72 00 66 00 61 00 63 00 65 00 e.r.f.a.c.e.</code></pre>
<h1 ><b>总结</b></h1>
<p >以上实现了UVC设备的枚举与打开关闭,已经完成了一半,后面加上图像数据的发送即可。</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]