From 68f968a63743afb819c36c7bc4e90f7d360a89f2 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Wed, 29 Feb 2012 16:29:00 +1300 Subject: [PATCH] sport: accept baud rates as a plain integer instead of a B* constant. Integer baud rates are converted to B* constants in the implementation. --- drivers/bsl.c | 2 +- drivers/flash_bsl.c | 7 +++---- drivers/uif.c | 6 +++--- util/sport.c | 39 +++++++++++++++++++++++++++++++++++++-- util/sport.h | 21 --------------------- 5 files changed, 44 insertions(+), 31 deletions(-) diff --git a/drivers/bsl.c b/drivers/bsl.c index 0ab19c9..1240a32 100644 --- a/drivers/bsl.c +++ b/drivers/bsl.c @@ -377,7 +377,7 @@ static device_t bsl_open(const struct device_args *args) dev->base.type = &device_bsl; - dev->serial_fd = sport_open(args->path, B460800, 0); + dev->serial_fd = sport_open(args->path, 460800, 0); if (SPORT_ISERR(dev->serial_fd)) { printc_err("bsl: can't open %s: %s\n", args->path, last_error()); diff --git a/drivers/flash_bsl.c b/drivers/flash_bsl.c index e0cbfb1..7d5fa94 100644 --- a/drivers/flash_bsl.c +++ b/drivers/flash_bsl.c @@ -641,7 +641,7 @@ static device_t flash_bsl_open(const struct device_args *args) memset(dev, 0, sizeof(*dev)); dev->base.type = &device_flash_bsl; - dev->serial_fd = sport_open(args->path, B9600, SPORT_EVEN_PARITY); + dev->serial_fd = sport_open(args->path, 9600, SPORT_EVEN_PARITY); if (SPORT_ISERR(dev->serial_fd)) { printc_err("flash_bsl: can't open %s: %s\n", args->path, last_error()); @@ -652,9 +652,8 @@ static device_t flash_bsl_open(const struct device_args *args) dev->long_password = args->flags & DEVICE_FLAG_LONG_PW; /* enter bootloader */ - if (enter_via_dtr_rts(dev) < 0) { - goto fail; - } + if (enter_via_dtr_rts(dev) < 0) + goto fail; usleep(500000); diff --git a/drivers/uif.c b/drivers/uif.c index 2799c55..39a0af9 100644 --- a/drivers/uif.c +++ b/drivers/uif.c @@ -127,12 +127,12 @@ transport_t uif_open(const char *device, uif_type_t type) switch (type) { case UIF_TYPE_FET: printc("Trying to open UIF on %s...\n", device); - tr->serial_fd = sport_open(device, B460800, 0); + tr->serial_fd = sport_open(device, 460800, 0); break; case UIF_TYPE_OLIMEX: printc("Trying to open Olimex (V2) on %s...\n", device); - tr->serial_fd = sport_open(device, B115200, 0); + tr->serial_fd = sport_open(device, 115200, 0); if (sport_set_modem(tr->serial_fd, 0) < 0) pr_error("warning: uif: failed to set " "modem control lines"); @@ -140,7 +140,7 @@ transport_t uif_open(const char *device, uif_type_t type) case UIF_TYPE_OLIMEX_V1: printc("Trying to open Olimex (V1) on %s...\n", device); - tr->serial_fd = sport_open(device, B500000, 0); + tr->serial_fd = sport_open(device, 500000, 0); break; case UIF_TYPE_OLIMEX_ISO: diff --git a/util/sport.c b/util/sport.c index c24963d..79634c6 100644 --- a/util/sport.c +++ b/util/sport.c @@ -26,18 +26,53 @@ #ifndef WIN32 +#ifndef B460800 +#define B460800 460800 +#endif + +#ifndef B500000 +#define B500000 500000 +#endif + +struct baud_rate { + int rate; + int code; +}; + +static const struct baud_rate baud_rates[] = { + {9600, B9600}, + {115200, B115200}, + {460800, B460800}, + {500000, B500000} +}; + +static int rate_to_code(int rate) +{ + int i; + + for (i = 0; i < ARRAY_LEN(baud_rates); i++) + if (baud_rates[i].rate == rate) + return baud_rates[i].code; + + return -1; +} + sport_t sport_open(const char *device, int rate, int flags) { int fd = open(device, O_RDWR | O_NOCTTY); struct termios attr; + int rate_code = rate_to_code(rate); if (fd < 0) return -1; tcgetattr(fd, &attr); cfmakeraw(&attr); - cfsetispeed(&attr, rate); - cfsetospeed(&attr, rate); + + if (rate_code >= 0) { + cfsetispeed(&attr, rate_code); + cfsetospeed(&attr, rate_code); + } if (flags & SPORT_EVEN_PARITY) attr.c_cflag |= PARENB; diff --git a/util/sport.h b/util/sport.h index f9236a6..31edf5b 100644 --- a/util/sport.h +++ b/util/sport.h @@ -25,14 +25,6 @@ #include #include -#ifndef B460800 -#define B460800 460800 -#endif - -#ifndef B500000 -#define B500000 500000 -#endif - typedef int sport_t; #define SPORT_ISERR(x) ((x) < 0) @@ -48,19 +40,6 @@ typedef HANDLE sport_t; #define SPORT_ISERR(x) ((x) == INVALID_HANDLE_VALUE) -#ifndef CBR_460800 -#define CBR_460800 460800 -#endif - -#ifndef CBR_500000 -#define CBR_500000 500000 -#endif - -#define B9600 CBR_9600 -#define B115200 CBR_115200 -#define B460800 CBR_460800 -#define B500000 CBR_500000 - #define SPORT_MC_DTR 0x01 #define SPORT_MC_RTS 0x02