eclipse-threadx / usbx

Eclipse ThreadX - USBX is a high-performance USB host, device, and on-the-go (OTG) embedded stack, that is fully integrated with Eclipse ThreadX RTOS
https://github.com/eclipse-threadx/rtos-docs/blob/main/rtos-docs/usbx/index.md
MIT License
148 stars 88 forks source link

${ux_ehci_ed_microframe_ssplit_count} is not calculated correctly #108

Closed xuzihan351 closed 3 months ago

xuzihan351 commented 1 year ago

I am using USBX host to access the mouse device. I found that some mouse insertion and removal operations caused the program to enter a hard fault. I have found that as long as the device has multiple interrupt endpoints, this situation will occur. So I tracked the correlation function of the interrupt endpoint. I found that in the ux_hcd_ehci_interrupt_endpoint_destroy function, the value of this variable changed from 0 to 255. When the device is plugged in again, _ux_host_stack_new_device_create will return UX_NO_BANDWIDTH_AVAILABLE because of the ux_ehci_ed_microframe_ssplit_count == 255. So I add a if ux_ehci_ed_microframe_ssplit_count > 0 before the ux_ehci_ed_microframe_ssplit_count--, the bug has disapperared. Please check this error and let me know the correct repair method.

xiaocq2001 commented 1 year ago

Hi, please try change interval in following lines to 8:

https://github.com/azure-rtos/usbx/blob/ef2fa7717be8ab751fdfb5d3f91e24fbbaea8dd6/common/usbx_host_controllers/src/ux_hcd_ehci_interrupt_endpoint_create.c#L269

https://github.com/azure-rtos/usbx/blob/ef2fa7717be8ab751fdfb5d3f91e24fbbaea8dd6/common/usbx_host_controllers/src/ux_hcd_ehci_interrupt_endpoint_create.c#L296

and check if that helps to remove the split count check.

xuzihan351 commented 1 year ago

It also result in an exception. image

xiaocq2001 commented 1 year ago

Thanks for the feedback. I think we may need more time to find out what's the root cause. In normal case, ux_ehci_ed_microframe_ssplit_count is increased on endpoint creating and decreased on endpoint destroy.

xuzihan351 commented 1 year ago

I tested several mouse and tracked the process of creating interrupt endpoints. For mouses with interrupt reporting cycles longer than 1 millisecond, ux_ehci_ed_microframe_ssplit_count will not increase during endpoiint creating. But it will decrease during destory.

xiaocq2001 commented 1 year ago

https://github.com/azure-rtos/usbx/blob/7c928b43db68b72970b3effd5a2582eb5a6869c7/common/usbx_host_controllers/src/ux_hcd_ehci_interrupt_endpoint_create.c#L243 interval calculation is not correct, if endpoint interval is larger than 1ms, final checked interval in code should be 8. Please try following code:

    /* Keep interval <= 1ms for micro-frame calculation.  */
    /* Make it index steps to move.  */
    if (interval >= 4)
    {
        interval = 3; /* Uses 3 for 1ms calculation.  */
    }
    else if (interval > 0)
    {
        interval --;
        interval &= 0x3;
    }
    interval = (1u << interval); /* 1 (1/8ms), 2, 4, 8 (1ms)  */

With this change, the previous interval change in L269 and L296 is not necessary.

xuzihan351 commented 1 year ago

It works!

xiaocq2001 commented 1 year ago

Glad to hear that.

xiaocq2001 commented 3 months ago

Fix is in latest code. Can close.