Commit 8304338c485d2f801fe7bcf833470f7e06da7e96

Con Kolivas 2014-01-06T17:19:23

Add bitfury detection loop to nanofury startup

diff --git a/driver-bitfury.c b/driver-bitfury.c
index bbde24b..ae8f5fd 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -412,7 +412,10 @@ static bool nf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
 		goto out;
 
 	info->osc6_bits = 54;
-	ret = nf1_reinit(bitfury, info);
+	if (!nf1_reinit(bitfury, info))
+		goto out;
+
+	ret = spi_detect_bitfury(bitfury, info);
 	if (ret) {
 		if (!add_cgpu(bitfury))
 			quit(1, "Failed to add_cgpu in nf1_detect_one");
diff --git a/driver-bitfury.h b/driver-bitfury.h
index 6e6eef6..9afb5b3 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -30,6 +30,8 @@ extern int opt_bxf_temp_target;
 #define NF1_PIN_PWR_EN 6
 
 #define SPIBUF_SIZE 16384
+#define BITFURY_REFRESH_DELAY 100
+#define BITFURY_DETECT_TRIES 3000 / BITFURY_REFRESH_DELAY
 
 struct bitfury_payload {
 	unsigned char midstate[32];
diff --git a/libbitfury.c b/libbitfury.c
index 06c6e62..6d27154 100644
--- a/libbitfury.c
+++ b/libbitfury.c
@@ -14,8 +14,6 @@
 #include "libbitfury.h"
 #include "sha2.h"
 
-#define BITFURY_REFRESH_DELAY 100
-
 void ms3steps(uint32_t *p)
 {
 	uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
@@ -202,7 +200,7 @@ void spi_send_init(struct bitfury_info *info)
 	ms3steps(atrvec);
 	ms3steps(&atrvec[20]);
 	ms3steps(&atrvec[40]);
-	memset(&w, 0, sizeof(w));
+	memset(w, 0, sizeof(w));
 	w[3] = 0xffffffff;
 	w[4] = 0x80000000;
 	w[15] = 0x00000280;
@@ -266,6 +264,31 @@ void spi_add_data(struct bitfury_info *info, uint16_t addr, const void *buf, int
 	spi_add_buf_reverse(info, buf, len);
 }
 
+bool spi_detect_bitfury(struct cgpu_info *bitfury, struct bitfury_info *info)
+{
+	unsigned int newbuf[17], oldbuf[17];
+	int i;
+
+	memset(newbuf, 0, 17 * 4);
+	memset(oldbuf, 0, 17 * 4);
+
+	for (i = 0; i < BITFURY_DETECT_TRIES; i++) {
+		spi_clear_buf(info);
+		spi_add_break(info);
+		spi_add_data(info, 0x3000, atrvec, 19 * 4);
+		if (!spi_txrx(bitfury, info))
+			return false;
+		memcpy(newbuf, info->spibuf + 4, 17 * 4);
+		if (newbuf[16] != 0 && newbuf[16] != 0xFFFFFFFF)
+			return false;
+		if (i && newbuf[16] != oldbuf[16])
+			return true;
+		cgsleep_ms(BITFURY_REFRESH_DELAY);
+		memcpy(oldbuf, newbuf, 17 * 4);
+	}
+	return false;
+}
+
 // Bit-banging reset... Each 3 reset cycles reset first chip in chain
 bool spi_reset(struct cgpu_info *bitfury, struct bitfury_info *info)
 {
diff --git a/libbitfury.h b/libbitfury.h
index 4b460df..b3c99ed 100644
--- a/libbitfury.h
+++ b/libbitfury.h
@@ -22,6 +22,7 @@ void spi_clear_buf(struct bitfury_info *info);
 void spi_add_buf(struct bitfury_info *info, const void *buf, const int sz);
 void spi_add_break(struct bitfury_info *info);
 void spi_add_data(struct bitfury_info *info, uint16_t addr, const void *buf, int len);
+bool spi_detect_bitfury(struct cgpu_info *bitfury, struct bitfury_info *info);
 bool spi_reset(struct cgpu_info *bitfury, struct bitfury_info *info);
 bool spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info);
 bool libbitfury_sendHashData(struct cgpu_info *bf);