Using i.MX 8M UART Ports in Linux Print

 

The i.MX 8M SoC implements 4 UART controllers (UART1-4), which can be used either by the Cortex-A53 or Cortex-M4 cores. The default Linux kernel configuration for the Emcraft i.MX 8M SOM reserves UART3 for use by FreeRTOS running on the Cortex-M4 core, while other UART ports are accessible from Linux.

in Linux, parameters of each UART controller (interrupt request numbers, DMA configuration, etc.) are specified in uartX child nodes of the soc node in the arch/arm64/boot/dts/freescale/fsl-imx8mq.dtsi file, located in the Linux kernel tree:

{ ... uart3: serial@30880000 { compatible = "fsl,imx8mq-uart", "fsl,imx6q-uart", "fsl,imx21-uart"; reg = <0x0 0x30880000 0x0 0x10000>; interrupts = ; clocks = <&clk IMX8MQ_CLK_UART3_ROOT>, <&clk IMX8MQ_CLK_UART3_ROOT>; clock-names = "ipg", "per"; dmas = <&sdma1 26 4 0>, <&sdma1 27 4 0>; dma-names = "rx", "tx"; status = "disabled"; }; ... };

Note that all UART ports are disabled by default in the above file, which is generic for the entire i.MX 8M family and the i.MX 8M processor in particular. For this reason arch/arm64/boot/dts/freescale/fsl-imx8mq.dtsi rarely needs to be modified, and all detailed UART port configuration is performed by board specific device tree files (.dts).

For the Emcraft i.MX 8M SOM configuration of the UART ports is performed in the
meta-emcraft/recipes-kernel/linux/linux-imx/imx8m-som.dts file. In particular, this file enables required ports as well as sets other UART related configuration parameters, such as presence of the flow control logic. Additionally, the meta-emcraft/recipes-kernel/linux/linux-imx/imx8m-som.dts file defines which I/O pins are used by a specific UART port on the Emcraft i.MX 8M SOM. Since a single i.MX 8M I/O pin can by shared between several peripherals, a care must be taken while changing these parameters.

The UART configuration for the Emcraft i.MX 8M SOM enables UART1, UART2 and UART4 ports. The UART1 is used as the Linux console, while UART2 and UART4 provide communication channel with the built-in BT module and PCIe extension cards on the PCIe1 bus.

In the below .dts example, UART3 is enabled and configured for using by Linux on the Emcraft i.MX 8M SOM:

&iomuxc { pinctrl-names = "default"; pinctrl-0 = <&pinctrl_csiclk>; imx8m-som { ..... pinctrl_uart3: uart3grp { fsl,pins = < MX8MQ_IOMUXC_UART3_RXD_UART3_DCE_RX 0x49 MX8MQ_IOMUXC_UART3_TXD_UART3_DCE_TX 0x49 >; }; ..... } } &uart3 { pinctrl-names = "default"; pinctrl-0 = <&pinctrl_uart3>; assigned-clocks = <&clk IMX8MQ_CLK_UART3_SRC>; assigned-clock-parents = <&clk IMX8MQ_CLK_25M>; status = "okay"; };

Whenever the Linux kernel finds an enabled UART port, it automatically creates a corresponding device node file, which can be used for accessing this port using standard Linux interfaces. For the i.MX 8M UART device node files have the following format: /dev/ttymxcX, where X starts from 0. Thus, for UART1 the kernel will create the /dev/ttymxc0 file, and so on.

Typically, UART ports are used to connect various equipment such as modems, sensors, additional computers and so on. In Linux, serial ports are accessed from C-level user space code using the standard POSIX APIs. These APIs are extensively defined in various materials available in the Internet. For instance, try googling for something like this "How to access serial ports in C".

As a simple test, the following commands can be used to send/receive text to/from UART3.

  • Enable the UART3 port in the meta-emcraft/recipes-kernel/linux/linux-imx/imx8m-som.dts file, then build and update the DTB as described in Building Linux Kernel and Booting Linux from SD Card application notes.
  • Connect the UART3 port to a host and open a serial terminal client on the host side. Then, configure the port on the i.MX 8M side and start the reader process:
  • root@imx8m-som:~# stty -echo raw speed 115200 < /dev/ttymxc2 root@imx8m-som:~# cat /dev/ttymxc2

  • Write a string in the terminal client on the host side and observe the echo coming from reader process, started on the i.MX 8M side:
  • root@imx8m-som:~# Hello from the host over UART

  • Stop the reader process on the i.MX 8M side by pressing Ctrl+C. Then send a string to the host and observe the string received by the terminal client on the host side:
  • root@imx8m-som:~# echo "Hello from the i.MX 8M over UART"

Note that the default Emcraft software distribution runs a FreeRTOS demo application that makes use of UART3 on the Cortex-M4 core. To disable the application enter the following commands in the U-Boot prompt:

u-boot=> setenv rtos_file none u-boot=> saveenv

The UART3 port can be accessed on the J5 connector (first serial port) on the Emcraft IMX8M-SOM-BSB board.