用于USB闪存的CubeMX stm32 FreeRTOS + USB主机MSC

时间:2018-09-06 13:06:13

标签: memory usb stm32 host drive

我正在使用CubeMX,TrueStudio,STM32F767ZI和HAL开发用于通过Fathost通过USB主机端口访问USB闪存驱动器的软件。 评估板上有一个主机和设备USB连接器连接到MCU。 在CubeMX中,我配置了FreeRTOS,USB Host MSC和FatFS。 这行得通,插入和拔出闪存驱动器时,我可以看到以下不同状态:

--- Plugging in USB flash drive ---
HOST_USER_CONNECTION
HOST_USER_CONNECTION
HOST_DEV_ATTACHED
phost->device.speed: 1
HOST_ENUMERATION
HOST_SET_CONFIGURATION
HOST_SET_WAKEUP_FEATURE
HOST_CHECK_CLASS
USBH_MSC_InterfaceInit
HOST_CLASS_REQUEST
USBH_MSC_ClassRequest
HOST_CLASS
USBH_MSC_Process
processId: 3, Appli_state 1
USBH_MSC_GetLUNInfo
info.capacity.block_nbr : 623395
info.capacity.block_size: 512
capacity                : 3116 MB
HOST_USER_CLASS_ACTIVE
FATFS_LinkDriver Failed
USBDISKPath:
processId: 2, Appli_state 2
USBH_MSC_GetLUNInfo
info.capacity.block_nbr : 62333951
info.capacity.block_size: 512
capacity                : 31166 MB
--- Unplugging USB flash drive ---
HOST_USER_DISCONNECTION
HOST_DEV_DISCONNECTED
USBH_MSC_InterfaceDeInit
processId: 5, Appli_state 3
USBH_MSC_GetLUNInfo
info.capacity.block_nbr : 62333951
info.capacity.block_size: 512
capacity                : 31166 MB

因此,在MX_USB_HOST_Init()之后;以上所有情况均在freertos.c中被调用。 从MX_FATFS_Init()中调用FATFS_LinkDriver()。 但是usbh_diskio.c中的函数没有被调用,所以那里出了点问题。

我正在查看的代码段如下:

freertos.c,其中调用了MX_USB_HOST_Init(),此后注册了MSC。

/* StartDefaultTask function */
void StartDefaultTask(void const * argument)
{
  /* init code for FATFS */
  MX_FATFS_Init();

  /* init code for LWIP */
  MX_LWIP_Init();

  /* init code for USB_HOST */
  MX_USB_HOST_Init();

  /* init code for USB_DEVICE */
  MX_USB_DEVICE_Init();

  /* init code for LIBJPEG */
  MX_LIBJPEG_Init();

  /* USER CODE BEGIN StartDefaultTask */

  MSC_LUNTypeDef info;
  uint8_t lun = 0;
  uint16_t val = 0;

  /* Infinite loop */
  for(;;)
  {
      /* USB Host Background task */
//      USBH_Process(&hUsbHostFS);

    val++;
    if (val >= 1000)
    {
        if (processIdOld != processId)
        {
            printf("processId: %d, Appli_state %d\n", processId, Appli_state);
            processIdOld = processId;
              USBH_MSC_GetLUNInfo(&hUSB_Host, lun, &info);
              uint32_t cap = info.capacity.block_nbr / 2000;
              printf("info.capacity.block_nbr : %ld\n", info.capacity.block_nbr);
              printf("info.capacity.block_size: %d\n", info.capacity.block_size);
              printf("capacity                : %d MB\n", cap);
        }
//      printf("1");
//      fflush(stdout);
//      HAL_GPIO_TogglePin(PIO10_LED_RS_ERR_GPIO_Port, PIO10_LED_RS_ERR_Pin);
        val = 0;
    }
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

usb_host.c,其中USB已初始化,并且可以看到在插入/拔出USB闪存驱动器时调用了USBH_UserProcess。

void MX_USB_HOST_Init(void)
{
  /* USER CODE BEGIN USB_HOST_Init_PreTreatment */

  /* USER CODE END USB_HOST_Init_PreTreatment */

  /* Init host Library, add supported class and start the library. */
  USBH_Init(&hUsbHostFS, USBH_UserProcess, HOST_FS);

  USBH_RegisterClass(&hUsbHostFS, USBH_AUDIO_CLASS);

  USBH_RegisterClass(&hUsbHostFS, USBH_CDC_CLASS);

  USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS);

  USBH_RegisterClass(&hUsbHostFS, USBH_HID_CLASS);

  USBH_RegisterClass(&hUsbHostFS, USBH_MTP_CLASS);

  USBH_Start(&hUsbHostFS);

  /* USER CODE BEGIN USB_HOST_Init_PostTreatment */

  /* USER CODE END USB_HOST_Init_PostTreatment */
}

/*
 * user callback definition
 */
static void USBH_UserProcess  (USBH_HandleTypeDef *phost, uint8_t id)
{
  /* USER CODE BEGIN CALL_BACK_1 */
  switch(id)
  {
  case HOST_USER_SELECT_CONFIGURATION:
      printf("HOST_USER_SELECT_CONFIGURATION\n");
      break;

  case HOST_USER_DISCONNECTION:
      printf("HOST_USER_DISCONNECTION\n");
      Appli_state = APPLICATION_DISCONNECT;
      break;

  case HOST_USER_CLASS_ACTIVE:
      printf("HOST_USER_CLASS_ACTIVE\n");
      Appli_state = APPLICATION_READY;
      break;

  case HOST_USER_CONNECTION:
      printf("HOST_USER_CONNECTION\n");
      Appli_state = APPLICATION_START;
      break;

  default:
      break;
  }
  /* USER CODE END CALL_BACK_1 */
}

上面的部分工作正常,沙发。 除了USB驱动程序的链接。 有没有人使用过这个或知道好的示例?

0 个答案:

没有答案