STM32 USART Library.

Some time ago I developed my own USART library for STM32, and it turned out to be very effective. So, I’ve decided to share it with you. I hope you’ll also find it very useful 🙂 In this article I’ll describe the USART library and some of its features and give an example of using it in real project.

My library is designed for STM32F4 microcontroller, but I also created the STM32F10x version. Although all the examples will be given for STM32F4 MCU, they’ll also be useful for STM32F10x, because this two versions, actually, have no differences.

The USART library consists of two files:

  • source file MT_STM32F4xx_USART.c
  • header file MT_STM32F4xx_USART.h

All library settings are located in header file, so, let’s open it in editor and analyze… The first and the most important point of configuration is selecting the USART unit which we would like to use in our project:

// Library configuration
// Select USART number

The possible values are:


In order to change the USART unit you should correct this line in header file. Moreover, you can set the baudrate of data exchange. To do this edit the following line:

//Set USART baudrate
#define MT_USART_BAUDRATE					(9600)

The default baudrate is set to 9600 b/s. While testing the library I tried different values from 4800 to 115200 b/s and it worked fine.

Now, let’s proceed to the source file MT_STM32F4xx_USART.c. All the functions needed for USART working are exactly located here. The first is initialization function:


You don’t have to pass any arguments to this function, It takes all the parameters from the configuraion (header) file.

Another two functions are intended for transmitting and receiving any amount of data:

MT_USART_SendData(uint8_t *pSendData, uint8_t nNumOfDataToSend)
MT_USART_ReceiveData(uint8_t* pReceivedData, uint8_t nNumOfDataToReceive)

The arguments are very simple:

  • the pointer to the data array where received data should be placed (in case of receiving). In case of transmitting it is the buffer for the data which should be transmitted. I think everything is clear there 🙂
  • the second argument is the number of bytes which should be transmitted/received.

All data transitions tke place in the interrupts which are located in the source file. Thus, you have no need to create them manually.

Now it is time to create a simple example project demonsrating library features. In order to do this you should add CMSIS, SPL and MT_USART_Library files in the new project. Here are the links to MT_USART_Library files:

Library for STM32F4 – MT_STM32F4xx_USART Library
Library for STM32F10x – MT_STM32F10x_USART Library

First of all we should include all files in our project:

#include "MT_STM32F4xx_USART.h"
// Declare variabled and data buffers
uint8_t sendData[8];
uint8_t receivedData[10];
uint8_t num = 8;
uint8_t numRx = 10;

Let’s transmit 8 bytes and receive 10 bytes via USART. It’s very easy to realize this with USART Library:

int main(void)
    // Set the data buffer
    sendData[0] = 0x00;
    sendData[1] = 0x11;
    sendData[2] = 0x22;
    sendData[3] = 0x33;
    sendData[4] = 0x44;
    sendData[5] = 0x55;
    sendData[6] = 0x66;
    sendData[7] = 0x77;
    // Initialization
    // Transmit data
    MT_USART_SendData(sendData, num);
    // Wait while the transmiting process is active
    // Receive data
    MT_USART_ReceiveData(receivedData, numRx);

When we start the transmitting process, the USART unit is busy, so, we have to wait until the ready flag is set to 1. After it becomes 1 we can start the receiving process.

If you want to use STM32F10x microcontoller, all the steps will be the same 🙂 Here are two projects with all libraries included:

Keil uVision 4 project for STM32F4
Keil uVision 4 project for STM32F10x

Thank you for your attention, I hope this post will help you in your work with STM32 microcontrollers!

Similar posts:

Like this post? Suggest to friends!

Leave a Reply

Your email address will not be published. Required fields are marked *