From 982af16f726bc85898a503f3c819b89bd8b0c56a Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Thu, 23 Oct 2025 12:07:08 +0200 Subject: [PATCH] Simplify micronucleus -P port handling --- src/micronucleus.c | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/src/micronucleus.c b/src/micronucleus.c index 7f015c20..d2e0a5b7 100644 --- a/src/micronucleus.c +++ b/src/micronucleus.c @@ -577,34 +577,21 @@ static int micronucleus_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { } static int micronucleus_open(PROGRAMMER *pgm, const char *port) { - pmsg_debug("micronucleus_open(\"%s\")\n", port); + pmsg_debug("%s(\"%s\")\n", __func__, port); if(pgm->bitclock) pmsg_warning("-c %s does not support adjustable bitclock speed; ignoring -B\n", pgmid); struct pdata *pdata = &my; - const char *bus_name = NULL; - char *dev_name = NULL; + const char *bus_name = NULL, *dev_name = NULL; - // If no -P was given or '-P usb' was given - if(str_eq(port, "usb")) { - port = NULL; - } else { - // Calculate bus and device names from -P option - if(str_starts(port, "usb") && ':' == port[3]) { - bus_name = port + 4; - dev_name = strchr(bus_name, ':'); - if(dev_name != NULL) { - *dev_name = '\0'; - dev_name++; - } - } + // Calculate bus and device names from -P usb:: option if present + if(str_starts(port, "usb:")) { + bus_name = port + 4; + if((dev_name = strchr(bus_name, ':'))) + dev_name++; } - if(port != NULL && dev_name == NULL) { - pmsg_error("invalid -P %s; use -P usb:bus:device\n", port); - return -1; - } // Determine VID/PID int vid = pgm->usbvid? pgm->usbvid: MICRONUCLEUS_VID; int pid = MICRONUCLEUS_PID; @@ -652,15 +639,13 @@ static int micronucleus_open(PROGRAMMER *pgm, const char *port) { continue; } - pmsg_notice("found device with Micronucleus V%d.%d, bus:device: %s:%s\n", + pmsg_notice("found device with Micronucleus V%d.%d, bus:device = %s:%s\n", pdata->major_version, pdata->minor_version, bus->dirname, device->filename); - // If -P was given, match device by device name and bus name - if(port != NULL) { - if(dev_name == NULL || !str_eq(bus->dirname, bus_name) || !str_eq(device->filename, dev_name)) { + // If -P usb:: was given, skip non-matching bus:device + if(bus_name && dev_name) + if(!str_busdev_eq(bus->dirname, bus_name) || !str_busdev_eq(device->filename, dev_name)) continue; - } - } if(pdata->major_version > MICRONUCLEUS_MAX_MAJOR_VERSION) { pmsg_warning("device with unsupported Micronucleus version V%d.%d\n", @@ -697,6 +682,11 @@ static int micronucleus_open(PROGRAMMER *pgm, const char *port) { break; } + if(bus_name && !dev_name) { // Delayed error message, so found devices are printed with -P usb:xyz + pmsg_error("invalid -P %s; use -P usb::\n", port); + return -1; + } + if(!pdata->usb_handle) { pmsg_error("cannot find device with Micronucleus bootloader (%04X:%04X)\n", vid, pid); return -1;