Skip to content

Commit d3adb48

Browse files
drivers: usb: udc: stm32: turn EP0 max packet size into a constant
The EP0 max packet size was de facto a constant because its value was the same regardless of which USB IP was in use. However, it was stored as part of the instance configuration anyways which is wasteful and slower. Create new "UDC_STM32_EP0_MAX_PACKET_SIZE" driver-level constant with which all usage of the per-instance configuration field is replaced. Signed-off-by: Mathieu Choplain <[email protected]>
1 parent 9ee617a commit d3adb48

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

drivers/usb/udc/udc_stm32.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ static const int syscfg_otg_hs_phy_clk[] = {
136136
};
137137
#endif
138138

139+
/*
140+
* Hardcode EP0 max packet size (bMaxPacketSize0) to 64,
141+
* which is the maximum allowed by the USB Specification
142+
* and supported by all STM32 USB controllers.
143+
*/
144+
#define UDC_STM32_EP0_MAX_PACKET_SIZE 64U
145+
139146
struct udc_stm32_data {
140147
PCD_HandleTypeDef pcd;
141148
const struct device *dev;
@@ -152,7 +159,6 @@ struct udc_stm32_config {
152159
uint32_t num_endpoints;
153160
uint32_t pma_offset;
154161
uint32_t dram_size;
155-
uint16_t ep0_mps;
156162
uint16_t ep_mps;
157163
/* PHY selected for use by instance */
158164
uint32_t selected_phy;
@@ -188,19 +194,20 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
188194
{
189195
struct udc_stm32_data *priv = hpcd2data(hpcd);
190196
const struct device *dev = priv->dev;
191-
const struct udc_stm32_config *cfg = dev->config;
192197
struct udc_ep_config *ep;
193198

194199
/* Re-Enable control endpoints */
195200
ep = udc_get_ep_cfg(dev, USB_CONTROL_EP_OUT);
196201
if (ep && ep->stat.enabled) {
197-
HAL_PCD_EP_Open(&priv->pcd, USB_CONTROL_EP_OUT, cfg->ep0_mps,
202+
HAL_PCD_EP_Open(&priv->pcd, USB_CONTROL_EP_OUT,
203+
UDC_STM32_EP0_MAX_PACKET_SIZE,
198204
EP_TYPE_CTRL);
199205
}
200206

201207
ep = udc_get_ep_cfg(dev, USB_CONTROL_EP_IN);
202208
if (ep && ep->stat.enabled) {
203-
HAL_PCD_EP_Open(&priv->pcd, USB_CONTROL_EP_IN, cfg->ep0_mps,
209+
HAL_PCD_EP_Open(&priv->pcd, USB_CONTROL_EP_IN,
210+
UDC_STM32_EP0_MAX_PACKET_SIZE,
204211
EP_TYPE_CTRL);
205212
}
206213

@@ -288,7 +295,6 @@ static int udc_stm32_tx(const struct device *dev, struct udc_ep_config *epcfg,
288295
struct net_buf *buf)
289296
{
290297
struct udc_stm32_data *priv = udc_get_private(dev);
291-
const struct udc_stm32_config *cfg = dev->config;
292298
uint8_t *data; uint32_t len;
293299
HAL_StatusTypeDef status;
294300

@@ -302,7 +308,7 @@ static int udc_stm32_tx(const struct device *dev, struct udc_ep_config *epcfg,
302308
len = buf->len;
303309

304310
if (epcfg->addr == USB_CONTROL_EP_IN) {
305-
len = MIN(cfg->ep0_mps, buf->len);
311+
len = MIN(UDC_STM32_EP0_MAX_PACKET_SIZE, buf->len);
306312
}
307313

308314
buf->data += len;
@@ -444,8 +450,7 @@ static void handle_msg_data_in(struct udc_stm32_data *priv, uint8_t epnum)
444450
}
445451

446452
if (ep == USB_CONTROL_EP_IN && buf->len) {
447-
const struct udc_stm32_config *cfg = dev->config;
448-
uint32_t len = MIN(cfg->ep0_mps, buf->len);
453+
uint32_t len = MIN(UDC_STM32_EP0_MAX_PACKET_SIZE, buf->len);
449454

450455
HAL_PCD_EP_Transmit(&priv->pcd, ep, buf->data, len);
451456

@@ -643,9 +648,8 @@ static void udc_stm32_mem_init(const struct device *dev)
643648

644649
LOG_DBG("DRAM size: %ub", cfg->dram_size);
645650

646-
if (cfg->ep_mps % 4 || cfg->ep0_mps % 4) {
647-
LOG_ERR("Not a 32-bit word multiple: ep0(%u)|ep(%u)",
648-
cfg->ep0_mps, cfg->ep_mps);
651+
if (cfg->ep_mps % 4) {
652+
LOG_ERR("Not a 32-bit word multiple: ep(%u)", cfg->ep_mps);
649653
return;
650654
}
651655

@@ -657,8 +661,8 @@ static void udc_stm32_mem_init(const struct device *dev)
657661
priv->occupied_mem = words * 4;
658662

659663
/* For EP0 TX, reserve only one MPS */
660-
HAL_PCDEx_SetTxFiFo(&priv->pcd, 0, cfg->ep0_mps / 4);
661-
priv->occupied_mem += cfg->ep0_mps;
664+
HAL_PCDEx_SetTxFiFo(&priv->pcd, 0, UDC_STM32_EP0_MAX_PACKET_SIZE / 4);
665+
priv->occupied_mem += UDC_STM32_EP0_MAX_PACKET_SIZE;
662666

663667
/* Reset TX allocs */
664668
for (unsigned int i = 1U; i < cfg->num_endpoints; i++) {
@@ -705,7 +709,6 @@ static int udc_stm32_ep_mem_config(const struct device *dev,
705709
static int udc_stm32_enable(const struct device *dev)
706710
{
707711
struct udc_stm32_data *priv = udc_get_private(dev);
708-
const struct udc_stm32_config *cfg = dev->config;
709712
HAL_StatusTypeDef status;
710713
int ret;
711714

@@ -720,14 +723,16 @@ static int udc_stm32_enable(const struct device *dev)
720723
}
721724

722725
ret = udc_ep_enable_internal(dev, USB_CONTROL_EP_OUT,
723-
USB_EP_TYPE_CONTROL, cfg->ep0_mps, 0);
726+
USB_EP_TYPE_CONTROL,
727+
UDC_STM32_EP0_MAX_PACKET_SIZE, 0);
724728
if (ret) {
725729
LOG_ERR("Failed enabling ep 0x%02x", USB_CONTROL_EP_OUT);
726730
return ret;
727731
}
728732

729733
ret |= udc_ep_enable_internal(dev, USB_CONTROL_EP_IN,
730-
USB_EP_TYPE_CONTROL, cfg->ep0_mps, 0);
734+
USB_EP_TYPE_CONTROL,
735+
UDC_STM32_EP0_MAX_PACKET_SIZE, 0);
731736
if (ret) {
732737
LOG_ERR("Failed enabling ep 0x%02x", USB_CONTROL_EP_IN);
733738
return ret;
@@ -1068,12 +1073,10 @@ static const struct udc_api udc_stm32_api = {
10681073
#define USB_NUM_BIDIR_ENDPOINTS DT_INST_PROP(0, num_bidir_endpoints)
10691074

10701075
#if defined(USB) || defined(USB_DRD_FS)
1071-
#define EP0_MPS 64U
10721076
#define EP_MPS 64U
10731077
#define USB_BTABLE_SIZE (8 * USB_NUM_BIDIR_ENDPOINTS)
10741078
#define USB_RAM_SIZE DT_INST_PROP(0, ram_size)
10751079
#else /* USB_OTG_FS */
1076-
#define EP0_MPS USB_OTG_MAX_EP0_SIZE
10771080
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs)
10781081
#define EP_MPS USB_OTG_HS_MAX_PACKET_SIZE
10791082
#elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otgfs) || DT_HAS_COMPAT_STATUS_OKAY(st_stm32_usb)
@@ -1094,7 +1097,6 @@ static const struct udc_stm32_config udc0_cfg = {
10941097
.num_endpoints = USB_NUM_BIDIR_ENDPOINTS,
10951098
.dram_size = USB_RAM_SIZE,
10961099
.pma_offset = USB_BTABLE_SIZE,
1097-
.ep0_mps = EP0_MPS,
10981100
.ep_mps = EP_MPS,
10991101
.selected_phy = UDC_STM32_NODE_PHY_ITFACE(DT_DRV_INST(0)),
11001102
.selected_speed = UDC_STM32_NODE_SPEED(DT_DRV_INST(0)),
@@ -1109,7 +1111,7 @@ static void priv_pcd_prepare(const struct device *dev)
11091111

11101112
/* Default values */
11111113
priv->pcd.Init.dev_endpoints = cfg->num_endpoints;
1112-
priv->pcd.Init.ep0_mps = cfg->ep0_mps;
1114+
priv->pcd.Init.ep0_mps = UDC_STM32_EP0_MAX_PACKET_SIZE;
11131115
priv->pcd.Init.speed = cfg->selected_speed;
11141116

11151117
/* Per controller/Phy values */
@@ -1332,7 +1334,7 @@ static int udc_stm32_driver_init0(const struct device *dev)
13321334
ep_cfg_out[i].caps.out = 1;
13331335
if (i == 0) {
13341336
ep_cfg_out[i].caps.control = 1;
1335-
ep_cfg_out[i].caps.mps = cfg->ep0_mps;
1337+
ep_cfg_out[i].caps.mps = UDC_STM32_EP0_MAX_PACKET_SIZE;
13361338
} else {
13371339
ep_cfg_out[i].caps.bulk = 1;
13381340
ep_cfg_out[i].caps.interrupt = 1;
@@ -1352,7 +1354,7 @@ static int udc_stm32_driver_init0(const struct device *dev)
13521354
ep_cfg_in[i].caps.in = 1;
13531355
if (i == 0) {
13541356
ep_cfg_in[i].caps.control = 1;
1355-
ep_cfg_in[i].caps.mps = cfg->ep0_mps;
1357+
ep_cfg_in[i].caps.mps = UDC_STM32_EP0_MAX_PACKET_SIZE;
13561358
} else {
13571359
ep_cfg_in[i].caps.bulk = 1;
13581360
ep_cfg_in[i].caps.interrupt = 1;

0 commit comments

Comments
 (0)