From 5ade91780578bca10bae048a58fed4b788b553d1 Mon Sep 17 00:00:00 2001 From: Michiel van Leeuwen Date: Fri, 28 Apr 2023 11:26:26 +0200 Subject: [PATCH 1/3] dwc2: configure fifo size to be twice the max_size This is needed in order to always be able to fit a packet in the fifo. Writing to the fifo is done from an interrupts that fires when the fifo is half-empty, so the fifo must be twice the packet size. --- src/portable/synopsys/dwc2/dcd_dwc2.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/portable/synopsys/dwc2/dcd_dwc2.c b/src/portable/synopsys/dwc2/dcd_dwc2.c index c6132a1f54..bcd5849530 100644 --- a/src/portable/synopsys/dwc2/dcd_dwc2.c +++ b/src/portable/synopsys/dwc2/dcd_dwc2.c @@ -534,6 +534,9 @@ void dcd_init (uint8_t rhport) int_mask = dwc2->gotgint; dwc2->gotgint |= int_mask; + // Configure TX FIFO to set the TX FIFO empty interrupt when half-empty + dwc2->gahbcfg &= ~GAHBCFG_TXFELVL; + // Required as part of core initialization. // TODO: How should mode mismatch be handled? It will cause // the core to stop working/require reset. @@ -645,7 +648,10 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) xfer->max_size = tu_edpt_packet_size(desc_edpt); xfer->interval = desc_edpt->bInterval; - uint16_t const fifo_size = tu_div_ceil(xfer->max_size, 4); + // The fifo-empty interrupt fires when the interrupt is half empty. In order + // to be able to write a packet at that point, the fifo must be twice the + // max_size. + uint16_t const fifo_size = tu_div_ceil(xfer->max_size * 2, 4); if(dir == TUSB_DIR_OUT) { From 678edbe20320fe4d17437a9175ec8ff88f752ecb Mon Sep 17 00:00:00 2001 From: Michiel van Leeuwen Date: Fri, 28 Apr 2023 11:39:39 +0200 Subject: [PATCH 2/3] Check correct interrupt flag Fixes #1737 --- src/portable/synopsys/dwc2/dcd_dwc2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/portable/synopsys/dwc2/dcd_dwc2.c b/src/portable/synopsys/dwc2/dcd_dwc2.c index bcd5849530..d840147893 100644 --- a/src/portable/synopsys/dwc2/dcd_dwc2.c +++ b/src/portable/synopsys/dwc2/dcd_dwc2.c @@ -1322,7 +1322,7 @@ void dcd_int_handler(uint8_t rhport) do { handle_rxflvl_irq(rhport); - } while(dwc2->gotgint & GINTSTS_RXFLVL); + } while(dwc2->gintsts & GINTSTS_RXFLVL); // Manage RX FIFO size if (_out_ep_closed) From 75cf8e21a7e525eb24b7fd34fe5859cba331fcc2 Mon Sep 17 00:00:00 2001 From: Michiel van Leeuwen Date: Thu, 4 May 2023 10:02:42 +0200 Subject: [PATCH 3/3] Use double-sized fifo only for IN endpoints --- src/portable/synopsys/dwc2/dcd_dwc2.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/portable/synopsys/dwc2/dcd_dwc2.c b/src/portable/synopsys/dwc2/dcd_dwc2.c index d840147893..c3d3ae0853 100644 --- a/src/portable/synopsys/dwc2/dcd_dwc2.c +++ b/src/portable/synopsys/dwc2/dcd_dwc2.c @@ -648,10 +648,7 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) xfer->max_size = tu_edpt_packet_size(desc_edpt); xfer->interval = desc_edpt->bInterval; - // The fifo-empty interrupt fires when the interrupt is half empty. In order - // to be able to write a packet at that point, the fifo must be twice the - // max_size. - uint16_t const fifo_size = tu_div_ceil(xfer->max_size * 2, 4); + uint16_t fifo_size = tu_div_ceil(xfer->max_size, 4); if(dir == TUSB_DIR_OUT) { @@ -697,6 +694,11 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) // In FIFO is allocated by following rules: // - IN EP 1 gets FIFO 1, IN EP "n" gets FIFO "n". + // The fifo-empty interrupt fires when the interrupt is half empty. In order + // to be able to write a packet at that point, the fifo must be twice the + // max_size. + fifo_size = fifo_size * 2; + // Check if free space is available TU_ASSERT(_allocated_fifo_words_tx + fifo_size + dwc2->grxfsiz <= _dwc2_controller[rhport].ep_fifo_size/4);