From 8aa50632adb1dff6eacf5a78b998b8def29b4c38 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Sat, 16 Mar 2013 23:36:47 +0000 Subject: Samples: More fxload improvements and cleanup * Type detection when bus,addr is specified * Improved check for FX3 image header * Switch back to using -p for bus,addr and use a comma to match the output of listdevs --- examples/fxload.c | 114 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 51 deletions(-) (limited to 'examples/fxload.c') diff --git a/examples/fxload.c b/examples/fxload.c index d69eea4..8537ca2 100644 --- a/examples/fxload.c +++ b/examples/fxload.c @@ -64,6 +64,17 @@ void logerror(const char *format, ...) va_end(ap); } +static int print_usage(int errcode) { + fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-p bus,addr] -i firmware\n"); + fprintf(stderr, " -i -- Firmware to upload\n"); + fprintf(stderr, " -t -- Target type: an21, fx, fx2, fx2lp, fx3\n"); + fprintf(stderr, " -d -- Target device, as an USB VID:PID\n"); + fprintf(stderr, " -p -- Target device, as a libusbx bus number and device address path\n"); + fprintf(stderr, " -v -- Increase verbosity\n"); + fprintf(stderr, " -V -- Print program version\n"); + return errcode; +} + #define FIRMWARE 0 #define LOADER 1 int main(int argc, char*argv[]) @@ -78,26 +89,26 @@ int main(int argc, char*argv[]) int fx_type = FX_TYPE_UNDEFINED, img_type[ARRAYSIZE(path)]; int i, j, opt, status; unsigned vid = 0, pid = 0; - unsigned busnum = 0, devaddr = 0; + unsigned busnum = 0, devaddr = 0, _busnum, _devaddr; libusb_device *dev, **devs; libusb_device_handle *device = NULL; struct libusb_device_descriptor desc; - while ((opt = getopt(argc, argv, "vV?hd:a:i:I:t:")) != EOF) + while ((opt = getopt(argc, argv, "vV?hd:p:i:I:t:")) != EOF) switch (opt) { case 'd': device_id = optarg; if (sscanf(device_id, "%x:%x" , &vid, &pid) != 2 ) { - fputs ("please specify VID & PID as \"vid:pid\", in hexadecimal format\n", stderr); + fputs ("please specify VID & PID as \"vid:pid\" in hexadecimal format\n", stderr); return -1; } break; - case 'a': + case 'p': device_path = optarg; - if (sscanf(device_path, "%u:%u", &busnum, &devaddr) != 2 ) { - fputs ("please specify bus number & device address as \"bus:addr\", in decimal format\n", stderr); + if (sscanf(device_path, "%u,%u", &busnum, &devaddr) != 2 ) { + fputs ("please specify bus number & device number as \"bus,dev\" in decimal format\n", stderr); return -1; } break; @@ -122,21 +133,17 @@ int main(int argc, char*argv[]) case '?': case 'h': default: - goto usage; + return print_usage(-1); } if (path[FIRMWARE] == NULL) { logerror("no firmware specified!\n"); -usage: - fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-a bus:addr] -i firmware\n"); - fprintf(stderr, " -i -- Firmware to upload\n"); - fprintf(stderr, " -t -- Target type: an21, fx, fx2, fx2lp, fx3\n"); - fprintf(stderr, " -d -- Target device, as an USB VID:PID\n"); - fprintf(stderr, " -a -- Target device, as a libusbx bus and address\n"); - fprintf(stderr, " -v -- Increase verbosity\n"); - fprintf(stderr, " -V -- Print program version\n"); - return -1; + return print_usage(-1); + } + if ((device_id != NULL) && (device_path != NULL)) { + logerror("only one of -d or -a can be specified\n"); + return print_usage(-1); } /* determine the target type */ @@ -149,7 +156,7 @@ usage: } if (i >= FX_TYPE_MAX) { logerror("illegal microcontroller type: %s\n", type); - goto usage; + return print_usage(-1); } } @@ -162,53 +169,58 @@ usage: libusb_set_debug(NULL, verbose); /* try to pick up missing parameters from known devices */ - if (device_path || (type == NULL) || (device_id == NULL)) { + if ((type == NULL) || (device_id == NULL) || (device_path != NULL)) { if (libusb_get_device_list(NULL, &devs) < 0) { logerror("libusb_get_device_list() failed: %s\n", libusb_error_name(status)); goto err; } for (i=0; (dev=devs[i]) != NULL; i++) { - if (device_path) { + _busnum = libusb_get_bus_number(dev); + _devaddr = libusb_get_device_address(dev); + if ((type != NULL) && (device_path != NULL)) { + // if both a type and bus,addr were specified, we just need to find our match if ((libusb_get_bus_number(dev) == busnum) && (libusb_get_device_address(dev) == devaddr)) break; - continue; - } - status = libusb_get_device_descriptor(dev, &desc); - if (status >= 0) { - if (verbose >= 2) - logerror("trying to match against %04x:%04x\n", desc.idVendor, desc.idProduct); - for (j=0; j= 0) { + if (verbose >= 2) { + logerror("trying to match against %04x:%04x (%d,%d)\n", + desc.idVendor, desc.idProduct, _busnum, _devaddr); + } + for (j=0; j