微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32--USB详细使用说明

STM32--USB详细使用说明

时间:11-27 来源:互联网 点击:

if (pInformation->

pInformation->

pInformation->

if (pInformation->

if (ValBit(pInformation->

pInformation->Ctrl_Info.PacketSize = pProperty-> pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize;
DataStageIn();
}
else//主机向设备
{
pInformation->ControlState = OUT_DATA;
vSetEPRxStatus(EP_RX_VALID);
}

return;
}

usb_coer.c的文件里面

void DataStageIn(void)
{
ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info;//端点信息保存在指针变量中
uint32_t save_wLength = pEPinfo->Usb_wLength;//得到字符的长度
uint32_t ControlState = pInformation->ControlState;//得到当前的状态

uint8_t *DataBuffer;
uint32_t Length;

if ((save_wLength == 0) && (ControlState == LAST_IN_DATA))//如果字符长度为0 且控制状态是最后输入的数据
{
if(Data_Mul_MaxPacketSize == TRUE)//如果字符的长度是数据包的整数倍
{

Send0LengthData();
ControlState = LAST_IN_DATA;
Data_Mul_MaxPacketSize = FALSE;//这一次发送0字节 状态转为最后输入阶段
}
else//字符的长度比数据包要小
{//数据已经发送完

ControlState = WAIT_STATUS_OUT;

#ifdef STM32F10X_CL
PCD_EP_Read (ENDP0, 0, 0);
#endif
#ifndef STM32F10X_CL
vSetEPTxStatus(EP_TX_STALL);//设置端点的发送状态停止
#endif
}
goto Expect_Status_Out;
}

Length = pEPinfo->PacketSize;//得到数据包大小 64字节
ControlState = (save_wLength <= Length) ? LAST_IN_DATA : IN_DATA;//比较大小得到是LAST_IN_DATA还是IN_DATA 18字节<64字节 ControlState = LAST_IN_DATA

if (Length > save_wLength)
{
Length = save_wLength;
}

DataBuffer = (*pEPinfo->CopyData)(Length);//DataBuffer指向要复制数据的地址 这个地址是随Usb_wOffset变化的

#ifdef STM32F10X_CL
PCD_EP_Write (ENDP0, DataBuffer, Length);
#else
//GetEPTxAddr(ENDP0) 得到发送缓冲区相应端点的地址
//将DataBuffer中的数据复制到相应的发送缓冲区中
UserToPMABufferCopy(DataBuffer, GetEPTxAddr(ENDP0), Length);
#endif

SetEPTxCount(ENDP0, Length);//设置相应的端点要发送的字节数

pEPinfo->Usb_wLength -= Length;//等于0
pEPinfo->Usb_wOffset += Length;//偏移到18
vSetEPTxStatus(EP_TX_VALID);//使能发送端点 只要主机的IN令牌包一来 SIE就会将描述符返回给主机

USB_StatusOut();
//设置接收端点有效 这个实际上使接受也有效,

Expect_Status_Out:
pInformation->ControlState = ControlState;//保存控制状态
}

***************(4)**************

uint8_t In0_Process(void)
{
uint32_t ControlState = pInformation->ControlState;

if ((ControlState == IN_DATA) || (ControlState ==LAST_IN_DATA))//进入到这里

{
DataStageIn();//第一次取设备描述符只取一次当前的状态变为WAIT_STATUS_IN表明设备等待状态过程 主机输出0字节

ControlState = pInformation->ControlState;
}

else if (ControlState == WAIT_STATUS_IN)//设置地址状态阶段进入这个程序
{
if ((pInformation->USBbRequest == SET_ADDRESS) &&
(Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)))
{
SetDeviceAddress(pInformation->USBwValue0);//设置使用新的地址
pUser_Standard_Requests->User_SetDeviceAddress();
}
(*pProperty->Process_Status_IN)();
ControlState = STALLED;//变为这个状态
}

else
{
ControlState = STALLED;
}

pInformation->ControlState = ControlState;

return Post0_Process();
}

uint8_t Out0_Process(void)
{
uint32_t ControlState = pInformation->ControlState;

if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA))
{

//主机在完成传输前终止传输
ControlState = STALLED;
}
else if ((ControlState == OUT_DATA) || (ControlState == LAST_OUT_DATA))
{
DataStageOut();
ControlState = pInformation->ControlState;
}

else if (ControlState == WAIT_STATUS_OUT)//进入到这个里面
{
(*pProperty->Process_Status_OUT)();//这个函数其实什么也没做
#ifndef STM32F10X_CL
ControlState = STALLED;//状态变成了终止发送和接受

#endif
}

else
{
ControlState = STALLED;
}

pInformation->ControlState = ControlState;

return Post0_Process();
}

***************(5)**************

获取设备描述符以后,主机再一次的复位设备,设备又进入初始状态。开始枚举的第二步设置地址。

void NoData_Setup0(void)
{
RESULT Result = USB_UNSUPPORT;
uint32_t RequestNo = pInformation->USBbRequest;
uint32_t ControlState;

if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) //设备请求
{

else if (RequestNo == SET_ADDRESS) /设置地址
{
if ((pInformation->USBwValue0 > 127) || (pInformation->USBwValue1 != 0)
|| (pInformation->USBwIndex != 0)
|| (pInformation->Current_Configuration != 0))

{
ControlState = STALLED;
goto exit_NoData_Setup0;
}
else
{
Result = USB_SUCCESS;

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top