Skip to content

Commit a525058

Browse files
committed
Use libhidapi for connecting to SNAP/PICKit4 in AVR mode
1 parent 6c23fcc commit a525058

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

src/pickit5.c

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,79 @@ static int pickit5_open(PROGRAMMER *pgm, const char *port) {
627627
my.pgm_type = PGM_TYPE_SNAP;
628628
}
629629

630-
return rv;
630+
// No PICkit4 / SNAP (PIC mode) nor PICkit5 found, look for programmer in AVR mode
631+
if(rv >= 0) // if a programmer in PIC mode found, we're done
632+
return rv;
633+
634+
// otherwise, try to see if there is a SNAP or PK4 in AVR mode
635+
pinfo.usbinfo.vid = USB_VENDOR_ATMEL;
636+
pinfo.usbinfo.pid = USB_DEVICE_SNAP_AVR_MODE;
637+
638+
pgm->fd.usb.max_xfer = USBDEV_MAX_XFER_3;
639+
pgm->fd.usb.rep = USBDEV_BULK_EP_READ_3;
640+
pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_3;
641+
pgm->fd.usb.eep = USBDEV_EVT_EP_READ_3;
642+
643+
const char *pgm_suffix = strchr(pgmid, '_')? strchr(pgmid, '_'): "";
644+
char part_option[32] = {}; // 32 should be enough
645+
646+
if(partdesc)
647+
snprintf(part_option, 31, "-p %s ", partdesc);
648+
else
649+
strcpy(part_option, "");
650+
651+
// Use LIBHIDAPI to connect to SNAP/PICkit4 if present.
652+
// For some reason, chances are smaller to get
653+
// permission denied errors when using LIBHIDAPI
654+
#if defined(HAVE_LIBHIDAPI)
655+
serdev = &usbhid_serdev;
656+
pgm->fd.usb.eep = 0;
657+
#endif
658+
659+
rv = serial_open(port, pinfo, &pgm->fd); // Try SNAP PID
660+
661+
if(rv >= 0) {
662+
msg_error("\n");
663+
cx->usb_access_error = 0;
664+
665+
pmsg_error("MPLAB SNAP in AVR mode detected\n");
666+
667+
imsg_error("to switch into PIC mode try\n");
668+
imsg_error("$ %s -c snap%s %s-P %s -x mode=pic\n", progname, pgm_suffix, part_option, port);
669+
imsg_error("or use the programmer in AVR mode with the following command:\n");
670+
imsg_error("$ %s -c snap%s %s-P %s\n", progname, pgm_suffix, part_option, port);
671+
672+
serial_close(&pgm->fd);
673+
return LIBAVRDUDE_EXIT;
674+
}
675+
pinfo.usbinfo.flags = PINFO_FL_SILENT;
676+
pgm->fd.usb.max_xfer = USBDEV_MAX_XFER_3;
677+
pgm->fd.usb.rep = USBDEV_BULK_EP_READ_3;
678+
pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_3;
679+
pgm->fd.usb.eep = USBDEV_EVT_EP_READ_3;
680+
pinfo.usbinfo.pid = USB_DEVICE_PICKIT4_AVR_MODE;
681+
rv = serial_open(port, pinfo, &pgm->fd); // Try PICkit4 PID
682+
683+
if(rv >= 0) {
684+
msg_error("\n");
685+
cx->usb_access_error = 0;
686+
687+
pmsg_error("PICkit 4 in AVR mode detected\n");
688+
689+
imsg_error("to switch into PIC mode try\n");
690+
imsg_error("$ %s -c pickit4%s %s-P %s -x mode=pic\n", progname, pgm_suffix, part_option, port);
691+
imsg_error("or use the programmer in AVR mode with the following command:\n");
692+
imsg_error("$ %s -c pickit4%s %s-P %s\n", progname, pgm_suffix, part_option, port);
693+
694+
serial_close(&pgm->fd);
695+
return LIBAVRDUDE_EXIT;
696+
}
697+
698+
pmsg_error("no device found matching VID 0x%04x and PID list: 0x%04x, 0x%04x, 0x%04x\n", USB_VENDOR_MICROCHIP,
699+
USB_DEVICE_PICKIT5, USB_DEVICE_PICKIT4_PIC_MODE, USB_DEVICE_SNAP_PIC_MODE);
700+
imsg_error("nor VID 0x%04x with PID list: 0x%04x, 0x%04x\n", USB_VENDOR_ATMEL, USB_DEVICE_PICKIT4_AVR_MODE, USB_DEVICE_SNAP_AVR_MODE);
701+
702+
return LIBAVRDUDE_EXIT;
631703
}
632704

633705
static void pickit5_close(PROGRAMMER *pgm) {

0 commit comments

Comments
 (0)