From bb808e8d5fb438dcc736e511a0d54118cf7c2dfb Mon Sep 17 00:00:00 2001
From: Jan Egil Ruud <janegil.ruud@micrchip.com>
Date: Thu, 1 Dec 2022 15:16:23 +0100
Subject: [PATCH] Skip chaching when size of memory is 0.

---
 src/avrcache.c | 18 +++++++++++-------
 src/jtag3.c    |  6 +-----
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/avrcache.c b/src/avrcache.c
index a0fa9082..ebb8fd89 100644
--- a/src/avrcache.c
+++ b/src/avrcache.c
@@ -343,7 +343,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) {
   for(size_t i = 0; i < sizeof mems/sizeof*mems; i++) {
     AVRMEM *mem = mems[i].mem;
     AVR_Cache *cp = mems[i].cp;
-    if(!mem || !cp->cont)
+    if(!mem || !cp->cont || !cp->size)
       continue;
 
     for(int pgno = 0, n = 0; n < cp->size; pgno++, n += cp->page_size) {
@@ -367,6 +367,8 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) {
     AVRMEM *mem = mems[i].mem;
     AVR_Cache *cp = mems[i].cp;
 
+    if(!cp->size)
+      continue;
     if(!cp->cont)           // Ensure cache is initialised from now on
       if(initCache(cp, pgm, p) < 0) {
         if(quell_progress)
@@ -419,7 +421,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) {
     for(size_t i = 0; i < sizeof mems/sizeof*mems; i++) {
       AVRMEM *mem = mems[i].mem;
       AVR_Cache *cp = mems[i].cp;
-      if(!mem)
+      if(!mem || !cp->size)
         continue;
 
       for(int pgno = 0, n = 0; n < cp->size; pgno++, n += cp->page_size)
@@ -433,7 +435,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) {
       for(size_t i = 0; i < sizeof mems/sizeof*mems; i++) {
         AVRMEM *mem = mems[i].mem;
         AVR_Cache *cp = mems[i].cp;
-        if(!mem)
+        if(!mem || !cp->size)
           continue;
 
         for(int ird = 0, pgno = 0, n = 0; n < cp->size; pgno++, n += cp->page_size) {
@@ -460,7 +462,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) {
     for(size_t i = 0; i < sizeof mems/sizeof*mems; i++) {
       AVRMEM *mem = mems[i].mem;
       AVR_Cache *cp = mems[i].cp;
-      if(!mem)
+      if(!mem || !cp->size)
         continue;
 
       if(mems[i].isflash) {     // flash
@@ -508,7 +510,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) {
   for(size_t i = 0; i < sizeof mems/sizeof*mems; i++) {
     AVRMEM *mem = mems[i].mem;
     AVR_Cache *cp = mems[i].cp;
-    if(!mem)
+    if(!mem || !cp->size)
       continue;
 
     for(int pgno = 0, n = 0; n < cp->size; pgno++, n += cp->page_size)
@@ -522,7 +524,7 @@ int avr_flush_cache(const PROGRAMMER *pgm, const AVRPART *p) {
     for(size_t i = 0; i < sizeof mems/sizeof*mems; i++) {
       AVRMEM *mem = mems[i].mem;
       AVR_Cache *cp = mems[i].cp;
-      if(!mem || !cp->cont)
+      if(!mem || !cp->cont || !cp->size)
         continue;
 
       for(int iwr = 0, pgno = 0, n = 0; n < cp->size; pgno++, n += cp->page_size) {
@@ -644,7 +646,7 @@ int avr_chip_erase_cached(const PROGRAMMER *pgm, const AVRPART *p) {
     AVRMEM *mem = mems[i].mem;
     AVR_Cache *cp = mems[i].cp;
 
-    if(!mem || !avr_has_paged_access(pgm, mem))
+    if(!mem || !cp->size || !avr_has_paged_access(pgm, mem))
       continue;
 
     if(!cp->cont)               // Init cache if needed
@@ -734,6 +736,8 @@ int avr_reset_cache(const PROGRAMMER *pgm, const AVRPART *p) {
 
   for(size_t i = 0; i < sizeof mems/sizeof*mems; i++) {
     AVR_Cache *cp = mems[i];
+    if(!cp->size)
+      continue;
     if(cp->cont)
       free(cp->cont);
     if(cp->copy)
diff --git a/src/jtag3.c b/src/jtag3.c
index 7b004e74..481e9783 100644
--- a/src/jtag3.c
+++ b/src/jtag3.c
@@ -2509,10 +2509,6 @@ unsigned char tpi_get_memtype(const AVRMEM *mem) {
   return memtype;
 }
 
-// void tpi_print_error_status(unsigned char error) {
-//   pmsg_error("error communicating with programmer, received status 0x%02x\n", error);
-// }
-
 /*
  * Send the data as a JTAGICE3 encapsulated TPI packet.
  */
@@ -2589,7 +2585,7 @@ int jtag3_command_tpi(const PROGRAMMER *pgm, unsigned char *cmd, unsigned int cm
  * initialize the AVR device and prepare it to accept commands
  */
 static int jtag3_initialize_tpi(const PROGRAMMER *pgm, const AVRPART *p) {
-  unsigned char cmd[4];
+  unsigned char cmd[3];
   unsigned char* resp;
   int status;