Skip to content

Commit 30047cb

Browse files
feature(dcd): review changes implemented
1 parent 3c141f2 commit 30047cb

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed

src/portable/synopsys/dwc2/dcd_dwc2.c

+45-17
Original file line numberDiff line numberDiff line change
@@ -630,26 +630,37 @@ 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)
633+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
634+
635+
// Check if IN/OUT endpoint is avaliable before opening it
636+
static bool dcd_ep_available(uint8_t rhport, uint8_t dir)
635637
{
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;
638+
printf("dcd_ep_available\n\n");
639+
// Verify that we have a vacant EP
640+
if ((dwc_ep_config[rhport].in_ep + dwc_ep_config[rhport].out_ep) > (dwc_ep_config[rhport].ep_max_count - 1)) {
641+
TU_LOG(1, "Trying to open an endpoint, but max number of endpoints: %d already opened on this target \r\n", dwc_ep_config[rhport].ep_max_count);
642+
return false;
640643
}
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;
644+
// Get the ep_count amount at the moment as a temporal variable and update it
645+
uint8_t new_ep_count = (dir) ? dwc_ep_config[rhport].in_ep : dwc_ep_config[rhport].out_ep;
646+
647+
// Verify overflow for IN EP ESP32Sx
648+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
649+
// ESP32Sx has 6 endpoints, from which only 5 can be confiugred as IN
650+
if ((dir) && (new_ep_count > (dwc_ep_config[rhport].ep_max_count - 1))) {
651+
TU_LOG(1, "Trying to open IN endpoint, but max number of IN endpoints: %d already opened on this target \r\n", dwc_ep_config[rhport].ep_max_count - 1);
652+
return false;
653+
}
654+
#endif // TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
655+
// Write new value back
656+
if(dir) {
657+
dwc_ep_config[rhport].in_ep = new_ep_count;
658+
} else {
659+
dwc_ep_config[rhport].out_ep = new_ep_count;
645660
}
646-
}
647-
648-
ep_avaliable_count[rhport].in_ep--;
649-
ep_avaliable_count[rhport].out_ep--;
650-
651-
return true;
661+
return true;
652662
}
663+
#endif // TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
653664

654665
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
655666
{
@@ -661,7 +672,11 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
661672
uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress);
662673
uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress);
663674

664-
TU_ASSERT(dcd_edpt_check_if_avaliable(rhport, dir));
675+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
676+
if (!dcd_ep_available(rhport, dir)) {
677+
return false;
678+
}
679+
#endif
665680

666681
xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
667682
xfer->max_size = tu_edpt_packet_size(desc_edpt);
@@ -742,6 +757,11 @@ void dcd_edpt_close_all (uint8_t rhport)
742757
dwc2_regs_t * dwc2 = DWC2_REG(rhport);
743758
uint8_t const ep_count = _dwc2_controller[rhport].ep_count;
744759

760+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
761+
dwc_ep_config[rhport].in_ep = 0;
762+
dwc_ep_config[rhport].out_ep = 0;
763+
#endif
764+
745765
// Disable non-control interrupt
746766
dwc2->daintmsk = (1 << DAINTMSK_OEPM_Pos) | (1 << DAINTMSK_IEPM_Pos);
747767

@@ -899,6 +919,14 @@ void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
899919

900920
dcd_edpt_disable(rhport, ep_addr, false);
901921

922+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
923+
if (dir) {
924+
dwc_ep_config[rhport].in_ep--;
925+
} else {
926+
dwc_ep_config[rhport].out_ep--;
927+
}
928+
#endif
929+
902930
// Update max_size
903931
xfer_status[epnum][dir].max_size = 0; // max_size = 0 marks a disabled EP - required for changing FIFO allocation
904932

src/portable/synopsys/dwc2/dwc2_esp32.h

+3-9
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,13 @@ static const dwc2_controller_t _dwc2_controller[] =
8282
#endif
8383
};
8484

85-
static ep_avaliable_count_t ep_avaliable_count[] =
85+
static dwc_ep_config_t dwc_ep_config[] =
8686
{
8787
#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
88+
{ .out_ep = 0, .in_ep = 0, .ep_max_count = DWC2_FS_EP_MAX },
9489
#endif
95-
9690
#ifdef DWC2_HS_PERIPH_BASE
97-
{ .out_ep = DWC2_HS_EP_MAX, .in_ep = DWC2_HS_EP_MAX},
91+
{ .out_ep = 0, .in_ep = 0, .ep_max_count = DWC2_HS_EP_MAX },
9892
#endif
9993
};
10094

src/portable/synopsys/dwc2/dwc2_type.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ 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;
35+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
36+
typedef struct {
37+
uint8_t out_ep;
38+
uint8_t in_ep;
39+
const uint8_t ep_max_count;
40+
}dwc_ep_config_t;
41+
#endif
4042

4143
/* DWC OTG HW Release versions */
4244
#define DWC2_CORE_REV_2_71a 0x4f54271a

0 commit comments

Comments
 (0)