This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

USB CDC Host

Hi !

I have to implement an USB CDC Host driver for STM32F4 controller.
Has anyone an idea/example how to do that. I only found examples for CDC Device.

Thanks

Marc

Parents
  • Here is a brief summary of ST Micro's host stack, which is common to STM32F105/107,F2xx,F4xx

    STM32F105/7 and STM32F2xx USB on-the-go Host and device library (UM1021) v2.0.0
    www.st.com/.../stm32_f105-07_f2xx_usb-host-device_lib.zip

    To implement CDC, modify usbh_msc_core.c
    Your application over CDC is written in usbh_usr.c, USBH_USR_Application()

    // Summary of ST Micro host stack
    //    STM32_F105-07_F2xx_USB-Host-Device_Lib_V2.0.0
    //    STM32F4-Discovery_FW_V1.1.0
    //     -- STM32_USB_Device/Host/OTG_Library are identical with above library
    
    //
    // Host stack Framework
    //
    
    #include "usbh_core.h"
    #include "usbh_usr.h"
    
    USB_OTG_CORE_HANDLE           USB_OTG_Core;
    USBH_HOST                     USB_Host;
    extern USBH_Class_cb_TypeDef  USBH_MSC_cb;
    extern USBH_Usr_cb_TypeDef    USR_Callbacks;
    
    int main(void)
    {
      /* Init Host Library */
      USBH_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USB_Host, &USBH_MSC_cb, &USR_Callbacks);
    
      while (1)
      {
        /* Host Task handler */
        USBH_Process(&USB_OTG_Core, &USB_Host);
      }
    }
    
    //
    // Class driver   - refer to usbh_msc_core.c for typical implementation
    //
    
    
    #include "usbh_core.h"
    // callbacks from USBH_Process() in main loop USBH_Class_cb_TypeDef USBH_MSC_cb = { USBH_ClassDrv_InterfaceInit, USBH_ClassDrv_InterfaceDeInit, USBH_ClassDrv_ClassRequest, USBH_ClassDrv_Handle, };
    static USBH_Status USBH_ClassDrv_InterfaceInit( USB_OTG_CORE_HANDLE *pdev, void *phost ) { USBH_HOST *pphost = phost;
    // check interface/endpoint descriptors, if the target interface is supported or not pphost->device_prop.Itf_Desc[0].bInterfaceClass pphost->device_prop.Itf_Desc[0].bInterfaceProtocol pphost->device_prop.Ep_Desc[0][0].bEndpointAddress
    // open pipes using USBH_Alloc_Channel() / USBH_Open_Channel()
    if ( not_supported ) pphost->usr_cb->USBH_USR_DeviceNotSupported();
    return USBH_OK; }
    static void USBH_ClassDrv_InterfaceDeInit (USB_OTG_CORE_HANDLE *pdev, void *phost) { // close pipes USB_OTG_HC_Halt() / USBH_Free_Channel() }
    static USBH_Status USBH_ClassDrv_ClassRequest(USB_OTG_CORE_HANDLE *pdev, void *phost) { // initialization of USBH_ClassDrv_Handle() return USBH_OK; }
    static USBH_Status USBH_ClassDrv_Handle(USB_OTG_CORE_HANDLE *pdev, void *phost) { // state machine to run this class // first, put class specific requests, if required // run class-specific protocol over USB while in idle, call ((USBH_HOST *)phost)->usr_cb->USBH_USR_Application()
    return USBH_OK; }
    //
    // Callbacks from host stack     -- usbh_usr.c
    //
    
    #include "usbh_usr.h"
    
    USBH_Usr_cb_TypeDef USR_Callbacks =
    {
      USBH_USR_Init,
      USBH_USR_DeInit,
      USBH_USR_DeviceAttached,
      USBH_USR_ResetDevice,
      USBH_USR_DeviceDisconnected,
      USBH_USR_OverCurrentDetected,
      USBH_USR_DeviceSpeedDetected,
      USBH_USR_Device_DescAvailable,
      USBH_USR_DeviceAddressAssigned,
      USBH_USR_Configuration_DescAvailable,
      USBH_USR_Manufacturer_String,
      USBH_USR_Product_String,
      USBH_USR_SerialNum_String,
      USBH_USR_EnumerationDone,
      USBH_USR_UserInput,
      USBH_USR_Application,
      USBH_USR_DeviceNotSupported,
      USBH_USR_UnrecoveredError
    };
    
    Most of callbacks are copied as is from existing usbh_usr.c
    
    int USBH_USR_Application(void)
    {
      //
      // application-specific state machine
      // called from USBH_Process() -> USBH_ClassDrv_Handle()
      //
    
      return 0;
    }
    
    // control transfer
    
    
    // fill setup packet phost->Control.setup.b.bmRequestType phost->Control.setup.b.bRequest phost->Control.setup.b.wValue.w phost->Control.setup.b.wIndex.w phost->Control.setup.b.wLength.w
    Call USBH_CtlReq() repeatedly while it returns USBH_BUSY when finishes, it returns USBH_OK / USBH_FAIL / USBH_NOT_SUPPORTED (got STALL from device)
    // bulk transfer OUT 1) call USBH_BulkSendData() 2) poll HCD_GetURB_State() while it returns USBH_BUSY when finishes, HCD_GetURB_State() returns URB_DONE IN 1) call USBH_BulkReceiveData() 2) same as above
    // interrupt transfer OUT USBH_InterruptSendData() IN USBH_InterruptReceiveData() same as above bulk transfer

    Tsuneo

Reply
  • Here is a brief summary of ST Micro's host stack, which is common to STM32F105/107,F2xx,F4xx

    STM32F105/7 and STM32F2xx USB on-the-go Host and device library (UM1021) v2.0.0
    www.st.com/.../stm32_f105-07_f2xx_usb-host-device_lib.zip

    To implement CDC, modify usbh_msc_core.c
    Your application over CDC is written in usbh_usr.c, USBH_USR_Application()

    // Summary of ST Micro host stack
    //    STM32_F105-07_F2xx_USB-Host-Device_Lib_V2.0.0
    //    STM32F4-Discovery_FW_V1.1.0
    //     -- STM32_USB_Device/Host/OTG_Library are identical with above library
    
    //
    // Host stack Framework
    //
    
    #include "usbh_core.h"
    #include "usbh_usr.h"
    
    USB_OTG_CORE_HANDLE           USB_OTG_Core;
    USBH_HOST                     USB_Host;
    extern USBH_Class_cb_TypeDef  USBH_MSC_cb;
    extern USBH_Usr_cb_TypeDef    USR_Callbacks;
    
    int main(void)
    {
      /* Init Host Library */
      USBH_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USB_Host, &USBH_MSC_cb, &USR_Callbacks);
    
      while (1)
      {
        /* Host Task handler */
        USBH_Process(&USB_OTG_Core, &USB_Host);
      }
    }
    
    //
    // Class driver   - refer to usbh_msc_core.c for typical implementation
    //
    
    
    #include "usbh_core.h"
    // callbacks from USBH_Process() in main loop USBH_Class_cb_TypeDef USBH_MSC_cb = { USBH_ClassDrv_InterfaceInit, USBH_ClassDrv_InterfaceDeInit, USBH_ClassDrv_ClassRequest, USBH_ClassDrv_Handle, };
    static USBH_Status USBH_ClassDrv_InterfaceInit( USB_OTG_CORE_HANDLE *pdev, void *phost ) { USBH_HOST *pphost = phost;
    // check interface/endpoint descriptors, if the target interface is supported or not pphost->device_prop.Itf_Desc[0].bInterfaceClass pphost->device_prop.Itf_Desc[0].bInterfaceProtocol pphost->device_prop.Ep_Desc[0][0].bEndpointAddress
    // open pipes using USBH_Alloc_Channel() / USBH_Open_Channel()
    if ( not_supported ) pphost->usr_cb->USBH_USR_DeviceNotSupported();
    return USBH_OK; }
    static void USBH_ClassDrv_InterfaceDeInit (USB_OTG_CORE_HANDLE *pdev, void *phost) { // close pipes USB_OTG_HC_Halt() / USBH_Free_Channel() }
    static USBH_Status USBH_ClassDrv_ClassRequest(USB_OTG_CORE_HANDLE *pdev, void *phost) { // initialization of USBH_ClassDrv_Handle() return USBH_OK; }
    static USBH_Status USBH_ClassDrv_Handle(USB_OTG_CORE_HANDLE *pdev, void *phost) { // state machine to run this class // first, put class specific requests, if required // run class-specific protocol over USB while in idle, call ((USBH_HOST *)phost)->usr_cb->USBH_USR_Application()
    return USBH_OK; }
    //
    // Callbacks from host stack     -- usbh_usr.c
    //
    
    #include "usbh_usr.h"
    
    USBH_Usr_cb_TypeDef USR_Callbacks =
    {
      USBH_USR_Init,
      USBH_USR_DeInit,
      USBH_USR_DeviceAttached,
      USBH_USR_ResetDevice,
      USBH_USR_DeviceDisconnected,
      USBH_USR_OverCurrentDetected,
      USBH_USR_DeviceSpeedDetected,
      USBH_USR_Device_DescAvailable,
      USBH_USR_DeviceAddressAssigned,
      USBH_USR_Configuration_DescAvailable,
      USBH_USR_Manufacturer_String,
      USBH_USR_Product_String,
      USBH_USR_SerialNum_String,
      USBH_USR_EnumerationDone,
      USBH_USR_UserInput,
      USBH_USR_Application,
      USBH_USR_DeviceNotSupported,
      USBH_USR_UnrecoveredError
    };
    
    Most of callbacks are copied as is from existing usbh_usr.c
    
    int USBH_USR_Application(void)
    {
      //
      // application-specific state machine
      // called from USBH_Process() -> USBH_ClassDrv_Handle()
      //
    
      return 0;
    }
    
    // control transfer
    
    
    // fill setup packet phost->Control.setup.b.bmRequestType phost->Control.setup.b.bRequest phost->Control.setup.b.wValue.w phost->Control.setup.b.wIndex.w phost->Control.setup.b.wLength.w
    Call USBH_CtlReq() repeatedly while it returns USBH_BUSY when finishes, it returns USBH_OK / USBH_FAIL / USBH_NOT_SUPPORTED (got STALL from device)
    // bulk transfer OUT 1) call USBH_BulkSendData() 2) poll HCD_GetURB_State() while it returns USBH_BUSY when finishes, HCD_GetURB_State() returns URB_DONE IN 1) call USBH_BulkReceiveData() 2) same as above
    // interrupt transfer OUT USBH_InterruptSendData() IN USBH_InterruptReceiveData() same as above bulk transfer

    Tsuneo

Children