From efa151eb10a11796bd36cf0f32c8202e60d9de01 Mon Sep 17 00:00:00 2001 From: Carl-Daniel Hailfinger Date: Wed, 6 Jan 2010 16:09:10 +0000 Subject: Move OS-dependent serial code from buspirate_spi.c to serial.c While at it, also rename a few functions to make it obvious that they are generic and not specific to the Bus Pirate. Corresponding to flashrom svn r830. Signed-off-by: Carl-Daniel Hailfinger Acked-by: Patrick Georgi --- buspirate_spi.c | 60 ++++----------------------------------------------------- flash.h | 4 ++++ serial.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 56 deletions(-) diff --git a/buspirate_spi.c b/buspirate_spi.c index a1c8265..62433d4 100644 --- a/buspirate_spi.c +++ b/buspirate_spi.c @@ -22,7 +22,6 @@ #include #include #include -#include #include "flash.h" #include "spi.h" @@ -36,63 +35,12 @@ int buspirate_serialport_setup(char *dev) 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 @@ int buspirate_spi_init(void) 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 @@ int buspirate_spi_shutdown(void) 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"); diff --git a/flash.h b/flash.h index e2e1e62..a80ec32 100644 --- a/flash.h +++ b/flash.h @@ -595,6 +595,10 @@ void sp_flush_incoming(void); 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" diff --git a/serial.c b/serial.c index e22a8fa..be88166 100644 --- a/serial.c +++ b/serial.c @@ -2,6 +2,7 @@ * This file is part of the flashrom project. * * Copyright (C) 2009 Urja Rannikko + * 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 @@ void sp_flush_incoming(void) } 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; +} -- cgit v1.1