cancel
Showing results for 
Search instead for 
Did you mean: 

How to call MX_USB_HOST_Process() on HID host projects with large main loop?

JMarq.1
Associate III

Hello,

I followed the ST training example to get an HID host working to interface a USB keyboard. Keys are correctly decoded on the USBH_HID_EventCallback() function. But, when having a main loop that lasts more than the milliseconds specified on the USB keyboard endpoint descriptor boot mode (i.e. bInterval = 8 or 10 ms), MX_USB_HOST_Process() is not called enough often and the class HID state machine stucks in HID_GET_DATA.

(see post https://community.st.com/s/question/0D53W00000qvJqOSAU/whats-the-problem-with-usb-host-hid)

So, how to call periodically MX_USB_HOST_Process() if the main loop needs let's say 30ms to run? Should I call MX_USB_HOST_Process() manually on different parts of the code being executed on the loop? That wouldn't be the desired solution... If this is the case, is there any restriction about how often can I call it?

I tried to call MX_USB_HOST_Process() using a background 3ms timer 2 interrupt, but if the HAL library calls USBH_Delay() function, the program gets hang inside it. I solved this setting a lower priority to the TIM2 interrupt to let HAL base tick timer higher priority and now it works and MX_USB_HOST_Process() is being called every 3 ms.

Inside HAL_Init():

· HAL_InitTick(TICK_INT_PRIORITY);

· HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);

TIM2 config:

· HAL_NVIC_SetPriority(TIM2_IRQn, 7, 7);

· NVIC_EnableIRQ(TIM2_IRQn);

Any other suggestion better than a periodic Timer interrupt?

BTW, if the keyboard is unplugged from the USB connector and then plugged again, a reset condition happens on the USB stack and a HALDelay(200) + HALDelay(100) is executed prior USB enumeration, leaving the whole program hang for such amount of time of 300ms. This can have side effects on the main loop execution freezed for 300ms.

2 REPLIES 2
JMarq.1
Associate III

Can the HID_MIN_POLL value in "usbh_hid.h" file be changed from 10U to 50U or 100U? This would allow a 50ms or 100ms timeout for large main loops. Would this change violate any USB 2.0 spec?

Stanford
Associate II

I discovered the same problem.  My STM32L4 is acting as a HID host to a barcode scanner.  MX_USB_HOST_Process is called in my main loop.  Since the main loop exceeds the ~1ms SOF period, the state handling in MX_USB_HOST_Process never enters the USBH_HID_GET_DATA state.  However, if I comment out my application handling part of the loop and only call MX_USB_HOST_Process, all works well and the STM32L4 receives all characters from the scanner.  I don't have a solution quite yet.  I thought about doing what you suggested (peppering calls to MX_USB_HOST_Process, which is a kludge).  Or, calling MX_USB_HOST_Process from a Timer ISR.  I'm going to dig around to see if there is a better way to handle this.