diff --git a/buspirate_spi.c b/buspirate_spi.c index c56155f77c5f8571afd600062876361bfe2bc308..9890d48297a4c93f41edb4e328c1afb2d3996ad9 100644 --- a/buspirate_spi.c +++ b/buspirate_spi.c @@ -22,6 +22,7 @@ #include <string.h> #include <stdlib.h> #include <ctype.h> +#include <unistd.h> #include "flash.h" #include "chipdrivers.h" #include "programmer.h" @@ -141,6 +142,20 @@ int buspirate_spi_init(void) /* Read any response and discard it. */ sp_flush_incoming(); } + /* USB is slow. The Bus Pirate is even slower. Apparently the flush + * action above is too fast or too early. Some stuff still remains in + * the pipe after the flush above, and one additional flush is not + * sufficient either. Use a 1.5 ms delay inside the loop to make + * mostly sure that at least one USB frame had time to arrive. + * Looping only 5 times is not sufficient and causes the + * ocassional failure. + * Folding the delay into the loop above is not reliable either. + */ + for (i = 0; i < 10; i++) { + usleep(1500); + /* Read any response and discard it. */ + sp_flush_incoming(); + } /* Enter raw bitbang mode */ buf[0] = 0x00; ret = buspirate_sendrecv(buf, 1, 5); @@ -148,6 +163,8 @@ int buspirate_spi_init(void) return ret; if (memcmp(buf, "BBIO", 4)) { msg_perr("Entering raw bitbang mode failed!\n"); + msg_pdbg("Got %02x%02x%02x%02x%02x\n", + buf[0], buf[1], buf[2], buf[3], buf[4]); return 1; } msg_pdbg("Raw bitbang mode version %c\n", buf[4]); @@ -159,8 +176,12 @@ int buspirate_spi_init(void) /* Enter raw SPI mode */ buf[0] = 0x01; ret = buspirate_sendrecv(buf, 1, 4); + if (ret) + return ret; if (memcmp(buf, "SPI", 3)) { msg_perr("Entering raw SPI mode failed!\n"); + msg_pdbg("Got %02x%02x%02x%02x\n", + buf[0], buf[1], buf[2], buf[3]); return 1; } msg_pdbg("Raw SPI mode version %c\n", buf[3]); diff --git a/serial.c b/serial.c index caf9389943a2abfae4a60465bdc97e44a4c03dc7..9957fb462072a87226e08df36ef7623faf3570d3 100644 --- a/serial.c +++ b/serial.c @@ -200,8 +200,10 @@ int serialport_write(unsigned char *buf, unsigned int writecnt) #else tmp = write(sp_fd, buf, writecnt); #endif - if (tmp == -1) + if (tmp == -1) { + msg_perr("Serial port write error!\n"); return 1; + } if (!tmp) msg_pdbg("Empty write\n"); writecnt -= tmp; @@ -221,8 +223,10 @@ int serialport_read(unsigned char *buf, unsigned int readcnt) #else tmp = read(sp_fd, buf, readcnt); #endif - if (tmp == -1) + if (tmp == -1) { + msg_perr("Serial port read error!\n"); return 1; + } if (!tmp) msg_pdbg("Empty read\n"); readcnt -= tmp;