Browse Source

nanofury: Attempt to be more resilient to problems

Luke Dashjr 12 years ago
parent
commit
67207e1451
3 changed files with 37 additions and 2 deletions
  1. 25 2
      driver-nanofury.c
  2. 11 0
      mcp2210.c
  3. 1 0
      mcp2210.h

+ 25 - 2
driver-nanofury.c

@@ -55,6 +55,8 @@ bool nanofury_spi_reset(struct mcp2210_device * const mcp)
 	return true;
 }
 
+static void nanofury_device_off(struct mcp2210_device *);
+
 static
 bool nanofury_spi_txrx(struct spi_port * const port)
 {
@@ -73,7 +75,7 @@ bool nanofury_spi_txrx(struct spi_port * const port)
 	while (bufsz >= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER)
 	{
 		if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, NANOFURY_MAX_BYTES_PER_SPI_TRANSFER))
-			return false;
+			goto err;
 		ptrrdbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
 		ptrwrbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
 		bufsz -= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
@@ -83,10 +85,16 @@ bool nanofury_spi_txrx(struct spi_port * const port)
 	if (bufsz > 0)
 	{
 		if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, bufsz))
-			return false;
+			goto err;
 	}
 	
 	return true;
+
+err:
+	mcp2210_spi_cancel(mcp);
+	nanofury_device_off(mcp);
+	hashes_done2(thr, -1, NULL);
+	return false;
 }
 
 static
@@ -119,6 +127,9 @@ bool nanofury_checkport(struct mcp2210_device * const mcp)
 	if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, MGV_HIGH))
 		goto fail;
 	
+	// cancel any outstanding SPI transfers
+	mcp2210_spi_cancel(mcp);
+	
 	// configure SPI
 	// This is the only place where speed, mode and other settings are configured!!!
 	if (!mcp2210_configure_spi(mcp, 200000, 0xffff, 0xffef, 0, 0, 0))
@@ -302,6 +313,17 @@ void nanofury_enable(struct thr_info * const thr)
 	bitfury_enable(thr);
 }
 
+static
+void nanofury_reinit(struct cgpu_info * const cgpu)
+{
+	struct thr_info * const thr = cgpu->thr[0];
+	struct mcp2210_device * const mcp = thr->cgpu_data;
+	
+	nanofury_device_off(mcp);
+	cgsleep_ms(1);
+	nanofury_enable(thr);
+}
+
 static
 void nanofury_shutdown(struct thr_info * const thr)
 {
@@ -318,6 +340,7 @@ struct device_drv nanofury_drv = {
 	.thread_init = nanofury_init,
 	.thread_disable = nanofury_disable,
 	.thread_enable = nanofury_enable,
+	.reinit_device = nanofury_reinit,
 	.thread_shutdown = nanofury_shutdown,
 	
 	.minerloop = minerloop_async,

+ 11 - 0
mcp2210.c

@@ -250,6 +250,17 @@ retry:
 	}
 }
 
+bool mcp2210_spi_cancel(struct mcp2210_device * const h)
+{
+	hid_device * const hid = h->hid;
+	uint8_t cmd[0x41] = {0,0x11}, buf[0x40];
+	
+	if (!mcp2210_io(hid, cmd, buf))
+		return false;
+	
+	return (buf[1] == 0);
+}
+
 static
 bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
 {

+ 1 - 0
mcp2210.h

@@ -20,6 +20,7 @@ struct mcp2210_device;
 extern struct mcp2210_device *mcp2210_open(struct lowlevel_device_info *);
 extern void mcp2210_close(struct mcp2210_device *);
 
+extern bool mcp2210_spi_cancel(struct mcp2210_device *);
 extern bool mcp2210_configure_spi(struct mcp2210_device *, uint32_t bitrate, uint16_t idlechipsel, uint16_t activechipsel, uint16_t chipseltodatadelay, uint16_t lastbytetocsdelay, uint16_t midbytedelay);
 extern bool mcp2210_set_spimode(struct mcp2210_device *, uint8_t spimode);
 extern bool mcp2210_spi_transfer(struct mcp2210_device *, const void *tx, void *rx, uint8_t sz);