基于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 ;
}

你可能感兴趣的:(职场,主机,usb,休闲,ohci)