Few months ago, I made a tutorial about circular buffer in STM32 using DMA and idle line detection. Although it was working pretty well, it was a little bit complex to work with and had few problems. Today in this tutorial, I am going to show you guys an alternative for that. Here, we are going to implement ring buffer using head and tail method in STM32.

This tutorial is written in STM32CUBEIDE. It can work with other IDEs also, you just have to use the library files. You can download the code at the end of the post. The library files are inside the project folders itself.

How the ring buffer works

Well, I would suggest that you google this query and take a look at the wikipedia page. There you will get a better explanation of the topic. But anyway, i will try to explain it, as much as i can.

Whenever UART receives data in the rx_buffer, the head gets incremented by 1. And when we read that data, tail gets incremented by 1. Now let’s assume that we sent a string “hello“. So the UART received 5 characters and they get stored in the rx_buffer. So head will now have a value of 5. The tail is still at 0, as we haven’t read the characters yet. If we read the first character, ‘h‘, tail will get incremented by 1. And once we read the entire string, head and tail will both be at 5.

Once the buffer is full, or the head is at the end of the buffer, on receiving new character, it will again go to the beginning of the buffer i.e at 0.

Reading the buffer, so that the tail can increment, is very important part of this process. The data will only be written in circular direction, if the data at that position (start of buffer) is already read. In other words, if the difference between head and tail is more than the buffer size, no data will be received in the buffer.


Setting up the ring buffer in STM32 is very easy, Easier than circular buffer at least. Below is my Cubemx setup. I am using baud rate of 115200 and interrupt is enabled. The rest is the usual setup.

CubeMx setup for uart

Next copy the uartringbuffer.h and uartringbuffer.c files in the code and also include it in the main file. These files are located under /src and /inc folders of the code attached here. You should also copy them to the same locations in your project.

Once copied, open the stm32….it.c file and copy extern void Uart_isr (UART_HandleTypeDef *huart); in the file. And at last we need to replace the default ISR with the one we have. So browse down the file to the void USART1_IRQHandler(void) and replace the default ISR just as shown in the picture below

ISR file edit

This completes the setup part Now let’s take a look inside some functions available.

Some Insight into the CODE

int IsDataAvailable(void);

checks whether the data is available to read. It returns 1 is there is some data in the rx_buffer, which is not read yet.

int Uart_read(void);

Reads the data from the rx_buffer and increment the tail by 1. The head in the rx_buffer get incremented when the data is received in the buffer via the interrupt. only reads 1 character and returns it.

void Uart_write(int c); 

writes the data to the tx_buffer and increment the head counter in the tx_buffer by 1. From where, the data is sent to the uart using interrupt and tail get incremented. This function only writes 1 character at a time.

void Uart_sendstring(const char *s);

prints the entire string to the uart.

void Uart_printbase (long n, uint8_t base);

prints the numbers in different format to the uart.

uint16_t Get_position (char *string);

Returns the position of the given string in the rx_buffer. This returns the position where the input string ends.

void Get_string (char *buffer);

copy the string from the rx_buffer, to the buffer. This function look for the terminating character (‘\n’) to identify that the string has been entered.

/*After the predefined string is reached, it will copy the numberofchars after that into the buffertosave
 *USAGE---->>> if (Get_after("some string", 8, buffer)) { do something here}
 * */
int Get_after (char *string, uint8_t numberofchars, char *buffertosave);

Get_after function is used to get the defined number of characters after the entered string is detected in the incoming data. The usage of this is shown below

if (Get_after("hello", 5, buffer))
  Uart_sendstring("the data is\r\n");

Let’s see an example to read and write the data

while (IsDataAvailable())
  int data = Uart_read();  // read the data in the rx_buffer
  Uart_write(data);  // send the data to the uart 

So, if the data is available in the rx_buffer, it will be read using Uart_read () function and than again written back to the uart. This way you can test whether the things are working properly or not. Below is the screenshot of the terminal

ring buffer working

Note that the pink color is the one that I am sending to the microcontroller and the black one is the received data back in the computer. As you can see above that either I sent 1 character, 2 characters or 5 characters, I always received the same data back from the microcontroller.

This way we can always receive the data of unknown length. Make sure that the size of the rx_buffer (in the uartringbuffer.h) is according to your need. If the data received at once, is higher than the size, the whole code will block there and you might have to reset the controller.




Check out the VIDEO Below


You can buy me a coffee sensor ūüôā

download the CODE below


Leave a Reply

4 Comment threads
4 Thread replies
Most reacted comment
Hottest comment thread
5 Comment authors
MehrdadDonzothomasvnladminKiel Recent comment authors
newest oldest most voted
Notify of

Hey is it Possible to control 5 different LED by One push button switch?


Hi, Thanks for sharing this code. The ZIP file that is included with this post has two methods that look very similar, namely Get_string and wait_until. The wait_until method included probably does not work as intended. It has two input parameters, “char *string” and “char *buffertostore”. In the method, one would expect the “*string” parameter to be used to, as the name implies, wait until that string has been received into the ring buffer, but the method does not use this parameter at all (in the shared code). Instead it looks like it only searches for the “\n” (line feed)… Read more »


How about porting this great code to xilinx SDK ? Should work ? XUartPs_Send() and XUartPs_Recv() ?



Thanks for the code and explanation. i have a question: is it possible to send an interrupt when receiving data is completely done and the line is idle using this library?