Move OS-dependent serial code from buspirate_spi.c to serial.c and rename a few functions to make it obvious that they are generic and not specific to the Bus Pirate.
Signed-off-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Index: flashrom-serial_move/flash.h =================================================================== --- flashrom-serial_move/flash.h (Revision 829) +++ flashrom-serial_move/flash.h (Arbeitskopie) @@ -595,6 +595,10 @@ int sp_openserport(char *dev, unsigned int baud); void __attribute__((noreturn)) sp_die(char *msg); extern int sp_fd; +int serialport_shutdown(void); +int serialport_write(unsigned char *buf, unsigned int writecnt); +int serialport_read(unsigned char *buf, unsigned int readcnt); +int serialport_discard_read(void);
#include "chipdrivers.h"
Index: flashrom-serial_move/buspirate_spi.c =================================================================== --- flashrom-serial_move/buspirate_spi.c (Revision 829) +++ flashrom-serial_move/buspirate_spi.c (Arbeitskopie) @@ -22,7 +22,6 @@ #include <string.h> #include <stdlib.h> #include <ctype.h> -#include <fcntl.h> #include "flash.h" #include "spi.h"
@@ -36,63 +35,12 @@ sp_fd = sp_openserport(dev, 115200); return 0; } - -int buspirate_serialport_shutdown(void) -{ - close(sp_fd); - return 0; -} - -int serialport_write(unsigned char *buf, unsigned int writecnt) -{ - int tmp = 0; - - while (tmp != writecnt) { - tmp = write(sp_fd, buf + tmp, writecnt - tmp); - if (tmp == -1) - return 1; - if (!tmp) - printf_debug("Empty write\n"); - } - - return 0; -} - -int serialport_read(unsigned char *buf, unsigned int readcnt) -{ - int tmp = 0; - - while (tmp != readcnt) { - tmp = read(sp_fd, buf + tmp, readcnt - tmp); - if (tmp == -1) - return 1; - if (!tmp) - printf_debug("Empty read\n"); - } - - return 0; -} - -int buspirate_discard_read(void) -{ - int flags; - - printf_debug("%s\n", __func__); - flags = fcntl(sp_fd, F_GETFL); - flags |= O_NONBLOCK; - fcntl(sp_fd, F_SETFL, flags); - sp_flush_incoming(); - flags &= ~O_NONBLOCK; - fcntl(sp_fd, F_SETFL, flags); - - return 0; -} #else #define buspirate_serialport_setup(...) 0 -#define buspirate_serialport_shutdown(...) 0 +#define serialport_shutdown(...) 0 #define serialport_write(...) 0 #define serialport_read(...) 0 -#define buspirate_discard_read(...) 0 +#define serialport_discard_read(...) 0 #endif
int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt) @@ -196,7 +144,7 @@ if (ret) return ret; /* Read any response and discard it. */ - ret = buspirate_discard_read(); + ret = serialport_discard_read(); if (ret) return ret; } @@ -302,7 +250,7 @@ return ret;
/* Shut down serial port communication */ - ret = buspirate_serialport_shutdown(); + ret = serialport_shutdown(); if (ret) return ret; printf_debug("Bus Pirate shutdown completed.\n"); Index: flashrom-serial_move/serial.c =================================================================== --- flashrom-serial_move/serial.c (Revision 829) +++ flashrom-serial_move/serial.c (Arbeitskopie) @@ -2,6 +2,7 @@ * This file is part of the flashrom project. * * Copyright (C) 2009 Urja Rannikko urjaman@gmail.com + * Copyright (C) 2009,2010 Carl-Daniel Hailfinger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -140,3 +141,54 @@ } return; } + +int serialport_shutdown(void) +{ + close(sp_fd); + return 0; +} + +int serialport_write(unsigned char *buf, unsigned int writecnt) +{ + int tmp = 0; + + while (tmp != writecnt) { + tmp = write(sp_fd, buf + tmp, writecnt - tmp); + if (tmp == -1) + return 1; + if (!tmp) + printf_debug("Empty write\n"); + } + + return 0; +} + +int serialport_read(unsigned char *buf, unsigned int readcnt) +{ + int tmp = 0; + + while (tmp != readcnt) { + tmp = read(sp_fd, buf + tmp, readcnt - tmp); + if (tmp == -1) + return 1; + if (!tmp) + printf_debug("Empty read\n"); + } + + return 0; +} + +int serialport_discard_read(void) +{ + int flags; + + printf_debug("%s\n", __func__); + flags = fcntl(sp_fd, F_GETFL); + flags |= O_NONBLOCK; + fcntl(sp_fd, F_SETFL, flags); + sp_flush_incoming(); + flags &= ~O_NONBLOCK; + fcntl(sp_fd, F_SETFL, flags); + + return 0; +}
Am Mittwoch, den 06.01.2010, 16:13 +0100 schrieb Carl-Daniel Hailfinger:
+int serialport_discard_read(void) +{
- int flags;
- printf_debug("%s\n", __func__);
- flags = fcntl(sp_fd, F_GETFL);
- flags |= O_NONBLOCK;
- fcntl(sp_fd, F_SETFL, flags);
- sp_flush_incoming();
- flags &= ~O_NONBLOCK;
- fcntl(sp_fd, F_SETFL, flags);
- return 0;
+}
I know that this patch is just about moving code, so of course the code will go in this way, but is there any reason not to use tcflush(sp_fd, TCIFLUSH); instead of this complicated function?
OK, I think I got it. tcflush is or was broken on Windows. See for example http://www.cygwin.com/ml/cygwin/2003-10/msg00797.html Maybe add a reference to cygwin as explanation?
Regards, Michael Karcher
Am Mittwoch, den 06.01.2010, 16:27 +0100 schrieb Michael Karcher:
Am Mittwoch, den 06.01.2010, 16:13 +0100 schrieb Carl-Daniel Hailfinger:
+int serialport_discard_read(void) +{
- int flags;
- printf_debug("%s\n", __func__);
- flags = fcntl(sp_fd, F_GETFL);
- flags |= O_NONBLOCK;
- fcntl(sp_fd, F_SETFL, flags);
- sp_flush_incoming();
- flags &= ~O_NONBLOCK;
- fcntl(sp_fd, F_SETFL, flags);
- return 0;
+}
I know that this patch is just about moving code, so of course the code will go in this way, but is there any reason not to use tcflush(sp_fd, TCIFLUSH); instead of this complicated function?
OK, I think I got it. tcflush is or was broken on Windows. See for example http://www.cygwin.com/ml/cygwin/2003-10/msg00797.html Maybe add a reference to cygwin as explanation?
Don't worry. They changed cygwin. See tcflush in http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/fhandler_serial.cc?re... It maps to a native PurgeComm which is just fine. Thanks to patrickg for the hint to recheck current code.
So no need to not use tcflush here.
Regards, Michael Karcher
On 06.01.2010 16:50, Michael Karcher wrote:
Am Mittwoch, den 06.01.2010, 16:27 +0100 schrieb Michael Karcher:
Am Mittwoch, den 06.01.2010, 16:13 +0100 schrieb Carl-Daniel Hailfinger:
+int serialport_discard_read(void)
[...] but is there any reason not to use tcflush(sp_fd, TCIFLUSH); instead of this complicated function?
OK, I think I got it. tcflush is or was broken on Windows. See for example http://www.cygwin.com/ml/cygwin/2003-10/msg00797.html Maybe add a reference to cygwin as explanation?
Don't worry. They changed cygwin. See tcflush in http://cygwin.com/cgi-bin/cvsweb.cgi/src/winsup/cygwin/fhandler_serial.cc?re... It maps to a native PurgeComm which is just fine. Thanks to patrickg for the hint to recheck current code.
So no need to not use tcflush here.
Patrick, can you take care of this in your serial port patch? Thanks.
Regards, Carl-Daniel
Am 06.01.2010 16:13, schrieb Carl-Daniel Hailfinger:
Move OS-dependent serial code from buspirate_spi.c to serial.c and rename a few functions to make it obvious that they are generic and not specific to the Bus Pirate.
Signed-off-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Acked-by: Patrick Georgi patrick.georgi@coresystems.de
On 06.01.2010 17:03, Patrick Georgi wrote:
Am 06.01.2010 16:13, schrieb Carl-Daniel Hailfinger:
Move OS-dependent serial code from buspirate_spi.c to serial.c and rename a few functions to make it obvious that they are generic and not specific to the Bus Pirate.
Signed-off-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Acked-by: Patrick Georgi patrick.georgi@coresystems.de
Thanks, r830.
Regards, Carl-Daniel