Skip to content

Commit

Permalink
Add single buffer mode option to USB to fix off-by-one corruption
Browse files Browse the repository at this point in the history
When using the debug mode, halting the CPU at a breakpoint can create an occasional condition where the HAL USB state machine encounters an off-by-one error in the packet handling. This error is un-recoverable. completely breaks USB communications, and requires you to reset the STM32.

This is caused by an issue with the double-buffering option available in the endpoint hardware.

Adding the option to disable double buffer mode, and to allow single buffer mode, fixes this issue, and, in my tests, also improves reliability in USB communication with noisy systems, such as motor controllers.

Single buffer mode can now be enabled with -DUSBD_CDC_USE_SINGLE_BUFFER at compile time. 

This commit does not break any other functions of STM32DUINO as far as I can tell.
  • Loading branch information
gigaj0ule authored Jul 12, 2024
1 parent b24801b commit e75a9a1
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libraries/USBDevice/src/usbd_ep_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ const ep_desc_t ep_def[] = {
#else
{0x00, PMA_EP0_OUT_ADDR, PCD_SNG_BUF},
{0x80, PMA_EP0_IN_ADDR, PCD_SNG_BUF},
#ifndef USBD_CDC_USE_SINGLE_BUFFER
{CDC_OUT_EP, PMA_CDC_OUT_ADDR, PCD_DBL_BUF},
#else
{CDC_OUT_EP, PMA_CDC_OUT_ADDR, PCD_SNG_BUF},
#endif
{CDC_IN_EP, PMA_CDC_IN_ADDR, PCD_SNG_BUF},
{CDC_CMD_EP, PMA_CDC_CMD_ADDR, PCD_SNG_BUF}
#endif
Expand Down

0 comments on commit e75a9a1

Please sign in to comment.