summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2025-10-09 21:42:32 +0700
committerGitHub <[email protected]>2025-10-09 21:42:32 +0700
commit0fab57b8c192615a019ac232229bf0e1d6b09c40 (patch)
treedae7741fdbecb101c4ae0c7d0f8d903881d78627
parent20c8e92f3a59bdc655a6d2b476ded21097068120 (diff)
parent16dd74912fd6627fcfa1dcd91a8e6f9150b0cced (diff)
Merge pull request #3279 from HiFiPhile/dwc2_ep0
dcd/dwc2: fix enumeration when EP0 size=8
-rw-r--r--hw/bsp/BoardPresets.json128
-rw-r--r--src/portable/synopsys/dwc2/dcd_dwc2.c36
-rw-r--r--src/portable/synopsys/dwc2/dwc2_type.h6
-rwxr-xr-xtools/gen_presets.py36
4 files changed, 137 insertions, 69 deletions
diff --git a/hw/bsp/BoardPresets.json b/hw/bsp/BoardPresets.json
index 335546837..044c74ee1 100644
--- a/hw/bsp/BoardPresets.json
+++ b/hw/bsp/BoardPresets.json
@@ -13,23 +13,17 @@
}
},
{
- "name": "adafruit_clue",
- "inherits": "default"
- },
- {
- "name": "adafruit_feather_esp32_v2",
- "inherits": "default"
- },
- {
- "name": "adafruit_feather_esp32c6",
- "inherits": "default"
- },
- {
- "name": "adafruit_feather_esp32s2",
- "inherits": "default"
+ "name": "default single config",
+ "hidden": true,
+ "description": "Configure preset for the ${presetName} board",
+ "generator": "Ninja",
+ "binaryDir": "${sourceDir}/build/${presetName}",
+ "cacheVariables": {
+ "BOARD": "${presetName}"
+ }
},
{
- "name": "adafruit_feather_esp32s3",
+ "name": "adafruit_clue",
"inherits": "default"
},
{
@@ -41,14 +35,6 @@
"inherits": "default"
},
{
- "name": "adafruit_magtag_29gray",
- "inherits": "default"
- },
- {
- "name": "adafruit_metro_esp32s2",
- "inherits": "default"
- },
- {
"name": "adafruit_metro_rp2350",
"inherits": "default"
},
@@ -189,42 +175,6 @@
"inherits": "default"
},
{
- "name": "espressif_addax_1",
- "inherits": "default"
- },
- {
- "name": "espressif_c3_devkitc",
- "inherits": "default"
- },
- {
- "name": "espressif_c6_devkitc",
- "inherits": "default"
- },
- {
- "name": "espressif_kaluga_1",
- "inherits": "default"
- },
- {
- "name": "espressif_p4_function_ev",
- "inherits": "default"
- },
- {
- "name": "espressif_s2_devkitc",
- "inherits": "default"
- },
- {
- "name": "espressif_s3_devkitc",
- "inherits": "default"
- },
- {
- "name": "espressif_s3_devkitm",
- "inherits": "default"
- },
- {
- "name": "espressif_saola_1",
- "inherits": "default"
- },
- {
"name": "f1c100s",
"inherits": "default"
},
@@ -823,6 +773,66 @@
{
"name": "xmc4700_relax",
"inherits": "default"
+ },
+ {
+ "name": "adafruit_feather_esp32_v2",
+ "inherits": "default single config"
+ },
+ {
+ "name": "adafruit_feather_esp32c6",
+ "inherits": "default single config"
+ },
+ {
+ "name": "adafruit_feather_esp32s2",
+ "inherits": "default single config"
+ },
+ {
+ "name": "adafruit_feather_esp32s3",
+ "inherits": "default single config"
+ },
+ {
+ "name": "adafruit_magtag_29gray",
+ "inherits": "default single config"
+ },
+ {
+ "name": "adafruit_metro_esp32s2",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_addax_1",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_c3_devkitc",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_c6_devkitc",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_kaluga_1",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_p4_function_ev",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_s2_devkitc",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_s3_devkitc",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_s3_devkitm",
+ "inherits": "default single config"
+ },
+ {
+ "name": "espressif_saola_1",
+ "inherits": "default single config"
}
],
"buildPresets": [
diff --git a/src/portable/synopsys/dwc2/dcd_dwc2.c b/src/portable/synopsys/dwc2/dcd_dwc2.c
index f10f0bdc3..9d9172b67 100644
--- a/src/portable/synopsys/dwc2/dcd_dwc2.c
+++ b/src/portable/synopsys/dwc2/dcd_dwc2.c
@@ -168,7 +168,7 @@ static void dma_setup_prepare(uint8_t rhport) {
- All EP OUT shared a unique OUT FIFO which uses (for Slave or Buffer DMA, Scatt/Gather DMA use different formula):
- 13 for setup packets + control words (up to 3 setup packets).
- 1 for global NAK (not required/used here).
- - Largest-EPsize/4 + 1. ( FS: 64 bytes, HS: 512 bytes). Recommended is "2 x (Largest-EPsize/4 + 1)"
+ - Largest-EPsize/4 + 1. (FS: 64 bytes, HS: 512 bytes). Recommended is "2 x (Largest-EPsize/4 + 1)"
- 2 for each used OUT endpoint
Therefore GRXFSIZ = 13 + 1 + 2 x (Largest-EPsize/4 + 1) + 2 x EPOUTnum
@@ -282,8 +282,7 @@ static void edpt_disable(uint8_t rhport, uint8_t ep_addr, bool stall) {
dwc2_dep_t* dep = &dwc2->ep[dir == TUSB_DIR_IN ? 0 : 1][epnum];
if (dir == TUSB_DIR_IN) {
- // Only disable currently enabled non-control endpoint
- if ((epnum == 0) || !(dep->diepctl & DIEPCTL_EPENA)) {
+ if (!(dep->diepctl & DIEPCTL_EPENA)) {
dep->diepctl |= DIEPCTL_SNAK | (stall ? DIEPCTL_STALL : 0);
} else {
// Stop transmitting packets and NAK IN xfers.
@@ -701,12 +700,23 @@ static void handle_bus_reset(uint8_t rhport) {
dcfg.address = 0;
dwc2->dcfg = dcfg.value;
- // Fixed both control EP0 size to 64 bytes
- dwc2->epin[0].ctl &= ~(0x03 << DIEPCTL_MPSIZ_Pos);
- dwc2->epout[0].ctl &= ~(0x03 << DOEPCTL_MPSIZ_Pos);
+ // 6. Configure maximum packet size for EP0
+ uint8_t mps = 0;
+ switch (CFG_TUD_ENDPOINT0_SIZE) {
+ case 8: mps = 3; break;
+ case 16: mps = 2; break;
+ case 32: mps = 1; break;
+ case 64: mps = 0; break;
+ default: mps = 0; break;
+ }
+
+ dwc2->epin[0].ctl &= ~DIEPCTL0_MPSIZ_Msk;
+ dwc2->epout[0].ctl &= ~DOEPCTL0_MPSIZ_Msk;
+ dwc2->epin[0].ctl |= mps << DIEPCTL0_MPSIZ_Pos;
+ dwc2->epout[0].ctl |= mps << DOEPCTL0_MPSIZ_Pos;
- xfer_status[0][TUSB_DIR_OUT].max_size = 64;
- xfer_status[0][TUSB_DIR_IN].max_size = 64;
+ xfer_status[0][TUSB_DIR_OUT].max_size = CFG_TUD_ENDPOINT0_SIZE;
+ xfer_status[0][TUSB_DIR_IN].max_size = CFG_TUD_ENDPOINT0_SIZE;
if(dma_device_enabled(dwc2)) {
dma_setup_prepare(rhport);
@@ -830,6 +840,11 @@ static void handle_rxflvl_irq(uint8_t rhport) {
static void handle_epout_slave(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepint_bm) {
if (doepint_bm.setup_phase_done) {
+ // Cleanup previous pending EP0 IN transfer if any
+ dwc2_dep_t* epin0 = &DWC2_REG(rhport)->epin[0];
+ if (epin0->diepctl & DIEPCTL_EPENA) {
+ edpt_disable(rhport, 0x80, false);
+ }
dcd_event_setup_received(rhport, _dcd_usbbuf.setup_packet, true);
return;
}
@@ -908,6 +923,11 @@ static void handle_epout_dma(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepi
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
if (doepint_bm.setup_phase_done) {
+ // Cleanup previous pending EP0 IN transfer if any
+ dwc2_dep_t* epin0 = &DWC2_REG(rhport)->epin[0];
+ if (epin0->diepctl & DIEPCTL_EPENA) {
+ edpt_disable(rhport, 0x80, false);
+ }
dma_setup_prepare(rhport);
dcd_dcache_invalidate(_dcd_usbbuf.setup_packet, 8);
dcd_event_setup_received(rhport, _dcd_usbbuf.setup_packet, true);
diff --git a/src/portable/synopsys/dwc2/dwc2_type.h b/src/portable/synopsys/dwc2/dwc2_type.h
index 0a8dacf5f..75643529f 100644
--- a/src/portable/synopsys/dwc2/dwc2_type.h
+++ b/src/portable/synopsys/dwc2/dwc2_type.h
@@ -1847,6 +1847,9 @@ TU_VERIFY_STATIC(offsetof(dwc2_regs_t, fifo ) == 0x1000, "incorrect size");
#define HPTXFSIZ_PTXFD HPTXFSIZ_PTXFD_Msk // Host periodic TxFIFO depth
/******************** Bit definition for DIEPCTL register ********************/
+#define DIEPCTL0_MPSIZ_Pos (0U)
+#define DIEPCTL0_MPSIZ_Msk (0x3UL << DIEPCTL0_MPSIZ_Pos) // 0x00000003
+#define DIEPCTL0_MPSIZ DIEPCTL0_MPSIZ_Msk // Maximum packet size(endpoint 0)
#define DIEPCTL_MPSIZ_Pos (0U)
#define DIEPCTL_MPSIZ_Msk (0x7FFUL << DIEPCTL_MPSIZ_Pos) // 0x000007FF
#define DIEPCTL_MPSIZ DIEPCTL_MPSIZ_Msk // Maximum packet size
@@ -2155,6 +2158,9 @@ TU_VERIFY_STATIC(offsetof(dwc2_regs_t, fifo ) == 0x1000, "incorrect size");
#define EPCTL_EPENA EPCTL_EPENA_Msk // Endpoint enable
/******************** Bit definition for DOEPCTL register ********************/
+#define DOEPCTL0_MPSIZ_Pos (0U)
+#define DOEPCTL0_MPSIZ_Msk (0x3UL << DOEPCTL0_MPSIZ_Pos) // 0x00000003
+#define DOEPCTL0_MPSIZ DOEPCTL0_MPSIZ_Msk // Maximum packet size(endpoint 0)
#define DOEPCTL_MPSIZ_Pos (0U)
#define DOEPCTL_MPSIZ_Msk (0x7FFUL << DOEPCTL_MPSIZ_Pos) // 0x000007FF
#define DOEPCTL_MPSIZ DOEPCTL_MPSIZ_Msk // Maximum packet size //Bit 1
diff --git a/tools/gen_presets.py b/tools/gen_presets.py
index 94b8d16b0..94a9361db 100755
--- a/tools/gen_presets.py
+++ b/tools/gen_presets.py
@@ -5,13 +5,20 @@ from pathlib import Path
def main():
board_list = []
+ board_list_esp = []
- # Find all board.cmake files
+ # Find all board.cmake files, exclude espressif
for root, dirs, files in os.walk("hw/bsp"):
for file in files:
- if file == "board.cmake":
+ if file == "board.cmake" and "espressif" not in root:
board_list.append(os.path.basename(root))
+ # Find all espressif boards
+ for root, dirs, files in os.walk("hw/bsp/espressif"):
+ for file in files:
+ if file == "board.cmake":
+ board_list_esp.append(os.path.basename(root))
+
print('Generating presets for the following boards:')
print(board_list)
@@ -29,8 +36,17 @@ def main():
"cacheVariables": {
"CMAKE_DEFAULT_BUILD_TYPE": "RelWithDebInfo",
"BOARD": r"${presetName}"
+ }},
+ {"name": "default single config",
+ "hidden": True,
+ "description": r"Configure preset for the ${presetName} board",
+ "generator": "Ninja",
+ "binaryDir": r"${sourceDir}/build/${presetName}",
+ "cacheVariables": {
+ "BOARD": r"${presetName}"
}}]
+ # Add non-espressif boards
presets['configurePresets'].extend(
sorted(
[
@@ -43,6 +59,22 @@ def main():
)
)
+ # Add espressif boards with single config generator
+ presets['configurePresets'].extend(
+ sorted(
+ [
+ {
+ 'name': board,
+ 'inherits': 'default single config'
+ }
+ for board in board_list_esp
+ ], key=lambda x: x['name']
+ )
+ )
+
+ # Combine all boards
+ board_list.extend(board_list_esp)
+
# Build presets
# no inheritance since 'name' doesn't support macro expansion
presets['buildPresets'] = sorted(