Skip to content

Commit

Permalink
usb: stm32: clarify calculation of FIFO sizes
Browse files Browse the repository at this point in the history
Magic constants throughout the code made this difficult to reason about,
especially with two different units of measurement (bytes and words) at
play.

Signed-off-by: Armin Brauns <armin.brauns@embedded-solutions.at>
  • Loading branch information
arbrauns authored and jhedberg committed Apr 8, 2024
1 parent c481525 commit e69ce5b
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions drivers/usb/device/usb_dc_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ static const struct gpio_dt_spec ulpi_reset =
/* We need n TX IN FIFOs */
#define TX_FIFO_NUM USB_NUM_BIDIR_ENDPOINTS

/* We need a minimum size for RX FIFO */
/* We need a minimum size for RX FIFO - exact number seemingly determined through trial and error */
#define RX_FIFO_EP_WORDS 160

/* 4-byte words TX FIFO */
#define TX_FIFO_WORDS ((USB_RAM_SIZE - 64) / 4 - RX_FIFO_EP_WORDS)

/* Allocate FIFO memory evenly between the TX FIFOs */
/* except the first TX endpoint need only 64 bytes */
#define TX_FIFO_EP_0_WORDS 16
#define TX_FIFO_WORDS (USB_RAM_SIZE / 4 - RX_FIFO_EP_WORDS - TX_FIFO_EP_0_WORDS)
/* Number of words for each remaining TX endpoint FIFO */
#define TX_FIFO_EP_WORDS (TX_FIFO_WORDS / (TX_FIFO_NUM - 1))

#endif /* USB */
Expand Down Expand Up @@ -450,7 +450,8 @@ static int usb_dc_stm32_init(void)
for (i = 0U; i < USB_NUM_BIDIR_ENDPOINTS; i++) {
if (i == 0) {
/* first endpoint need only 64 byte for EP_TYPE_CTRL */
HAL_PCDEx_SetTxFiFo(&usb_dc_stm32_state.pcd, i, 16);
HAL_PCDEx_SetTxFiFo(&usb_dc_stm32_state.pcd, i,
TX_FIFO_EP_0_WORDS);
} else {
HAL_PCDEx_SetTxFiFo(&usb_dc_stm32_state.pcd, i,
TX_FIFO_EP_WORDS);
Expand Down

0 comments on commit e69ce5b

Please sign in to comment.