UART stands for Universal Asynchronous Receiver-Transmitter. These kinds of interfaces are used to achieve asynchronous serial communication between devices. The transmission speed and the data format are configurable.

There are 4 main pins that may be used on a UART interface:

RX, the pin that will receive data from another device

TX, the pin that will transmit data to another device

RTS, request-to-send

CTS, clear-to-send

Note: The RTS and CTS signals are used to achieve hardware flow control. We will not delve into this topic in this tutorial. Therefore, we will only use the RX and TX (and ground) pins.

The following block diagram shows some ways in which UART can be accessed:

The Apalis Evaluation Board offers 4 UART interfaces common to all SoMs from the Apalis family, presented in the Toradex Name column from the table below. The other columns provide information about the respective protocols, converters or electric standards in which the signals are available on the carrier board:

Toradex Name

Serial-to-USB converter

RS232

TTL 3.3V

Notes

UART1

X29

X28 bottom

X6/X7

-

UART2

-

X28 top

X6/X7

-

UART3

-

-

X3/X4

X30 - IrDA

UART4

-

-

X3/X4

X38 - Mezzanine

The following table relates the Toradex Name to the corresponding Linux device file:

Toradex Name

Device

UART1

/dev/ttyS0

UART2

/dev/ttyTHS1

UART3

/dev/ttyTHS2

UART4

/dev/ttyTHS3

In this lesson, the UART2 interface is used in the examples and the Toradex naming convention (Toradex Name) is used unless otherwise stated.

Note: Please refer to the FAQ for more information on how to use UARTs that are disabled in the default BSP configuration.

Copyright (c) 2017, Toradex All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Toradex nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Toradex BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Using some functions of the termios library, get the serial port attributes and configure the communication parameters and other properties. This example uses the fairly common 9600/8N1 configuration, standing for a baud rate of 9600 baud, 8 data bits, no parity bit and 1 stop bit.

After writing to the serial port, you can read from it. Since it's a loopback, you will read what you just wrote and save it to the buffer variable. Also, don't forget to use the standard system call close() to close the device when you're done. To avoid timing problems, use a delay between writing and reading.

You can use the other available UART interfaces simply by changing the device path and the connected pins. The paths are listed in UART (Linux) for every module. To find the correct pins, refer to your module's datasheet.

Extra configuration effort may be required when using a UART that is not standard to the respective Computer on Module family. Usually this means that you have to modify the device-tree.

The debug UART is used as a serial debug console by default. Toradex does not recommend using it for any other purpose, since it will prevent you from analysing error messages or controlling the system via serial. However, if your design allows you to disable the serial console, you can do it by following this article. If you do this, you can use the debug UART by following the same instructions provided in this lesson with minor changes.