Skip to content

Commit 56f94bb

Browse files
committed
dcd_ch58x: fix toggle, SETUP race, and bus reset handling
- Switch from AUTO_TOG to manual toggle for all endpoints to fix toggle mismatch after clear-stall causing bus resets - Fix SETUP race condition: track ep0_completion_pending and setup_pending to avoid arming stale EP0 transfers - Defer Set Address to ISR after status ZLP is ACK'd - Improve bus reset: reset all EPs, clear pending state - Fix EP4 DMA macro to correctly map to EP0 base (0x10) - Move CH58X_UIS_TOG_OK to common section (used by both DCD/HCD) - Add #ifndef guards for BOARD_TUD/TUH_RHPORT in board.h - Track isochronous per direction: isochronous[ep][dir] - NAK EP0 OUT on completion to prevent premature data acceptance
1 parent a2ea022 commit 56f94bb

4 files changed

Lines changed: 131 additions & 86 deletions

File tree

hw/bsp/ch58x/boards/yd-ch582m/board.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ extern "C" {
4747

4848
// Dual-port: USB1 (rhport 0) = Device, USB2 (rhport 1) = Host
4949
// Swap these two if you want the opposite assignment
50+
#ifndef BOARD_TUD_RHPORT
5051
#define BOARD_TUD_RHPORT 0
52+
#endif
53+
#ifndef BOARD_TUH_RHPORT
5154
#define BOARD_TUH_RHPORT 1
55+
#endif
5256

5357
#ifdef __cplusplus
5458
}

src/portable/wch/ch58x_usbfs_reg.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,10 @@
5656

5757
//--------------------------------------------------------------------+
5858
// Endpoint DMA / T_LEN / CTRL register accessors
59-
//
60-
// EP0-EP3 DMA : base + 0x10 + ep*4 (EP4 shares EP0 DMA, no own register)
61-
// EP5-EP7 DMA : base + 0x54 + (ep-5)*4
62-
// EP0-EP4 TLEN: base + 0x20 + ep*4
63-
// EP5-EP7 TLEN: base + 0x64 + (ep-5)*4
64-
// EP0-EP4 CTRL: base + 0x22 + ep*4
65-
// EP5-EP7 CTRL: base + 0x66 + (ep-5)*4
66-
//
67-
// Using computed addresses avoids per-TU copies of static lookup tables.
68-
// EP4 DMA is not accessed directly (it shares EP0's DMA register).
6959
//--------------------------------------------------------------------+
7060

71-
// EP DMA is 16-bit (only low 16 bits of RAM address, high bits implied 0x2000)
72-
#define CH58X_EP_DMA(base, ep) (*(volatile uint16_t *)((base) + ((ep) <= 3 ? (0x10u + (ep)*4u) : (0x54u + ((ep)-5u)*4u))))
61+
// EP DMA is 16-bit (only low 16 bits of RAM address, high bits implied 0x2000), EP4 shares EP0's DMA register (no independent DMA), so ep==4 maps to base+0x10
62+
#define CH58X_EP_DMA(base, ep) (*(volatile uint16_t *)((base) + ((ep) <= 3 ? (0x10u + (ep)*4u) : ((ep) == 4 ? 0x10u : (0x54u + ((ep)-5u)*4u)))))
7363
#define CH58X_EP_TLEN(base, ep) (*(volatile uint8_t *)((base) + ((ep) <= 4 ? (0x20u + (ep)*4u) : (0x64u + ((ep)-5u)*4u))))
7464
#define CH58X_EP_CTRL(base, ep) (*(volatile uint8_t *)((base) + ((ep) <= 4 ? (0x22u + (ep)*4u) : (0x66u + ((ep)-5u)*4u))))
7565

@@ -121,6 +111,7 @@
121111
//--------------------------------------------------------------------+
122112
#define CH58X_INT_ST_ENDP(x) (((x) >> 0) & 0x0F)
123113
#define CH58X_INT_ST_TOKEN(x) (((x) >> 4) & 0x03)
114+
#define CH58X_UIS_TOG_OK 0x40 // toggle match flag (device and host)
124115
#define CH58X_UIS_SETUP_ACT 0x80
125116

126117
// Token PID values
@@ -147,7 +138,7 @@
147138
// Bit 7: RB_UEP_R_TOG RX data toggle
148139
// Bit 6: RB_UEP_T_TOG TX data toggle
149140
// Bit 5: (reserved)
150-
// Bit 4: RB_UEP_AUTO_TOG auto toggle (EP1/2/3 only)
141+
// Bit 4: RB_UEP_AUTO_TOG auto toggle (EP1/2/3/5/6/7, not EP0/EP4)
151142
// Bit 3: R_RES1 RX response high
152143
// Bit 2: R_RES0 RX response low
153144
// Bit 1: T_RES1 TX response high
@@ -253,7 +244,6 @@
253244
// USB_INT_ST host-mode bits
254245
//--------------------------------------------------------------------+
255246
#define CH58X_UIS_H_RES_MASK 0x0F
256-
#define CH58X_UIS_TOG_OK 0x40
257247

258248
//--------------------------------------------------------------------+
259249
// USB_DEV_AD bits

0 commit comments

Comments
 (0)