Skip to content

Commit ae6add1

Browse files
jgangemideadprogram
authored andcommitted
feat: add ESP32-C5 USB-JTAG PostConnect with LP_WDT disable
- detect USB-JTAG/Serial via ROM .bss at 0x4085F514 - reuse shared disableWatchdogsLP helper (same register offsets as C6)
1 parent 8d59044 commit ae6add1

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

pkg/espflasher/target_esp32c5.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package espflasher
22

3+
// ESP32-C5 register addresses for USB interface detection and watchdog control.
4+
// Reference: esptool/targets/esp32c5.py
5+
const (
6+
esp32c5UARTDevBufNo uint32 = 0x4085F514 // ROM .bss: active console interface
7+
esp32c5UARTDevBufNoUSBJTAGSerial uint32 = 3 // USB-JTAG/Serial active
8+
9+
esp32c5LPWDTConfig0 uint32 = 0x600B1C00
10+
esp32c5LPWDTWProtect uint32 = 0x600B1C18
11+
esp32c5LPWDTSWDConf uint32 = 0x600B1C1C
12+
esp32c5LPWDTSWDWProtect uint32 = 0x600B1C20
13+
)
14+
315
// ESP32-C5 target definition.
416
// Reference: https://github.com/espressif/esptool/blob/master/esptool/targets/esp32c5.py
517

@@ -39,4 +51,27 @@ var defESP32C5 = &chipDef{
3951
},
4052

4153
FlashSizes: defaultFlashSizes(),
54+
55+
PostConnect: esp32c5PostConnect,
56+
}
57+
58+
// esp32c5PostConnect detects the USB interface type and disables watchdogs
59+
// when connected via USB-JTAG/Serial. Without this, the LP WDT fires
60+
// during flash and resets the chip mid-operation.
61+
// Reference: esptool/targets/esp32c5.py _post_connect()
62+
func esp32c5PostConnect(f *Flasher) error {
63+
uartDev, err := f.ReadRegister(esp32c5UARTDevBufNo)
64+
if err != nil {
65+
// In secure download mode, the register may be unreadable.
66+
// Default to non-USB behavior (safe fallback).
67+
return nil
68+
}
69+
70+
if uartDev == esp32c5UARTDevBufNoUSBJTAGSerial {
71+
f.usesUSB = true
72+
f.logf("USB-JTAG/Serial interface detected, disabling watchdogs")
73+
return disableWatchdogsLP(f, esp32c5LPWDTConfig0, esp32c5LPWDTWProtect, esp32c5LPWDTSWDConf, esp32c5LPWDTSWDWProtect)
74+
}
75+
76+
return nil
4277
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package espflasher
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestESP32C5PostConnectUSBJTAG(t *testing.T) {
12+
writeCount := 0
13+
readCount := 0
14+
15+
mc := &mockConnection{
16+
readRegFunc: func(addr uint32) (uint32, error) {
17+
readCount++
18+
if addr == esp32c5UARTDevBufNo {
19+
return esp32c5UARTDevBufNoUSBJTAGSerial, nil
20+
}
21+
// Return 0 for SWD conf read
22+
return 0, nil
23+
},
24+
writeRegFunc: func(addr, value, mask, delayUS uint32) error {
25+
writeCount++
26+
return nil
27+
},
28+
}
29+
f := &Flasher{
30+
conn: mc,
31+
opts: &FlasherOptions{},
32+
}
33+
34+
err := esp32c5PostConnect(f)
35+
require.NoError(t, err)
36+
assert.True(t, f.usesUSB, "usesUSB should be true for USB-JTAG/Serial")
37+
assert.Greater(t, writeCount, 0, "should have written registers to disable watchdog")
38+
}
39+
40+
func TestESP32C5PostConnectUART(t *testing.T) {
41+
mc := &mockConnection{
42+
readRegFunc: func(addr uint32) (uint32, error) {
43+
return 0, nil // Not USB, return UART value
44+
},
45+
}
46+
f := &Flasher{
47+
conn: mc,
48+
opts: &FlasherOptions{},
49+
}
50+
51+
err := esp32c5PostConnect(f)
52+
require.NoError(t, err)
53+
assert.False(t, f.usesUSB, "usesUSB should be false for UART")
54+
}
55+
56+
func TestESP32C5PostConnectSecureMode(t *testing.T) {
57+
// Simulate read error (secure download mode)
58+
mc := &mockConnection{
59+
readRegFunc: func(addr uint32) (uint32, error) {
60+
return 0, errors.New("register not readable") // Simulate unreadable register
61+
},
62+
}
63+
f := &Flasher{
64+
conn: mc,
65+
opts: &FlasherOptions{},
66+
}
67+
68+
err := esp32c5PostConnect(f)
69+
require.NoError(t, err, "should gracefully handle read error")
70+
assert.False(t, f.usesUSB, "should default to non-USB on read error")
71+
}

0 commit comments

Comments
 (0)