随机暂停

问题描述:

我试图开发使用此处提供的示例的LIN总线主控器:随机暂停

https://github.com/trainman419/linux-lin/tree/master/misc/tty_lin_master

本质上这送出LIN协议消息通过串行端口。

我将代码稍微改了一点,以使其对低级别功能测试更简单。我想看看一个LIN分析器是否能正确解码一个非常原始的LIN消息,但我遇到了与串口有关的奇怪问题。我通过/ dev/ttymxc4(RS-232)接口发送了几个连续的字符,但在数据包传输过程中,我看到在中间的某个地方随机暂停。有趣的是,这种停顿从某个价值开始,我俘获了8.6ms,但随后逐渐缩小直到它消失......但是随后它再次启动。

从本质上讲,如果你看一下主,我真的只是发送10个字符通过RS-232 ...

下面的代码,如果任何人有任何想法:

/* 
* UART-LIN master implementation 
*/ 

#define USE_TERMIOS2 

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <string.h> 
#include <stdint.h> 
#include <sys/ioctl.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <time.h> /* clock_nanosleep */ 
#include <getopt.h> 

#ifndef USE_TERMIOS2 
    #include <linux/serial.h> /* struct struct_serial */ 
    #include <termios.h> 
#else /*USE_TERMIOS2*/ 
    #include <asm/ioctls.h> 
    #include <asm/termbits.h> 
#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; 

struct sllin sllin_data = { 
    .tty = &sllin_tty_data, 
}; 

/* ------------------------------------------------------------------------ */ 

#ifndef USE_TERMIOS2 

static int tty_set_baudrate(struct sllin_tty *tty, int baudrate) 
{ 
    /* Set "non-standard" baudrate in serial_struct struct */ 
    tty->sattr.flags &= (~ASYNC_SPD_MASK); 
    tty->sattr.flags |= (ASYNC_SPD_CUST); 
    tty->sattr.custom_divisor = (tty->sattr.baud_base + baudrate/2)/baudrate; 
    if (ioctl(tty->tty_fd, TIOCSSERIAL, &tty->sattr) < 0) 
    { 
     perror("ioctl TIOCSSERIAL"); 
     return -1; 
    } 

    return 0; 
} 

static int tty_flush(struct sllin_tty *tty, int queue_selector) 
{ 
    return tcflush(tty->tty_fd, queue_selector); 
} 

#else /*USE_TERMIOS2*/ 

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)) { 
     fprintf(stderr, "Not a terminal.\n"); 
     return -1; 
    } 

    /* Flush input and output queues. */ 
    if (tty_flush(tty, TCIOFLUSH) != 0) { 
     perror("tcflush"); 
     return -1;; 
    } 

#ifndef USE_TERMIOS2 

    /* Save settings for later restoring */ 
    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) { 
     perror("tcgetattr"); 
     return -1; 
    } 

    /* 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*/ 

    /* 8 data bits     */ 
    /* Enable receiver    */ 
    /* Ignore CD (local connection) */ 
    tty->tattr.c_cflag = CS8 | CREAD | CLOCAL; 
    tty->tattr.c_iflag = 0; 
    tty->tattr.c_oflag = NL0 | CR0 | TAB0 | BS0 | VT0 | FF0; 
    tty->tattr.c_lflag = 0; 

    tty->tattr.c_cc[VINTR] = '\0'; 
    tty->tattr.c_cc[VQUIT] = '\0'; 
    tty->tattr.c_cc[VERASE] = '\0'; 
    tty->tattr.c_cc[VKILL] = '\0'; 
    tty->tattr.c_cc[VEOF]  = '\0'; 
    tty->tattr.c_cc[VTIME] = '\0'; 
    tty->tattr.c_cc[VMIN]  = 1; 
    tty->tattr.c_cc[VSWTC] = '\0'; 
    tty->tattr.c_cc[VSTART] = '\0'; 
    tty->tattr.c_cc[VSTOP] = '\0'; 
    tty->tattr.c_cc[VSUSP] = '\0'; 
    tty->tattr.c_cc[VEOL]  = '\0'; 
    tty->tattr.c_cc[VREPRINT] = '\0'; 
    tty->tattr.c_cc[VDISCARD] = '\0'; 
    tty->tattr.c_cc[VWERASE] = '\0'; 
    tty->tattr.c_cc[VLNEXT] = '\0'; 
    tty->tattr.c_cc[VEOL2] = '\0'; 

#ifndef USE_TERMIOS2 
    /* Set TX, RX speed to 38400 -- this value allows 
     to use custom speed in struct struct_serial */ 
    cfsetispeed(&tty->tattr, B38400); 
    cfsetospeed(&tty->tattr, B38400); 

    if (tcsetattr(tty->tty_fd, TCSANOW, &tty->tattr) == -1) { 
     perror("tcsetattr()"); 
     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); 

    return 0; 
} 

int sllin_open(struct sllin *sl, const char *dev_fname, int baudrate) 
{ 
    int fd; 

    sl->lin_baud = baudrate; 

    /* Calculate baudrate for sending LIN break */ 
    sl->lin_break_baud = (sl->lin_baud * 2)/3; 

    fd = open(dev_fname, O_RDWR); 
    if (fd < 0) { 
     perror("open()"); 
     return -1; 
    } 
    sl->tty->tty_fd = fd; 

    return tty_set_mode(sl->tty, sl->lin_baud); 
} 

int main() 
{ 
    struct sllin *sl = &sllin_data; 

    char *dev_fname = "/dev/ttymxc4"; 
    int lin_baudrate = 19200; 
    int lin_id = 1; 

    if (sllin_open(sl, dev_fname, lin_baudrate) < 0) { 
     fprintf (stderr, "sllin_open open failed\n"); 
     exit(EXIT_FAILURE); 
    } 

    fcntl(fileno(stdin), F_SETFL, O_NONBLOCK); 
    printf("Press enter to terminate.\n\n"); 


    while(1) { 
     char c; 

     tty_flush(sl->tty, TCIOFLUSH); 

     unsigned int buff[10] = {1,2,3,4,5,6,7,8,9,10}; 
     // debug 
     write(sl->tty->tty_fd, &buff[0], 1); 
     write(sl->tty->tty_fd, &buff[1], 1); 
     write(sl->tty->tty_fd, &buff[2], 1); 
     write(sl->tty->tty_fd, &buff[3], 1); 
     write(sl->tty->tty_fd, &buff[4], 1); 
     write(sl->tty->tty_fd, &buff[5], 1); 
     write(sl->tty->tty_fd, &buff[6], 1); 
     write(sl->tty->tty_fd, &buff[7], 1); 
     write(sl->tty->tty_fd, &buff[8], 1); 
     write(sl->tty->tty_fd, &buff[9], 1); 
     // debug 

     sleep(1); 

     if (read(fileno(stdin), &c, 1) > 0) 
      break; 
    } 

    return EXIT_SUCCESS; 
} 
+1

这可能与芯片的FIFO相关(https://en.wikipedia.org/wiki/16550_UART#The_16550_FIFO)? 10不是标准尺寸之一,可能会造成延误。 –

+1

我正在使用Gateworks Ventana GW5100 http://www.gateworks.com/product/item/ventana-gw5100-network-processor – NoS89

从本质上讲,如果你看看主,我实际上只是通过RS-232发送10个字符...

问题是你的输出方法。
而不是十个write()系统调用每个只有一个字节(这是非常低效的),只使用一个write()作为十个字节的缓冲区。

假设这是在Linux下执行的,每个系统调用都会允许调度程序暂停你的进程(因此间隙)。
如果只使用一个系统调用,则设备驱动程序将尽可能快地传输数据(只有DMA或中断延迟可能会导致xmit间隙)。

更换这一切

write(sl->tty->tty_fd, &buff[0], 1); 
    write(sl->tty->tty_fd, &buff[1], 1); 
    write(sl->tty->tty_fd, &buff[2], 1); 
    write(sl->tty->tty_fd, &buff[3], 1); 
    write(sl->tty->tty_fd, &buff[4], 1); 
    write(sl->tty->tty_fd, &buff[5], 1); 
    write(sl->tty->tty_fd, &buff[6], 1); 
    write(sl->tty->tty_fd, &buff[7], 1); 
    write(sl->tty->tty_fd, &buff[8], 1); 
    write(sl->tty->tty_fd, &buff[9], 1); 

与眼前这个

write(sl->tty->tty_fd, buff, 10); 

而且,更换写的睡眠()tcdrain()读取。

+1

在我最后的实现中,我将需要打破write()sys调用,因为LIN协议在帧的开始处需要13个连续的0个字符,实质上,我这样做的方式是将波特率更改为12800以发送这些字符,然后将其更改回19200以用于其余的标题。因为在两个字节之间会有一个停止字符,所以我不能简单地以减少的波特率发送两个0x00字节作为13个连续0的替换字节。 我应该使用不同的系统调用吗? (是的,这是在Linux下执行的) – NoS89

+1

我还应该补充一点,当我将波特率更改为12800以便发送看起来像13个连续的0时,我只需发送一个单字节0x00,速度为12800。对于从设备,这显示为19200波特的13位0。 – NoS89

+1

我只能回答发布的问题。我无法阅读您的想法或预测您的(不可理解的)*“最终实施”*。我解释了你发布的具体问题的解决方案,你甚至无法表达一丝感谢。 – sawdust