STM32 USB OTG HOST库挂起尝试使用FatF创建文件

时间:2016-01-15 22:28:00

标签: c embedded stm32 usb-otg fatfs

我正在尝试在USB闪存上使用FatFs创建一个文件,但我的f_open调用尝试读取启动扇区以进行首次文件系统挂载会挂起此函数。

DRESULT disk_read (
                   BYTE drv,            /* Physical drive number (0) */
                   BYTE *buff,          /* Pointer to the data buffer to store read data */
                   DWORD sector,        /* Start sector number (LBA) */
                   BYTE count           /* Sector count (1..255) */
                     )
{
  BYTE status = USBH_MSC_OK;

  if (drv || !count) return RES_PARERR;
  if (Stat & STA_NOINIT) return RES_NOTRDY;


  if(HCD_IsDeviceConnected(&USB_OTG_Core))
  {  

    do
    {
      status = USBH_MSC_Read10(&USB_OTG_Core, buff,sector,512 * count);
      USBH_MSC_HandleBOTXfer(&USB_OTG_Core ,&USB_Host);

      if(!HCD_IsDeviceConnected(&USB_OTG_Core))
      { 
        return RES_ERROR;
      }      
    }
    while(status == USBH_MSC_BUSY ); // Loop wich create hanging state
  }

  if(status == USBH_MSC_OK)
    return RES_OK;
  return RES_ERROR;

}

主要问题是循环创造悬挂状态

while(status == USBH_MSC_BUSY );

所以我不知道如何避免这种情况。使用debuger我发现状态是由结构CmdStateMachine的参数USBH_MSC_BOTXferParam引起的,类型USBH_BOTXfer_TypeDef等于CMD_UNINITIALIZED_STATE,这实际上会导致错过USBH_MSC_Read10的switch语句功能

/**
  * @brief  USBH_MSC_Read10 
  *         Issue the read command to the device. Once the response received, 
  *         it updates the status to upper layer
  * @param  dataBuffer : DataBuffer will contain the data to be read
  * @param  address : Address from which the data will be read
  * @param  nbOfbytes : NbOfbytes to be read
  * @retval Status
  */
uint8_t USBH_MSC_Read10(USB_OTG_CORE_HANDLE *pdev,
                        uint8_t *dataBuffer,
                        uint32_t address,
                        uint32_t nbOfbytes)
{
  uint8_t index;
  static USBH_MSC_Status_TypeDef status = USBH_MSC_BUSY;
  uint16_t nbOfPages;
  status = USBH_MSC_BUSY;

  if(HCD_IsDeviceConnected(pdev))
  {
    switch(USBH_MSC_BOTXferParam.CmdStateMachine)
    {
    case CMD_SEND_STATE:
      /*Prepare the CBW and relevent field*/
      USBH_MSC_CBWData.field.CBWTransferLength = nbOfbytes;
      USBH_MSC_CBWData.field.CBWFlags = USB_EP_DIR_IN;
      USBH_MSC_CBWData.field.CBWLength = CBW_LENGTH;

      USBH_MSC_BOTXferParam.pRxTxBuff = dataBuffer;

      for(index = CBW_CB_LENGTH; index != 0; index--)
      {
        USBH_MSC_CBWData.field.CBWCB[index] = 0x00;
      }

      USBH_MSC_CBWData.field.CBWCB[0]  = OPCODE_READ10; 

      /*logical block address*/

      USBH_MSC_CBWData.field.CBWCB[2]  = (((uint8_t*)&address)[3]);
      USBH_MSC_CBWData.field.CBWCB[3]  = (((uint8_t*)&address)[2]);
      USBH_MSC_CBWData.field.CBWCB[4]  = (((uint8_t*)&address)[1]);
      USBH_MSC_CBWData.field.CBWCB[5]  = (((uint8_t*)&address)[0]);

      /*USBH_MSC_PAGE_LENGTH = 512*/
      nbOfPages = nbOfbytes/ USBH_MSC_PAGE_LENGTH;  

      /*Tranfer length */
      USBH_MSC_CBWData.field.CBWCB[7]  = (((uint8_t *)&nbOfPages)[1]) ; 
      USBH_MSC_CBWData.field.CBWCB[8]  = (((uint8_t *)&nbOfPages)[0]) ; 


      USBH_MSC_BOTXferParam.BOTState = USBH_MSC_SEND_CBW;
      /* Start the transfer, then let the state machine 
      magage the other transactions */
      USBH_MSC_BOTXferParam.MSCState = USBH_MSC_BOT_USB_TRANSFERS;
      USBH_MSC_BOTXferParam.BOTXferStatus = USBH_MSC_BUSY;
      USBH_MSC_BOTXferParam.CmdStateMachine = CMD_WAIT_STATUS;

      status = USBH_MSC_BUSY;

      break;

    case CMD_WAIT_STATUS:

      if((USBH_MSC_BOTXferParam.BOTXferStatus == USBH_MSC_OK) && \
        (HCD_IsDeviceConnected(pdev)))
      { 
        /* Commands successfully sent and Response Received  */       
        USBH_MSC_BOTXferParam.CmdStateMachine = CMD_SEND_STATE;
        status = USBH_MSC_OK;      
      }
      else if (( USBH_MSC_BOTXferParam.BOTXferStatus == USBH_MSC_FAIL ) && \
        (HCD_IsDeviceConnected(pdev)))
      {
        /* Failure Mode */
        USBH_MSC_BOTXferParam.CmdStateMachine = CMD_SEND_STATE;
      }

      else if ( USBH_MSC_BOTXferParam.BOTXferStatus == USBH_MSC_PHASE_ERROR )
      {
        /* Failure Mode */
        USBH_MSC_BOTXferParam.CmdStateMachine = CMD_SEND_STATE;
        status = USBH_MSC_PHASE_ERROR;    
      }
      else
      {
        /* Wait for the Commands to get Completed */
        /* NO Change in state Machine */
      }
      break;

    default:
      break;
    }
  }
  return status;
}

这是USBH_BOTXfer_TypeDef类型声明;

typedef struct _BOTXfer
{
uint8_t MSCState;
uint8_t MSCStateBkp;
uint8_t MSCStateCurrent;
uint8_t CmdStateMachine;
uint8_t BOTState;
uint8_t BOTStateBkp;
uint8_t* pRxTxBuff;
uint16_t DataLength;
uint8_t BOTXferErrorCount;
uint8_t BOTXferStatus;
} USBH_BOTXfer_TypeDef;

在调试期间,我发现它的所有字段都是0x00。

以下是我的FatFs电话

int main(void)
{
    FATFS Fat;
    FIL file;
    FRESULT fr;

    RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;

    /* Enable SWO output */
    DBGMCU->CR = 0x00000020;

    GPIOD->MODER=0x55000000;
    GPIOD->OTYPER = 0x00000000;
    GPIOD->OSPEEDR = 0x00000001;

    while(1)
    {   
        if (!USB_MSC_IsInitialized())
        {
            USB_MSC_Initialize();
        }

        if (USB_MSC_IsConnected())
        {
            GPIOD->ODR = (1 << 15);

            disk_initialize(0);

            fr = f_mount(0, &Fat);

            if(fr == FR_OK)
            {           
                fr = f_open(&file,"0:DP_lab8.pdf",(FA_CREATE_ALWAYS | FA_WRITE));

                if (fr == FR_OK)
                {
                    f_close(&file);
                }

                f_mount(0, NULL);
            }
        }
        else
        {
            GPIOD->ODR = (1 << 14);
        }

        USB_MSC_Main();
    }
}

USB_MSC_IsConnected功能是:

int USB_MSC_IsConnected(void)
{
    if (g_USB_MSC_HostStatus == USB_DEV_NOT_SUPPORTED)
    {
        USB_MSC_Uninitialize();
    }

    return !(g_USB_MSC_HostStatus == USB_DEV_DETACHED ||
        g_USB_MSC_HostStatus == USB_HOST_NO_INIT ||
      g_USB_MSC_HostStatus == USB_DEV_NOT_SUPPORTED);
}

设备状态为:

typedef enum
{
    USB_HOST_NO_INIT = 0,  /* USB interface not initialized */
    USB_DEV_DETACHED,      /* no device connected */
    USB_SPEED_ERROR,       /* unsupported USB speed */
    USB_DEV_NOT_SUPPORTED, /* unsupported device */
    USB_DEV_WRITE_PROTECT, /* device is write protected */
    USB_OVER_CURRENT,      /* overcurrent detected */
    USB_DEV_CONNECTED      /* device connected and ready */
} USB_HostStatus;

标准USB HOST用户回调接收g_USB_MSC_HostStatus的值。

1 个答案:

答案 0 :(得分:1)

我认为这是ST主机库中的一个错误。因为我的USB主机无法通过枚举阶段,所以我一直在寻找它。修复后,堆栈正常。

“STM32Cube / Repository / STM32Cube_FW_F7_V1.13.0 / Middlewares / ST / STM32_USB_Host_Library / Core / Inc”中的 usbh_def.h 文件中有union _USB_Setup(任何芯片,不仅仅是F7,任何版本,不仅仅是V1.13.0)。它有uint16_t bmRequestType和uint16_t bRequest。从USB规格开始,这两个文件必须是uint8_t。修复此问题使得usb主机可以根据需要进行操作。枚举阶段通过,以及所有其他阶段。