From d3d625a0e1027c0f328d2d1f620390ecec1fb3cf Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 11 Nov 2005 21:13:25 +0000 Subject: Add basic device initialization --- tools/csr_bcsp.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) (limited to 'tools/csr_bcsp.c') diff --git a/tools/csr_bcsp.c b/tools/csr_bcsp.c index 8847fec5..582d6859 100644 --- a/tools/csr_bcsp.c +++ b/tools/csr_bcsp.c @@ -27,15 +27,73 @@ #include #include +#include +#include +#include #include +#include + +#include "csr.h" static uint16_t seqnum = 0x0000; +static int fd = -1; + int csr_open_bcsp(char *device) { - fprintf(stderr, "Transport not implemented\n"); + struct termios ti; - return -1; + if (!device) + device = "/dev/ttyS0"; + + fd = open(device, O_RDWR | O_NOCTTY); + if (fd < 0) { + fprintf(stderr, "Can't open serial port: %s (%d)\n", + strerror(errno), errno); + return -1; + } + + tcflush(fd, TCIOFLUSH); + + if (tcgetattr(fd, &ti) < 0) { + fprintf(stderr, "Can't get port settings: %s (%d)\n", + strerror(errno), errno); + close(fd); + return -1; + } + + cfmakeraw(&ti); + + ti.c_cflag |= CLOCAL; + ti.c_cflag &= ~CRTSCTS; + ti.c_cflag |= PARENB; + ti.c_cflag &= ~PARODD; + ti.c_cflag &= ~CSIZE; + ti.c_cflag |= CS8; + ti.c_cflag &= ~CSTOPB; + + ti.c_cc[VMIN] = 1; + ti.c_cc[VTIME] = 0; + + cfsetospeed(&ti, B38400); + + if (tcsetattr(fd, TCSANOW, &ti) < 0) { + fprintf(stderr, "Can't change port settings: %s (%d)\n", + strerror(errno), errno); + close(fd); + return -1; + } + + tcflush(fd, TCIOFLUSH); + + if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) < 0) { + fprintf(stderr, "Can't set non blocking mode: %s (%d)\n", + strerror(errno), errno); + close(fd); + return -1; + } + + return 0; } static int do_command(uint16_t command, uint16_t seqnum, uint16_t varid, uint8_t *value, uint16_t length) @@ -57,4 +115,5 @@ int csr_write_bcsp(uint16_t varid, uint8_t *value, uint16_t length) void csr_close_bcsp(void) { + close(fd); } -- cgit