Few months ago, I made a tutorial about circular buffer in stm32 using DMA and ideal 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 tale method in STM32.
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, tale 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 tale is still at 0, as we haven’t read the characters yet. If we read the first character, ‘h‘, tale will get incremented by 1. And once we read the entire string, head and tale 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 tale 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 tale 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.
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
This completes the setup part Now let’s take a look inside some functions available.
Some Insight into the CODE
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.
Reads the data from the rx_buffer and increment the tale 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.
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 tale get incremented. This function only writes 1 character at a time.
prints the entire string to the uart.
prints the numbers in different format to the uart.
Returns the position of the given string in the rx_buffer. This returns the position where the input string ends.
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.
Let’s see an example to read and write the data
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
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.
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST