|Using STM32 UART Ports in Linux|
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:
In stm32-som.dtsi, configure the USART3 signals on the STM32 pins PC10 and PC11:
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:
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.
/ # stty -echo raw speed 115200 < /dev/ttyS2
/ # echo "Hello from STM32 over UART" > /dev/ttyS2