sport: accept baud rates as a plain integer instead of a B* constant.

Integer baud rates are converted to B* constants in the implementation.
This commit is contained in:
Daniel Beer 2012-02-29 16:29:00 +13:00
parent 25da331e21
commit 68f968a637
5 changed files with 44 additions and 31 deletions

View File

@ -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());

View File

@ -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);

View File

@ -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:

View File

@ -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;

View File

@ -25,14 +25,6 @@
#include <termios.h>
#include <sys/ioctl.h>
#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