Implement and mcp2210 spi transfer function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/mcp2210.c b/mcp2210.c
index 2f653f4..b9ee4ff 100644
--- a/mcp2210.c
+++ b/mcp2210.c
@@ -291,3 +291,42 @@ mcp2210_set_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int bitrate,
buf[20] = spimode;
return mcp2210_send_recv(cgpu, buf, C_MCP_SETSPISETTING);
}
+
+/* Perform an spi transfer of *length bytes and return the amount of data
+ * returned in the same buffer in *length */
+bool mcp2210_spi_transfer(struct cgpu_info *cgpu, char *data, unsigned int *length)
+{
+ char buf[MCP2210_BUFFER_LENGTH];
+ uint8_t res;
+
+ if (unlikely(*length > 60 || !*length)) {
+ applog(LOG_ERR, "%s %d: Unable to spi transfer %u bytes", cgpu->drv->name,
+ cgpu->device_id, *length);
+ return false;
+ }
+retry:
+ memset(buf, 0, MCP2210_BUFFER_LENGTH);
+ buf[0] = MCP2210_SPI_TRANSFER;
+ buf[1] = *length;
+
+ memcpy(buf + 4, data, *length);
+ if (!mcp2210_send_recv(cgpu, buf, C_MCP_SPITRANSFER))
+ return false;
+
+ res = (uint8_t)buf[1];
+ switch(res) {
+ case MCP2210_SPI_TRANSFER_SUCCESS:
+ *length = buf[2];
+ if (*length)
+ memcpy(data, buf + 4, *length);
+ return true;
+ case MCP2210_SPI_TRANSFER_ERROR_IP:
+ cgsleep_ms(40);
+ goto retry;
+ case MCP2210_SPI_TRANSFER_ERROR_NA:
+ applog(LOG_WARNING, "%s %d: External owner error on mcp2210 spi transfer",
+ cgpu->drv->name, cgpu->device_id);
+ default:
+ return false;
+ }
+}
diff --git a/mcp2210.h b/mcp2210.h
index d8510b4..b65cac5 100644
--- a/mcp2210.h
+++ b/mcp2210.h
@@ -60,5 +60,6 @@ bool
mcp2210_set_spi_transfer_settings(struct cgpu_info *cgpu, unsigned int bitrate, unsigned int icsv,
unsigned int acsv, unsigned int cstdd, unsigned int ldbtcsd,
unsigned int sdbd, unsigned int bpst, unsigned int spimode);
+bool mcp2210_spi_transfer(struct cgpu_info *cgpu, char *data, unsigned int *length);
#endif /* MCP2210_H */