Skip to content

Commit 4c24ffb

Browse files
feature(dcd): Check avaliable EPs while opening them
1 parent 3f8200b commit 4c24ffb

File tree

6 files changed

+44
-72
lines changed

6 files changed

+44
-72
lines changed

src/device/dcd.h

-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,6 @@ TU_ATTR_WEAK bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t l
190190
// Configure and enable an ISO endpoint according to descriptor
191191
TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
192192

193-
// Get max number of endpoints
194-
void dcd_get_edpt_max_count(uint8_t rhport, uint8_t *in_edpt_max_count, uint8_t *out_edpt_max_count);
195-
196193
//--------------------------------------------------------------------+
197194
// Event API (implemented by stack)
198195
//--------------------------------------------------------------------+

src/device/usbd.c

-25
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ tu_static osal_queue_t _usbd_q;
297297
static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
298298
static bool process_set_config(uint8_t rhport, uint8_t cfg_num);
299299
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
300-
static bool usbd_edpt_check_max_count(uint8_t rhport);
301300

302301
// from usbd_control.c
303302
void usbd_control_reset(void);
@@ -397,9 +396,6 @@ bool tud_init (uint8_t rhport)
397396
// skip if already initialized
398397
if ( tud_inited() ) return true;
399398

400-
// check if required endpoint count is correct
401-
TU_ASSERT(usbd_edpt_check_max_count(rhport));
402-
403399
TU_LOG(USBD_DBG, "USBD init on controller %u\r\n", rhport);
404400
TU_LOG_INT(USBD_DBG, sizeof(usbd_device_t));
405401
TU_LOG_INT(USBD_DBG, sizeof(tu_fifo_t));
@@ -1424,25 +1420,4 @@ bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep
14241420
return dcd_edpt_iso_activate(rhport, desc_ep);
14251421
}
14261422

1427-
static bool usbd_edpt_check_max_count(uint8_t rhport)
1428-
{
1429-
uint8_t edpt_target_in_max = 0, edpt_target_out_max = 0;
1430-
dcd_get_edpt_max_count(rhport, &edpt_target_in_max, &edpt_target_out_max);
1431-
const uint8_t edpt_total_required_count = CFG_TUD_IN_EP_COUNT + CFG_TUD_OUT_EP_COUNT;
1432-
const uint8_t edpt_target_max = TU_MAX(edpt_target_out_max, edpt_target_in_max);
1433-
1434-
if (edpt_total_required_count > edpt_target_max) {
1435-
TU_LOG(1, "Number of required endpoints: %d is more than max number of endpoints on target: %d\r\n", edpt_total_required_count, edpt_target_max);
1436-
return false;
1437-
}
1438-
1439-
// ESP32Sx has 6 endpoints, from which only 5 can be confiugred as IN
1440-
// check if the device has no more than 5 IN endpoints
1441-
if (CFG_TUD_IN_EP_COUNT > edpt_target_in_max) {
1442-
TU_LOG(1, "Number of required IN endpoints: %d is more than max number of IN endpoints on target: %d\r\n", CFG_TUD_IN_EP_COUNT, edpt_target_in_max);
1443-
return false;
1444-
}
1445-
1446-
return true;
1447-
}
14481423
#endif

src/device/usbd.h

-30
Original file line numberDiff line numberDiff line change
@@ -858,36 +858,6 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
858858
/* Endpoint Out */\
859859
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
860860

861-
//--------------------------------------------------------------------+
862-
// Calculate used endpoint count
863-
//--------------------------------------------------------------------+
864-
865-
// Calculate used IN endpoints
866-
#define CFG_TUD_IN_EP_COUNT 2 * CFG_TUD_CDC + \
867-
CFG_TUD_MSC + \
868-
CFG_TUD_HID + \
869-
CFG_TUD_MIDI + \
870-
CFG_TUD_CUSTOM_CLASS + \
871-
2 * CFG_TUD_ECM_RNDIS + \
872-
2 * CFG_TUD_NCM + \
873-
2 * CFG_TUD_DFU + \
874-
2 * CFG_TUD_DFU_RUNTIME + \
875-
2 * CFG_TUD_BTH + \
876-
CFG_TUD_BTH_ISO_ALT_COUNT
877-
878-
// Calculate used OUT endpoints
879-
#define CFG_TUD_OUT_EP_COUNT CFG_TUD_CDC + \
880-
CFG_TUD_MSC + \
881-
CFG_TUD_HID + \
882-
CFG_TUD_MIDI + \
883-
CFG_TUD_CUSTOM_CLASS + \
884-
CFG_TUD_ECM_RNDIS + \
885-
CFG_TUD_NCM + \
886-
CFG_TUD_DFU + \
887-
CFG_TUD_DFU_RUNTIME + \
888-
CFG_TUD_BTH + \
889-
CFG_TUD_BTH_ISO_ALT_COUNT
890-
891861
#ifdef __cplusplus
892862
}
893863
#endif

src/portable/synopsys/dwc2/dcd_dwc2.c

+22-14
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,27 @@ void dcd_sof_enable(uint8_t rhport, bool en)
630630
/* DCD Endpoint port
631631
*------------------------------------------------------------------*/
632632

633+
// Check is IN/OUT endpoint is avaliable before opening it
634+
static bool dcd_edpt_check_if_avaliable(uint8_t rhport, uint8_t direction)
635+
{
636+
if (direction) { // IN direction
637+
if (ep_avaliable_count[rhport].in_ep < 1) {
638+
TU_LOG(1, "Trying to open IN endpoint, but max number of IN endpoints already opened on this target \r\n");
639+
return false;
640+
}
641+
} else { // OUT direction
642+
if (ep_avaliable_count[rhport].out_ep < 1) {
643+
TU_LOG(1, "Trying to open OUT endpoint, but max number of OUT endpoints already opened on this target \r\n");
644+
return false;
645+
}
646+
}
647+
648+
ep_avaliable_count[rhport].in_ep--;
649+
ep_avaliable_count[rhport].out_ep--;
650+
651+
return true;
652+
}
653+
633654
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
634655
{
635656
(void) rhport;
@@ -640,7 +661,7 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
640661
uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress);
641662
uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress);
642663

643-
TU_ASSERT(epnum < ep_count);
664+
TU_ASSERT(dcd_edpt_check_if_avaliable(rhport, dir));
644665

645666
xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
646667
xfer->max_size = tu_edpt_packet_size(desc_edpt);
@@ -923,19 +944,6 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
923944
}
924945
}
925946

926-
void dcd_get_edpt_max_count(uint8_t rhport, uint8_t *in_edpt_max_count, uint8_t *out_edpt_max_count)
927-
{
928-
uint8_t const max_ep_count = _dwc2_controller[rhport].ep_count;
929-
930-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
931-
*in_edpt_max_count = max_ep_count - 1; // esp32Sx has only 5 IN endpoints
932-
*out_edpt_max_count = max_ep_count;
933-
#else
934-
*in_edpt_max_count = max_ep_count;
935-
*out_edpt_max_count = max_ep_count;
936-
#endif
937-
}
938-
939947
/*------------------------------------------------------------------*/
940948

941949
// Read a single data packet from receive FIFO

src/portable/synopsys/dwc2/dwc2_esp32.h

+16
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ static const dwc2_controller_t _dwc2_controller[] =
8282
#endif
8383
};
8484

85+
static ep_avaliable_count_t ep_avaliable_count[] =
86+
{
87+
#ifdef DWC2_FS_PERIPH_BASE
88+
// ESP32Sx has 6 endpoints, from which only 5 can be confiugred as IN
89+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
90+
{ .out_ep = DWC2_FS_EP_MAX, .in_ep = DWC2_FS_EP_MAX - 1},
91+
#else
92+
{ .out_ep = DWC2_FS_EP_MAX, .in_ep = DWC2_FS_EP_MAX},
93+
#endif
94+
#endif
95+
96+
#ifdef DWC2_HS_PERIPH_BASE
97+
{ .out_ep = DWC2_HS_EP_MAX, .in_ep = DWC2_HS_EP_MAX},
98+
#endif
99+
};
100+
85101
static intr_handle_t usb_ih[DWC2_PERIPH_COUNT];
86102

87103
static void dcd_int_handler_wrap(void* arg)

src/portable/synopsys/dwc2/dwc2_type.h

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ typedef struct
3232
uint32_t ep_fifo_size;
3333
}dwc2_controller_t;
3434

35+
typedef struct
36+
{
37+
int8_t out_ep;
38+
int8_t in_ep;
39+
}ep_avaliable_count_t;
40+
3541
/* DWC OTG HW Release versions */
3642
#define DWC2_CORE_REV_2_71a 0x4f54271a
3743
#define DWC2_CORE_REV_2_72a 0x4f54272a

0 commit comments

Comments
 (0)