Skip to content

Commit c98fc3c

Browse files
committed
Fix on windows + Allow bypass mode for ch32v006
1 parent eb474e0 commit c98fc3c

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

ch32fun/ch32fun.mk

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Default prefix for Windows
22
ifeq ($(OS),Windows_NT)
33
PREFIX?=riscv64-unknown-elf
4+
ifeq ($(shell which $(PREFIX)),)
5+
PREFIX:=riscv-none-elf
6+
endif
47
# Check if riscv64-unknown-elf-gcc exists
58
else ifneq ($(shell which riscv64-unknown-elf-gcc),)
69
PREFIX?=riscv64-unknown-elf

minichlink/minichlink.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ int main( int argc, char ** argv )
578578
}
579579
case 'w':
580580
{
581-
struct InternalState * iss = (struct InternalState*)(((struct ProgrammerStructBase*)dev)->internal);
581+
//struct InternalState * iss = (struct InternalState*)(((struct ProgrammerStructBase*)dev)->internal);
582582
if( argchar[2] != 0 ) goto help;
583583
iarg++;
584584
argchar = 0; // Stop advancing

minichlink/minichlink.exe

512 Bytes
Binary file not shown.

minichlink/pgm-wch-linke.c

+25-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ static void printChipInfo(enum RiscVChip chip) {
6363

6464
static int checkChip(enum RiscVChip chip) {
6565
switch(chip) {
66+
case CHIP_UNKNOWN:
67+
case CHIP_CH32V002:
68+
case CHIP_CH32V004:
6669
case CHIP_CH32V003:
70+
case CHIP_CH32V006:
71+
case CHIP_CH32V005:
6772
case CHIP_CH32X03x:
6873
return 0; // Use direct mode
6974
case CHIP_CH32V10x:
@@ -365,21 +370,29 @@ static int LESetupInterface( void * d )
365370
// My capture differs in this case: \x05 instead of \x09 -> But does not seem to be needed
366371
//wch_link_command( dev, "\x81\x0c\x02\x05\x01", 5, 0, 0, 0 ); //Reply is: 820c0101
367372

373+
int unknown_chip_fallback = 0;
374+
368375
// This puts the processor on hold to allow the debugger to run.
369376
int already_tried_reset = 0;
370377
int is_already_connected = 0;
371378
do
372379
{
373380
// Read DMSTATUS - in case we are a ch32x035, or other chip that does not respond to \x81\x0d\x01\x02.
374381
wch_link_command( dev, "\x81\x08\x06\x05\x11\x00\x00\x00\x00\x01", 11, (int*)&transferred, rbuff, 1024 ); // Reply: Ignored, 820d050900300500
375-
if( transferred == 9 && rbuff[8] != 0x02 && rbuff[8] != 0x03 && rbuff[8] != 0x00 )
382+
383+
// There's a couple situations where the older firmware on the official programmer freaks out.
384+
// We don't want to inconvenience our users, so just try to work through it by falling back to the minichlink functions.
385+
// Part connected. But the programmer doesn't know what it is.
386+
if( transferred == 9 && ( ( rbuff[4] == 0x00 ) ||
387+
( rbuff[8] != 0x02 && rbuff[8] != 0x03 && rbuff[8] != 0x00 ) ) )
376388
{
377389
// Already connected.
378390
if( is_already_connected )
379391
{
380392
printf( "Already Connected\n" );
381393
// Still need to read in the data so we can select the correct chip.
382394
wch_link_command( dev, "\x81\x0d\x01\x02", 4, (int*)&transferred, rbuff, 1024 ); // ?? this seems to work?
395+
unknown_chip_fallback = 1;
383396
break;
384397
}
385398
is_already_connected = 1;
@@ -394,8 +407,11 @@ static int LESetupInterface( void * d )
394407
fprintf(stderr, "link error, nothing connected to linker (%d = [%02x %02x %02x %02x]). Trying to put processor in hold and retrying.\n", transferred, rbuff[0], rbuff[1], rbuff[2], rbuff[3]);
395408

396409
// Give up if too long
397-
if( already_tried_reset > 10 )
398-
return -1;
410+
if( already_tried_reset > 5 )
411+
{
412+
unknown_chip_fallback = 1;
413+
break;
414+
}
399415

400416
wch_link_multicommands( (libusb_device_handle *)dev, 1, 4, "\x81\x0d\x01\x13" ); // Try forcing reset line low.
401417
wch_link_command( (libusb_device_handle *)dev, "\x81\x0d\x01\xff", 4, 0, 0, 0); //Exit programming
@@ -428,6 +444,12 @@ static int LESetupInterface( void * d )
428444
return -1;
429445
}
430446

447+
if( unknown_chip_fallback )
448+
{
449+
printf( "Unknown chip fallback\n" );
450+
chip = CHIP_UNKNOWN;
451+
}
452+
431453
printChipInfo(chip);
432454

433455
iss->target_chip_type = chip;

0 commit comments

Comments
 (0)