diff options
| author | Marcel Holtmann <marcel@holtmann.org> | 2005-11-11 21:13:25 +0000 | 
|---|---|---|
| committer | Marcel Holtmann <marcel@holtmann.org> | 2005-11-11 21:13:25 +0000 | 
| commit | d3d625a0e1027c0f328d2d1f620390ecec1fb3cf (patch) | |
| tree | 3b0380c66e46f7bbe9a4796463a51534f65eaf51 | |
| parent | 858c6b70e59fc7b44243c0c7fb0455323e723b1d (diff) | |
Add basic device initialization
| -rw-r--r-- | tools/csr_bcsp.c | 63 | 
1 files changed, 61 insertions, 2 deletions
| 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 <stdio.h>  #include <errno.h> +#include <fcntl.h> +#include <unistd.h> +#include <string.h>  #include <stdint.h> +#include <termios.h> + +#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);  } | 
