Author Topic: UART Question  (Read 12614 times)

0 Members and 2 Guests are viewing this topic.

Offline skysurf76Topic starter

  • Regular Contributor
  • *
  • Posts: 65
UART Question
« on: November 19, 2022, 07:19:38 pm »
I've run into an interesting theoretical problem.  Lets say we have two microcontrollers whose UARTs are connected together (rx-tx, tx-rx), and one is spitting out data at 115200 nonstop. 1 start bit, 1 stop bit, no parity, 8 data bits.

We power up the second microcontroller and all it does is listen.  We know that a stop bit is low, and a start bit is a transition from high to low.  So at the end of every bit we have a low high low pattern.

The question is what if we intentionally put that low high low pattern into the data that MCU1 is sending.  How can MCU2 ever determine the boundaries of the data (where the byte begins and ends)?
« Last Edit: November 19, 2022, 07:38:48 pm by skysurf76 »
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4602
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: UART Question
« Reply #1 on: November 19, 2022, 07:32:24 pm »
It can't. Synchronisation requires a break character, which is effectively the line being high for more than a complete character's worth of time. This can be detected unambiguously, and from that point onwards, the first low bit is known to be a start bit.

Note also that if you transmit continuously on a UART interface, then eventually you'll lose characters because of the slight difference in baud rate between the two ends. There must always be occasional breaks to ensure that the long-term average data rate from the transmitter doesn't exceed the Rx rate at the receiver.

Offline skysurf76Topic starter

  • Regular Contributor
  • *
  • Posts: 65
Re: UART Question
« Reply #2 on: November 19, 2022, 07:36:27 pm »
Thats what I figured intuitively, but I couldn't find anything about it when searching for it.  Thank you for the clarification!
 

Offline wasedadoc

  • Super Contributor
  • ***
  • Posts: 1730
  • Country: gb
Re: UART Question
« Reply #3 on: November 19, 2022, 07:37:05 pm »
So at the end of every bit we have a low high low pattern.
Not so.  If adjacent bits are the same logical level there is no change of the UART's output voltage between them.
 

Offline CaptDon

  • Super Contributor
  • ***
  • Posts: 2633
  • Country: is
Re: UART Question
« Reply #4 on: November 19, 2022, 08:09:13 pm »
This is why often you will see 1.5 or 2 stop bits selected. The choice of 1.5 stop bits causes a shift word to word when out of sync so there is a chance after 8 to 10 words (bytes, characters, whatever) the link may sync up. Also, with UART's using a 16X or 64X clock, once sync'd they tend to stay sync'd even with slightly different clock speeds at each end. System throughput overruns are a major concern and is the reason for the handshaking signals. To prevent missing characters which is very important when the data is encrypted.
Collector and repairer of vintage and not so vintage electronic gadgets and test equipment. What's the difference between a pizza and a musician? A pizza can feed a family of four!! Classically trained guitarist. Sound engineer.
 
The following users thanked this post: ledtester

Online langwadt

  • Super Contributor
  • ***
  • Posts: 5849
  • Country: dk
Re: UART Question
« Reply #5 on: November 19, 2022, 08:28:07 pm »
It can't. Synchronisation requires a break character, which is effectively the line being high for more than a complete character's worth of time. This can be detected unambiguously, and from that point onwards, the first low bit is known to be a start bit.

Note also that if you transmit continuously on a UART interface, then eventually you'll lose characters because of the slight difference in baud rate between the two ends. There must always be occasional breaks to ensure that the long-term average data rate from the transmitter doesn't exceed the Rx rate at the receiver.

depends on the UART, I quite sure some UARTs with start looking for a startbit as soon as they have seen the ~middle of the stop bit
 
The following users thanked this post: westfw

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 11441
  • Country: fi
Re: UART Question
« Reply #6 on: November 20, 2022, 07:02:31 pm »
It really can't. This is an inherent design flaw in UART, and it actually sometimes causes real problems. The solution is long enough idle time. The TX side must insert these idle slots, and RX side should not start to output anything before it detects at least one idle condition.

But actual system designers often forget about this requirement, as it "usually" works out without any consideration, because many "typical" communication patterns have those idle periods anyway.

EDIT: wrote break, meant idle.
« Last Edit: November 22, 2022, 04:20:12 pm by Siwastaja »
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 19278
  • Country: us
  • DavidH
Re: UART Question
« Reply #7 on: November 20, 2022, 11:04:26 pm »
Note also that if you transmit continuously on a UART interface, then eventually you'll lose characters because of the slight difference in baud rate between the two ends. There must always be occasional breaks to ensure that the long-term average data rate from the transmitter doesn't exceed the Rx rate at the receiver.

The receiving UART should be synchronizing on every start bit so baud rate drift is removed.  I have never seen a UART lose synchronization because of continuous reception except when the signal was corrupted.

Typically the input is sampled at 8 or 16 times the baud rate so the middle of the start bit is used for synchronization, and then each bit is sampled multiple times and majority logic used to determine the value.
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 5849
  • Country: dk
Re: UART Question
« Reply #8 on: November 21, 2022, 12:47:05 am »
Note also that if you transmit continuously on a UART interface, then eventually you'll lose characters because of the slight difference in baud rate between the two ends. There must always be occasional breaks to ensure that the long-term average data rate from the transmitter doesn't exceed the Rx rate at the receiver.

The receiving UART should be synchronizing on every start bit so baud rate drift is removed.  I have never seen a UART lose synchronization because of continuous reception except when the signal was corrupted.

Typically the input is sampled at 8 or 16 times the baud rate so the middle of the start bit is used for synchronization, and then each bit is sampled multiple times and majority logic used to determine the value.

there is no transition in the middle of the start bit, the falling edge at the beginning of the startbit is what is used to find the middle of the bits

if the transmitter is slightly faster than the receiver that edge will happen before the end of the stop bit


 

Online westfw

  • Super Contributor
  • ***
  • Posts: 4670
  • Country: us
Re: UART Question
« Reply #9 on: November 22, 2022, 12:58:23 am »
Quote
Synchronisation requires a break character, which is effectively the line being high for more than a complete character's worth of time.
A "break" character is the line being LOW for more than a complete character time.  What you want for re-synchronization (and what you describe) is "line idle state." (line HIGH for a while.)
Also, it doesn't actually have to last for more than a complete character time; it just has to continue till the idle state overlaps where there should be a stop bit.  (a full byte+ time guarantees this.)

Quote
Note also that if you transmit continuously on a UART interface, then eventually you'll lose characters because of the slight difference in baud rate between the two ends. There must always be occasional breaks to ensure that the long-term average data rate from the transmitter doesn't exceed the Rx rate at the receiver.
In theory, that's the "STOP bit" that is already present between characters.  AFAIK, most UARTs wil report a "new" byte received as soon as they detect a STOP bit, which could be any HIGH state following the last data bit, or could be after whatever algorithm the UART uses to detect a "bit" (probably sampling about half way through the bit.)  After "detecting" the stop bit, the UART will then asynchronously wait for the next START bit.
This is why UARTs sample at (typically) 1/16 the baud rate, and what the "asynchronous" part in the name means; they do NOT require that the bits show up on bit-time clock edges.

Also in theory, if the UART detects a "framing error", it will reset the logic and start looking for a start bit (LOW state) again.
That means that with "random" continuous data will probably re-synchronize all by itself, after a while.  A data pattern that always puts a HIGH state on the line when the UART expects a stop bit (and therefore does not cause a framing error) is pretty uncommon.  Many "simple" UART drivers don't bother to do anything special WRT framing errors, but the UART hardware still does ... something.

I've seen this happen in real life, with data sources like newswires connected to "Terminal Servers."  Reboot the terminal server, and the data coming out the other end is wrong for some time, but ... not that long.

UART receivers implemented in software (bit-banged) don't usually over-sample their bit detection, but may tie their start-bit detection to a much faster clock (ie some implementations use a pin-change interrupt to detect the start bit, so they're pretty much sampling the data at something near the CPU clock rate.)

It would be an interesting experiment to see how short the stop bit can be before a UART doesn't like it...

Different UART implementations may behave slightly differently.  But not very much.  This is really old technology at this point, such that anything that behaves significantly differently would be labeled "buggy."
 

Offline CaptDon

  • Super Contributor
  • ***
  • Posts: 2633
  • Country: is
Re: UART Question
« Reply #10 on: November 22, 2022, 01:57:07 am »
David Hess and West, you guys are correct. There 'may' have been problems when UART's were clocked at 1X Baud Rate but when using 16X or 64X today's UART's do all the dirty work to stay in sync!!! We routinely transmit megabyte streams of data at 115K using the RS-232 signalling levels and I don't fear data loss. We routinely test with 'communication test fixtures' that let us intentionally mess with the baud rate raising or lowering it until errors are produced. You can really do some bad things until errors finally begin to show up!! One of the design goals of a good UART with oversampling clock is to intentionally re-sync at the beginning of every word thereby 'recentering' itself to anticipate the next start bit.

 
Collector and repairer of vintage and not so vintage electronic gadgets and test equipment. What's the difference between a pizza and a musician? A pizza can feed a family of four!! Classically trained guitarist. Sound engineer.
 

Offline redkitedesign

  • Regular Contributor
  • *
  • Posts: 111
  • Country: nl
    • Red Kite Design
Re: UART Question
« Reply #11 on: November 22, 2022, 06:14:47 am »
The question is what if we intentionally put that low high low pattern into the data that MCU1 is sending.  How can MCU2 ever determine the boundaries of the data (where the byte begins and ends)?

it can't, and it doesn't matter. Since you need to continue inserting the fake stop and start bits in the dataword for MCU2 to continue considering the real start and stop bits as data bits, you are rather limited in the data you can actually transmit.

One you start sending real data, the receiving MCU will find that any synchronization on databits fails, and will (depending on the quality of the MCU UART implementation) resync on another start bit. In the end, especially if there are breaks in the transmission (which can only end with a start bit) the receiving controller will resynchronize.

Timing differences shouldn't be an issue: even if the RX is not byte-synced, there are enough edges in the signal (at least 2 per word) to keep a bit-clock synchronized.

In theory, you could insert a stop and start bit in the middle of the data word, and then the two other halves (before and after the fake) could get swapped on reception.
If that is a serious risk in your application, just put an extra 0-byte in your datagram.
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 11441
  • Country: fi
Re: UART Question
« Reply #12 on: November 22, 2022, 04:22:34 pm »
David Hess and West, you guys are correct. There 'may' have been problems when UART's were clocked at 1X Baud Rate but when using 16X or 64X today's UART's do all the dirty work to stay in sync!!! We routinely transmit megabyte streams of data at 115K using the RS-232 signalling levels and I don't fear data loss. We routinely test with 'communication test fixtures' that let us intentionally mess with the baud rate raising or lowering it until errors are produced. You can really do some bad things until errors finally begin to show up!! One of the design goals of a good UART with oversampling clock is to intentionally re-sync at the beginning of every word thereby 'recentering' itself to anticipate the next start bit.

Do you realize this had absolutely nothing to do with what was discussed on this thread? Did you accidentally post to a wrong thread?
 

Online voltsandjolts

  • Supporter
  • ****
  • Posts: 3904
  • Country: gb
Re: UART Question
« Reply #13 on: November 22, 2022, 04:41:21 pm »
How can MCU2 ever determine the boundaries of the data (where the byte begins and ends)?

If you find yourself stuck in this situation (i.e. transmission is continuous) and you have to make it work, the only way I can see is by MCU2 firmware searching for a known unique character sequence. Switch the uart reception off and on (dead time of one bit time), search for that sequence, repeat until found. Yuk.

Maybe if the transmission contains a very limited character set you could do something smarter?
Maybe use a software "uart receiver" (just bit pattern detection really) initially to sync up, then switch to hardware uart at the correct time?
 

Offline CaptDon

  • Super Contributor
  • ***
  • Posts: 2633
  • Country: is
Re: UART Question
« Reply #14 on: November 22, 2022, 07:49:20 pm »
Thanks for the smart-ass comeback Swi, So just put a 1.5 stop bit time in there. Every word that is incorrectly accepted do to incorrect start timing will shift by 1/2 bit time until the damn thing does finally gain syncronization!!! and 16X or 64X oversampling helps in this situation because NO F--king bit pattern will match the extra half-bit stop timing. Wasn't 1.5 stop bits common for RTTY on the old mechanical machines to help maintain the probability of regaining sync when it was lost due to fading or interference? That is how you fix a problem. As for the original question, if the bit pattern is 1010101010101010101 for ever maybe it won't sync but depending on the number of characters in a word which could be 5,6,7, or 8 you can't in some cases create the 10101010 or 01010101010 repeating pattern and for a robust link you would incorporate this to your advantage. This can be done by having the parity bit always hi or always low  to create an extra long stop bit break. This also has nothing to do with the original post or the price of beans in China. Just wanted to bring that to your attention for your next snarky ass reply.
Collector and repairer of vintage and not so vintage electronic gadgets and test equipment. What's the difference between a pizza and a musician? A pizza can feed a family of four!! Classically trained guitarist. Sound engineer.
 

Offline CaptDon

  • Super Contributor
  • ***
  • Posts: 2633
  • Country: is
Re: UART Question
« Reply #15 on: November 23, 2022, 04:16:53 pm »
The answer to the O.P.'s question, If you could create a perfect repeating bit pattern of 10101010101...... Then yes, there would be no way to identify a true start bit and a true stop bit without some sort of idle period to establish sync. An engineer tasked with creating a robust link would insure by way of 1.5 or 2 bit stop periods and also in some cases setting the parity bit to be the same as a stop bit and using 2 stop bits in addition. This would create an unusually long stop period that may help establish sync. Also with the choice of how many bits you will include (most UART's can be set to 5,6,7, or 8) you can create a situation where the repeating bit pattern is impossible and the link will re-sync by itself at some point. Yes, in a poorly implemented link it may be impossible to find syncronization but that possibility is designed out by good engineering practices.
Sorry for my snarky comebacks.

Collector and repairer of vintage and not so vintage electronic gadgets and test equipment. What's the difference between a pizza and a musician? A pizza can feed a family of four!! Classically trained guitarist. Sound engineer.
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 11441
  • Country: fi
Re: UART Question
« Reply #16 on: November 23, 2022, 04:21:29 pm »
1.5 stop bits works in theory but I wouldn't count on it because UARTs are notorious for having implementations which lack features or are outright buggy. 1.5 stop bit mode has seen almost no use during last few decades. Even if the receiver gets the framing error part right, it would still need to correctly synchronize to the continuous stream of bits.

I have actually encountered a system once which was unable to synchronize. It had good clock accuracy and good signal integrity (which are indeed unrelated), but had continuous stream of data with no idle periods, and data which either did not either generate framing errors, or if it did, resynchronization never succeeded. It simply did not work, at all, because of the power sequencing order. Solution was to deliberately force idle periods in the data.

Because 99.9% of actual UART usage patterns include idle periods longer than a character at least now and then, any other way of resynchronization gets very little testing, and especially USB-UART adapters are rushed to the market with not much testing.
« Last Edit: November 23, 2022, 04:23:28 pm by Siwastaja »
 

Offline Peabody

  • Super Contributor
  • ***
  • Posts: 2787
  • Country: us
Re: UART Question
« Reply #17 on: November 24, 2022, 04:56:07 am »
In my experience, if both sides are using approximately the same baud rate, there is no loss of sync.  If you feed a square wave into RxD it will report a continuous stream of  the character "U", or 0x55, at 8N1. The "A" in UART is for asynchronous.  When it detects the beginning of the start bit, it then waits for 1.5 bit periods to sample the first data bit, then waits one additional bit period for each of the following data bits and the stop bit so as to read the data line in the middle of the bit period. If the stop bit is high, as it should be, it immediately transfers the received byte into the buffer and triggers the interrupt to indicate that a byte has been received, and then it starts looking for the next start bit.  So the send and receive clocks can actually be 5% off, or maybe even more, and it will still work perfectly.  There's no need for delays unless the receiving end can't process the data fast enough to keep up, but of course that has nothing to do with the UART.

I once designed ML routines to enable the C64 to perform continous simultaneous transmit and receive at 2400 baud, so as to emulate the 6551 UART chip which it didn't have, and my code just duplicated the 6551 datasheet explanation, as described above.  It worked fine.  I suspect modern UART peripherals still do it exactly the same way.
 

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 8812
  • Country: ca
    • LinkedIn
Re: UART Question
« Reply #18 on: November 24, 2022, 07:22:32 am »
Once any side begins a transmission after a dead period greater than the character length, that transmitter automatically becomes the master reference character clock at the fall of it's TXD output.  The receive side  tracks and corrects it's bit clock alignment so when it transmits a character, it is both bit synchronous and also byte synchronous.

So long as the bit size and stop bit are properly set, a continuous transmission of 0x55 would never mess up as the receiver hardware would always keep bit count from the beginning never loosing count even if your software misses a character interrupt.  The only time this would fail is if you are software enabling / disabling / re-setting up your UART.

Here I made my own synchronous UART written in verilog and I demonstrated a simulation of how I re-adapt to slight baud errors and maintain a bit and character lock when transmitting if I first received a com event from an external host.
See sim illustration here:  https://www.eevblog.com/forum/fpga/verilog-rs232-uart-and-rs232-debugger-source-code-and-educational-tutorial/

My first illustration shows how even with a huge baud rate error, my oversampling via source system clock will re-align it's character transmitter bit-stream to the incoming received clock.  If you understand verilog, my source code is well documented and illustrates how this clock re-alignment is done, but it basically boils down to an oversampling routine bit re-alignment to the drop of the incoming RXD input.

Offline m k

  • Super Contributor
  • ***
  • Posts: 3554
  • Country: fi
Re: UART Question
« Reply #19 on: November 24, 2022, 12:39:33 pm »
The question is what if we intentionally put that low high low pattern into the data that MCU1 is sending.  How can MCU2 ever determine the boundaries of the data (where the byte begins and ends)?

And so, does it matter?

Since beginning is not important the count can't be either.
And for all other cases the data is always correct, until it drifts and resyncs.
Advance-Aneng-Appa-AVO-Beckman-Danbridge-Data Precision-Data Tech-Fluke-General Radio-H. W. Sullivan-Heathkit-HP-Kaise-Kyoritsu-Leeds & Northrup-Mastech-OR-X-REO-Schneider-Simpson-Sinclair-Tektronix-Tokyo Rikosha-Topward-Triplett-Tritron-YFE
(plus work shop of the world unknowns)
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 19278
  • Country: us
  • DavidH
Re: UART Question
« Reply #20 on: November 24, 2022, 02:33:01 pm »
David Hess and West, you guys are correct. There 'may' have been problems when UART's were clocked at 1X Baud Rate but when using 16X or 64X today's UART's do all the dirty work to stay in sync!!! We routinely transmit megabyte streams of data at 115K using the RS-232 signalling levels and I don't fear data loss.

Even in the 1970s, I saw RS-232 systems run continuously for hours at a time without loss of synchronization.  In the 1980s and 1990s, I had systems that ran for days from 2400 to 115200 baud without ever a problem.  Clock drift in asynchronous systems has been a solved problem for a long time.

there is no transition in the middle of the start bit, the falling edge at the beginning of the startbit is what is used to find the middle of the bits

if the transmitter is slightly faster than the receiver that edge will happen before the end of the stop bit

I do not know exactly what logic is used, but I have to assume that the start bit is typically sampled multiple times to find the middle since the hardware for this is already present.  They are *not* going to rely on just edge detection because it would be particularly error prone.  At 8 time oversampling, maybe they wait for 3 out of 4 start bit samples.  None of the various UART datasheets I have studied discuss this particular aspect.
 
The following users thanked this post: BrianHG

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 8812
  • Country: ca
    • LinkedIn
Re: UART Question
« Reply #21 on: November 24, 2022, 05:19:41 pm »
They are *not* going to rely on just edge detection because it would be particularly error prone.  At 8 time oversampling, maybe they wait for 3 out of 4 start bit samples.  None of the various UART datasheets I have studied discuss this particular aspect.

Oversampling was for RX timing optimization.  The RS232/485 line driver transceiver was responsible for correcting edge noise.  The 'maybe they wait for 3 out of 4 start bit samples' was used for aligning to a superior sweet sample point withing each transmitted bit being received.  If you look at my posted sync UART sim with it's bit rate error tolerance, you will notice that instead of a simple 3-4 clocks, I actually work out the number of sub-sample clocks necessary for centering the sampling of each bit as my UART always samples at the full system clock, even if it is 50MHz or eve >100MHz.

Online langwadt

  • Super Contributor
  • ***
  • Posts: 5849
  • Country: dk
Re: UART Question
« Reply #22 on: November 24, 2022, 05:25:08 pm »
David Hess and West, you guys are correct. There 'may' have been problems when UART's were clocked at 1X Baud Rate but when using 16X or 64X today's UART's do all the dirty work to stay in sync!!! We routinely transmit megabyte streams of data at 115K using the RS-232 signalling levels and I don't fear data loss.

Even in the 1970s, I saw RS-232 systems run continuously for hours at a time without loss of synchronization.  In the 1980s and 1990s, I had systems that ran for days from 2400 to 115200 baud without ever a problem.  Clock drift in asynchronous systems has been a solved problem for a long time.

there is no transition in the middle of the start bit, the falling edge at the beginning of the startbit is what is used to find the middle of the bits

if the transmitter is slightly faster than the receiver that edge will happen before the end of the stop bit

I do not know exactly what logic is used, but I have to assume that the start bit is typically sampled multiple times to find the middle since the hardware for this is already present.  They are *not* going to rely on just edge detection because it would be particularly error prone.  At 8 time oversampling, maybe they wait for 3 out of 4 start bit samples.  None of the various UART datasheets I have studied discuss this particular aspect.

you have to detect the edge to find the center of the bits. So from idle, when you samples goes from 1 to 0 you know there is ~1.5 bit time to the center of the first data bit, the every 1 bit time, you are at the center at the next bit, once you get to the stop bit you start looking for a start bit again.  That is where a slow receiver might get in trouble with a continuous stream of data, because the start bit might start before it is looking at the stop bit

 

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 8812
  • Country: ca
    • LinkedIn
Re: UART Question
« Reply #23 on: November 24, 2022, 05:34:41 pm »
 :box: LOL,my UART receiver operates only at 1/2 stop bit.  Anything larger and it will re-align to the source in real time.

However, I do realize this discussion is about industry standard UARTs, though I cannot discount that modern MCU UARTs don't do the same thing, IE have the same tolerance to ignore the stop bit in the RX while only placing the selected stop-bits in their TX output.

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 11441
  • Country: fi
Re: UART Question
« Reply #24 on: November 24, 2022, 05:38:30 pm »
Again, clock drift is a different thing. When you detect a start bit, you can also zero the reference time, this is why clock drift in itself is not a problem, unless the error is very large.

The question here is, can you detect what is a start bit? The consequence of this question is much larger: if you can't, not only does clock drift become a problem, but byte separation will, too: you may be treating start and stop bits as actual data, along with the data bits that belong to the different byte. Some of these misinterpretations generate framing errors, but then again, if you detect one, the receiver only knows something is wrong. The question still is, what is the correct start bit?

And the answer to the question, can you detect a start bit, is, you always can't if the data stream is continuous, with no idle periods, with certain data. I'm too lazy to try to come up with an example with exact data pattern this could happen with, but I have seen it happen, and it's worth noting different hardware might behave differently, some may have better detection mechanisms than others, e.g. try different synchronization points until framing errors disappear. Simplest implementations might just look for idle followed by transition, which is very flaky in noisy conditions or with the wrong (tx first) power-up sequence.

The problem just seems rare because usually serial communication uses idle periods. Even if you have a longer burst of continuous data, it's still often preceded by some kind of higher level hand-shaking part.
« Last Edit: November 24, 2022, 05:43:17 pm by Siwastaja »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->