// Copy the transfer buffer to the endpoint0's buffer BufferCopy_UserToPMA( vsDeviceInfo.TransInfo.pBuffer+wOffset, // transfer buffer GetBuffDescTable_TXAddr(ENDP0), // endpoint 0 TX address wLength);
// Update the data lengths vsDeviceInfo.TransInfo.wLength -= wLength; vsDeviceInfo.TransInfo.wOffset += wLength;
return RESULT_LASTDATA; }
return RESULT_SUCCESS; } 如果发送的数据长度大于端点设置的最大数据包长度,数据将分割为若干次发送,记录发送数据的状态包含在结构体TRANSFER_INFO中: // ***************************************************************************** // TRANSFER_INFO // ***************************************************************************** typedef struct _TRANSFER_INFO { unsigned short wLength; // total lengths data will be transmit unsigned short wOffset; // number of data be transmited unsigned short wPacketSize; // endpoints packet max size unsigned char* pBuffer; // address of data buffer } TRANSFER_INFO, *PTRANSFER_INFO; TRANSFER_INFO.wLength记录发送的数据长度,如果非0,表示有数据需要被发送。 TRANSFER_INFO.wOffset记录已发送的数据长度,用以确定数据缓冲TRANSFER_INFO.pBuffer的偏移量。
需要了解的一点:USB主机向USB设备正确发送一请求后(这部分的处理由硬件完成),USB主机将间隔若干次的向USB设备索取响应数据,STM32 USB TX状态为NAK说明不响应USB主机,USB主机在超时后退出此次请求;TX状态为STLL说明中断此次请求,USB主机将无条件退出请求;TX状态为VALID说明设备已准备好数据发送,USB主机将从USB设备读取数据。 以非0长度数据请求的GET_DESCRIPTOR请求为例的响应过程: CTR_SETUP0()->SETUP0_Data()->SR_GetDescriptor()->SETUP0_TransData()
RESULT SR_GetDescriptor(void) { // RequestType: device->host, standard request and device recipient if(vsDeviceInfo.SetupData.bmRequestType == RT_D2H_STANDARD_DEVICE) { // SetupData.wValue.b.MSB: descriptor type // SetupData.wValue.b.LSB: descriptor index switch(vsDeviceInfo.SetupData.wValue.b.MSB) { case DESCRIPTOR_DEVICE: return SR_GetDescriptor_Device(); case DESCRIPTOR_CONFIG: return SR_GetDescriptor_Config(); case DESCRIPTOR_STRING: return SR_GetDescriptor_String();
default: return RESULT_UNSUPPORT; } }
return RESULT_UNSUPPORT; } GET_DESCRIPTOR请求属于USB协议中的标准请求(standard request)并且数据方向为设备至主机(device->host),分设备描述符、配置描述符、字符串描述符三种。已设备描述符为例: RESULT SR_GetDescriptor_Device(void) { // Assigned the device descriptor to the transfer vsDeviceInfo.TransInfo.wOffset = 0; vsDeviceInfo.TransInfo.wPacketSize = ENDP0_PACKETSIZE; vsDeviceInfo.TransInfo.pBuffer = DescBuffer_Device.pBuff; vsDeviceInfo.TransInfo.wLength = DescBuffer_Device.wLen; vsDeviceInfo.eControlState = CS_GET_DESCRIPTOR;
0x01, // iManufacturer: Index of string descriptor describing manufacturer 0x02, // iProduct: Index of string descriptor describing product 0x03, // iSerialNumber: Index of string descriptor describing the device serial number
0x01 // bNumConfigurations: number of configurations };
配置描述符:前9个字节格式固定,后面紧跟的各种描述结构跟实际配置有关,每增加一种描述结构,该描述结构的第一字节说明了结构的长度,第二直接说明了结构的类型。在配置描述符中一般包含配置描述、接口描述、端点描述,如果需要同样可增加自定义的描述。使用标准USB设备类别时,配置描述符的结构也必须满足此类标准设备的数据结构。 const unsigned char cbDescriptor_Config[DESC_SIZE_CONFIG] = { // Descriptor of configuration 0x09, // lengths DESCRIPTOR_CONFIG, // descriptor type
DESC_SIZE_CONFIG, // Total configuration descriptor lengths LSB 0x00, // Total configuration descriptor lengths MSB
0x01, // bNumInterfaces: Total number of interfaces 0x01, // bConfigurationValue: Configuration value 0x00, // iConfiguration: Index of string descriptor describing the configuration
0xA0, // bmAttributes: bus powered // bit 4...0 : Reserved, set to 0 // bit 5 : Remote wakeup (1:yes) // bit 6 : Self power (1:yes) // bit 7 : Reserved, set to 1
0x32, // bMaxPower: this current is used for detecting Vbus = 100mA
// Descriptor of interface 0x09, DESCRIPTOR_INTERFACE,
0x00, // bInterfaceNumber: Number of Interface 0x00, // bAlternateSetting: Alternate setting
0x02, // bNumEndpoints: Number of endpoints except EP0 0x00, // bInterfaceClass: 0x00, // bInterfaceSubClass: 0x00, // nInterfaceProtocol:
0x00, // iInterface: Index of string descriptor describing the interface
// Descriptor of endpoint1 OUT 0x07, DESCRIPTOR_ENDPOINT,
0x01, // bEndpointAddress // bit 3...0 : the endpoint number // bit 6...4 : reserved // bit 7 : 0(OUT), 1(IN)
0x03, // bmAttributes // bit 1...0 : Transfer type // 00(CONTROL), 01(ISOCHRONOUS), 10(BULK), 11(INTERRUPT) // bit 3...2 : Synchronization type // 00(No Synch), 01(Asynchronous), 10(Adaptive), 11(Synchronous) // bit 5...4 : Endpoint Usage type // 00(data), 01(Feedback), 10(Implicit feedback data endpoint), 11(Reserved) // bit 7...6 : Reserved, must be zero
0x40, // packet size LSB 0x00, // packet size MSB
0x20, // polling interval time: 32ms
// Descriptor of endpoint2 IN 0x07, DESCRIPTOR_ENDPOINT,
0x82, // bEndpointAddress // bit 3...0 : the endpoint number // bit 6...4 : reserved // bit 7 : 0(OUT), 1(IN)
0x03, // bmAttributes // bit 1...0 : Transfer type // 00(CONTROL), 01(ISOCHRONOUS), 10(BULK), 11(INTERRUPT) // bit 3...2 : Synchronization type // 00(No Synch), 01(Asynchronous), 10(Adaptive), 11(Synchronous) // bit 5...4 : Endpoint Usage type // 00(data), 01(Feedback), 10(Implicit feedback data endpoint), 11(Reserved) // bit 7...6 : Reserved, must be zero
用户390654 2008-11-25 10:35
zhangzq71_564169741 2008-11-25 09:00