Skip to content

Commit

Permalink
riscv32: add ch32v003 target probe
Browse files Browse the repository at this point in the history
  • Loading branch information
perigoso authored and Rafael Silva committed Jan 23, 2025
1 parent e088f65 commit dbb40bb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/target/ch32vx.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
#include "target_internal.h"
#include "buffer_utils.h"

/*
* IDCODE register
* [31:16] - REVID
* [15:0] - DEVID
*/
#define CH32V003X_IDCODE 0x1ffff7c4U

/* IDCODE register */
#define CH32VX_IDCODE 0x1ffff704U
#define CH32VX_IDCODE_MASK 0x0ffffff0f
Expand Down Expand Up @@ -61,6 +68,35 @@ static void ch32vx_read_uid(target_s *const t, uint8_t *const uid)
write_be4(uid, uid_reg_offset, target_mem32_read32(t, CH32VX_ESIG_UID1 + uid_reg_offset));
}

bool ch32v003x_probe(target_s *const target)
{
const uint32_t idcode = target_mem32_read32(target, CH32V003X_IDCODE);

switch (idcode & CH32VX_IDCODE_MASK) {
case 0x00300500U: /* CH32V003F4P6 */
case 0x00310500U: /* CH32V003F4U6 */
case 0x00320500U: /* CH32V003A4M6 */
case 0x00330500U: /* CH32V003J4M6 */
break;
default:
DEBUG_INFO("Unrecognized CH32V003x IDCODE: 0x%08" PRIx32 "\n", idcode);
return false;
break;
}

target->driver = "CH32V003";

const size_t flash_size = ch32vx_read_flash_size(target);
DEBUG_INFO("CH32V003x flash size: %zu\n", flash_size);
(void)flash_size;

target->part_id = idcode;

target_add_commands(target, ch32vx_cmd_list, "CH32Vx");

return true;
}

bool ch32vx_probe(target_s *const target)
{
const uint32_t idcode = target_mem32_read32(target, CH32VX_IDCODE);
Expand Down
1 change: 1 addition & 0 deletions src/target/riscv32.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ bool riscv32_probe(target_s *const target)
default:
break;
case JEP106_MANUFACTURER_WCH:
PROBE(ch32v003x_probe);
PROBE(ch32vx_probe);
break;
}
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 @@ -104,6 +104,7 @@ CORTEXM_PROBE_WEAK_NOP(rp2040_rescue_probe)
TARGET_PROBE_WEAK_NOP(apollo_3_probe)
TARGET_PROBE_WEAK_NOP(at32f40x_probe)
TARGET_PROBE_WEAK_NOP(at32f43x_probe)
TARGET_PROBE_WEAK_NOP(ch32v003x_probe)
TARGET_PROBE_WEAK_NOP(ch32vx_probe)
TARGET_PROBE_WEAK_NOP(ch32f1_probe)
TARGET_PROBE_WEAK_NOP(ch579_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 @@ -58,6 +58,7 @@ bool at32f43x_probe(target_s *target);
bool ch32f1_probe(target_s *target); // will catch all the clones
bool ch579_probe(target_s *target);
bool efm32_probe(target_s *target);
bool ch32v003x_probe(target_s *target);
bool ch32vx_probe(target_s *target);
bool gd32f1_probe(target_s *target);
bool gd32f4_probe(target_s *target);
Expand Down

0 comments on commit dbb40bb

Please sign in to comment.