cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F071C using multiple UART ports to send and recieve data.

VBote
Associate II

Dear community,

I am working on a project, and I am running into problems with the use of (multiple) UART prots at the same time. I hope, that you can help me.

Project description

-------------------

The project I am working on is a modular system of connected devices. The MCU of every device is STM32F071C8U6 and it is placed on square PCB. On each side of the square (PCB) I've placed pads which are connected to the 4 UART interfaces of the MCU in such way, that you can connect 2 PCBs on any side. The idea is to create a matrix of devices which are able to "talk" to each other via the UART interfaces.

A control interface (PC serial port) is connected to one of the units, and sends commands to the device. Every device must listen for commands on every port. When a command is recieved it is processed, stored (every command changes global variable) and if the command is valid and new for the device it should be sent to the rest localy connected devices - for example - if the command is recieved on UART2 it should be processed and sent to UART1, UART3 and UART4. Using this simple rule I am trying to achieve simple broadcast functionality over all connected devices on the matrix. 

Implementation

-------------------

I am using CubeMX to configure the MCU and Keil uVision environment. For the UART comms I am using HAL drivers. I am unable to use DMA, because there are not enough DMA streams for all 4 uarts, polling is also not an option, because I am using the main loop for communication with sensors, PWM control and other calculations. My current implementation is interrupt based.

The communication protocol I am using is very simple - just two bytes : 

-------------------

| 8 bits | 8 bits |

|Command | data |

-------------------

The problem

-------------------

1. Data corruption - When I have only 1 or two devices connected everithing works. The command from the control interface is recieved by the first device and sent to the next, and both devices are executing the commands propperly. When I connect more devices some of the devices are executig random commands, which are not sent by the control interface. I susspect that it is possible for the recieving function to miss one byte and the data to be out of allignment (data interpreted like a command and/or vice versa). I need a mechanism to avoid such scenario. 

2. Commands not recieved - happens to the devices in the middle of the matrix - maybe the device does not have enough time to process UART data comming almost simultaneously from 3 different ports.

Baisically I need a way to be able to send and recieve UART data from and to all 4 ports (almost) simultaneosuly and reliably. 

Any help would be greatly appreciated!

10 REPLIES 10
S.Ma
Principal

BREAK is a special long pause "data" which can be sent or detected optionally by the USART.

A good way to synchronise message transmission and receive data in proper order.

Now I assume that all USART have same interrupt IRQ level priority.

You have 4 USART RX byte by byte interrupt to handle.

Reset your RX buffer upon BREAK event (if present) and accumulate incoming data.

Each Buffer should be at least 20 bytes to debug.

Once you have received 2 bytes, set a flag to stop incoming data until the main loop process its.

You'll have 4 flags, 4 RX ISRs.

In the main loop, use a while loop to poll from these flags, then decide to transmit/forward/discard/end incoming message to other USART.

For transmit, just use polling for now, no interrupt.

This will let understand the mecanics and casdacing modes. you will be able to easily put priorities and breakpoints.

Once functional, you can optimize TX as ISR if you wish and probably after checking the propagation method is right.

2D cascading rise the configuration test odds.