Patchwork buspiratespi on mingw

login
register
about
Submitter Patrick Georgi
Date 2010-01-06 20:50:32
Message ID <4B44F798.4000707@georgi-clan.de>
Download mbox | patch
Permalink /patch/740/
State Accepted
Commit r832
Headers show

Comments

Patrick Georgi - 2010-01-06 20:50:32
Hi,

attached patch makes the buspiratespi programmer compile and run on
mingw. Build tested on linux, too.

Tested without any device attached to the buspirate, which gave me much
text (with -V) ending in:
Found chip "Generic unknown SPI chip (REMS)" (0 KB, SPI) at physical
address 0x0.
===
This flash part has status NOT WORKING for operations: PROBE READ ERASE
WRITE
Please email a report to flashrom@flashrom.org if any of the above
operations
work correctly for you with this flash part. Please include the flashrom
output with the additional -V option for all operations you tested (-V, -rV,
-wV, -EV), and mention which mainboard or programmer you tested. Thanks
for your help!
===
No operations were specified.
buspirate_sendrecv: write 1, read 5
Sending 0x00, receiving 0x42 0x42 0x49 0x4f 0x31
Raw bitbang mode version 1
buspirate_sendrecv: write 1, read 0
Sending 0x0f, receiving
Bus Pirate shutdown completed.

Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
Carl-Daniel Hailfinger - 2010-01-06 22:12:20
On 06.01.2010 21:50, Patrick Georgi wrote:
> attached patch makes the buspiratespi programmer compile and run on
> mingw. Build tested on linux, too.
>
> Tested without any device attached to the buspirate, which gave me much
> text (with -V) ending in:
> Found chip "Generic unknown SPI chip (REMS)" (0 KB, SPI) at physical
> address 0x0.
>   

I'm still a bit surprised that it only found the generic REMS chip, but
not the generic RDID chip. That looks a bit like corruption on the wire
or a floating bus which changes state a lot between commands.

> No operations were specified.
> buspirate_sendrecv: write 1, read 5
> Sending 0x00, receiving 0x42 0x42 0x49 0x4f 0x31
> Raw bitbang mode version 1
> buspirate_sendrecv: write 1, read 0
> Sending 0x0f, receiving
> Bus Pirate shutdown completed.
>
> Signed-off-by: Patrick Georgi <patrick.georgi@coresystems.de>
>   

Thanks for adding Windows support!

Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>

Regards,
Carl-Daniel

Patch

Index: flash.h

===================================================================
--- flash.h	(Revision 831)

+++ flash.h	(Arbeitskopie)

@@ -28,6 +28,11 @@ 

 #include <stdint.h>
 #include <stdio.h>
 #include "hwaccess.h"
+#ifdef _WIN32
+#include <windows.h>
+#undef min
+#undef max
+#endif
 
 typedef unsigned long chipaddr;
 
@@ -591,10 +596,16 @@ 

 void serprog_delay(int delay);
 
 /* serial.c */
+#if _WIN32
+typedef HANDLE fdtype;
+#else
+typedef int fdtype;
+#endif
+
 void sp_flush_incoming(void);
-int sp_openserport(char *dev, unsigned int baud);
+fdtype sp_openserport(char *dev, unsigned int baud);
 void __attribute__((noreturn)) sp_die(char *msg);
-extern int sp_fd;
+extern fdtype sp_fd;
 int serialport_shutdown(void);
 int serialport_write(unsigned char *buf, unsigned int writecnt);
 int serialport_read(unsigned char *buf, unsigned int readcnt);
Index: serial.c

===================================================================
--- serial.c	(Revision 831)

+++ serial.c	(Arbeitskopie)

@@ -30,9 +30,13 @@ 

 #include <sys/stat.h>
 #include <errno.h>
 #include <inttypes.h>
+#ifdef _WIN32
+#include <conio.h>
+#else
 #include <termios.h>
+#endif
 
-int sp_fd;
+fdtype sp_fd;
 
 void __attribute__((noreturn)) sp_die(char *msg)
 {
@@ -40,6 +44,7 @@ 

 	exit(1);
 }
 
+#ifndef _WIN32
 struct baudentry {
 	int flag;
 	unsigned int baud;
@@ -94,9 +99,36 @@ 

 #endif
 	{0, 0}			/* Terminator */
 };
+#endif
 
-int sp_openserport(char *dev, unsigned int baud)
+fdtype sp_openserport(char *dev, unsigned int baud)
 {
+#ifdef _WIN32
+	HANDLE fd;
+	fd = CreateFile(dev, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
+	if (fd == INVALID_HANDLE_VALUE) {
+		sp_die("Error: cannot open serial port");
+	}
+	DCB dcb;
+	if (!GetCommState(fd, &dcb)) {
+		sp_die("Error: Could not fetch serial port configuration");
+	}
+	switch (baud) {
+		case 9600: dcb.BaudRate = CBR_9600; break;
+		case 19200: dcb.BaudRate = CBR_19200; break;
+		case 38400: dcb.BaudRate = CBR_38400; break;
+		case 57600: dcb.BaudRate = CBR_57600; break;
+		case 115200: dcb.BaudRate = CBR_115200; break;
+		default: sp_die("Error: Could not set baud rate");
+	}
+	dcb.ByteSize=8;
+	dcb.Parity=NOPARITY;
+	dcb.StopBits=ONESTOPBIT;
+	if (!SetCommState(fd, &dcb)) {
+		sp_die("Error: Could not change serial port configuration");
+	}
+	return fd;
+#else
 	struct termios options;
 	int fd, i;
 	fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
@@ -125,26 +157,39 @@ 

 	options.c_oflag &= ~OPOST;
 	tcsetattr(fd, TCSANOW, &options);
 	return fd;
+#endif
 }
 
 void sp_flush_incoming(void)
 {
+#ifdef _WIN32
+	PurgeComm(sp_fd, PURGE_RXCLEAR);
+#else
 	tcflush(sp_fd, TCIFLUSH);
+#endif
 	return;
 }
 
 int serialport_shutdown(void)
 {
+#ifdef _WIN32
+	CloseHandle(sp_fd);
+#else
 	close(sp_fd);
+#endif
 	return 0;
 }
 
 int serialport_write(unsigned char *buf, unsigned int writecnt)
 {
-	int tmp = 0;
+	long tmp = 0;
 
 	while (writecnt > 0) {
+#ifdef _WIN32
+		WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
+#else
 		tmp = write(sp_fd, buf, writecnt);
+#endif
 		if (tmp == -1)
 			return 1;
 		if (!tmp)
@@ -158,10 +203,14 @@ 

 
 int serialport_read(unsigned char *buf, unsigned int readcnt)
 {
-	int tmp = 0;
+	long tmp = 0;
 
 	while (readcnt > 0) {
+#ifdef _WIN32
+		ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
+#else
 		tmp = read(sp_fd, buf, readcnt);
+#endif
 		if (tmp == -1)
 			return 1;
 		if (!tmp)