Difference between revisions of "How to use UART for Diagnostic"

From ESS-WIKI
Jump to: navigation, search
 
Line 10: Line 10:
  
 
Using C, use the struct termios to set the initial baud rate:
 
Using C, use the struct termios to set the initial baud rate:
<pre style="background: rgb(255, 255, 255); margin: 3px 0px; padding: 10px; border-radius: 4px; border: 1px dotted rgb(204, 204, 204); color: rgb(0, 0, 0); text-transform: none; line-height: 1.4285; text-indent: 0px; letter-spacing: normal; overflow: auto; font-family: monospace; font-size: 14px; font-style: normal; font-weight: 400; word-spacing: 0px; display: block; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; hyphens: none;" class="cpp"><span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <termios.h></span>
+
<pre style="background: rgb(255, 255, 255); margin: 3px 0px; padding: 10px; border-radius: 4px; border: 1px dotted rgb(204, 204, 204); color: rgb(0, 0, 0); text-transform: none; line-height: 1.4285; text-indent: 0px; letter-spacing: normal; overflow: auto; font-family: monospace; font-size: 14px; font-style: normal; font-weight: 400; word-spacing: 0px; display: block; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; hyphens: none;" class="cpp">#include <termios.h>
 
...
 
...
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">struct</span> termios tty<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
struct termios tty;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">int</span> fd<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
int fd;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">int</span> flags <span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> O_RDWR <span style="color: rgb(0, 0, 64); box-sizing: border-box;">|</span> O_NOCTTY <span style="color: rgb(0, 0, 64); box-sizing: border-box;">|</span> O_NONBLOCK<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
 
&nbsp;
 
&nbsp;
fd <span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> open<span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span><span style="color: rgb(255, 0, 0); box-sizing: border-box;">"/dev/ttyS0"</span>, flags<span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
fd = open("/dev/ttyS0", flags);
 
&nbsp;
 
&nbsp;
tcgetattr<span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>fd, <span style="color: rgb(0, 0, 64); box-sizing: border-box;">&</span>tty<span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
tcgetattr(fd, &tty);
 
&nbsp;
 
&nbsp;
tty.<span style="color: rgb(0, 119, 136); box-sizing: border-box;">c_cflag</span> <span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>tty.<span style="color: rgb(0, 119, 136); box-sizing: border-box;">c_cflag</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">&</span> ~CSIZE<span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">|</span> CS8<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.<span style="color: rgb(0, 119, 136); box-sizing: border-box;">c_cflag</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">|</span><span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> B115200<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
tty.c_cflag |= B115200;
 
&nbsp;
 
&nbsp;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">if</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>tcsetattr <span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>fd, TCSANOW, <span style="color: rgb(0, 0, 64); box-sizing: border-box;">&</span>tty<span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">!</span><span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> <span style="color: rgb(0, 0, 221); box-sizing: border-box;">0</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span>
+
if (tcsetattr (fd, TCSANOW, &tty) != 0)
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">{</span>
+
{
    <span style="color: rgb(0, 0, 221); box-sizing: border-box;">fprintf</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span><span style="color: rgb(0, 0, 255); box-sizing: border-box;">stderr</span>, <span style="color: rgb(255, 0, 0); box-sizing: border-box;">"error %d from tcsetattr"</span>, <span style="color: rgb(0, 0, 255); box-sizing: border-box;">errno</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;&nbsp;&nbsp; fprintf (stderr, "error %d from tcsetattr", errno);
    <span style="color: rgb(0, 0, 255); box-sizing: border-box;">return</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">-</span><span style="color: rgb(0, 0, 221); box-sizing: border-box;">1</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;&nbsp;&nbsp; return -1;
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span></pre>
+
}</pre>
  
  
Line 79: Line 79:
  
  
<pre style="background: rgb(255, 255, 255); margin: 3px 0px; padding: 10px; border-radius: 4px; border: 1px dotted rgb(204, 204, 204); color: rgb(0, 0, 0); text-transform: none; line-height: 1.4285; text-indent: 0px; letter-spacing: normal; overflow: auto; font-family: monospace; font-size: 14px; font-style: normal; font-weight: 400; word-spacing: 0px; display: block; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; hyphens: none;" class="cpp"><span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <stdio.h></span>
+
<pre style="background: rgb(255, 255, 255); margin: 3px 0px; padding: 10px; border-radius: 4px; border: 1px dotted rgb(204, 204, 204); color: rgb(0, 0, 0); text-transform: none; line-height: 1.4285; text-indent: 0px; letter-spacing: normal; overflow: auto; font-family: monospace; font-size: 14px; font-style: normal; font-weight: 400; word-spacing: 0px; display: block; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; hyphens: none;" class="cpp">#include <stdio.h>
<span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <fcntl.h></span>
+
#include <fcntl.h>
<span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <errno.h></span>
+
#include <errno.h>
<span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <asm/termios.h></span>
+
#include <asm/termios.h>
 
&nbsp;
 
&nbsp;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">int</span> main<span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span><span style="color: rgb(0, 0, 255); box-sizing: border-box;">int</span> argc, <span style="color: rgb(0, 0, 255); box-sizing: border-box;">char</span><span style="color: rgb(0, 0, 64); box-sizing: border-box;">*</span> argv<span style="color: rgb(0, 128, 0); box-sizing: border-box;">[</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">]</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">{</span>
+
int main(int argc, char* argv[]) {
 
&nbsp;
 
&nbsp;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">int</span> retval, fd, speed<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;int retval, fd, speed;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">struct</span> termios2 ntio<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;struct termios2 ntio;
 
&nbsp;
 
&nbsp;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">if</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>argc <span style="color: rgb(0, 0, 64); box-sizing: border-box;">!</span><span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> <span style="color: rgb(0, 0, 221); box-sizing: border-box;">3</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">{</span>
+
&nbsp;if (argc != 3) {
<span style="color: rgb(0, 0, 221); box-sizing: border-box;">printf</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span><span style="color: rgb(255, 0, 0); box-sizing: border-box;">"Usage: %s /dev/ttyXXX 20000<span style="color: rgb(0, 0, 153); font-weight: bold; box-sizing: border-box;">\n</span>"</span>, argv<span style="color: rgb(0, 128, 0); box-sizing: border-box;">[</span><span style="color: rgb(0, 0, 221); box-sizing: border-box;">0</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">]</span>, argv<span style="color: rgb(0, 128, 0); box-sizing: border-box;">[</span><span style="color: rgb(0, 0, 221); box-sizing: border-box;">0</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">]</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>                 
+
&nbsp;&nbsp;printf("Usage: %s /dev/ttyXXX 20000\n", argv[0], argv[0]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">return</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">-</span><span style="color: rgb(0, 0, 221); box-sizing: border-box;">1</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;&nbsp;return -1;
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
+
&nbsp;}
 
&nbsp;
 
&nbsp;
speed <span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> <span style="color: rgb(0, 0, 221); box-sizing: border-box;">atoi</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>argv<span style="color: rgb(0, 128, 0); box-sizing: border-box;">[</span><span style="color: rgb(0, 0, 221); box-sizing: border-box;">2</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">]</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;speed = atoi(argv[2]);
fd <span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> open<span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>argv<span style="color: rgb(0, 128, 0); box-sizing: border-box;">[</span><span style="color: rgb(0, 0, 221); box-sizing: border-box;">1</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">]</span>, O_RDONLY<span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;fd = open(argv[1], O_RDONLY);
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">if</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>fd <span style="color: rgb(0, 0, 128); box-sizing: border-box;"><</span> <span style="color: rgb(0, 0, 221); box-sizing: border-box;">0</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">{</span>
+
&nbsp;if (fd < 0) {
<span style="color: rgb(0, 0, 221); box-sizing: border-box;">perror</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span><span style="color: rgb(255, 0, 0); box-sizing: border-box;">"Open"</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;&nbsp;perror("Open");
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">return</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">-</span><span style="color: rgb(0, 0, 221); box-sizing: border-box;">1</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;&nbsp;return -1;
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
+
&nbsp;}
 
&nbsp;
 
&nbsp;
ioctl<span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>fd, TCGETS2, <span style="color: rgb(0, 0, 64); box-sizing: border-box;">&</span>ntio<span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;ioctl(fd, TCGETS2, &ntio);
ntio.<span style="color: rgb(0, 119, 136); box-sizing: border-box;">c_cflag</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">&</span><span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> ~CBAUD<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;ntio.c_cflag &= ~CBAUD;
ntio.<span style="color: rgb(0, 119, 136); box-sizing: border-box;">c_cflag</span> <span style="color: rgb(0, 0, 64); box-sizing: border-box;">|</span><span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> BOTHER<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;ntio.c_cflag |= BOTHER;
ntio.<span style="color: rgb(0, 119, 136); box-sizing: border-box;">c_ispeed</span> <span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> speed<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;ntio.c_ispeed = speed;
ntio.<span style="color: rgb(0, 119, 136); box-sizing: border-box;">c_ospeed</span> <span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> speed<span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;ntio.c_ospeed = speed;
retval <span style="color: rgb(0, 0, 128); box-sizing: border-box;">=</span> ioctl<span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>fd, TCSETS2, <span style="color: rgb(0, 0, 64); box-sizing: border-box;">&</span>ntio<span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;retval = ioctl(fd, TCSETS2, &ntio);
close<span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>fd<span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;close(fd);
 
&nbsp;
 
&nbsp;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">if</span> <span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span>retval <span style="color: rgb(0, 0, 128); box-sizing: border-box;">==</span> <span style="color: rgb(0, 0, 221); box-sizing: border-box;">0</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span>
+
&nbsp;if (retval == 0)
<span style="color: rgb(0, 0, 221); box-sizing: border-box;">printf</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span><span style="color: rgb(255, 0, 0); box-sizing: border-box;">"New baud configured<span style="color: rgb(0, 0, 153); font-weight: bold; box-sizing: border-box;">\n</span>"</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;&nbsp;printf("New baud configured\n");
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">else</span>
+
&nbsp;else
<span style="color: rgb(0, 0, 221); box-sizing: border-box;">perror</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">(</span><span style="color: rgb(255, 0, 0); box-sizing: border-box;">"ioctl"</span><span style="color: rgb(0, 128, 0); box-sizing: border-box;">)</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;&nbsp;perror("ioctl");
 
&nbsp;
 
&nbsp;
<span style="color: rgb(0, 0, 255); box-sizing: border-box;">return</span> <span style="color: rgb(0, 0, 221); box-sizing: border-box;">0</span><span style="color: rgb(0, 128, 128); box-sizing: border-box;">;</span>
+
&nbsp;return 0;
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span></pre>
+
}</pre>
  
 
[https://developer.toradex.cn/knowledge-base/uart-(linux)#RS485 RS-485]
 
[https://developer.toradex.cn/knowledge-base/uart-(linux)#RS485 RS-485]
Line 126: Line 126:
  
 
On carrier boards the following flags should be used:
 
On carrier boards the following flags should be used:
<pre style="background: rgb(255, 255, 255); margin: 3px 0px; padding: 10px; border-radius: 4px; border: 1px dotted rgb(204, 204, 204); color: rgb(0, 0, 0); text-transform: none; line-height: 1.4285; text-indent: 0px; letter-spacing: normal; overflow: auto; font-family: monospace !important; font-size: 14px; font-style: normal; font-weight: 400; word-spacing: 0px; display: block; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; hyphens: none;"><code style="padding: 0px; border-radius: 0px; color: inherit; font-family: monospace !important; font-size: 14px !important; white-space: pre-wrap !important; box-sizing: border-box; background-color: transparent; overflow-wrap: break-word;">...
 
    /* Enable RS485 mode: */
 
    rs485conf.flags |= SER_RS485_ENABLED;
 
  
    /* or, set logical level for RTS pin equal to 0 when sending: */
+
...<br/>&nbsp;&nbsp;&nbsp; /* Enable RS485 mode: */<br/>&nbsp;&nbsp;&nbsp; rs485conf.flags |= SER_RS485_ENABLED;
    rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
 
  
    /* Set logical level for RTS pin equal to 1 after sending: */
+
&nbsp;&nbsp;&nbsp; /* or, set logical level for RTS pin equal to 0 when sending: */<br/>&nbsp;&nbsp;&nbsp; rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
    rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
 
  
    /* Enable receiver during sending, required for i.MX devices */
+
&nbsp;&nbsp;&nbsp; /* Set logical level for RTS pin equal to 1 after sending: */<br/>&nbsp;&nbsp;&nbsp; rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
    rs485conf.flags |= SER_RS485_RX_DURING_TX;
+
 
...</code></pre>
+
&nbsp;&nbsp;&nbsp; /* Enable receiver during sending, required for i.MX devices */<br/>&nbsp;&nbsp;&nbsp; rs485conf.flags |= SER_RS485_RX_DURING_TX;<br/>...
  
 
[https://developer.toradex.cn/knowledge-base/uart-(linux)#enablingdisabling-dma Enabling/disabling DMA]
 
[https://developer.toradex.cn/knowledge-base/uart-(linux)#enablingdisabling-dma Enabling/disabling DMA]

Latest revision as of 10:29, 20 March 2020

Linux serial port/UART access from userspace is provided through tty-devices under /dev. Depending on the driver used to drive the UART, those devices can have different names.


Configuration

From user space, one can use the command line utility stty to configure the serial speed. Then the port can be treated as as a regular file:

# stty -F /dev/ttyS1 115200
# echo Test > /dev/S1

Using C, use the struct termios to set the initial baud rate:

#include <termios.h>
...
struct termios tty;
int fd;
int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
 
fd = open("/dev/ttyS0", flags);
 
tcgetattr(fd, &tty);
 
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_cflag |= B115200;
 
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
    fprintf (stderr, "error %d from tcsetattr", errno);
    return -1;
}


A fixed list of baud rates are pre-defined:

Baud rate Symbol
921600 B921600
460800 B460800
230400 B230400
115200 B115200
57600 B57600
38400 B38400
19200 B19200
9600 B9600
4800 B4800
2400 B2400
1200 B1200

Non-standard baud rate

Vybrid modules

With the following userspace C program one can configure UART custom baud rates:


#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <asm/termios.h>
 
int main(int argc, char* argv[]) {
 
 int retval, fd, speed;
 struct termios2 ntio;
 
 if (argc != 3) {
  printf("Usage: %s /dev/ttyXXX 20000\n", argv[0], argv[0]);                  
  return -1;
 }
 
 speed = atoi(argv[2]);
 fd = open(argv[1], O_RDONLY);
 if (fd < 0) {
  perror("Open");
  return -1;
 }
 
 ioctl(fd, TCGETS2, &ntio);
 ntio.c_cflag &= ~CBAUD;
 ntio.c_cflag |= BOTHER;
 ntio.c_ispeed = speed;
 ntio.c_ospeed = speed;
 retval = ioctl(fd, TCSETS2, &ntio);
 close(fd);
 
 if (retval == 0)
  printf("New baud configured\n");
 else
  perror("ioctl");
 
 return 0;
}

RS-485

The Carrier Board implements RS-485 using a single transceiver in a half-duplex communication mode, which means that one medium is shared for transmitting and receiving data. The transceiver switches to transmit mode when the RTS signal is asserted (low active).

RS-485 defines the electrical characteristics of drivers and receivers for serial communication and supports bus topology. How RS-485 is exactly implemented depends on application and requirements. The RS-485 support has been tested using the configuration as found on Carrier Boards.

User Space

On carrier boards the following flags should be used:

...
    /* Enable RS485 mode: */
    rs485conf.flags |= SER_RS485_ENABLED;

    /* or, set logical level for RTS pin equal to 0 when sending: */
    rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);

    /* Set logical level for RTS pin equal to 1 after sending: */
    rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;

    /* Enable receiver during sending, required for i.MX devices */
    rs485conf.flags |= SER_RS485_RX_DURING_TX;
...

Enabling/disabling DMA

The current driver has a weak DMA implementation, which can lead to lock ups, especially on the RX side. The non-DMA (PIO) mode is much more robust, and thanks to the hardware FIFO also quite efficient. The easiest way to disable the DMA mode is to overwrite the dma-names property:

@@ -254,6 +254,7 @@
 
 &uart0 {
        status = "okay";
+       dma-names = "", "";
 };
 
 &uart1 {