From 173b4f9d0ac90924764d8c3174ad3e3ab934a0cd Mon Sep 17 00:00:00 2001
From: Jan Egil Ruud <janegil.ruud@microchip.com>
Date: Mon, 11 Jul 2022 14:07:45 +0200
Subject: [PATCH 1/3] Clean up and simplify hvupdi handling, and set default
 hvupdi_variant to -1.

---
 src/avrpart.c |  1 +
 src/jtag3.c   | 13 ++++---------
 2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/src/avrpart.c b/src/avrpart.c
index dc6def44..5a192e2d 100644
--- a/src/avrpart.c
+++ b/src/avrpart.c
@@ -573,6 +573,7 @@ AVRPART * avr_new_part(void)
   memset(p->signature, 0xFF, 3);
   p->ctl_stack_type = CTL_STACK_NONE;
   p->ocdrev = -1;
+  p->hvupdi_variant = -1;
 
   p->mem = lcreat(NULL, 0);
   p->mem_alias = lcreat(NULL, 0);
diff --git a/src/jtag3.c b/src/jtag3.c
index 1c003a82..ae5d1912 100644
--- a/src/jtag3.c
+++ b/src/jtag3.c
@@ -1259,8 +1259,7 @@ static int jtag3_initialize(PROGRAMMER * pgm, AVRPART * p)
         PDATA(pgm)->use_hvupdi == true &&
         p->hvupdi_variant != HV_UPDI_VARIANT_1) {
       for (hvupdi_support = lfirst(pgm->hvupdi_support); hvupdi_support != NULL; hvupdi_support = lnext(hvupdi_support)) {
-        unsigned int sup = (unsigned int)(*(int *)(ldata(hvupdi_support)));
-        if(sup == p->hvupdi_variant) {
+        if(*(int *) ldata(hvupdi_support) == p->hvupdi_variant) {
           avrdude_message(MSG_NOTICE, "%s: Sending HV pulse to targets %s pin\n",
             progname, p->hvupdi_variant == HV_UPDI_VARIANT_0 ? "UPDI" : "RESET");
           parm[0] = PARM3_UPDI_HV_SIMPLE_PULSE;
@@ -1496,8 +1495,7 @@ static int jtag3_parseextparms(PROGRAMMER * pgm, LISTID extparms)
       continue;
     }
 
-    else if ((matches(extended_param, "hvupdi") || matches(extended_param, "hvupdi=1")) &&
-      (matches(ldata(lfirst(pgm->id)), "pickit4_updi") || matches(ldata(lfirst(pgm->id)), "powerdebugger_updi"))) {
+    else if (matches(extended_param, "hvupdi")) {
       PDATA(pgm)->use_hvupdi = true;
       continue;
     }
@@ -1650,12 +1648,9 @@ static int jtag3_open_updi(PROGRAMMER * pgm, char * port)
   avrdude_message(MSG_NOTICE2, "%s: jtag3_open_updi()\n", progname);
 
   LNODEID ln;
-  unsigned int hv_sup;
   avrdude_message(MSG_NOTICE2, "%s: HV UPDI support:", progname);
-  for (ln = lfirst(pgm->hvupdi_support); ln; ln = lnext(ln)) {
-    hv_sup = (unsigned int)(*(int *)ldata(ln));
-    avrdude_message(MSG_NOTICE2, " %d", hv_sup);
-  }
+  for (ln = lfirst(pgm->hvupdi_support); ln; ln = lnext(ln))
+    avrdude_message(MSG_NOTICE2, " %d", *(int *) ldata(ln));
   avrdude_message(MSG_NOTICE2, "\n", progname);
 
   if (jtag3_open_common(pgm, port) < 0)

From 2478c1874591693ccff4c689e39238687bc2bc93 Mon Sep 17 00:00:00 2001
From: Jan Egil Ruud <janegil.ruud@microchip.com>
Date: Mon, 11 Jul 2022 15:26:41 +0200
Subject: [PATCH 2/3] Do not let the hvupdi extended option take any
 configuration values. The hvupdi type is implied by the part configuration.

---
 src/jtag3.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/jtag3.c b/src/jtag3.c
index ae5d1912..f07e0319 100644
--- a/src/jtag3.c
+++ b/src/jtag3.c
@@ -1495,7 +1495,7 @@ static int jtag3_parseextparms(PROGRAMMER * pgm, LISTID extparms)
       continue;
     }
 
-    else if (matches(extended_param, "hvupdi")) {
+    else if (strcmp(extended_param, "hvupdi") == 0) {
       PDATA(pgm)->use_hvupdi = true;
       continue;
     }

From 69ee5da6130471d62bf170735b7f82cc93128107 Mon Sep 17 00:00:00 2001
From: Jan Egil Ruud <janegil.ruud@microchip.com>
Date: Tue, 12 Jul 2022 12:01:17 +0200
Subject: [PATCH 3/3] Improve error handling for devices that does not support
 HVUPDI.

---
 src/jtag3.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/jtag3.c b/src/jtag3.c
index f07e0319..becfd8a1 100644
--- a/src/jtag3.c
+++ b/src/jtag3.c
@@ -1254,17 +1254,21 @@ static int jtag3_initialize(PROGRAMMER * pgm, AVRPART * p)
     }
 
     // Generate UPDI high-voltage pulse if user asks for it and hardware supports it
-    LNODEID hvupdi_support;
+    LNODEID support;
     if (p->flags & AVRPART_HAS_UPDI &&
         PDATA(pgm)->use_hvupdi == true &&
         p->hvupdi_variant != HV_UPDI_VARIANT_1) {
-      for (hvupdi_support = lfirst(pgm->hvupdi_support); hvupdi_support != NULL; hvupdi_support = lnext(hvupdi_support)) {
-        if(*(int *) ldata(hvupdi_support) == p->hvupdi_variant) {
+      parm[0] = PARM3_UPDI_HV_NONE;
+      for (support = lfirst(pgm->hvupdi_support); support != NULL; support = lnext(support)) {
+        if(*(int *) ldata(support) == p->hvupdi_variant) {
           avrdude_message(MSG_NOTICE, "%s: Sending HV pulse to targets %s pin\n",
             progname, p->hvupdi_variant == HV_UPDI_VARIANT_0 ? "UPDI" : "RESET");
           parm[0] = PARM3_UPDI_HV_SIMPLE_PULSE;
           break;
         }
+        if (parm[0] == PARM3_UPDI_HV_NONE)
+          avrdude_message(MSG_INFO, "%s: %s does not support sending HV pulse to target %s\n",
+            progname, pgm->desc, p->desc);
       }
       if (jtag3_setparm(pgm, SCOPE_AVR, 3, PARM3_OPT_12V_UPDI_ENABLE, parm, 1) < 0)
         return -1;
@@ -1495,7 +1499,8 @@ static int jtag3_parseextparms(PROGRAMMER * pgm, LISTID extparms)
       continue;
     }
 
-    else if (strcmp(extended_param, "hvupdi") == 0) {
+    else if ((strcmp(extended_param, "hvupdi") == 0) &&
+             (lsize(pgm->hvupdi_support) > 1)) {
       PDATA(pgm)->use_hvupdi = true;
       continue;
     }