Using STM32 UART Ports in Linux Print


Unless you have reconfigured Linux to run the console on some interface other than the serial UART (USART1), the serial device driver is already enabled in your kernel configuration and Linux makes use of STM32 USART1 for the serial console and the high-level shell. This application note explains how to enable additional UART ports in Linux.

Do not enable any UART interfaces except for those that you plan to use in your application. For one thing, unless an UART interface is actually required, you want to keep it in reset in order to save some power. Another consideration is that UART signals may conflict with other I/O interfaces on the STM32F4 pins. More on that right below.

Allocation of the STM32 pins to specific I/O interfaces is defined in the following files in the kernel tree: projects/rootfs/rootfs.dts.STM32F4X9 and in linux/arch/arm/boot/dts/stm32-som.dtsi.

For example, let's add USART3 to the system. In rootfs.dts.STM32F4X9, add a node for USART3:

&usart3 {
status = "okay";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_usart3>;

In stm32-som.dtsi, configure the USART3 signals on the STM32 pins PC10 and PC11:

usart3 {
pinctrl_usart3: usart3-0 {
st,pins {
tx = <&gpioc 10 ALT7
rx = <&gpioc 11 ALT7

Please refer to the STM32-SOM_UART_allocation.pdf for detailed information on allocation of the UART interfaces per STM32F4 pins. Where an UART interface is available in the breadboard area of the SOM-BSB-EXT development baseboard, the document provides information regarding how to access those interfaces on the baseboard.

There must be a device node in the target root file system for each UART port to allow accessing it using standard Linux interfaces. In the rootfs project, this device node is created by Linux automatically during the system bootup. Note that /dev/ttyS0 corresponds to USART1, /dev/ttyS1 to USART2 and so on.

Having updated your project configuration as described above, build the bootable Linux image (<project>.uImage) by running make in the project directory. The procedure described here explains how to install Linux image (uImage) to the target.

When you boot the newly installed Linux image on the target, there will be a message in the kernel bootstrap print-out indicating that additional UARTs interfaces are available:

STM32 USART driver initialized
stm32-pinctrl pin-controller: maps: function usart3 group usart3-0 num 3
40004800.serial: ttyS2 at MMIO 0x40004800 (irq = 20, base_baud = 2812500) is a stm32f4-usart

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 stadard 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 USART3.

  • Loopback the RX and TX lines of USART3 on the baseboard. Then, configure port and start the reader process:
  • / # stty -echo raw speed 115200 < /dev/ttyS2
    / # cat /dev/ttyS2 &

  • Write a string to USART3 and observe the echo coming from reader:
  • / # echo "Hello from STM32 over UART" > /dev/ttyS2
    Hello from STM32 over UART
    / #