Skip to content

Commit

Permalink
riscv32: add ch32vx target probe
Browse files Browse the repository at this point in the history
  • Loading branch information
perigoso committed Feb 23, 2023
1 parent fba1ab9 commit 095148d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/target/ch32f1.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ static bool ch32f1_flash_write(target_flash_s *f, target_addr_t dest, const void
#define FLASH_SR_EOP (1U << 5U) // End of programming
#define FLASH_BEGIN_ADDRESS_CH32 0x8000000U

/* RISC-V targets CH32Vx */
#define CH32VX_CHIPID 0x1ffff704U
#define CH32VX_CHIPID_FAMILY_OFFSET 20U
#define CH32VX_CHIPID_FAMILY_MASK (0xfffU << CH32VX_CHIPID_FAMILY_OFFSET)

/* "fast" Flash driver for CH32F10x chips */
static void ch32f1_add_flash(target_s *t, uint32_t addr, size_t length, size_t erasesize)
{
Expand Down Expand Up @@ -215,6 +220,63 @@ bool ch32f1_probe(target_s *t)
return true;
}

bool ch32vx_probe(target_s *t)
{
/* CHIPID table
* CH32V303CBT6: 0x303305x4
* CH32V303RBT6: 0x303205x4
* CH32V303RCT6: 0x303105x4
* CH32V303VCT6: 0x303005x4
* CH32V305FBP6: 0x305205x8
* CH32V305RBT6: 0x305005x8
* CH32V307WCU6: 0x307305x8
* CH32V307FBP6: 0x307205x8
* CH32V307RCT6: 0x307105x8
* CH32V307VCT6: 0x307005x8
*/

const uint32_t chipid = target_mem_read32(t, CH32VX_CHIPID);
switch (chipid & 0xffffff0f) {
case 0x30330504U: /* CH32V303CBT6 */
case 0x30320504U: /* CH32V303RBT6 */
case 0x30310504U: /* CH32V303RCT6 */
case 0x30300504U: /* CH32V303VCT6 */
case 0x30520508U: /* CH32V305FBP6 */
case 0x30500508U: /* CH32V305RBT6 */
case 0x30730508U: /* CH32V307WCU6 */
case 0x30720508U: /* CH32V307FBP6 */
case 0x30710508U: /* CH32V307RCT6 */
case 0x30700508U: /* CH32V307VCT6 */
break;
default:
return false;
break;
}

DEBUG_WARN("CH32V CHIPID 0x%" PRIx32 " \n", chipid);

const uint16_t family = (chipid & CH32VX_CHIPID_FAMILY_MASK) >> CH32VX_CHIPID_FAMILY_OFFSET;
switch (family) {
case 0x303U:
t->driver = "CH32V303";
break;
case 0x305U:
t->driver = "CH32V305";
break;
case 0x307U:
t->driver = "CH32V307";
break;
default:
return false;
break;
}
DEBUG_WARN("CH32V family 0x%" PRIx32 " \n", family);

t->part_id = chipid;

return true;
}

/* Fast erase of CH32 devices */
bool ch32f1_flash_erase(target_flash_s *f, target_addr_t addr, size_t len)
{
Expand Down
3 changes: 3 additions & 0 deletions src/target/riscv32.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ bool riscv32_probe(target_s *const target)
case JEP106_MANUFACTURER_RV_GIGADEVICE:
PROBE(gd32vf1_probe);
break;
case JEP106_MANUFACTURER_WCH:
PROBE(ch32vx_probe);
break;
}
#if PC_HOSTED == 0
gdb_outf("Please report unknown device with Designer 0x%x\n", target->designer_code);
Expand Down
1 change: 1 addition & 0 deletions src/target/target_probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ CORTEXM_PROBE_WEAK_NOP(nrf51_mdm_probe)
CORTEXM_PROBE_WEAK_NOP(efm32_aap_probe)
CORTEXM_PROBE_WEAK_NOP(rp_rescue_probe)

TARGET_PROBE_WEAK_NOP(ch32vx_probe)
TARGET_PROBE_WEAK_NOP(ch32f1_probe)
TARGET_PROBE_WEAK_NOP(gd32f1_probe)
TARGET_PROBE_WEAK_NOP(gd32vf1_probe)
Expand Down
1 change: 1 addition & 0 deletions src/target/target_probe.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ bool nrf51_mdm_probe(adiv5_access_port_s *ap);
bool efm32_aap_probe(adiv5_access_port_s *ap);
bool rp_rescue_probe(adiv5_access_port_s *ap);

bool ch32vx_probe(target_s *t);
bool ch32f1_probe(target_s *t); // will catch all the clones
bool at32fxx_probe(target_s *t); // STM32 clones from Artery
bool mm32l0xx_probe(target_s *t);
Expand Down

0 comments on commit 095148d

Please sign in to comment.