|
|
(One intermediate revision by the same user not shown) |
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; |
| | | |
− | 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); |
| | | |
− | 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); |
| | | |
− | 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; |
| | | |
− | <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>
| + | 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>
| + | return -1; |
− | <span style="color: rgb(0, 128, 0); box-sizing: border-box;">}</span>
| + | }</pre> |
− | </pre> | |
| | | |
| | | |
Line 80: |
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> |
| | | |
− | <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[]) { |
| | | |
− | <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>
| + | 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>
| + | struct termios2 ntio; |
| | | |
− | <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>
| + | 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>
| + | printf("Usage: %s /dev/ttyXXX 20000\n", argv[0], argv[0]); |
− | <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>
| + | return -1; |
− | <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>
| + | 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>
| + | 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>
| + | 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>
| + | 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>
| + | return -1; |
− | <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>
| + | 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>
| + | 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>
| + | 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>
| + | 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>
| + | 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>
| + | 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>
| + | close(fd); |
| | | |
− | <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>
| + | 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>
| + | printf("New baud configured\n"); |
− | <span style="color: rgb(0, 0, 255); box-sizing: border-box;">else</span>
| + | 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>
| + | perror("ioctl"); |
| | | |
− | <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>
| + | 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 128: |
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/> /* Enable RS485 mode: */<br/> rs485conf.flags |= SER_RS485_ENABLED; |
− | rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
| |
| | | |
− | /* Set logical level for RTS pin equal to 1 after sending: */
| + | /* or, set logical level for RTS pin equal to 0 when sending: */<br/> rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND); |
− | rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
| |
| | | |
− | /* Enable receiver during sending, required for i.MX devices */
| + | /* Set logical level for RTS pin equal to 1 after sending: */<br/> rs485conf.flags |= SER_RS485_RTS_AFTER_SEND; |
− | rs485conf.flags |= SER_RS485_RX_DURING_TX;
| + | |
− | ... | + | /* Enable receiver during sending, required for i.MX devices */<br/> rs485conf.flags |= SER_RS485_RX_DURING_TX;<br/>... |
− | </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] |
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.
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:
With the following userspace C program one can configure UART custom baud rates:
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.
...
/* Enable RS485 mode: */
rs485conf.flags |= SER_RS485_ENABLED;
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: