Skip to content

Commit 0ae267c

Browse files
authored
add support to select the alt boot pin between PA5(HD0) and PA13(SCK) with -b option (#13)
* feat: select alt boot pin between PA5 and PA13 for wch56x * chore: update README.md with -b option
1 parent 89e8359 commit 0ae267c

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Usage: wch-ch56x-isp [OPTION]...
3434
-p, --progress Display progress
3535
-v, --verify Do verify after erase/program
3636
-r, --reset Reset MCU at end
37+
-b, --altbootpin=VALUE Set alt boot pin on(PA13) or off(PA5), will INVALID the image inside
3738
-d, --mcudebug=VALUE Set MCU debug mode on or off
3839
-f, --flashfile=VALUE Flash file (binary file *.bin)
3940
```

wch-ch56x-isp.c

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ static struct cag_option options[] =
161161
.value_name = NULL,
162162
.description = "Reset MCU at end"
163163
},
164+
{
165+
.identifier = 'b',
166+
.access_letters = "b",
167+
.access_name = "altbootpin",
168+
.value_name = "VALUE",
169+
.description = "Set alt boot pin on(PA13) or off(PA5), will INVALID the image inside"
170+
},
164171
{
165172
.identifier = 'd',
166173
.access_letters = "d",
@@ -188,6 +195,9 @@ struct cag_configuration
188195
int do_verify;
189196
int do_show_config;
190197

198+
int do_alt_boot_pin;
199+
const char* alt_boot_pin;
200+
191201
int do_mcu_debug;
192202
const char* mcu_debug_mode;
193203

@@ -529,9 +539,39 @@ cmd_read_conf(u16 cfgmask, size_t len, u8 *cfg)
529539
return len;
530540
}
531541

542+
u8 cmd_alt_boot_pin(u8 enable)
543+
{
544+
u8 req[] = { CFG_MASK_ALL, 0x00 };
545+
546+
u8 buf[60];
547+
size_t got;
548+
549+
#ifdef DEBUG
550+
printf("cmd_enable_alt_boot_pin isp_send_cmd()\n");
551+
#endif
552+
isp_send_cmd(CMD_READ_CONFIG, sizeof(req), req);
553+
got = isp_recv_cmd(CMD_READ_CONFIG, sizeof(buf), buf);
554+
if (got < 2)
555+
die("read conf fail: not received enough bytes\n");
556+
557+
buf[0] = CFG_MASK_RDPR_USER_DATA_WPR;
558+
buf[6] = enable ? 0xf0 : 0xf2;
559+
buf[9] = enable ? 0x0f : 0x0d;
560+
561+
isp_send_cmd(CMD_WRITE_CONFIG, 14, buf);
562+
got = usb_recv(6, buf);
563+
if (got != 6)
564+
die("send config command: wrong response length\n");
565+
566+
#ifdef DEBUG
567+
print_hex(buf, 6);
568+
#endif
569+
return buf[4] == 0; // success if this byte is zero
570+
}
571+
532572
u8 cmd_debug_mode(u8 enable)
533573
{
534-
u8 req[] = { 0x1f, 0x00 };
574+
u8 req[] = { CFG_MASK_ALL, 0x00 };
535575
const u8 enable_debug_req = 0xe5;
536576
const u8 disable_debug_req = 0x45;
537577

@@ -561,7 +601,7 @@ u8 cmd_debug_mode(u8 enable)
561601

562602
u8 cmd_set_flash_size(size_t flash_file_size_bytes)
563603
{
564-
u8 req[] = { 0x1f, 0x00 };
604+
u8 req[] = { CFG_MASK_ALL, 0x00 };
565605
u8 buf[60];
566606
size_t got;
567607
u8 flash_size_config_kb;
@@ -911,10 +951,14 @@ static void
911951
ch569_print_config(const char* message, size_t len, const u8 *cfg)
912952
{
913953
u32 nv;
954+
u32 alt_boot_pin;
914955

915956
if (len < 12)
916957
return;
917958

959+
alt_boot_pin = !!(((~cfg[4]) & cfg[7]) & BIT(1));
960+
printf("alt boot pin: %s\n", (char*[]){"PA5","PA13"}[alt_boot_pin]);
961+
918962
nv = (cfg[8] << 0) | (cfg[9] << 8) | (cfg[10] << 16) | (cfg[11] << 24);
919963
printf("%s nv=0x%08X\n", message, nv);
920964
printf("[4] RESET_EN %d: %s\n", !!(nv & BIT(4)),
@@ -982,6 +1026,10 @@ main(int argc, char *argv[])
9821026
case 'c':
9831027
config.do_show_config = 1;
9841028
break;
1029+
case 'b':
1030+
config.do_alt_boot_pin = 1;
1031+
config.alt_boot_pin = cag_option_get_value(&context);
1032+
break;
9851033
case 'd':
9861034
config.do_mcu_debug = 1;
9871035
config.mcu_debug_mode = cag_option_get_value(&context);
@@ -1006,6 +1054,42 @@ main(int argc, char *argv[])
10061054
if (argc < 1)
10071055
die("missing command\n");
10081056

1057+
if(config.do_alt_boot_pin)
1058+
{
1059+
if(config.alt_boot_pin == NULL)
1060+
die("Invalid alt boot bin option (only on or off are supported)\n");
1061+
1062+
if (!((dev_id == 0x69) || (dev_id == 0x65)))
1063+
die("alt boot bin feature is currently only available for the CH565/569!");
1064+
1065+
if(strcmp(config.alt_boot_pin, "on") == 0)
1066+
{
1067+
if (cmd_alt_boot_pin(1))
1068+
{
1069+
printf_timing("successfully enabled alt boot bin\n");
1070+
}
1071+
else
1072+
{
1073+
printf_timing("failed to enable alt boot bin\n");
1074+
}
1075+
}
1076+
else if(strcmp(config.alt_boot_pin, "off") == 0)
1077+
{
1078+
if (cmd_alt_boot_pin(0))
1079+
{
1080+
printf_timing("successfully disabled alt boot bin\n");
1081+
}
1082+
else
1083+
{
1084+
printf_timing("failed to disable alt boot bin\n");
1085+
}
1086+
}
1087+
else
1088+
{
1089+
die("Invalid alt boot bin option (only on or off are supported)\n");
1090+
}
1091+
}
1092+
10091093
if(config.do_mcu_debug)
10101094
{
10111095
if(config.mcu_debug_mode == NULL)

0 commit comments

Comments
 (0)