Description of variables is:
PCLK – Peripheral Clock in the Hz
MULVAL – part of fractional divider register to set the clock pre-scaler
DIVADDVAL – part of fractional divider register to set the clock pre-scalar
Note: when choosing MULVAL and DIVADDVAL values, take into consideration limitations (MULVAL cannot be 0, while DIVADDVAL can, MULVAL should be grater or equal to DIVADDVAL, etc.)
UxDLM – baud rate divider value
UxDLL – baud rate divider value
If your UART details are:
- PCLK = 25 MHz
- Baud rate = 115200
You should use following values:
- MULVAL = 15
- DIVADDVAL = 2
- UxDLM = 0
UxDLL = 12
Now, when you have all these numbers, you can start with UART initialization by writing these values into registers.
The next thing you should also consider when using UART is the challenges that UART poses depending on the type of application. For example, in real-time applications – originally UART was not designed for usage in real-time applications, but with some modifications, like taking care that UART has non-blocking functions, it can be used as well. Also, you should be especially careful when sending large amounts of data.
To prevent data loss during transmission “flow control” is used. There are two types of flow control: hardware flow control (RTS/CTS) and software flow control (XON/XOFF).
Why UART?
Every protocol has its advantages and disadvantages, including UART. We will highlight some of them.
Advantages
- Well documented and supported protocol.
- Easy to set up.
- Only two wires are necessary for full-duplex data transmission.
- Parity bit ensures basic error checking is integrated in the data frame.
- No need for clock signal.
- If both sides are set up for it, frame structure can be changed.
Disadvantages
- The size of the data frame is limited to a maximum of 9 bits if parity bit is not used, and 8 bits if party bit is used.
- Speed for data transfer is less compared with parallel data transfer.
- UART supports only communication between two devices – no multiple masters or (and) slaves.
- Appropriate baud rate must be selected on the transmitter and receiver side.
- Transmitter and receiver must agree on the rules of transmission in advance.
Where UART?
UART is one of the simplest and most used serial protocols. Today, we can find UART in GPS receivers, GSM and GPRS modems, Bluetooth modules, RFID based applications, Wireless communication systems.
Some fields where UART can (and is) used are debugging that can help with catching bugs in early development process, for manufacturing function-level tracking, also for customer or client updates and for testing purposes.
Example – using UART on STM32 with Zephyr
What is Zephyr and why it should be used is covered in our blog that you can find on our page: https://www.byte-lab.com/why-developers-should-choose-zephyr-rtos/. To configure UART on Zephyr, you need to add configuration options in proj.conf file and edit main.c file (if you already have an application directory set up). Zephyr provides three different ways to access the UART peripheral with different API functions:
- Polling API
- Interrupt-driven API
- Asynchronous API (using DMA)
Below is a code example for UART using Interrupt-driven API. With the Interrupt-drive API, slow communication can happen in the background while the thread can continue doing some other work.
Zephyr provides samples for many drivers, sensors, etc. on this git page: https://github.com/zephyrproject-rtos/zephyr/tree/main/samples.
main.c