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

From ESS-WIKI
Jump to: navigation, search
(Created page with "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...")
 
Line 25: Line 25:
 
<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>
 
<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>
 
<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, 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&nbsp;%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>
+
     <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>
 
     <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>
 
     <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>
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
+
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span></pre>
</pre>
 
  
  
Line 91: Line 90:
 
&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>
 
<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>
<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:&nbsp;%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>                   
+
<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>                   
 
<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>
 
<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>
 
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
 
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
Line 116: Line 115:
 
&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>
 
<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>
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
+
<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 140: Line 138:
 
     /* Enable receiver during sending, required for i.MX devices */
 
     /* Enable receiver during sending, required for i.MX devices */
 
     rs485conf.flags |= SER_RS485_RX_DURING_TX;
 
     rs485conf.flags |= SER_RS485_RX_DURING_TX;
...
+
...</code></pre>
</code></pre>
 
  
 
[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]

Revision as of 10:25, 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:

<span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <termios.h></span>
...
<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>
<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>
<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>
 
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>
 
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>
 
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.<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>
 
<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>
<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>
    <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>
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>


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:


<span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <stdio.h></span>
<span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <fcntl.h></span>
<span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <errno.h></span>
<span style="color: rgb(51, 153, 0); box-sizing: border-box;">#include <asm/termios.h></span>
 
<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>
 
	<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>
	<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>
 
	<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>
		<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>                   
		<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>
	<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
 
	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>
	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>
	<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>
		<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>
		<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>
	<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
 
	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>
	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>
	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>
	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>
	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>
	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>
	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>
 
	<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>
		<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>
	<span style="color: rgb(0, 0, 255); box-sizing: border-box;">else</span>
		<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>
 
	<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>
<span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>

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:

<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: */
    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;
...</code>

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 {