【USB】USB设备类代码表(USB Class Code)

本文介绍了USB设备类代码的基本概念及其应用。详细解释了BaseClass、SubClass和Protocol的作用,并列举了不同类别的USB设备及其在设备描述符和接口描述符中的使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

USB设备类代码表官网链接:http://www.usb.org/developers/defined_class

USB defines class code information that is used to identify a device’s functionality and to nominally load a device driver based on that functionality. The information is contained in three bytes with the names Base Class, SubClass, and Protocol. (Note that ‘Base Class’ is used in this description to identify the first byte of the Class Code triple. That terminology is not used in the USB Specification). There are two places on a device where class code information can be placed.One place is in the Device Descriptor, and the other is in Interface Descriptors. Some defined class codes are allowed to be used only in a Device Descriptor, others can be used in both Device and Interface Descriptors, and some can only be used in Interface Descriptors. The table below shows the currently defined set of Base Class values, what the generic usage is, and where that Base Class can be used (either Device or Interface Descriptors or both).

USB定义了设备类的类别码信息,它的功能包括:可以用来识别设备并且加载设备驱动。这种代码信息有包含Base Class[基类])、SubClass[子类])、Protocol[协议])一共占有3个字节(注意:**[基类]**在文本中,用来标识三个字节的种类代码的首字节;在USB规范中没有使用这个术语)。设备中有两个地方可以存放种类代码信息,一个是设备描述符,另一个是接口描述符。已经定义的种类代码,有些只能放在设备描述符里,有些只能放在接口描述符里,有些两种描述符里都可以用。下表给出现在已经定了的基类的值的集合,通常用法是什么,基类用在什么场合(设备描述符或者接口描述符)。

Base ClassDescriptor UsageDescription
00hDeviceUse class information in the Interface Descriptors种类信息定义在接口描述符中
01hInterfaceAudio音频设备
02hBothCommunications&CDC通信设备
03hInterfaceHID(Human Interface Device)人机接口设备
05hInterfacePhysical物理设备
06hInterfaceImage图像设备
07hInterfacePrinter打印机
08hInterfaceMass Storage 大容量存储
09hDeviceHub集线器
0AhInterfaceCDC-Data通信设备
0BhInterfaceSmart Card智能卡
0DhInterfaceContent Security内容安全设备
0EhInterfaceVideo视频设备
0FhInterfacePersonal Healthcare个人健康设备
10hInterfaceAudio/Video Devices声音/视频设备
11hDeviceBillboard Device Class广播牌设备
12hInterfaceUSB Type-C Bridge Class
DChBothDiagnostic Device
E0hInterfaceWireless Controller
EFhBothMiscellaneous
FEhInterfaceApplication Specific
FFhBothVendor Specific
### STM32F103C8T6 USB 数据收发教程 STM32F103C8T6支持USB全速设备功能,可以实现数据的高效传输。为了使该微控制器能够通过USB接口进行数据的接收和发送,通常需要配置USB外设并编写相应的固件程序。 #### 配置USB外设 在初始化阶段,需设置USB时钟、GPIO引脚以及中断优先级等参数[^1]: ```c // 初始化USB外设 void MX_USB_DEVICE_Init(void) { /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */ /* USER CODE END USB_DEVICE_Init_PreTreatment */ hUsbDeviceFS.dev_class = &USBD_CDC_fops; USBD_Init(&hUsbDeviceFS, &VCP_Desc, DEVICE_FS); USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC); USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS); USBD_Start(&hUsbDeviceFS); /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */ /* USER CODE END USB_DEVICE_Init_PostTreatment */ } ``` 这段代码展示了如何启动USB设备,并注册CDC(通信设备)接口操作函数指针表。 #### 发送数据到PC端 当应用程序准备向计算机发送数据时,调用`USBD_CDC_SendData()`函数即可完成此过程。下面是一个简单的例子说明怎样构建字符串并通过串口发送给主机侧的应用软件: ```c char buffer[]="Hello from STM32!"; USBD_CDC_SendData((uint8_t*)buffer,strlen(buffer)); ``` 这里定义了一个字符数组作为消息体,接着将其传递给上述提到的方法来执行实际的数据传送动作。 #### 接收来自PC端的数据 对于从上位机接收到的信息,则可以通过重写`USBD_CDC_ReceiveCallback()`回调方法来进行处理。每当有新的可用字节到达缓冲区时就会触发这个事件处理器,在其中读取这些新到来的内容并做进一步解析或响应: ```c static int8_t CDC_Receive(uint8_t* Buf, uint32_t *Len) { // Process received data here return (USBD_OK); } extern "C" void USBD_CDC_ReceiveCallback(uint8_t dev_num,uint8_t ep_addr,void *pdata,uint16_t length){ CDC_Receive(pdata,&length); } ``` 以上实现了对接收到的数据包长度检查及其具体内容提取的功能逻辑。 #### 示例项目结构 一个完整的基于CubeMX生成的基础工程框架下添加了必要的源文件用于管理USB连接状态变化通知机制、线程同步等问题;同时提供了几个实用工具宏定义简化编程工作量。 ```plaintext Project/ ├── Core/Inc/usbd_cdc_if.h ├── Core/Src/usbd_cdc_if.c └── Middleware/ST/STM32_USB_Device_Library/ ├── Class/cdc/src/usbd_cdc.c └── usbd_conf_template.c ``` 在这个目录树形图里列出了主要涉及的部分头文件(.h) 和 实现文件 (.c),它们共同协作完成了整个USB通讯流程的设计与实施。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值