Search by Tags

Basic UART usage - Aster Carrier Board - Colibri VFxx


Your progress


Getting Started with UART


In this lesson, you will:

  • Learn the basics of using UART on Linux
  • Read and write from UART using the command line
  • Read and write from UART using an application written in C

The information provided in this guide is based in Toradex' knowledge base article UART (Linux).

Materials Required

The Aster Carrier Board offers 3 UART interfaces:

Serial-to-USB converter Pin header
UART_A X4 X11/X12
UART_B - X20

For this tutorial, we will use the UART_B interface. You will need:

  • Jumper wires
  • USB Type-B to Type-A cable

Note: Depending on the module used, the UART interfaces are indexed with numbers (UART1, UART2, etc.) or letters (UARTA, UARTB, etc.). For this tutorial, we'll stick with the nomenclature used by the carrier board, which indexes the UART peripherals using letters (UART_A, UART_B, etc.).

Step 1

UART stands for Universal Asynchronous Receiver-Transmitter. These kind 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 pins.

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

  • UART Block Diagram

    UART interfaces block diagram

Step 2

The following table contains the UART interfaces available by default along with their designators on the module, the board and Linux device.

Module Base Board Linux device
UART_A UART_A /dev/ttyLP0
UART_B UART_B /dev/ttyLP2

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

Step 3

The correspondence between UART interface signals and SoM/Carrier Board pins is provided in the following table:

Note: The notation CONNECTOR.PIN will be employed in this lesson, e.g. X12.5 means pin 5 on the X12 connector.

UART signals and pins

Step 4

A loopback test will be run from command-line. This will send and receive data from the same serial port to verify if it's working.

Connect your board's UART_B TX pin (X20.8) to its RX pin (X20.10).

  • Aster UART Wiring

    Aster UART Wiring

Step 5

From a serial terminal connected to the module, configure the UART_B baud rate using the stty command. We will use a baud rate of 9600 baud:

stty -F /dev/ttyLP2 9600 -echo

Use the cat command to listen for incoming data on the serial port:

cat < /dev/ttyLP2 &

Write to the serial port. The characters sent will be printed back to you in the next line:

echo "Testing UART" > /dev/ttyLP2
Testing UART

Step 6

An UART loopback test can also be written in C.

Termios, the Unix API for terminal I/O will be employed.

An example will be introduced step-by-step. First of all, include some libraries and initialize variables:

Warning: The source codes provided in this guide are distributed under the 3-clause BSD license terms. See below:

Loopback example - Initializing

Step 7

To use the serial port, open the serial device using the standard system call open(), passing the device path and the flags as arguments.

Loopback example - Opening device

Step 8

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.

Loopback example - Configuring device

Step 9

Now you can use the standard system calls write() and read() to write to and read from the serial port.

First, flush the receiving buffer to discard old data. Then, store a string into the buffer variable and write it to the serial port.

Loopback example - Reading and writing

Step 10

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 reading and writing.

Loopback example - Closing device

Step 11

Test the complete code. Create and configure an Eclipse project as described in Module 2.

Build and run the following code:

Loopback test code

Step 12

We can improve upon the code by adding error checks:

Improved loopback test

Step 13

The following example demonstrates communication between two UART ports on your board. It shows how to use more than one serial port at a time.

For this example, we will call UART_B "first_uart" and UNO_UART "second_uart".

Connect UART_B_RXD (X20.10) to UNO_UART_TXD (X17.2) and UART_B_TXD (X20.8) to UNO_UART_RXD (X17.1).

Build and run the following code:

Example communicating between UARTs on the same board


This lesson covers the basics of UART usage on Linux. There are other important topics that were not discussed. This FAQ section provides further information and sources.

How can use another UART interface instead of UART_B
Can UART_A be used for general purpose
How can I disable DMA for RX