STM32之USB host库:USBH_Process学习理解
void USBH_Process(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost)
{
volatile USBH_Status status = USBH_FAIL;
if ((HCD_IsDeviceConnected(pdev) == 0)&& (phost->gState != HOST_IDLE)) // 若是未连接&&不空闲状态下
{
if(phost->gState != HOST_DEV_DISCONNECTED)
{
phost->gState = HOST_DEV_DISCONNECTED;
}
}
switch (phost->gState)
{
case HOST_IDLE : //空闲状态
if (HCD_IsDeviceConnected(pdev)) //如果 设备连接上了
{
phost->gState = HOST_DEV_ATTACHED; //就把状态转入 -> HOST_DEV_ATTACHED
USB_OTG_BSP_mDelay(100); //延时
}
break;
case HOST_DEV_ATTACHED : //接触状态(但是没有连接上)
phost->usr_cb->DeviceAttached(); //调用usbh_usr.c中的用户回调函数 -> USBH_USR_DeviceAttached()
phost->Control.hc_num_out = USBH_Alloc_Channel(pdev, 0x00); //给USB终端设备分配的传出通道
phost->Control.hc_num_in = USBH_Alloc_Channel(pdev, 0x80); //分配USB设备的传入通道
if ( HCD_ResetPort(pdev) == 0) //如果 检测到 复位命令
{
phost->usr_cb->ResetDevice(); //那么执行 USB复位,不是主机复位!
phost->device_prop.speed = HCD_GetCurrentSpeed(pdev); //获得当前设备的速度
phost->gState = HOST_ENUMERATION; //然后 状态转入 -> HOST_ENUMERATION
phost->usr_cb->DeviceSpeedDetected(phost->device_prop.speed); //调用用户回调函数,在LCD上显示设备速度信息
USBH_Open_Channel (pdev, //开启入口通道
phost->Control.hc_num_in,
phost->device_prop.address,
phost->device_prop.speed,
EP_TYPE_CTRL,
phost->Control.ep0size);
USBH_Open_Channel (pdev, //开启出口通道
phost->Control.hc_num_out,
phost->device_prop.address,
phost->device_prop.speed,
EP_TYPE_CTRL,
phost->Control.ep0size);
}
break;
case HOST_ENUMERATION: //枚举进程
if ( USBH_HandleEnum(pdev , phost) == USBH_OK) //如果枚举完成
{
phost->usr_cb->EnumerationDone(); //就执行用户回调,可以在显示器上显示枚举完毕信息
phost->gState = HOST_USR_INPUT; //然后转入下一个状态 -> HOST_USR_INPUT
}
break;
case HOST_USR_INPUT: //用户输入行为
if ( phost->usr_cb->UserInput() =
= USBH_USR_RESP_OK) //如果对用户输入有响应
{
if((phost->class_cb->Init(pdev, phost))\
== USBH_OK)
{
phost->gState = HOST_CLASS_REQUEST;