From bed6aab084d56a12efed90cf4eb2538c689b481f Mon Sep 17 00:00:00 2001
From: joerg_wunsch <joerg_wunsch@81a1dc3b-b13d-400b-aceb-764788c761c2>
Date: Mon, 9 Oct 2006 09:56:10 +0000
Subject: [PATCH] Submitted by freckle@sf.net: * stk500.c (stk500_paged_write):
 Send the command and the data payload within a single write(). patch #5025:
 Improve stk500.c robustness

Submitted by Matthias Ringwald:
* stk500.c (stk500_open): do not flush the serial line after
getting in sync with the programmer.
patch #5293: stk500.c: no drain after sync (-> allow BTnode
Bootloader to work on cygwin)


git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@670 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog | 13 +++++++++++++
 stk500.c  | 25 +++++++++++++------------
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2a5800af..159dc169 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2006-10-09 Joerg Wunsch <j@uriah.heep.sax.de>
+
+	Submitted by freckle@sf.net:
+	* stk500.c (stk500_paged_write): Send the command and the data
+	payload within a single write().
+	patch #5025: Improve stk500.c robustness
+
+	Submitted by Matthias Ringwald:
+	* stk500.c (stk500_open): do not flush the serial line after
+	getting in sync with the programmer.
+	patch #5293: stk500.c: no drain after sync (-> allow BTnode
+	Bootloader to work on cygwin)
+
 2006-09-29 Joerg Wunsch <j@uriah.heep.sax.de>
 
 	* pgm.h: Fix prototype for gettimeofday().
diff --git a/stk500.c b/stk500.c
index a17f301b..c5ed1ba6 100644
--- a/stk500.c
+++ b/stk500.c
@@ -590,8 +590,6 @@ static int stk500_open(PROGRAMMER * pgm, char * port)
   if (stk500_getsync(pgm) < 0)
     return -1;
 
-  stk500_drain(pgm, 0);
-
   return 0;
 }
 
@@ -656,13 +654,14 @@ static int stk500_loadaddr(PROGRAMMER * pgm, unsigned int addr)
 static int stk500_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m, 
                               int page_size, int n_bytes)
 {
-  unsigned char buf[16];
+  unsigned char buf[page_size + 16];
   int memtype;
   unsigned int addr;
   int a_div;
   int block_size;
   int tries;
   unsigned int n;
+  unsigned int i;
   int flash;
 
   if (page_size == 0) {
@@ -728,16 +727,18 @@ static int stk500_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
   retry:
     tries++;
     stk500_loadaddr(pgm, addr/a_div);
-    buf[0] = Cmnd_STK_PROG_PAGE;
-    buf[1] = (block_size >> 8) & 0xff;
-    buf[2] = block_size & 0xff;
-    buf[3] = memtype;
-    stk500_send(pgm, buf, 4);
 
-	stk500_send(pgm, &m->buf[addr], block_size);
-
-    buf[0] = Sync_CRC_EOP;
-    stk500_send(pgm, buf, 1);
+    /* build command block and avoid multiple send commands as it leads to a crash
+        of the silabs usb serial driver on mac os x */
+    i = 0;
+    buf[i++] = Cmnd_STK_PROG_PAGE;
+    buf[i++] = (block_size >> 8) & 0xff;
+    buf[i++] = block_size & 0xff;
+    buf[i++] = memtype;
+    memcpy(&buf[i], &m->buf[addr], block_size);
+    i += block_size;
+    buf[i++] = Sync_CRC_EOP;
+    stk500_send( pgm, buf, i);
 
     if (stk500_recv(pgm, buf, 1) < 0)
       exit(1);