Search by Tags
  • Computer on Module
    Apalis iMX6 Dual/Quad
  • Carrier Board
    Ixora Carrier Board
  • Computer on Module OS
    Linux
  • Development PC OS
    Linux

Basic UART usage - Ixora Carrier Board - Apalis iMX6

 
 

Attention: the Quickstart Guide for BSP 2.8, based on the Ångström distribution, is not being updated anymore. Depending on your SoM, you have different options:

Vybrid and Tegra: the information is provided as-is and still accurate, since newer Toradex BSPs are not ported to those SoMs. Just keep in mind that the Guides are not being maintained anymore, even if we find bugs or outdated instructions.

Apalis TK1 (all variants), Colibri iMX6ULL (all variants), Colibri iMX7S 256MB and Colibri iMX7D 512MB: these computer on modules are still regularly maintained in our BSPs and, to get started, you must check the software page Toradex BSP Layers and Reference Images for Yocto Project. Since Torizon is not supported, at the moment a Quickstart Guide is not available.

All other i.MX-based SoMs: you have two options to get started with embedded Linux: the first is to follow the Quickstart Guide for Torizon, which provides the greatest out-of-the-box experience, or if you choose to use Yocto, check the software page Toradex BSP Layers and Reference Images for Yocto Project.

Overview

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 on Toradex's knowledge base article UART (Linux).

Attention: Never connect a RS-232 output pin to a TTL input. This might lead to permanent damage.

Materials Required

  • Jumper wires

Step 1

Be introduced to the UART protocol:

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:


  • UART Block Diagram

    UART interfaces block diagram

Step 2

Get used to the naming conventions:

The Ixora Carrier 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 RS232 TTL 3.3V
UART1 X22 -
UART2 X21 -
UART3 X21 X27
UART4 - X27

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

Toradex Name Device
UART1 /dev/ttymxc0
UART2 /dev/ttymxc1
UART3 /dev/ttymxc3
UART4 /dev/ttymxc4

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.

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 UART2 TX pin (X21.5) to its RX pin (X21.3).


  • Ixora UART wiring

    Ixora UART wiring

Step 5

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

stty -F /dev/ttymxc1 9600 -echo

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

cat < /dev/ttymxc1 &

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

echo "Testing UART" > /dev/ttymxc1
Testing UART

Step 6

A 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:

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

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 UART2 "first_uart" and UART3 "second_uart".

Connect UART2_RXD (X21.3) to UART3_TXD (X21.7) and UART2_TXD (X21.5) to UART3_RXD (X21.1).

Build and run the following code:

Example communicating between UARTs on the same board

FAQ

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 the one used in this lesson
Can the debug UART be used for general purpose
How can I disable DMA for RX