Skip to main content
Explorer II
May 6, 2024
Solved

USB Host "ALL Classes"

  • May 6, 2024
  • 1 reply
  • 1875 views

Hello everyone, I'm a bit confused about using USB.
I need to use the USB port both to create a CDC connection with a PC and to read/write data from USB flash drives.
Initially, I wanted to create a project that could handle both HOST and DEVICE modes, but I see that I would have to generate two separate projects and then manually copy some libraries.
I also tried the USB Dual Role configuration, but I don't understand its purpose as it doesn't generate anything related to USB.
Then, I saw that I can create a configuration like USB HOST  with mode HOST SUPPORTING ALL CLASSES so I’m asking if with this mode I can do both things, and if you could point me to any examples of how to do this.

I thank you in advance.

    This topic has been closed for replies.
    Best answer by SDall.11

    Solved!!!
    The problem was the DEVICE deinitialization.
    Without this, all works fine.

    Thanks.

    1 reply

    Technical Moderator
    May 6, 2024

    Hi @SDall.11 

     

    First, you need to make sure that your hardware support USB OTG which includes ID pin and switch between host and device role. Which STM32 product are you using?

    SDall.11Author
    Explorer II
    May 6, 2024

    Hi FBL, I use STM32F412VET6

     

    SDall.11Author
    Explorer II
    May 8, 2024
    Hello everyone,
    I finally created one project exclusively for DEVICE and one exclusively for HOST, then merged them together.
    I've made some progress since I'm able to switch from HOST mode to DEVICE mode, but I can't go the other way because when I try, I end up in the Error_Handler.
    The software starts in HOST mode, and I've done some tests that show I can write to a USB flash drive.
    When I switch to DEVICE mode, everything works fine, and I can connect to the PC via VCP.
    The problem arises when I switch back to HOST mode, resulting in the aforementioned error.
    Here's the function I call to switch from HOST to DEVICE:
     
    if (Appli_state != APPLICATION_DISCONNECT && Appli_state != APPLICATION_IDLE)
    {
    USBH_LL_Disconnect(&hUsbHostFS);
    USBH_Stop(&hUsbHostFS);
    }
     
    if (hUsbHostFS.pActiveClass != NULL)
    {
    hUsbHostFS.pActiveClass->DeInit(&hUsbHostFS);
    }
     
    USBH_LL_DriverVBUS(&hUsbHostFS, FALSE);
    MX_USB_DEVICE_Init();
     
     
    and here's the function I call for the reverse transition:
     
    USBD_Stop(&hUsbDeviceFS);
    USBD_DeInit(&hUsbDeviceFS);
    MX_USB_HOST_Init();
    MX_FATFS_Init();
     
    I can't seem to figure out where the issue might be.
    Any suggestions?
    Thanks.