Patchwork Bus Pirate SPI fine tuning.

login
register
about
Submitter Brian Salcedo
Date 2013-01-02 01:28:41
Message ID <20130101202841.75a3fad9@nakia>
Download mbox | patch
Permalink /patch/3834/
State New
Headers show

Comments

Brian Salcedo - 2013-01-02 01:28:41
This negates my previous pull-up resistors patch.

Please see http://dangerousprototypes.com/docs/SPI_(binary) for more
information.

This patch adds additional fine tuning parameters for SPI on the Bus
Pirate. If documenting these parameters, please refer the reader to the
Bus Pirate documentation - not flashrom!

The original initialization parameters are retained. (0xb and 0xa
respectively)

Signed-off-by: Brian Salcedo <bsalcedo@gmx.us>

Patch

--- trunk/buspirate_spi.c	2013-01-01 19:30:38.492239798 -0500
+++ flashrom/buspirate_spi.c	2013-01-01 20:19:56.419854159 -0500
@@ -201,8 +201,18 @@ 
 
 int buspirate_spi_init(void)
 {
+	char bp_peripherals = 0xb;
+	char bp_spi_config = 0xa;
 	char *dev = NULL;
 	char *speed = NULL;
+	char *bp_power = NULL;
+	char *bp_pullups = NULL;
+	char *bp_aux = NULL;
+	char *bp_cs = NULL;
+	char *bp_output = NULL;
+	char *bp_ckidle = NULL;
+	char *bp_ckedge = NULL;
+	char *bp_sample = NULL;
 	char *tmp;
 	unsigned int fw_version_major = 0;
 	unsigned int fw_version_minor = 0;
@@ -233,6 +243,73 @@ 
 	}
 	free(speed);
 
+	/* Fine tuning SPI options */
+	bp_power = extract_programmer_param("power");
+	bp_pullups = extract_programmer_param("pullups");
+	bp_aux = extract_programmer_param("aux");
+	bp_cs = extract_programmer_param("cs");
+
+	bp_output = extract_programmer_param("output");
+	bp_ckidle = extract_programmer_param("ckidle");
+	bp_ckedge = extract_programmer_param("ckedge");
+	bp_sample = extract_programmer_param("sample");
+
+	if(bp_power) {
+		if(atoi(bp_power) == 0)
+			bp_peripherals &= ~8;
+	}
+
+	msg_pinfo("buspirate: [power] supplies are %s.\n", ((bp_peripherals >> 3) & 1) ? "ON" : "OFF");
+
+	if(bp_pullups) {
+		if(atoi(bp_pullups) == 1)
+			bp_peripherals |= 4;
+	}
+
+	msg_pinfo("buspirate: [pullups] are %s.\n", ((bp_peripherals >> 2) & 1) ? "ENABLED" : "DISABLED");
+
+	if(bp_aux) {
+		if(atoi(bp_aux) == 0)
+			bp_peripherals &= ~2;
+	}
+
+	msg_pinfo("buspirate: [aux] pin is %s.\n", ((bp_peripherals >> 1) & 1) ? "3.3v" : "GND");
+
+	if(bp_cs) {
+		if(atoi(bp_cs) == 0)
+			bp_peripherals &= ~1;
+	}
+
+	msg_pinfo("buspirate: [cs] is active %s.\n", (bp_peripherals & 1) ? "LOW" : "HIGH");
+
+	if(bp_output) {
+		if(atoi(bp_output) == 0)
+			bp_spi_config &= ~8;
+	}
+
+	msg_pinfo("buspirate: [output] mode is %s.\n", ((bp_spi_config >> 3) & 1) ? "3.3v" : "HiZ");
+
+	if(bp_ckidle) {
+		if(atoi(bp_ckidle) == 1)
+			bp_spi_config |= 4;
+	}
+
+	msg_pinfo("buspirate: [ckidle] phase is %s.\n", ((bp_spi_config >> 2) & 1) ? "HIGH" : "LOW");
+
+	if(bp_ckedge) {
+		if(atoi(bp_ckedge) == 0)
+			bp_spi_config &= ~2;
+	}
+
+	msg_pinfo("buspirate: [ckedge] is %s.\n", ((bp_spi_config >> 1) & 1) ? "Active to Idle" : "Idle to Active");
+
+	if(bp_sample) {
+		if(atoi(bp_sample) == 1)
+			bp_spi_config |= 1;
+	}
+
+	msg_pinfo("buspirate: [sample] is at %s.\n", (bp_spi_config  & 1) ? "END" : "MIDDLE");
+
 	/* Default buffer size is 19: 16 bytes data, 3 bytes control. */
 #define DEFAULT_BUFSIZE (16 + 3)
 	bp_commbuf = malloc(DEFAULT_BUFSIZE);
@@ -394,8 +471,8 @@ 
 		return 1;
 	}
 
-	/* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
-	bp_commbuf[0] = 0x40 | 0xb;
+	/* 0100wxyz - Configure peripherals w=power, x=pull-ups, y=AUX, z=CS */
+	bp_commbuf[0] = 0x40 | bp_peripherals;
 	ret = buspirate_sendrecv(bp_commbuf, 1, 1);
 	if (ret)
 		return 1;
@@ -413,9 +490,9 @@ 
 		msg_perr("Protocol error while setting SPI speed!\n");
 		return 1;
 	}
-	
-	/* Set SPI config: output type, idle, clock edge, sample */
-	bp_commbuf[0] = 0x80 | 0xa;
+
+	/* 1000wxyz - SPI config, w=HiZ/3.3v, x=CKP idle, y=CKE edge, z=SMP sample */ 
+	bp_commbuf[0] = 0x80 | bp_spi_config;
 	ret = buspirate_sendrecv(bp_commbuf, 1, 1);
 	if (ret)
 		return 1;