Skip to main content
HDevi.1
Associate II
April 15, 2022
Question

Synchronize 2 STM32 micro using UART and DMA

  • April 15, 2022
  • 7 replies
  • 5635 views

Hello,

I am trying to synchronize 2 STM32 microcontroller's using UART and DMA. System clock is 80MHz. Timer1 is set to interrupt at 1ms with 1us resolution. Synchronization logic is executed every 1ms, Serial Transmission at master/slave side is every 1ms. In-order to synchronize perfectly reception at both the ends should be at the same time (1ms). Ideal skew should be 0. If Skew is more the Timer period tries to adjust according to skew.....this is intended sync logic I am trying to implement. Please let me know if you have any suggestions or questions as Sync is achieved sometimes but not always.

Thanks!

This topic has been closed for replies.

7 replies

Tesla DeLorean
Guru
April 15, 2022

What's the accuracy of the clocks in each of the systems?

How far apart are the systems?

If they sync to a common pulse you should be way sub-ms at this point.

Latch the value of a free running 32-bit timer on each system, use that as your timebase.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
HDevi.1
HDevi.1Author
Associate II
April 15, 2022

Hello Thanks for the reply.

1.I am not sure what you mean by accuracy of clocks.......The 2 microcontroller have separate clock source which is 8MHz crystal.....

2. Both microcontroller chips are approx 1 cm apart.

Tesla DeLorean
Guru
April 15, 2022

The offset each has in ppm?​

Why not use a common clock source?​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
HDevi.1
HDevi.1Author
Associate II
April 16, 2022

  1. Please see below screenshot from component datasheet.
  2. That's a good point. But, unfortunately I have a requirement that 2 chips need to be sourced with different source as one might fail.

0693W00000Ly89AQAR.png

Tesla DeLorean
Guru
April 16, 2022

Do they have to synchronize to an external source of time, or just each other?

I'd probably have each generate a 1PPS based on their own crystal/PLL clock, and then measure the clock cycles locally, and use that timebase/reference as a means to place 1ms period interactions. Should be able to trigger thing at better than us level.

Probably use 32-bit TIM2/TIM5

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
HDevi.1
HDevi.1Author
Associate II
April 16, 2022

They have to Sync to each other with UART communication skew of 0 usec(ideally).

MM..1
Chief III
April 16, 2022

You dont describe, what you sync. UART is async protocol and crystal ppm on speeds 115200 is not relevant error.

HDevi.1
HDevi.1Author
Associate II
April 16, 2022

I want to sync both MCU's in-order to Tx/Rx message at same time. It's async protocol, but I want to sync it based on skew of Tx and Rx of slave device.

MM..1
Chief III
April 17, 2022

I dont see idea n your design, but normal use is HALF or FULL DUPLEX.

If slave waits for receive data in Receive_IT , then on master start sending, slave start receive in same time, no sync required. = HALF DUPLEX.

If next code same time on slave wait receive start and initiate transmit = FULL DUPLEX

Pavel A.
April 16, 2022

>  But, unfortunately I have a requirement that 2 chips need to be sourced with different source as one

might fail.

This doesn't seem to make much sense. If clock source of one MCU can fail, then it won't be able to communicate as well.

Every STM32 has internal clock and can detect failure of external clock.

Make common redundant external clocks, or run each MCU on its internal clock (more reliable without external oscillator) and use SPI for communication?

HDevi.1
HDevi.1Author
Associate II
April 16, 2022

Purpose of two different external oscillator:

If clock source of one mcu fails.....other MCU (working) would atleast know that it cannot communicate with failed mcu(report a fault)...and can switch the I/O's into safe state atleast. SPI communication is good option...but it would atleast need 3/4 dedicated pins..right?

Pavel A.
April 16, 2022

STM32 SPI usually have half duplex option. So two pins: clock and data. Chip select can be permanently active (not connected between the MCUs).

Piranha
Principal III
April 16, 2022

> In-order to synchronize perfectly reception

Why is it necessary?