Skip to main content
Associate III
August 22, 2026
Solved

Unwanted bytes with UART in master and slave MCU

  • August 22, 2026
  • 4 replies
  • 47 views

Hello St Teams,

I have two MCUs (one as master and other as slave). They can send and receive data through UART (UART+DMA). The master/slave is always listening. Whenever, the slave receives command from the master, it sends data back to the master. The DMA is interrupted after receiving specified length of data in both MCUs. In the code, I check the first and the last byte to see everything works correctly. But the problem arises when I want to connect two MCUs via cable. At this time, the MCUs receive unwanted bytes due to glitching.
How I can make the whole system robust to unwanted bytes when the data length is fixed? What strategy would you suggest?

Best,
Davood. 

Best answer by Karl Yamashita

Use DMA in circular mode with idle interrupt. If using normal mode, it is very limiting and you can get out of sync.

 

If you have a fixed packet size, you'll need at least a CRC for the last byte. 

You'll need to create a circular buffer for the received DMA data. During Half, Complete or Idle interrupt, save the data to the circular buffer. As more data is received, you continue to add to the circular buffer.

Since you know the fixed length of the packet, you can parse through the circular buffer and calculate the CRC for the n amount of packet bytes minus the CRC byte. Then compare result to the CRC byte. 

If you've got a good CRC, then process the data or save the good packet in a queue for later processing in main while loop.

If bad CRC, then shift out the first byte. Wait until you get another byte and then recalculate the packet CRC. Repeat until you get a good packet.

4 replies

Andrew Neil
Super User
August 22, 2026

@Davood_Kesha  wrote:

But the problem arises when I want to connect two MCUs via cable. . 


How long is this cable?

Does it include a GND?

 

Note that UART alone is not suitable for long cables - you should add something like RS232, RS422, etc ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate III
August 22, 2026

Actually I am using RS485 ICs and the GND is good. 
My problem is how to make system robust against incorrect bytes???
The master controller is sending command regularly (it does not matter if the cable is connected or not).
Sometimes when I connect the cable, the received data by the slave is not complete. Since the slave DMA is configured for a specified length, it cannot be modified in the next transmissions. Then I cannot consider the first and last bytes.

 

Karl Yamashita
Karl YamashitaBest answer
Principal
August 22, 2026

Use DMA in circular mode with idle interrupt. If using normal mode, it is very limiting and you can get out of sync.

 

If you have a fixed packet size, you'll need at least a CRC for the last byte. 

You'll need to create a circular buffer for the received DMA data. During Half, Complete or Idle interrupt, save the data to the circular buffer. As more data is received, you continue to add to the circular buffer.

Since you know the fixed length of the packet, you can parse through the circular buffer and calculate the CRC for the n amount of packet bytes minus the CRC byte. Then compare result to the CRC byte. 

If you've got a good CRC, then process the data or save the good packet in a queue for later processing in main while loop.

If bad CRC, then shift out the first byte. Wait until you get another byte and then recalculate the packet CRC. Repeat until you get a good packet.

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source