summaryrefslogblamecommitdiffstats
path: root/sw/dmx2artnet/settty.c
blob: 65f54b1b42c2b44133bb5e5dafd7cefad738a0db (plain) (tree)


























































































                                                                                               
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <poll.h>
#include <sys/types.h>
#include <error.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#define termios asmtermios
#define winsize asmwinsize
#define termio asmtermio
#include <asm/termios.h>
#undef termios
#undef winsize
#undef termio
#define S0(x) (x ? x : "")
// start bit: low, 2 stop bits high
int rate (int what_rate, int uart) {
	int r = 0;
	struct termios2 uartattr2;
	if (ioctl(uart, TCGETS2, &uartattr2) == -1) {
		error_at_line(0, errno, __FILE__, __LINE__, "ioctl TCGETS2, fd: %d", uart);
		r = 1;
		goto r;
	}
	uartattr2.c_cflag &= ~CBAUD;
	uartattr2.c_cflag |= BOTHER;
	uartattr2.c_ispeed = what_rate;
	uartattr2.c_ospeed = what_rate;
	if (ioctl(uart, TCSETS2, &uartattr2) == -1) {
		error_at_line(0, errno, __FILE__, __LINE__, "ioctl TCSETS2");
		r = 2;
		goto r;
	}
r:
	return r;
}
int main (int argc, char ** argv) {
	int r = 0;
	if (argc != 1+1)
		error_at_line(1, 0, __FILE__, __LINE__, "usage: %s /dev/ttyUSB0", S0(argv[0]));
	int uart = open(argv[1], O_WRONLY | O_NOCTTY | O_NDELAY | O_CLOEXEC);
	if (uart == -1)
		error_at_line(3, errno, __FILE__, __LINE__, "open");
	struct termios uartattr;
	if (tcgetattr(uart, &uartattr) == -1) {
		error_at_line(0, errno, __FILE__, __LINE__, "tcgetattr");
		r = 4;
		goto r;
	}
	uartattr.c_iflag = 0;
	uartattr.c_oflag = 0;
	uartattr.c_cflag = CS8 | CSTOPB;
	uartattr.c_lflag = 0;
	if (tcflush(uart, TCIOFLUSH) == -1) {
		error_at_line(0, errno, __FILE__, __LINE__, "tcflush");
		r = 5;
		goto r;
	}
	if (tcsetattr(uart, TCSANOW, &uartattr) == -1) {
		error_at_line(0, errno, __FILE__, __LINE__, "tcsetattr");
		r = 6;
		goto r;
	}
	if (tcflush(uart, TCIOFLUSH) == -1) {
		error_at_line(0, errno, __FILE__, __LINE__, "tcflush");
		r = 15;
		goto r;
	}
	switch (rate(250000, uart)) {
		case 1:
			r = 20;
			goto r;
			break;
		case 2:
			r = 21;
			goto r;
			break;
		default:
			break;
	}
r:
	if (close(uart) == -1)
		error_at_line(22, errno, __FILE__, __LINE__, "close(uart)");
	return r;
}