diff --git a/minichlink/ardulink.c b/minichlink/ardulink.c index cd28635be0d3b4999302c21056d41fb392400c6a..a070a26f540d05b58f982ea92cf758495aefc47a 100644 --- a/minichlink/ardulink.c +++ b/minichlink/ardulink.c @@ -37,7 +37,7 @@ int ArdulinkWriteReg32(void * dev, uint8_t reg_7_bit, uint32_t command) if (serial_dev_read(&((ardulink_ctx_t*)dev)->serial, buf, 1) == -1) return -errno; - return buf[0] == '+' ? 0 : -EPROTO; + return buf[0] == '+' ? 0 : -71; // EPROTO } int ArdulinkReadReg32(void * dev, uint8_t reg_7_bit, uint32_t * commandresp) @@ -78,9 +78,9 @@ int ArdulinkControl3v3(void * dev, int power_on) { return -errno; if (c != '+') - return -EPROTO; + return -71; // EPROTO - usleep(20000); + MCF.DelayUS(dev, 20000); return 0; } @@ -97,11 +97,26 @@ int ArdulinkExit(void * dev) return 0; } +int ArdulinkSetupInterface( void * dev ) +{ + char first; + // Let the bootloader do its thing. + MCF.DelayUS(dev, 3UL*1000UL*1000UL); + + if (serial_dev_read(&((ardulink_ctx_t*)dev)->serial, &first, 1) == -1) { + perror("read"); + return -1; + } + + if (first != '!') { + fprintf(stderr, "Ardulink: not the sync character.\n"); + return -1; + } +} void * TryInit_Ardulink(const init_hints_t* hints) { ardulink_ctx_t *ctx; - char first; if (!(ctx = calloc(sizeof(ardulink_ctx_t), 1))) { perror("calloc"); @@ -143,19 +158,6 @@ void * TryInit_Ardulink(const init_hints_t* hints) return NULL; } - // Let the bootloader do its thing. - usleep(3UL*1000UL*1000UL); - - if (serial_dev_read(&ctx->serial, &first, 1) == -1) { - perror("read"); - return NULL; - } - - if (first != '!') { - fprintf(stderr, "Ardulink: not the sync character.\n"); - return NULL; - } - fprintf(stderr, "Ardulink: synced.\n"); MCF.WriteReg32 = ArdulinkWriteReg32; @@ -164,6 +166,7 @@ void * TryInit_Ardulink(const init_hints_t* hints) MCF.Control3v3 = ArdulinkControl3v3; MCF.DelayUS = ArdulinkDelayUS; MCF.Exit = ArdulinkExit; + MCF.SetupInterface = ArdulinkSetupInterface; return ctx; } diff --git a/minichlink/minichlink.c b/minichlink/minichlink.c index 16860c2cc1d0540702c707f2e0797847b0e7119d..0abb1ff5441d6c34c3593b22a156d198a2628e00 100644 --- a/minichlink/minichlink.c +++ b/minichlink/minichlink.c @@ -71,37 +71,30 @@ void * MiniCHLinkInitAsDLL( struct MiniChlinkFunctions ** MCFO, const init_hints return dev; } -void parse_possible_init_hints(int argc, char **argv, init_hints_t *hints) -{ - if (!hints) - return; - int c; - opterr = 0; - /* we're only interested in the value for the COM port, given in a -c parameter */ - /* the '-' is really important so that getopt does not permutate the argv array and messes up parsing later */ - while ((c = getopt(argc, argv, "-c:")) != -1) - { - switch (c) - { - case 'c': - // we can use the pointer as-is because it points in our - // argv array and that is stable. - hints->serial_port = optarg; - break; - } - } -} - #if !defined( MINICHLINK_AS_LIBRARY ) && !defined( MINICHLINK_IMPORT ) int main( int argc, char ** argv ) { + int i; + if( argc > 1 && argv[1][0] == '-' && argv[1][1] == 'h' ) { goto help; } init_hints_t hints; memset(&hints, 0, sizeof(hints)); - parse_possible_init_hints(argc, argv, &hints); + + // Scan for possible hints. + for( i = 0; i < argc; i++ ) + { + char * v = argv[i]; + if( strncmp( v, "-c", 2 ) == 0 ) + { + i++; + if( i < argc ) + hints.serial_port = argv[i]; + } + } + void * dev = MiniCHLinkInitAsDLL( 0, &hints ); if( !dev ) { diff --git a/minichlink/minichlink.exe b/minichlink/minichlink.exe index a52fdbc65872c0fc70a1facfb7fa9cbe22a35552..d3404712091bdd8dcc125d58deeb556c528a3c93 100644 Binary files a/minichlink/minichlink.exe and b/minichlink/minichlink.exe differ diff --git a/minichlink/serial_dev.h b/minichlink/serial_dev.h index a75733f41870308408ce0b2f5b82d20ef7ccfe54..7c7e4f1eea1f1b369b08f0508739112a84c60ca5 100644 --- a/minichlink/serial_dev.h +++ b/minichlink/serial_dev.h @@ -9,6 +9,7 @@ #define IS_WINDOWS #define DEFAULT_SERIAL_NAME "\\\\.\\COM3" #else +#include <unistd.h> #include <termios.h> #include <fcntl.h> #include <sys/ioctl.h> @@ -17,7 +18,6 @@ #endif /* these are available on all platforms */ #include <errno.h> -#include <unistd.h> #include <stdio.h> typedef struct { diff --git a/minichlink/winbuild.bat b/minichlink/winbuild.bat index 785472441f6d5d1db88cd6282afe61fc9f21a39b..9c4095bc4ce65dcea556e50f2dc1a094cc04f303 100644 --- a/minichlink/winbuild.bat +++ b/minichlink/winbuild.bat @@ -1 +1 @@ -tcc minichlink.c pgm-esp32s2-ch32xx.c pgm-wch-linke.c minichgdb.c nhc-link042.c -DWIN32 -lws2_32 -lsetupapi libusb-1.0.dll +tcc minichlink.c pgm-esp32s2-ch32xx.c serial_dev.c ardulink.c pgm-b003fun.c pgm-wch-linke.c minichgdb.c nhc-link042.c -DWIN32 -lws2_32 -lsetupapi libusb-1.0.dll