2021-12-23 11:43 PM
I'm writing for stm32f417 MCU. The project is generated using STM32CubeMX with firmware package: STM32Cube FW_F4 V1.26.2 (the latest so far).
In the USB Host Library there is a file related to HID class:
<my_proj_path>\Middlewares\ST\STM32_USB_Host_Library\Class\HID\Src\usbh_hid_parser.c
There is a function:
uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx)
It parses items of incoming reports. And there is a code in it:
/* read data bytes in little endian order */
for (x = 0U; x < ((ri->size & 0x7U) ? (ri->size / 8U) + 1U : (ri->size / 8U)); x++)
{
val = (uint32_t)((uint32_t)(*data) << (x * 8U));
}
val = (val >> shift) & ((1U << ri->size) - 1U);
You can see here the multiple type casting. It's just meaningless overhead. But if you iterate through the loop assigning a value to the variable val it'll be overwritten on every iteration. Moreover the data pointer also stays unchanged during the loop.
At least this line should be changed to:
val |= (uint32_t)(*data++) << (x * 8U);
I also suggest you to test this function over different size and shift values (not only <= 8)
WBR
2022-01-04 02:04 AM
Hello @funfunfun ,
Thanks for your feedback?
What kind of error did you get and could you please share your .ioc file to reproduce the issue?,
Thanks,
Sara.
2022-01-04 04:55 AM
Hi @Sara BEN HADJ YAHYA ,
funfunfun is right, this is an error.
The error will exhibit itself only if there are items with length > 8 or items which cross byte boundaries. In the usage with example HID report descriptors there are no such items, so you won't see the effect of the error with anything you can click in CubeMX, but it will bite when somebody tries to use a custom report descriptor.
JW
2022-01-04 07:58 AM
Hello,
This is not a CubeMX issue, I'm adding @Imen DAHMEN to the loop to investigate the issue.
Regards,
Sara.
2022-01-18 10:01 AM
Is there any follow-up here? I am getting what I believe to be a similar error on an F2 device using a custom report descriptor. It causes class parsing errors resulting in intermittent USB enumeration.
2022-01-18 10:28 AM
> intermittent USB enumeration.
The error described here will exhibit itself only if there are items with length > 8 or items which cross byte boundaries.The workaround described above should help with this particular error.
JW
2024-08-14 08:40 AM
Nice solution!
I tried a number of permutations with the HID_Report_ItemTypedef values thinking I must not be understanding the intended use of the function, but came to the same conclusion that the line you modified was the issue. Thanks for sharing it!