@@ -630,26 +630,37 @@ void dcd_sof_enable(uint8_t rhport, bool en)
630
630
/* DCD Endpoint port
631
631
*------------------------------------------------------------------*/
632
632
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 )
635
637
{
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;
640
643
}
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 ;
645
660
}
646
- }
647
-
648
- ep_avaliable_count [rhport ].in_ep -- ;
649
- ep_avaliable_count [rhport ].out_ep -- ;
650
-
651
- return true;
661
+ return true;
652
662
}
663
+ #endif // TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
653
664
654
665
bool dcd_edpt_open (uint8_t rhport , tusb_desc_endpoint_t const * desc_edpt )
655
666
{
@@ -661,7 +672,11 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
661
672
uint8_t const epnum = tu_edpt_number (desc_edpt -> bEndpointAddress );
662
673
uint8_t const dir = tu_edpt_dir (desc_edpt -> bEndpointAddress );
663
674
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
665
680
666
681
xfer_ctl_t * xfer = XFER_CTL_BASE (epnum , dir );
667
682
xfer -> max_size = tu_edpt_packet_size (desc_edpt );
@@ -742,6 +757,11 @@ void dcd_edpt_close_all (uint8_t rhport)
742
757
dwc2_regs_t * dwc2 = DWC2_REG (rhport );
743
758
uint8_t const ep_count = _dwc2_controller [rhport ].ep_count ;
744
759
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
+
745
765
// Disable non-control interrupt
746
766
dwc2 -> daintmsk = (1 << DAINTMSK_OEPM_Pos ) | (1 << DAINTMSK_IEPM_Pos );
747
767
@@ -899,6 +919,14 @@ void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
899
919
900
920
dcd_edpt_disable (rhport , ep_addr , false);
901
921
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
+
902
930
// Update max_size
903
931
xfer_status [epnum ][dir ].max_size = 0 ; // max_size = 0 marks a disabled EP - required for changing FIFO allocation
904
932
0 commit comments