From 6efb01e9e277c1dff94e4c477ada731f516f18eb Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Fri, 25 Nov 2011 02:41:27 +0100 Subject: [PATCH] Userspace LIN is compiled with USE_TERMIOS2 by default now. If compiled with USE_TERMIOS2 then new kernel methods is used to set generic baudrate. The baudrate values are specified by c_ospeed and c_ispeed fields of struct termios2 and new IOCTL numbers TCSETS2 and TCGETS2 are used for terminal parameters setting and reading. Signed-off-by: Pavel Pisa --- misc/tty_lin_master/main.c | 106 ++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 7 deletions(-) diff --git a/misc/tty_lin_master/main.c b/misc/tty_lin_master/main.c index 8d490c9..a111fde 100644 --- a/misc/tty_lin_master/main.c +++ b/misc/tty_lin_master/main.c @@ -2,28 +2,44 @@ * UART-LIN master implementation */ + +#define USE_TERMIOS2 + #include #include #include #include #include -#include #include #include -#include /* struct struct_serial */ #include #include #include /* clock_nanosleep */ #include + +#ifndef USE_TERMIOS2 + #include /* struct struct_serial */ + #include +#else /*USE_TERMIOS2*/ + #include + #include +#endif /*USE_TERMIOS2*/ + #include "lin_common.h" #define LIN_HDR_SIZE 2 struct sllin_tty { int tty_fd; + +#ifndef USE_TERMIOS2 struct termios tattr_orig; struct termios tattr; struct serial_struct sattr; +#else /*USE_TERMIOS2*/ + struct termios2 tattr_orig; + struct termios2 tattr; +#endif /*USE_TERMIOS2*/ }; struct sllin_tty sllin_tty_data; @@ -33,6 +49,9 @@ struct sllin sllin_data = { }; /* ------------------------------------------------------------------------ */ + +#ifndef USE_TERMIOS2 + static void tty_reset_mode(struct sllin_tty *tty) { tcsetattr(tty->tty_fd, TCSANOW, &tty->tattr_orig); @@ -61,6 +80,41 @@ static int tty_set_baudrate(struct sllin_tty *tty, int baudrate) return 0; } +static int tty_flush(struct sllin_tty *tty, int queue_selector) +{ + return tcflush(tty->tty_fd, queue_selector); +} + +#else /*USE_TERMIOS2*/ + +static void tty_reset_mode(struct sllin_tty *tty) +{ + ioctl(tty->tty_fd, TCSETS2, &tty->tattr_orig); +} + +static int tty_set_baudrate(struct sllin_tty *tty, int baudrate) +{ + tty->tattr.c_ospeed = baudrate; + tty->tattr.c_ispeed = baudrate; + tty->tattr.c_cflag &= ~CBAUD; + tty->tattr.c_cflag |= BOTHER; + + if(ioctl(tty->tty_fd, TCSETS2, &tty->tattr)) { + perror("ioctl TIOCSSERIAL"); + return -1; + } + + return 0; +} + +static int tty_flush(struct sllin_tty *tty, int queue_selector) +{ + return ioctl(tty->tty_fd, TCFLSH, queue_selector); +} + +#endif /*USE_TERMIOS2*/ + + static int tty_set_mode(struct sllin_tty *tty, int baudrate) { if(!isatty(tty->tty_fd)) { @@ -69,20 +123,46 @@ static int tty_set_mode(struct sllin_tty *tty, int baudrate) } /* Flush input and output queues. */ - if (tcflush(tty->tty_fd, TCIOFLUSH) != 0) { + if (tty_flush(tty, TCIOFLUSH) != 0) { perror("tcflush"); return -1;; } +#ifndef USE_TERMIOS2 + /* Save settings for later restoring */ - tcgetattr(tty->tty_fd, &tty->tattr_orig); + if (tcgetattr(tty->tty_fd, &tty->tattr_orig) < 0) { + perror("tcgetattr"); + return -1; + } /* Save settings into global variables for later use */ - if (tcgetattr(tty->tty_fd, &tty->tattr) < 0) + if (tcgetattr(tty->tty_fd, &tty->tattr) < 0) { perror("tcgetattr"); + return -1; + } - if (ioctl(tty->tty_fd, TIOCGSERIAL, &tty->sattr) < 0) + /* Save settings into global variables for later use */ + if (ioctl(tty->tty_fd, TIOCGSERIAL, &tty->sattr) < 0) { perror("ioctl TIOCGSERIAL"); + } + +#else /*USE_TERMIOS2*/ + + /* Save settings for later restoring */ + if (ioctl(tty->tty_fd, TCGETS2, &tty->tattr_orig) < 0) { + perror("ioctl TCGETS2"); + return -1; + } + + /* Save settings into global variables for later use */ + if (ioctl(tty->tty_fd, TCGETS2, &tty->tattr) < 0) { + perror("ioctl TCGETS2"); + return -1; + } + +#endif /*USE_TERMIOS2*/ + /* Set RAW mode */ #if 0 @@ -123,6 +203,7 @@ static int tty_set_mode(struct sllin_tty *tty, int baudrate) tty->tattr.c_cc[VEOL2] = '\0'; #endif +#ifndef USE_TERMIOS2 /* Set TX, RX speed to 38400 -- this value allows to use custom speed in struct struct_serial */ cfsetispeed(&tty->tattr, B38400); @@ -133,6 +214,17 @@ static int tty_set_mode(struct sllin_tty *tty, int baudrate) return -1; } +#else /*USE_TERMIOS2*/ + + /* Set new parameters with previous speed and left */ + /* tty_set_baudrate() to do the rest */ + if(ioctl(tty->tty_fd, TCSETS2, &tty->tattr)) { + perror("ioctl TIOCSSERIAL"); + return -1; + } + +#endif /*USE_TERMIOS2*/ + /* Set real speed */ tty_set_baudrate(tty, baudrate); @@ -179,7 +271,7 @@ int send_header(struct sllin *sl, int lin_id) buff[2] = lin_id; /* LIN ID: 1 */ printf("send_header() invoked\n"); - tcflush(sl->tty->tty_fd, TCIOFLUSH); + tty_flush(sl->tty, TCIOFLUSH); /* Decrease speed to send BREAK (simulated with 0x00 data frame) */ -- 2.39.2