Skip to content

Commit 00d8ee0

Browse files
drivers: usb: udc: stm32: configure OTGFS/HS RxFIFO size using Kconfig
Create a new Kconfig option allowing to tweak the RxFIFO size on OTG_FS and OTG_HS instances, and replace the old hardcoded method with this new mecanism. The default value of 600 bytes yields a similar size to the the previous hardcoded default of 160 words (= 640 bytes) when combined with the fixed overhead computed by the driver (~56 bytes on OTG_FS with 6 endpoints). Also fix a tiny error in a logging message (DRAM size in bytes, not bits). Signed-off-by: Mathieu Choplain <[email protected]>
1 parent 8940344 commit 00d8ee0

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

drivers/usb/udc/Kconfig.stm32

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ config UDC_STM32_MAX_QMESSAGES
3636
help
3737
Maximum number of messages for handling of STM32 USBD ISR events.
3838

39+
config UDC_STM32_OTG_RXFIFO_BASELINE_SIZE
40+
int "Baseline RxFIFO size (in bytes)"
41+
default 600
42+
depends on DT_HAS_ST_STM32_OTGFS_ENABLED \
43+
|| DT_HAS_ST_STM32_OTGHS_ENABLED
44+
help
45+
Baseline value for RXFIFO size computation
46+
47+
The OTG_FS and OTG_HS USB controllers use a single "RxFIFO" to hold
48+
data received by all OUT endpoints. The RxFIFO's size is influenced
49+
by various parameters: the optimal value depends on the exact USB
50+
configuration that will be used, which the driver does not know by
51+
the time it has to configure the RxFIFO size.
52+
53+
The total RxFIFO size will be equal to the value of this option
54+
plus a fixed overhead that the driver can derive from the hardware
55+
configuration (e.g., number of endpoints implemented).
56+
57+
Refer to STM32 Reference Manuals for more details about the RxFIFO.
58+
3959
config UDC_STM32_CLOCK_CHECK
4060
bool "Runtime USB 48MHz clock check"
4161
default y if !(SOC_SERIES_STM32F1X || SOC_SERIES_STM32F3X || SOC_SERIES_STM32U5X)

drivers/usb/udc/udc_stm32.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,16 +660,27 @@ static void udc_stm32_mem_init(const struct device *dev)
660660
{
661661
struct udc_stm32_data *priv = udc_get_private(dev);
662662
const struct udc_stm32_config *cfg = dev->config;
663-
int words;
663+
uint32_t rxfifo_size; /* in words */
664664

665-
LOG_DBG("DRAM size: %ub", cfg->dram_size);
665+
LOG_DBG("DRAM size: %uB", cfg->dram_size);
666666

667-
/* The documentation is not clear at all about RX FiFo size requirement,
668-
* 160 has been selected through trial and error.
667+
/*
668+
* In addition to the user-provided baseline, RxFIFO should fit:
669+
* - Global OUT NAK (1 word)
670+
* - Received packet information (1 word)
671+
* - Transfer complete status information (2 words per OUT endpoint)
672+
*
673+
* Align user-provided baseline up to 32-bit word size then
674+
* add this "fixed" overhead to obtain the final RxFIFO size.
669675
*/
670-
words = MAX(160, DIV_ROUND_UP(cfg->ep_mps, 4U));
671-
HAL_PCDEx_SetRxFiFo(&priv->pcd, words);
672-
priv->occupied_mem = words * 4;
676+
rxfifo_size = DIV_ROUND_UP(CONFIG_UDC_STM32_OTG_RXFIFO_BASELINE_SIZE, 4U);
677+
rxfifo_size += 2U; /* Global OUT NAK and Rx packet info */
678+
rxfifo_size += 2U * cfg->num_endpoints;
679+
680+
LOG_DBG("RxFIFO size: %uB", rxfifo_size * 4U);
681+
682+
HAL_PCDEx_SetRxFiFo(&priv->pcd, rxfifo_size);
683+
priv->occupied_mem = rxfifo_size * 4U;
673684

674685
/* For EP0 TX, reserve only one MPS */
675686
HAL_PCDEx_SetTxFiFo(&priv->pcd, 0, DIV_ROUND_UP(UDC_STM32_EP0_MAX_PACKET_SIZE, 4U));

0 commit comments

Comments
 (0)