From 5fec3915e3aa88c96b6201dad84ebf2771bb22f4 Mon Sep 17 00:00:00 2001 From: bmx <none> Date: Mon, 8 May 2023 10:34:22 +0200 Subject: [PATCH] Better detection of wch programmer and version also check if the target is connected, instead of reading nonsense in the void and cherry on top, check for -h flag at the top to avoid plugging something to get help Signed-off-by: bmx <none> --- minichlink/minichlink.c | 16 ++++++++++------ minichlink/pgm-wch-linke.c | 26 ++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/minichlink/minichlink.c b/minichlink/minichlink.c index 018b49e..7462f27 100644 --- a/minichlink/minichlink.c +++ b/minichlink/minichlink.c @@ -23,22 +23,22 @@ void * MiniCHLinkInitAsDLL( struct MiniChlinkFunctions ** MCFO ) void * dev = 0; if( (dev = TryInit_WCHLinkE()) ) { - fprintf( stderr, "Found WCH LinkE\n" ); + fprintf( stderr, "Found WCH Link\n" ); } else if( (dev = TryInit_ESP32S2CHFUN()) ) { fprintf( stderr, "Found ESP32S2 Programmer\n" ); } - else if ((dev = TryInit_NHCLink042())) - { - fprintf( stderr, "Found NHC-Link042 Programmer\n" ); - } + else if ((dev = TryInit_NHCLink042())) + { + fprintf( stderr, "Found NHC-Link042 Programmer\n" ); + } else { fprintf( stderr, "Error: Could not initialize any supported programmers\n" ); return 0; } - + SetupAutomaticHighLevelFunctions( dev ); if( MCFO ) { @@ -50,6 +50,10 @@ void * MiniCHLinkInitAsDLL( struct MiniChlinkFunctions ** MCFO ) #if !defined( MINICHLINK_AS_LIBRARY ) && !defined( MINICHLINK_IMPORT ) int main( int argc, char ** argv ) { + if (argv[1][0] == '-' && argv[1][1] == 'h') + { + goto help; + } void * dev = MiniCHLinkInitAsDLL( 0 ); if( !dev ) { diff --git a/minichlink/pgm-wch-linke.c b/minichlink/pgm-wch-linke.c index 530f366..fcdaa83 100644 --- a/minichlink/pgm-wch-linke.c +++ b/minichlink/pgm-wch-linke.c @@ -190,16 +190,38 @@ static int LESetupInterface( void * d ) uint32_t transferred = 0; // This puts the processor on hold to allow the debugger to run. - wch_link_command( dev, "\x81\x0d\x01\x03", 4, 0, 0, 0 ); // Reply: Ignored, 820d050900300500 + wch_link_command( dev, "\x81\x0d\x01\x03", 4, (int*)&transferred, rbuff, 1024 ); // Reply: Ignored, 820d050900300500 // Place part into reset. wch_link_command( dev, "\x81\x0d\x01\x01", 4, (int*)&transferred, rbuff, 1024 ); // Reply is: "\x82\x0d\x04\x02\x08\x02\x00" + switch(rbuff[5]) { + case 1: + fprintf(stderr, "WCH Programmer is CH549 version %d.%d\n",rbuff[3], rbuff[4]); + break; + case 2: + fprintf(stderr, "WCH Programmer is CH32V307 version %d.%d\n",rbuff[3], rbuff[4]); + break; + case 3: + fprintf(stderr, "WCH Programmer is CH32V203 version %d.%d\n",rbuff[3], rbuff[4]); + break; + case 4: + fprintf(stderr, "WCH Programmer is LinkB version %d.%d\n",rbuff[3], rbuff[4]); + break; + default: + fprintf(stderr, "Unknown WCH Programmer %02x\n", rbuff[5]); + return -1; + } // TODO: What in the world is this? It doesn't appear to be needed. wch_link_command( dev, "\x81\x0c\x02\x09\x01", 5, 0, 0, 0 ); //Reply is: 820c0101 // This puts the processor on hold to allow the debugger to run. - wch_link_command( dev, "\x81\x0d\x01\x02", 4, 0, 0, 0 ); // Reply: Ignored, 820d050900300500 + wch_link_command( dev, "\x81\x0d\x01\x02", 4, (int*)&transferred, rbuff, 1024 ); // Reply: Ignored, 820d050900300500 + if (rbuff[0] == 0x81 && rbuff[1] == 0x55 && rbuff[2] == 0x01 && rbuff[3] == 0x01) + { + fprintf(stderr, "link error, nothing connected to linker\n"); + return -1; + } // For some reason, if we don't do this sometimes the programmer starts in a hosey mode. MCF.WriteReg32( d, DMCONTROL, 0x80000001 ); // Make the debug module work properly. -- GitLab