STM32 USBv1: Off-by-one in NUM_BLOCK for OUT transfers of sizes >62 Bytes Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
WowSuchName
Posts: 7
Joined: Mon Sep 07, 2020 2:00 pm
Has thanked: 2 times
Been thanked: 2 times

STM32 USBv1: Off-by-one in NUM_BLOCK for OUT transfers of sizes >62 Bytes

Postby WowSuchName » Sun Apr 09, 2023 10:01 am

Hi everybody,

I had some issues with a USB bulk function on an STM32L1 controller (basically an echo for now) in that replied garbage. I could narrow it down to the USB peripheral wreaking havoc in my IN region of PMA. Given the datasheet [1, pp. 615/3018 - 616/3018, especially table 107], this happens due to NUM_BLOCK being one too large in the USBv1 implementation: For BL_SIZE=0 this field is 1-indexed while for BL_SIZE=1 it starts with 0 (Intuitive design, isn't it? :D).

With the following change I got rid of the problem:

os/hal/ports/STM32/LLD/USBv1/hal_usb_lld.c:643

nblocks = (((((epcp->out_maxsize - 1U) | 0x1FU) + 1U) / 32U) << 10) |

should read

nblocks = ((((epcp->out_maxsize - 1U) | 0x1FU) / 32U) << 10) |

,i.e., round down instead of up for packet sizes >62 bytes.

I'm pretty certain about this, however I wonder why nobody else stumbled across this issue. Anyway, here is the remaining information:

- ChibiOS version
21.11.1 (as of the tag ver21.11.1 on the github mirror)

- Compiler
gcc version 12.2.0 (Arch Repository)
newlib version 4.2.0.20211231-1

- Platform and board
STM32L151CBT6A on a custom board

- Nature of the problem / Failure Mode
Data is being corrupted in the PMA.


Cheers
Tim

[1] ST: Reference Manual RM0038. Revision 17: https://www.st.com/resource/en/referenc ... ronics.pdf

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32 USBv1: Off-by-one in NUM_BLOCK for OUT transfers of sizes >62 Bytes

Postby Giovanni » Sun Apr 09, 2023 1:41 pm

Hi,

Thanks, will look into this.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32 USBv1: Off-by-one in NUM_BLOCK for OUT transfers of sizes >62 Bytes  Topic is solved

Postby Giovanni » Tue Apr 11, 2023 12:39 pm

Hi,

The same problem (and a very old one...) was present also in the USBv2 driver, fixed both, please give it a try.

Giovanni

WowSuchName
Posts: 7
Joined: Mon Sep 07, 2020 2:00 pm
Has thanked: 2 times
Been thanked: 2 times

Re: STM32 USBv1: Off-by-one in NUM_BLOCK for OUT transfers of sizes >62 Bytes

Postby WowSuchName » Thu May 18, 2023 12:16 pm

Hi Giovanni,

thank you very much for the quick reply. I'll gladly test the revised driver if you can point me to the sources (the github mirror does not contain those changes).


Cheers
Tim

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32 USBv1: Off-by-one in NUM_BLOCK for OUT transfers of sizes >62 Bytes

Postby Giovanni » Thu May 18, 2023 1:55 pm

Hi,

The official repository is the svn available here: https://sourceforge.net/projects/chibios/

Git mirrors are updated from that one periodically.

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32 USBv1: Off-by-one in NUM_BLOCK for OUT transfers of sizes >62 Bytes

Postby Giovanni » Sun May 28, 2023 9:25 am

Hi,

Can this be closed?

Giovanni

WowSuchName
Posts: 7
Joined: Mon Sep 07, 2020 2:00 pm
Has thanked: 2 times
Been thanked: 2 times

Re: STM32 USBv1: Off-by-one in NUM_BLOCK for OUT transfers of sizes >62 Bytes

Postby WowSuchName » Wed May 31, 2023 1:53 pm

Hi Giovanni,

sorry for the late reply, had to prepare the hardware. Works like expected as of the current trunk (r16277). Thanks for fixing it!


Best Regards,
Tim


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 11 guests