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

STM32F4 HID Host custom device

Hello,

I am interested to make USB HID HOST which can communicate with custom hid device instead of mice or keyboard.

For that I have to send data to that custom hid device.
I have used below fuction for dataout
USBH_InterruptSendData (pdev, (unsigned char *)&HID_Machine.buff, HID_Machine.length, HID_Machine.hc_num_out);

I am only doing out transaction form Host(STM32) to device. Everything is perfect and working fine up to 14th frame but 15th frame get corrupted, I have analyzer showing "child has an error". So I am unable to understand the cause of issue, is it memory overflow at host?

If I continue data transmission with new data then host is sending old data packet onwards

I used dual core example for ST and modified USBH_HID_Handle() as below (Only two cases are shown here)


case HID_SEND_DATA:

while(USB_OTG_IsEvenFrame(pdev) == FALSE);

USBH_InterruptSendData (pdev, (unsigned char *)&HID_Machine.buff, HID_Machine.length, HID_Machine.hc_num_out);

HID_Machine.state = HID_BUSY;

HID_Machine.timer = HCD_GetCurrentFrame(pdev);

break;

case HID_BUSY:

if(( HCD_GetCurrentFrame(pdev) - HID_Machine.timer) >= HID_Machine.poll)

{ HID_Machine.state = HID_SEND_DATA; //Retry++; //1 }

else if(HCD_GetURB_State(pdev , HID_Machine.hc_num_out) == URB_DONE) //last transaction was successful or not(USB request Block)

{ HID_Machine.state = HID_GET_DATA;

BD_Data_Sent_Flag=1;

}

else if(HCD_GetURB_State(pdev, HID_Machine.hc_num_out) == URB_STALL )

{

if( (USBH_ClrFeature(pdev, pphost, HID_Machine.ep_addr, HID_Machine.hc_num_out)) == USBH_OK)

{

HID_Machine.state = HID_SEND_DATA;

} }

break;


Please help...

  • I'm doing a project like you.I wan't to send and recieve data form my custom HID device.Could you send me a example please.Thank you.
    T try to copy your code above but It appears error at BD_Data_Sent_Flag=1;
    What is BD_Data_Sent_Flag.
    My host HID is STM32F4-Discovery.

  • Hi,

    Use STM32_USB-Host-Device_Lib_V2.1.0 because I have found some issue with 2.0.0(Problem while transmitting data to HID device)

    Considering you have done BSP configuration.

    Do following modification in usbh_hid_core.c for your custom device(other than mice or keyboard)

    if(pphost->device_prop.Itf_Desc[0].bInterfaceSubClass == HID_BOOT_CODE || pphost->device_prop.Itf_Desc[0].bInterfaceSubClass == 0x00)//Custom device subclass =0xff { /*Decode Bootclass Protocl: Mouse or Keyboard*/
    // if(pphost->device_prop.Itf_Desc[0].bInterfaceProtocol == HID_KEYBRD_BOOT_CODE)
    // {
    // HID_Machine.cb = &HID_KEYBRD_cb;
    // }
    // else if(pphost->device_prop.Itf_Desc[0].bInterfaceProtocol == HID_MOUSE_BOOT_CODE)

    // {
    // HID_Machine.cb = &HID_MOUSE_cb;
    // }
    // else
    if(pphost->device_prop.Itf_Desc[0].bInterfaceProtocol == 0x00) { HID_Machine.cb = &HID_CUSTOM_cb; // just like mice }

    Donot use any variable posted by me because it is just control that I want to handle.Instead use standard library.

  • Thank you for reply.
    It appears eror at
    HID_Machine.cb = &HID_CUSTOM_cb;
    Where I need to define it.
    And after that how can I send data to HID device.
    Here ís my code
    int main(void)
    { __IO uint32_t i = 0, count; SystemInit(); USART_Config(); /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32fxxx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32fxxx.c file */

    printf(" MCU Start\r\n");

    /* Init Host Library */ USBH_Init(&USB_OTG_Core_dev,
    #ifdef USE_USB_OTG_FS USB_OTG_FS_CORE_ID,
    #else USB_OTG_HS_CORE_ID,
    #endif &USB_Host, &HID_cb, &USR_Callbacks);

    while (1) { /* Host Task handler */ USBH_Process(&USB_OTG_Core_dev , &USB_Host); }
    } I modify from STM32_USB-Host-Device_Lib_V2.1.0
    My Host HID (STM32-F4 Dícovery) is found HID device.

  • Could you post your code in this :
    static USBH_Status USBH_HID_Handle(USB_OTG_CORE_HANDLE *pdev , void *phost)
    I'm just learnt STM32F4-Discovery for 3 weeks.

  • Hi Krunal,

    > I have analyzer showing "child has an error".

    Do you mean a hardware protocol analyzer?
    What does "child has an error" exactly mean?
    Packet format error (such as CRC, EOP timing or no response)?
    Which packet causes the error, OUT, DATA0/1 or ACK?

    Tsuneo

  • Thanks Tsuneo for reply.

    I don't know meaning of "child has an error" but it is showing by beagle analyzer for OUT transaction.
    Anyway all problems were resolved with 2.1.0 library. Now I am able to transmit data to the device without any problem.

    Using 2.0.0 library, ST told me that "you are transmitting data in asynchronous mode instead of synchronous mode". I don't go into the details with this type of configuration but definitely I am interested to understand it in future.

    Krunal

  • Hi Krunal,
    Can you help me.
    Thank you.

  • Hello Khoa Tran,

    I have already posted main part for data transmission in my first message. You have to add this portion in between HID_SYNC and HID_GET_DATA in

    static USBH_Status USBH_HID_Handle(USB_OTG_CORE_HANDLE *pdev , void *phost)

    Copy your data into the (that you want to transmit to the device)
    HID_Machine.buff and provide length of data in HID_Machine.length.

    for your question, HID_CUSTOM_cb giving error

    use this
    HID_cb_TypeDef HID_CUSTOM_cb=
    { Device_Init, Device_Decode,
    };

    static void Device_Init ( void)
    { // Add your code here
    }

    static void Device_Decode(uint8_t *data)
    { // Add your code here
    }

    Krunal

  • Thank you Krunal.
    In main.c we just call USBH_Process(&USB_OTG_Core_dev , &USB_Host); and it will transfer data to HID Device.
    I want to know what data my Host is transfer.How can I do it?

  • Thank you Krunal.
    My Host can recieve data from my custom HID Device but I still don't know how to send data from host to device.

  • Dear Tran,

    I hope you have implemented whatever I suggested previously.

    Just implement below logic.

    Copy your data into the (that you want to transmit to the device)
    HID_Machine.buff and provide length of data in HID_Machine.length.

    And set HID machine state to HID_SEND_DATA whenever you want to transmit data.

    Krunal

  • Set HID_Machine.state = HID_SEND_DATA; in main.c ?
    I want to my host send data when I push a button.
    I set in main.c and It give a error "use of undeclared identifier 'HID_Machine'" but If I define

    #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED #if defined ( __ICCARM__ ) /*!< IAR Compiler */ #pragma data_alignment=4 #endif
    #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
    __ALIGN_BEGIN HID_Machine_TypeDef HID_Machine __ALIGN_END ;

    It give a error "HID_Machine mutiply defined (by usbh_hid_core.o and main.o)"
    p/s : I included usbh_hid_core.h in main.c
    Thank you.

  • Hi Krunal,
    Thank you for helping me.
    I can transfer and recieve data from my custom HID Device now.
    Khoa.

  • You are welcome and congratulation!!

  • Hi Khor,
    Now I used STM32F4 HID Host custom device,the logic is when I pressed button ,HOST send data to HID decive ,can you tell me the send logic in USBH_HID_Handle?

    Thanks!