基于OHCI的USB主机 —— UFI命令监视代码

在调用完上面的UFI命令后,ufiCmdRunning.state就被设置为UFI_STATE_BEGIN态,在主循环中调用本函数,用来判断UFI命令是否执行完毕,以及是否执行成功。
/**

 * UFI命令监视函数,判断命令是否执行完毕,底层有错误则进行容错处理

 */

short funUfiRunning(void)

{

    switch(ufiCmdRunning.state)

    {

       //开始运行命令

       case UFI_STATE_BEGIN:

           usbTimer = 0;

           ufiCmdRunning.count = 0;

           ufiCmdRunning.state = UFI_STATE_RUNNING;

           break;

 

       //命令运行中

       case UFI_STATE_RUNNING:

           //检查命令是否执行完毕

           if(usbIsCmdOver() == USB_CMD_OVER)

           {

              //USB协议层命令执行失败

              if (usbGetCmdResult() != 0)

              {

                  //底层命令执行失败后,对端口复位后重新执行,重新执行超过150次则认为失败

                  ufiCmdRunning.count++;

 

                  if (ufiCmdRunning.count > 150)

                  {

                     ufiCmdRunning.error = UFI_ERR_MUCH_ERR;

                     ufiCmdRunning.state = UFI_STATE_OVER;

                  }

                  else

                  {

                     //复位BulkOut端口

                     usbClearBulkOut();

                     ufiCmdRunning.state = UFI_STATE_RESET_OUT;

                  }

                  break;

              }

 

              //UFI命令执行成功

              if ((ufiCmdRunning.csw.bCSW_Status) == 0x00 && (ufiCmdRunning.csw.dCSW_Signature == 0x53425355))

              {

                  ufiCmdRunning.error = 0;

              }

              //UFI命令执行失败

              else

              {

                  ufiCmdRunning.error = UFI_ERR_CSW;

              }

              ufiCmdRunning.state = UFI_STATE_OVER;

           }

           //底层命令超时,返回失败

           else if (usbTimer >= 200)

           {

              ufiCmdRunning.error = UFI_ERR_TIMEOUT;

              ufiCmdRunning.state = UFI_STATE_OVER;

           }

           break;

 

       //BulkOut端口复位结束

       case UFI_STATE_RESET_OUT:

           if(usbIsCmdOver() == USB_CMD_OVER)

           {

              //复位BulkIn端口

              usbClearBulkIn();

              ufiCmdRunning.state = UFI_STATE_RESET_IN;

           }

           break;

 

       //BulkIn端口复位结束

       case UFI_STATE_RESET_IN:

           if(usbIsCmdOver() == USB_CMD_OVER)

           {

              //读取端口错误原因

              ufiRequestSense(bulkDataBuf);

              ufiCmdRunning.state = UFI_STATE_REQUEST_SENSE;

           }

           break;

 

       //端口错误原因读取完毕

       case UFI_STATE_REQUEST_SENSE:

           if(usbIsCmdOver() == USB_CMD_OVER)

           {

              //重新执行底层Bulk命令

              ufiCmdRunning.funUfiCmd((unsigned char *)&ufiCmdRunning.cbw, ufiCmdRunning.pBuf, (unsigned char *)&ufiCmdRunning.csw);

 

              usbTimer = 0;

              ufiCmdRunning.state = UFI_STATE_RUNNING;

           }

           break;

    }

    return ufiCmdRunning.state;

}





本文转自 tywali 51CTO博客,原文链接:http://blog.51cto.com/lancelot/318291,如需转载请自行联系原作者
上一篇:基于OHCI的USB主机 —— 寄存器(复位)


下一篇:五大经典算法之回溯法