From 688b628018675a4f30f2cda37623412e9374d149 Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Thu, 7 Jun 2012 14:07:17 +0000
Subject: [PATCH] * usbtiny.c (usbtiny_paged_load, usbtiny_paged_write): fix
 breakage introduced by the recent page handling reorg; it used to cause an
 infinite loop

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk@1091 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 avrdude/ChangeLog |  6 ++++++
 avrdude/usbtiny.c | 39 ++++++++++++++++-----------------------
 2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/avrdude/ChangeLog b/avrdude/ChangeLog
index a2999f19..052aa607 100644
--- a/avrdude/ChangeLog
+++ b/avrdude/ChangeLog
@@ -1,3 +1,9 @@
+2012-06-07  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	* usbtiny.c (usbtiny_paged_load, usbtiny_paged_write):
+	fix breakage introduced by the recent page handling reorg;
+	it used to cause an infinite loop
+
 2012-05-04  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	Xmega page erase implementation for XPROG (AVRISPmkII, STK600)
diff --git a/avrdude/usbtiny.c b/avrdude/usbtiny.c
index 887e2c3e..fa73da71 100644
--- a/avrdude/usbtiny.c
+++ b/avrdude/usbtiny.c
@@ -432,9 +432,9 @@ static void usbtiny_disable ( PROGRAMMER* pgm ) {}
 */
 static int usbtiny_paged_load (PROGRAMMER * pgm, AVRPART * p, AVRMEM* m,
                                unsigned int page_size,
-                               unsigned int i, unsigned int n_bytes)
+                               unsigned int addr, unsigned int n_bytes)
 {
-  unsigned int maxaddr = i + n_bytes;
+  unsigned int maxaddr = addr + n_bytes;
   int chunk;
   int function;
 
@@ -446,20 +446,16 @@ static int usbtiny_paged_load (PROGRAMMER * pgm, AVRPART * p, AVRMEM* m,
     function = USBTINY_EEPROM_READ;
   }
 
-  for (; i < maxaddr; i += chunk) {
+  for (; addr < maxaddr; addr += chunk) {
     chunk = PDATA(pgm)->chunk_size;         // start with the maximum chunk size possible
 
-    // If we want to xmit less than a chunk, thats OK
-    if	(chunk > n_bytes-i)
-      chunk = n_bytes - i;
-
     // Send the chunk of data to the USBtiny with the function we want
     // to perform
     if (usb_in(pgm,
 	       function,          // EEPROM or flash
 	       0,                 // delay between SPI commands
-	       i,                 // index
-	       m->buf + i,        // pointer to where we store data
+	       addr,              // address in memory
+	       m->buf + addr,     // pointer to where we store data
 	       chunk,             // number of bytes
 	       32 * PDATA(pgm)->sck_period)  // each byte gets turned into a 4-byte SPI cmd
 	< 0) {
@@ -479,9 +475,9 @@ static int usbtiny_paged_load (PROGRAMMER * pgm, AVRPART * p, AVRMEM* m,
 */
 static int usbtiny_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
                                unsigned int page_size,
-                               unsigned int i, unsigned int n_bytes)
+                               unsigned int addr, unsigned int n_bytes)
 {
-  unsigned int maxaddr = i + n_bytes;
+  unsigned int maxaddr = addr + n_bytes;
   int chunk;        // Size of data to write at once
   int next;
   int function;     // which SPI command to use
@@ -496,14 +492,15 @@ static int usbtiny_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
 
   delay = 0;
   if (! m->paged) {
+    unsigned int poll_value;
     // Does this chip not support paged writes?
-    i = (m->readback[1] << 8) | m->readback[0];
-    if (usb_control(pgm, USBTINY_POLL_BYTES, i, 0 ) < 0)
+    poll_value = (m->readback[1] << 8) | m->readback[0];
+    if (usb_control(pgm, USBTINY_POLL_BYTES, poll_value, 0 ) < 0)
       return -1;
     delay = m->max_write_delay;
   }
 
-  for (; i < maxaddr; i=next) {
+  for (; addr < maxaddr; addr += chunk) {
     // start with the max chunk size
     chunk = PDATA(pgm)->chunk_size;
 
@@ -511,15 +508,11 @@ static int usbtiny_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
     if (m->paged && chunk > page_size)
       chunk = page_size;
 
-    // if there's less data remaining than one chunk
-    if (chunk > n_bytes-i)
-      chunk = n_bytes - i;
-
     if (usb_out(pgm,
 		function,       // Flash or EEPROM
 		delay,          // How much to wait between each byte
-		i,              // Index of data
-		m->buf + i,     // Pointer to data
+		addr,           // Address in memory
+		m->buf + addr,  // Pointer to data
 		chunk,          // Number of bytes to write
 		32 * PDATA(pgm)->sck_period + delay  // each byte gets turned into a
 	                             // 4-byte SPI cmd  usb_out() multiplies
@@ -528,11 +521,11 @@ static int usbtiny_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
       return -1;
     }
 
-    next = i + chunk;       // Calculate what address we're at now
+    next = addr + chunk;       // Calculate what address we're at now
     if (m->paged
-	&& ((next % page_size) == 0 || next == n_bytes) ) {
+	&& ((next % page_size) == 0 || next == maxaddr) ) {
       // If we're at a page boundary, send the SPI command to flush it.
-      avr_write_page(pgm, p, m, (unsigned long) i);
+      avr_write_page(pgm, p, m, (unsigned long) addr);
     }
   }
   return n_bytes;