Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit 095148d

Browse files
committed
riscv32: add ch32vx target probe
1 parent fba1ab9 commit 095148d

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/target/ch32f1.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ static bool ch32f1_flash_write(target_flash_s *f, target_addr_t dest, const void
6565
#define FLASH_SR_EOP (1U << 5U) // End of programming
6666
#define FLASH_BEGIN_ADDRESS_CH32 0x8000000U
6767

68+
/* RISC-V targets CH32Vx */
69+
#define CH32VX_CHIPID 0x1ffff704U
70+
#define CH32VX_CHIPID_FAMILY_OFFSET 20U
71+
#define CH32VX_CHIPID_FAMILY_MASK (0xfffU << CH32VX_CHIPID_FAMILY_OFFSET)
72+
6873
/* "fast" Flash driver for CH32F10x chips */
6974
static void ch32f1_add_flash(target_s *t, uint32_t addr, size_t length, size_t erasesize)
7075
{
@@ -215,6 +220,63 @@ bool ch32f1_probe(target_s *t)
215220
return true;
216221
}
217222

223+
bool ch32vx_probe(target_s *t)
224+
{
225+
/* CHIPID table
226+
* CH32V303CBT6: 0x303305x4
227+
* CH32V303RBT6: 0x303205x4
228+
* CH32V303RCT6: 0x303105x4
229+
* CH32V303VCT6: 0x303005x4
230+
* CH32V305FBP6: 0x305205x8
231+
* CH32V305RBT6: 0x305005x8
232+
* CH32V307WCU6: 0x307305x8
233+
* CH32V307FBP6: 0x307205x8
234+
* CH32V307RCT6: 0x307105x8
235+
* CH32V307VCT6: 0x307005x8
236+
*/
237+
238+
const uint32_t chipid = target_mem_read32(t, CH32VX_CHIPID);
239+
switch (chipid & 0xffffff0f) {
240+
case 0x30330504U: /* CH32V303CBT6 */
241+
case 0x30320504U: /* CH32V303RBT6 */
242+
case 0x30310504U: /* CH32V303RCT6 */
243+
case 0x30300504U: /* CH32V303VCT6 */
244+
case 0x30520508U: /* CH32V305FBP6 */
245+
case 0x30500508U: /* CH32V305RBT6 */
246+
case 0x30730508U: /* CH32V307WCU6 */
247+
case 0x30720508U: /* CH32V307FBP6 */
248+
case 0x30710508U: /* CH32V307RCT6 */
249+
case 0x30700508U: /* CH32V307VCT6 */
250+
break;
251+
default:
252+
return false;
253+
break;
254+
}
255+
256+
DEBUG_WARN("CH32V CHIPID 0x%" PRIx32 " \n", chipid);
257+
258+
const uint16_t family = (chipid & CH32VX_CHIPID_FAMILY_MASK) >> CH32VX_CHIPID_FAMILY_OFFSET;
259+
switch (family) {
260+
case 0x303U:
261+
t->driver = "CH32V303";
262+
break;
263+
case 0x305U:
264+
t->driver = "CH32V305";
265+
break;
266+
case 0x307U:
267+
t->driver = "CH32V307";
268+
break;
269+
default:
270+
return false;
271+
break;
272+
}
273+
DEBUG_WARN("CH32V family 0x%" PRIx32 " \n", family);
274+
275+
t->part_id = chipid;
276+
277+
return true;
278+
}
279+
218280
/* Fast erase of CH32 devices */
219281
bool ch32f1_flash_erase(target_flash_s *f, target_addr_t addr, size_t len)
220282
{

src/target/riscv32.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ bool riscv32_probe(target_s *const target)
101101
case JEP106_MANUFACTURER_RV_GIGADEVICE:
102102
PROBE(gd32vf1_probe);
103103
break;
104+
case JEP106_MANUFACTURER_WCH:
105+
PROBE(ch32vx_probe);
106+
break;
104107
}
105108
#if PC_HOSTED == 0
106109
gdb_outf("Please report unknown device with Designer 0x%x\n", target->designer_code);

src/target/target_probe.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ CORTEXM_PROBE_WEAK_NOP(nrf51_mdm_probe)
7575
CORTEXM_PROBE_WEAK_NOP(efm32_aap_probe)
7676
CORTEXM_PROBE_WEAK_NOP(rp_rescue_probe)
7777

78+
TARGET_PROBE_WEAK_NOP(ch32vx_probe)
7879
TARGET_PROBE_WEAK_NOP(ch32f1_probe)
7980
TARGET_PROBE_WEAK_NOP(gd32f1_probe)
8081
TARGET_PROBE_WEAK_NOP(gd32vf1_probe)

src/target/target_probe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bool nrf51_mdm_probe(adiv5_access_port_s *ap);
4040
bool efm32_aap_probe(adiv5_access_port_s *ap);
4141
bool rp_rescue_probe(adiv5_access_port_s *ap);
4242

43+
bool ch32vx_probe(target_s *t);
4344
bool ch32f1_probe(target_s *t); // will catch all the clones
4445
bool at32fxx_probe(target_s *t); // STM32 clones from Artery
4546
bool mm32l0xx_probe(target_s *t);

0 commit comments

Comments
 (0)