Skip to content

Commit 0fab57b

Browse files
authored
Merge pull request #3279 from HiFiPhile/dwc2_ep0
dcd/dwc2: fix enumeration when EP0 size=8
2 parents 20c8e92 + 16dd749 commit 0fab57b

File tree

4 files changed

+137
-69
lines changed

4 files changed

+137
-69
lines changed

hw/bsp/BoardPresets.json

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,17 @@
1313
}
1414
},
1515
{
16-
"name": "adafruit_clue",
17-
"inherits": "default"
18-
},
19-
{
20-
"name": "adafruit_feather_esp32_v2",
21-
"inherits": "default"
22-
},
23-
{
24-
"name": "adafruit_feather_esp32c6",
25-
"inherits": "default"
26-
},
27-
{
28-
"name": "adafruit_feather_esp32s2",
29-
"inherits": "default"
16+
"name": "default single config",
17+
"hidden": true,
18+
"description": "Configure preset for the ${presetName} board",
19+
"generator": "Ninja",
20+
"binaryDir": "${sourceDir}/build/${presetName}",
21+
"cacheVariables": {
22+
"BOARD": "${presetName}"
23+
}
3024
},
3125
{
32-
"name": "adafruit_feather_esp32s3",
26+
"name": "adafruit_clue",
3327
"inherits": "default"
3428
},
3529
{
@@ -40,14 +34,6 @@
4034
"name": "adafruit_fruit_jam",
4135
"inherits": "default"
4236
},
43-
{
44-
"name": "adafruit_magtag_29gray",
45-
"inherits": "default"
46-
},
47-
{
48-
"name": "adafruit_metro_esp32s2",
49-
"inherits": "default"
50-
},
5137
{
5238
"name": "adafruit_metro_rp2350",
5339
"inherits": "default"
@@ -188,42 +174,6 @@
188174
"name": "ek_tm4c123gxl",
189175
"inherits": "default"
190176
},
191-
{
192-
"name": "espressif_addax_1",
193-
"inherits": "default"
194-
},
195-
{
196-
"name": "espressif_c3_devkitc",
197-
"inherits": "default"
198-
},
199-
{
200-
"name": "espressif_c6_devkitc",
201-
"inherits": "default"
202-
},
203-
{
204-
"name": "espressif_kaluga_1",
205-
"inherits": "default"
206-
},
207-
{
208-
"name": "espressif_p4_function_ev",
209-
"inherits": "default"
210-
},
211-
{
212-
"name": "espressif_s2_devkitc",
213-
"inherits": "default"
214-
},
215-
{
216-
"name": "espressif_s3_devkitc",
217-
"inherits": "default"
218-
},
219-
{
220-
"name": "espressif_s3_devkitm",
221-
"inherits": "default"
222-
},
223-
{
224-
"name": "espressif_saola_1",
225-
"inherits": "default"
226-
},
227177
{
228178
"name": "f1c100s",
229179
"inherits": "default"
@@ -823,6 +773,66 @@
823773
{
824774
"name": "xmc4700_relax",
825775
"inherits": "default"
776+
},
777+
{
778+
"name": "adafruit_feather_esp32_v2",
779+
"inherits": "default single config"
780+
},
781+
{
782+
"name": "adafruit_feather_esp32c6",
783+
"inherits": "default single config"
784+
},
785+
{
786+
"name": "adafruit_feather_esp32s2",
787+
"inherits": "default single config"
788+
},
789+
{
790+
"name": "adafruit_feather_esp32s3",
791+
"inherits": "default single config"
792+
},
793+
{
794+
"name": "adafruit_magtag_29gray",
795+
"inherits": "default single config"
796+
},
797+
{
798+
"name": "adafruit_metro_esp32s2",
799+
"inherits": "default single config"
800+
},
801+
{
802+
"name": "espressif_addax_1",
803+
"inherits": "default single config"
804+
},
805+
{
806+
"name": "espressif_c3_devkitc",
807+
"inherits": "default single config"
808+
},
809+
{
810+
"name": "espressif_c6_devkitc",
811+
"inherits": "default single config"
812+
},
813+
{
814+
"name": "espressif_kaluga_1",
815+
"inherits": "default single config"
816+
},
817+
{
818+
"name": "espressif_p4_function_ev",
819+
"inherits": "default single config"
820+
},
821+
{
822+
"name": "espressif_s2_devkitc",
823+
"inherits": "default single config"
824+
},
825+
{
826+
"name": "espressif_s3_devkitc",
827+
"inherits": "default single config"
828+
},
829+
{
830+
"name": "espressif_s3_devkitm",
831+
"inherits": "default single config"
832+
},
833+
{
834+
"name": "espressif_saola_1",
835+
"inherits": "default single config"
826836
}
827837
],
828838
"buildPresets": [

src/portable/synopsys/dwc2/dcd_dwc2.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static void dma_setup_prepare(uint8_t rhport) {
168168
- All EP OUT shared a unique OUT FIFO which uses (for Slave or Buffer DMA, Scatt/Gather DMA use different formula):
169169
- 13 for setup packets + control words (up to 3 setup packets).
170170
- 1 for global NAK (not required/used here).
171-
- Largest-EPsize/4 + 1. ( FS: 64 bytes, HS: 512 bytes). Recommended is "2 x (Largest-EPsize/4 + 1)"
171+
- Largest-EPsize/4 + 1. (FS: 64 bytes, HS: 512 bytes). Recommended is "2 x (Largest-EPsize/4 + 1)"
172172
- 2 for each used OUT endpoint
173173
174174
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) {
282282
dwc2_dep_t* dep = &dwc2->ep[dir == TUSB_DIR_IN ? 0 : 1][epnum];
283283

284284
if (dir == TUSB_DIR_IN) {
285-
// Only disable currently enabled non-control endpoint
286-
if ((epnum == 0) || !(dep->diepctl & DIEPCTL_EPENA)) {
285+
if (!(dep->diepctl & DIEPCTL_EPENA)) {
287286
dep->diepctl |= DIEPCTL_SNAK | (stall ? DIEPCTL_STALL : 0);
288287
} else {
289288
// Stop transmitting packets and NAK IN xfers.
@@ -701,12 +700,23 @@ static void handle_bus_reset(uint8_t rhport) {
701700
dcfg.address = 0;
702701
dwc2->dcfg = dcfg.value;
703702

704-
// Fixed both control EP0 size to 64 bytes
705-
dwc2->epin[0].ctl &= ~(0x03 << DIEPCTL_MPSIZ_Pos);
706-
dwc2->epout[0].ctl &= ~(0x03 << DOEPCTL_MPSIZ_Pos);
703+
// 6. Configure maximum packet size for EP0
704+
uint8_t mps = 0;
705+
switch (CFG_TUD_ENDPOINT0_SIZE) {
706+
case 8: mps = 3; break;
707+
case 16: mps = 2; break;
708+
case 32: mps = 1; break;
709+
case 64: mps = 0; break;
710+
default: mps = 0; break;
711+
}
712+
713+
dwc2->epin[0].ctl &= ~DIEPCTL0_MPSIZ_Msk;
714+
dwc2->epout[0].ctl &= ~DOEPCTL0_MPSIZ_Msk;
715+
dwc2->epin[0].ctl |= mps << DIEPCTL0_MPSIZ_Pos;
716+
dwc2->epout[0].ctl |= mps << DOEPCTL0_MPSIZ_Pos;
707717

708-
xfer_status[0][TUSB_DIR_OUT].max_size = 64;
709-
xfer_status[0][TUSB_DIR_IN].max_size = 64;
718+
xfer_status[0][TUSB_DIR_OUT].max_size = CFG_TUD_ENDPOINT0_SIZE;
719+
xfer_status[0][TUSB_DIR_IN].max_size = CFG_TUD_ENDPOINT0_SIZE;
710720

711721
if(dma_device_enabled(dwc2)) {
712722
dma_setup_prepare(rhport);
@@ -830,6 +840,11 @@ static void handle_rxflvl_irq(uint8_t rhport) {
830840

831841
static void handle_epout_slave(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepint_bm) {
832842
if (doepint_bm.setup_phase_done) {
843+
// Cleanup previous pending EP0 IN transfer if any
844+
dwc2_dep_t* epin0 = &DWC2_REG(rhport)->epin[0];
845+
if (epin0->diepctl & DIEPCTL_EPENA) {
846+
edpt_disable(rhport, 0x80, false);
847+
}
833848
dcd_event_setup_received(rhport, _dcd_usbbuf.setup_packet, true);
834849
return;
835850
}
@@ -908,6 +923,11 @@ static void handle_epout_dma(uint8_t rhport, uint8_t epnum, dwc2_doepint_t doepi
908923
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
909924

910925
if (doepint_bm.setup_phase_done) {
926+
// Cleanup previous pending EP0 IN transfer if any
927+
dwc2_dep_t* epin0 = &DWC2_REG(rhport)->epin[0];
928+
if (epin0->diepctl & DIEPCTL_EPENA) {
929+
edpt_disable(rhport, 0x80, false);
930+
}
911931
dma_setup_prepare(rhport);
912932
dcd_dcache_invalidate(_dcd_usbbuf.setup_packet, 8);
913933
dcd_event_setup_received(rhport, _dcd_usbbuf.setup_packet, true);

src/portable/synopsys/dwc2/dwc2_type.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,9 @@ TU_VERIFY_STATIC(offsetof(dwc2_regs_t, fifo ) == 0x1000, "incorrect size");
18471847
#define HPTXFSIZ_PTXFD HPTXFSIZ_PTXFD_Msk // Host periodic TxFIFO depth
18481848

18491849
/******************** Bit definition for DIEPCTL register ********************/
1850+
#define DIEPCTL0_MPSIZ_Pos (0U)
1851+
#define DIEPCTL0_MPSIZ_Msk (0x3UL << DIEPCTL0_MPSIZ_Pos) // 0x00000003
1852+
#define DIEPCTL0_MPSIZ DIEPCTL0_MPSIZ_Msk // Maximum packet size(endpoint 0)
18501853
#define DIEPCTL_MPSIZ_Pos (0U)
18511854
#define DIEPCTL_MPSIZ_Msk (0x7FFUL << DIEPCTL_MPSIZ_Pos) // 0x000007FF
18521855
#define DIEPCTL_MPSIZ DIEPCTL_MPSIZ_Msk // Maximum packet size
@@ -2155,6 +2158,9 @@ TU_VERIFY_STATIC(offsetof(dwc2_regs_t, fifo ) == 0x1000, "incorrect size");
21552158
#define EPCTL_EPENA EPCTL_EPENA_Msk // Endpoint enable
21562159

21572160
/******************** Bit definition for DOEPCTL register ********************/
2161+
#define DOEPCTL0_MPSIZ_Pos (0U)
2162+
#define DOEPCTL0_MPSIZ_Msk (0x3UL << DOEPCTL0_MPSIZ_Pos) // 0x00000003
2163+
#define DOEPCTL0_MPSIZ DOEPCTL0_MPSIZ_Msk // Maximum packet size(endpoint 0)
21582164
#define DOEPCTL_MPSIZ_Pos (0U)
21592165
#define DOEPCTL_MPSIZ_Msk (0x7FFUL << DOEPCTL_MPSIZ_Pos) // 0x000007FF
21602166
#define DOEPCTL_MPSIZ DOEPCTL_MPSIZ_Msk // Maximum packet size //Bit 1

tools/gen_presets.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55

66
def main():
77
board_list = []
8+
board_list_esp = []
89

9-
# Find all board.cmake files
10+
# Find all board.cmake files, exclude espressif
1011
for root, dirs, files in os.walk("hw/bsp"):
1112
for file in files:
12-
if file == "board.cmake":
13+
if file == "board.cmake" and "espressif" not in root:
1314
board_list.append(os.path.basename(root))
1415

16+
# Find all espressif boards
17+
for root, dirs, files in os.walk("hw/bsp/espressif"):
18+
for file in files:
19+
if file == "board.cmake":
20+
board_list_esp.append(os.path.basename(root))
21+
1522
print('Generating presets for the following boards:')
1623
print(board_list)
1724

@@ -29,8 +36,17 @@ def main():
2936
"cacheVariables": {
3037
"CMAKE_DEFAULT_BUILD_TYPE": "RelWithDebInfo",
3138
"BOARD": r"${presetName}"
39+
}},
40+
{"name": "default single config",
41+
"hidden": True,
42+
"description": r"Configure preset for the ${presetName} board",
43+
"generator": "Ninja",
44+
"binaryDir": r"${sourceDir}/build/${presetName}",
45+
"cacheVariables": {
46+
"BOARD": r"${presetName}"
3247
}}]
3348

49+
# Add non-espressif boards
3450
presets['configurePresets'].extend(
3551
sorted(
3652
[
@@ -43,6 +59,22 @@ def main():
4359
)
4460
)
4561

62+
# Add espressif boards with single config generator
63+
presets['configurePresets'].extend(
64+
sorted(
65+
[
66+
{
67+
'name': board,
68+
'inherits': 'default single config'
69+
}
70+
for board in board_list_esp
71+
], key=lambda x: x['name']
72+
)
73+
)
74+
75+
# Combine all boards
76+
board_list.extend(board_list_esp)
77+
4678
# Build presets
4779
# no inheritance since 'name' doesn't support macro expansion
4880
presets['buildPresets'] = sorted(

0 commit comments

Comments
 (0)