Managing Multiple UARTs in STM32
Few months ago, I wrote a tutorial about using head and tail in UART to receive and transmit data of unknown length. In that tutorial, I only used single UART to communicate with the computer. Now, in order to use some serial communication based sensors for eg- ESP8266, GPS, GSM, etc, we need to have one dedicated UART PORT to communicate with the sensor, and another UART PORT should be connected to the computer.
So, in this tutorial, we are going to manage 2 UARTs with Head and Tail method. Both would be able to receive and transmit data of unknown length. And after reading this, you will be able to use all those sensor, that require serial transmission.
In this tutorial, i am going to use STM32F446RE along with CUBEIDE and ESP8266. Which is a wifi device and works with serial transmission. Now for the sake of simplicity, let’s assume that I don’t know what response is it going to send, after I supply any AT command to this device.
The setup part is very simple. In the figure below you can see that I have enabled 2 UARTs i.e. USART1 and USART2. Both of them have the BAUD RATE of 115200, and interrupt is enabled.
UART1 is connected to ESP8266 in a way that the RX pin of UART is connected to TX pin of WiFi, and TX pin of UART is connected to RX pin of WiFi. UART2 is connected to the computer in the same manner.
L4 series and G series MCUs have some issues with UART_ISR_TXE. If you are having issues even after making changes according to UPDATE 1, then Replace the Uart_isr with the file https://controllerstech.com/wp-content/uploads/2020/10/uart_isr_multi_new.c
Those, who have errors in the Uart_isr function, like DR or SR registers are not present, replace the Uart_isr code with the one provided in this file https://controllerstech.com/wp-content/uploads/2020/04/Uart_isr.c
Some insight into the code
After generating the project, you have to include the library files into the project. Basically
UartRingbuffer_multi.c file will go in the src folder and
UartRingbuffer_multi.h file will go in the inc folder.
Above, the UARTs are being defined for the respective handlers. If you are not using uart1 or uart2, you can change them here. Don’t change the name, or else you have to make the changes everywhere in the code .
Ringbuf_init initializes both the Ring buffers. This must be called in the main file before working with Ring buffer.
IsDataAvailable checks if the data is available to read in the input buffer. The parameter, UART_HandleTypeDef *uart, is the UART where you want to check for the availability of the data.
This function returns ‘1’ if the data is available.
Uart_read reads the data from the input uart and save this data in the corresponding RX buffer. It also increments the tail, so as to indicate that the data has been read from that position, and this makes the position available to store new incoming data.
The parameter is the uart, that you want to read the data from.
Uart_write writes the data in the TX buffer to the input UART. After writing the data, the tail gets incremented and this makes up space for the new data to be stored in the TX buffer.
Uart_sendstring sends the input string to the input UART.
Get_after gets the numberofchars (number of characters) after the input string is received in the incoming stream of data. These characters would be saved in the buffertosave buffer. Once the characters are received, the function will return a ‘1’.
It must be used in the
if loop so that it can wait for the characters to be received. As the function is looking for the characters in the incoming data, the tail value in the RX buffer also increments here.
Copy_upto Copies the data from the Rx buffer of the given uart, into the entered buffer. But it will copy until the entered string is reached.
Wait_for will wait until a predefined string is received in the Rx buffer of the entered uart. If it does, than the function will return 1.
Look_for will look for a particular string inside the given buffer. If the string is found, it will return 1.
First of all I have defined the new names to the uart handlers as shown below
Next we need to initialize the ring buffer in the main function
In the while loop, if the controller receives the data from device, it will send it to the PC. And if it receives the data from the PC, it will send it to the device. This way it will act as a mediator in between device and computer.
AT+GMR command is sent from the computer to the controller. Which, on receiving this data, will send it to the device. The device sends the response, which is again received by the controller and than sent back to the computer. That response is being displayed on the picture above.
Now the the very important function i.e. Get_after. Basically, it gets the entered number of characters after the predefined string is detected in the incoming data.
In the example above, we want to get 8 characters after the string “AT version:” in the received data, which is sent from wifi_uart. And these characters will be saved in buffer. After the characters are received, the if loop will be executed, and these characters will display on the serial terminal.
As you can see above, the contents of the if loop being displayed on the serial terminal along with the 8 characters that we received. Note that, the tail in the RX buffer increments while the data is being checked for the string, so you have to use this function very carefully.
. . . . . DOWNLOAD . . . . .
You can buy me acontrollerstech
coffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code
- WavePlayer using STM32 Discovery January 16, 2021
- How to use SPI in LPC2148 December 26, 2020
- STM32 ADC MULTI CHANNEL WITHOUT DMA December 24, 2020
- Send and Receive data to PC without UART (STM32 USB COM) December 10, 2020
- STM32 GPIO INPUT Configuration December 4, 2020
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020