Author Topic: Rules for standard UART format  (Read 9823 times)

0 Members and 1 Guest are viewing this topic.

Offline Kittu20Topic starter

  • Regular Contributor
  • *
  • Posts: 115
  • Country: in
Rules for standard UART format
« on: December 29, 2022, 10:15:06 am »
What are the rules for standard UART ?

Suppose we have two different size of microcontroller's one is 32 bits long and other is 8 bit long. We want to transfer one byte from 32 MCU to 8 bit MCU using UART over RS232 interface.

UART is configured for 9600,N,8,1

Can 32 bit MCU tranfer 1byte to 8 bit MCU using UART?
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Rules for standard UART format
« Reply #1 on: December 29, 2022, 10:23:51 am »
There are absolutely no standards or rules.

Both sides are yours? You decice.

Communicating with existing system? Read the manual.

1 byte is of course obvious: there are no different options with byte orders, headers or whatever. Just send the byte.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17841
  • Country: fr
Re: Rules for standard UART format
« Reply #2 on: December 29, 2022, 07:22:07 pm »
The most common standard for "UART" communication is RS-232, in which a number of parameters are specified.
But UART is a generic term, and UART is not RS-232. Unless you absolutely have to be compliant with RS-232, you can do whatever you want. You won't get a fine if you don't comply with RS-232. That's a guarantee!

Obviously if you have control over the two sides, you can use whatever speed, voltage level and parity you want, and there is absolutely no problem with UART at a few Mbps these days as long as you have a short cable/PCB trace or use differential drivers such as RS485/CAN/etc (in which case up to 10 Mbps over 1 km is possible. And it's still 'UART' communication.)
 
The following users thanked this post: tellurium

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Rules for standard UART format
« Reply #3 on: December 30, 2022, 07:05:25 am »
Well, reading the opening post carefully again, it contains all the relevant information, basically a full specification of the system.

The answer to OP's question is simply: yes, of course, why not?
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 30418
  • Country: nl
    • NCT Developments
Re: Rules for standard UART format
« Reply #4 on: December 30, 2022, 07:12:41 am »
What are the rules for standard UART ?

Suppose we have two different size of microcontroller's one is 32 bits long and other is 8 bit long. We want to transfer one byte from 32 MCU to 8 bit MCU using UART over RS232 interface.

UART is configured for 9600,N,8,1

Can 32 bit MCU tranfer 1byte to 8 bit MCU using UART?
UARTs work on a per character basis. The easiest ways to talk between systems is using a text based protocol. For example: HTTP that is used for websites is text based. Text doesn't depend on byte order or anything system specific.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Rules for standard UART format
« Reply #5 on: December 30, 2022, 10:33:50 am »
UARTs work on a per character basis. The easiest ways to talk between systems is using a text based protocol. For example: HTTP that is used for websites is text based. Text doesn't depend on byte order or anything system specific.

Well, OP did specify "one byte".

Generally speaking, interfaces often provide hardware delimitation, i.e., packetization or framing (however you call it), for certain maximum message length. If the payload happens to fit within this limitation, implementation will be extremely trivial; user will get complete "packets" out of any (sanely designed) peripheral.

For UART, this limitation is typically 8 bits, which is so little that usually one needs to add another layer of framing, in software. But not always; maybe 8-bit messages are all the OP needs.

If the OP needs 8 bits of information, then not using text, but instead sending a single byte is far easier, as one does not need to think about framing at all. Sending 8 bits over text would require a protocol specification (of framing) plus implementation and verification of this protocol. It can be as simple as converting back and forth using itoa()/atoi() and using semicolon (';') to delimit messages, but it's still some work.

However, if the actual payload is limited to approx 6 bits of information, then this single byte can be "text", i.e., limited to printable ASCII characters, which indeed makes machine-human interaction easier. I use such simple protocols a lot, especially in throw-away tests/debugging, e.g. a simple keypress from '0' to '9' sets a power level. No need to write any parser. But of course, you are limited to realistically maybe 4-5 bits of information, picking convenient characters.
« Last Edit: December 30, 2022, 04:02:45 pm by Siwastaja »
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 18929
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Rules for standard UART format
« Reply #6 on: January 04, 2023, 10:11:39 am »
You may find that the buffer size on your 32 bit micro controller is still 8 or 9 bits, I am working with SAMC and the most bits you can do is 9 in one send. I kind of wish 32 were possible but 8/9 bits is the standard we picked.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12782
  • Country: nz
Re: Rules for standard UART format
« Reply #7 on: January 04, 2023, 11:11:47 am »
I usually just create a fixed length framed packet with a 3 char start code. Like STR.  (different 3 byte start codes can signify different packet lengths with different data)
Then I have some data in groups of 2 separated by an underscore. (Usually the data is raw binary data, not ascii encoded decimals/hex or anything. but being in groups of 2 it's easy to encode as hex if that makes more sense)
And then I end with 1 xor checksum byte of the data .

So like STR_34_1A_F
Where STR says start of frame, 341A is 4 bytes of data and F is xor checksum of data.
The underscore before every 2 bytes is simply a dirty hack to ensure STR can never happen in the data.

Is it a good packet structure, no
Is it efficient, no
Should you copy it, probably not.
But it's simple and easy to work with as bytes arrive in the RX interrupt. You can detect packet start, copy it into array and set flag if checksum correct so action can be taken in main() based on the data.
« Last Edit: January 04, 2023, 11:15:16 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Rules for standard UART format
« Reply #8 on: January 04, 2023, 12:29:54 pm »
Is it a good packet structure, no
Is it efficient, no

It's very typical, and there is nothing really wrong with it. It is easy to understand and implement, and well scalable because unsupported messages can be ignored (thanks to length field - I assume length is actually part of your start header - if not, make it so!). It is also very efficient because lack of features like bit stuffing, escaping etc. allows zero-copy and/or DMA implementations - payload data is usable as is after checksum verification.

Can you make something more robust against large amounts of corruption or data loss? Something that can be guaranteed to correctly synchronized in any weird data pattern? Of course. But are UART links normally that unreliable, or does unrecoverable wrong synchronization happen in real world with the scheme you describe? I'd say no. It's fine; I do that all the time and never had to replace it with something else due to any kind of problems.
« Last Edit: January 04, 2023, 12:32:54 pm by Siwastaja »
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5399
  • Country: nl
Re: Rules for standard UART format
« Reply #9 on: January 04, 2023, 02:05:50 pm »
What are the rules for standard UART ?
UART is configured for 9600,N,8,1

It's standard to set the parameters in the order they are sent, so that would be: 9600,8,N,1 And that means 9600 baud 8 Databits, No parity, 1 Stop bit. and that is the same, regardless of whether you are using a tiny 4-bit microcontrolloller, or an 64bit monster PC. RS232 uses inverted signal levels at +/- 10V (or thereabouts) while the "UART" uses signal levels which are the same as whatever process is used to etch the silicon.

Timing is quite important. You can have around 2% to 3.5% timing error. The arduino boards tend to use a 16MHz crystal that is not compatible with standard uart baudrates, and combined with the limited baudrate generator of most AVR controllers. As a result most of the timing budget is already used up because of this mismatch. If you're unlucky and the "other side" is also off by a percent or so you get bit errors.
« Last Edit: January 04, 2023, 02:09:18 pm by Doctorandus_P »
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: Rules for standard UART format
« Reply #10 on: January 05, 2023, 10:08:04 am »
Modern UART peripherals have typically an option for using 9-bit data for easier framing and/or automatic address recognition. The 9th bit will be set at at the start of a new frame, otherwise the 9th bit will remain cleared.

If one cannot or want to use 9th bit frames, there are two tricks for creating simple framing:

- Modern UART peripherals are able to generate and detect a break condition.
- It is possible to introduce a deliberate parity error as a framing marker.

If those options are not feasible, one can use a simple a SLIP frame format: https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol
There are more efficient, but more complex implementations for creating this kind of soft framings.

Another simple solution for transmitting arbitrary binary data is to transmit the data bytes using ASCII hex bytes, ie. each 8-bit data byte will be transmitted as two bytes. Framing is very simple as one can use ASCII characters for frame markers. This will reduce the bandwidth to half.
« Last Edit: January 05, 2023, 10:15:29 am by Kalvin »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12782
  • Country: nz
Re: Rules for standard UART format
« Reply #11 on: January 05, 2023, 10:14:05 am »
Yes, the 9th bit is very useful if you have the option for it.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12782
  • Country: nz
Re: Rules for standard UART format
« Reply #12 on: January 05, 2023, 10:16:03 am »
Is it a good packet structure, no
Is it efficient, no
I assume length is actually part of your start header - if not, make it so!)

I like having the 3 char header to identify a very wide range of packet structures , lengths and contents.
I mean it kind does signify the length, just not defined as a number.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 3912
  • Country: gb
Re: Rules for standard UART format
« Reply #13 on: January 05, 2023, 10:43:48 am »
If those options are not feasible, one can use a simple a SLIP frame format: https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol
There are more efficient, but more complex implementations for creating this kind of soft framings.

Perhaps you're alluding to COBS, which is a little trickier than SLIP and needs more RAM, but it can be much more efficient with its low overhead.

But I think the OP is really looking for simplicity, if they are even here anymore.
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 18929
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Rules for standard UART format
« Reply #14 on: January 05, 2023, 10:56:37 am »
The 9th bit is generally optional, I've not come across a micro that forces you to use it. On an 8 bit system being forced to do 9 bits would be silly as it doubles the buffer writes for an 8 bit bus (OK maybe they are 16 bit busses) but all these things harp back to 40+ years ago so will have all sorts of options that are for backward compatibility or have just always been there.
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 3912
  • Country: gb
Re: Rules for standard UART format
« Reply #15 on: January 05, 2023, 11:00:29 am »
The 9th bit is generally optional, I've not come across a micro that forces you to use it. On an 8 bit system being forced to do 9 bits would be silly as it doubles the buffer writes for an 8 bit bus (OK maybe they are 16 bit busses) but all these things harp back to 40+ years ago so will have all sorts of options that are for backward compatibility or have just always been there.

Nah, the 9th bit isn't a data bit.
It's just a flag which normally means "this is an address/command byte" and is used on multi-node master driven uart busses, like this device for example.
 

Online Simon

  • Global Moderator
  • *****
  • Posts: 18929
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Rules for standard UART format
« Reply #16 on: January 05, 2023, 11:14:54 am »
Yes so back in the day, 8 bits was standard, the 9th bit was added later but you can always choose. Given how low level the hardware is you get to do what you need to do for any situation, but there will always be the option to choose 8 or 9 bits.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Rules for standard UART format
« Reply #17 on: January 05, 2023, 11:26:10 am »
I like having the 3 char header to identify a very wide range of packet structures , lengths and contents.
I mean it kind does signify the length, just not defined as a number.

I understand. The benefit of explicitly giving a number is, you can extend the protocol and make the old nodes ignore messages they do not recognize (unknown message id), by skipping len bytes. If you need to look up the length based on the message id, then you need to know all the messages (their lengths) in advance.

Without knowing the length, you always go out of sync, and getting back in sync would mean losing some messages.
 
The following users thanked this post: pardo-bsso

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 11460
  • Country: fi
Re: Rules for standard UART format
« Reply #18 on: January 05, 2023, 11:31:31 am »
Nah, the 9th bit isn't a data bit.
It's just a flag which normally means "this is an address/command byte"

The 9th bit would be excellent way to introduce robust packet framing; for example, just set 9th bit as '1' at the first header byte of message. Or toggle the bit between messages.

Sadly, 9-bit UART is a rarity! Not all MCUs, UARTs on PCs or USB-UART adapters have support for it.

I remember doing a protocol based on the same idea, using the 8th bit for framing, and limiting to 7-bit data. But that 7-bit data was total PITA so I quickly got rid of it.
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 3912
  • Country: gb
Re: Rules for standard UART format
« Reply #19 on: January 05, 2023, 11:39:48 am »
8051 and a lot of PICs (all?) have the 9th bit option, and option to only get an interrupt if the 9th bit is set. That's ideal for multi-node bus, slaves don't waste time processing every packet/byte to see if its addressed to them. That scheme is used in the PIC firmware example I linked above. But I hate using 8051 and PICs now, only when forced to :P
« Last Edit: January 05, 2023, 11:55:27 am by voltsandjolts »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12782
  • Country: nz
Re: Rules for standard UART format
« Reply #20 on: January 05, 2023, 01:04:45 pm »
I like having the 3 char header to identify a very wide range of packet structures , lengths and contents.
I mean it kind does signify the length, just not defined as a number.

I understand. The benefit of explicitly giving a number is, you can extend the protocol and make the old nodes ignore messages they do not recognize (unknown message id), by skipping len bytes. If you need to look up the length based on the message id, then you need to know all the messages (their lengths) in advance.

Without knowing the length, you always go out of sync, and getting back in sync would mean losing some messages.

Should never go out of sync, any occurrence of a known 3byte start block zeros the array index and begins packet read.
Is the advantage of having that underscore in the packet. The 3byte start block can only ever exist at the start
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4606
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: Rules for standard UART format
« Reply #21 on: January 05, 2023, 01:12:48 pm »
One 'gotcha' to note - some UART peripherals (or at least, their documentation) consider the 9th bit to be the parity bit. If you configure them to 8 bit mode with parity, you only actually get 7 data bits, because the parity bit is the 8th bit.

If you want 8 data bits, plus parity, then you have to use "9 bit" mode, and if you want 9 data bits plus parity, then you're out of luck.

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12782
  • Country: nz
Re: Rules for standard UART format
« Reply #22 on: January 05, 2023, 01:23:47 pm »
Pretty sure AVR's are fine with 9bit and parity.

Infact i think AVRs have a multi processor mode for linking many MCUs on one UART bus and filter out packets in hardware if they are not addressed to the MCU in question. And i think that feature uses the 9th bit.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5399
  • Country: nl
Re: Rules for standard UART format
« Reply #23 on: January 05, 2023, 02:34:58 pm »
I've designed my own "home automation" protocol built around the 9th bit for recognizing the first (address) byte of a packet.
Both AVR and STM32 microcontrollers have built in hardware especially designed for this.
For example you can have it just ignore all traffic until it received a byte with that 9th bit active, and this reduces overhead significantly on an RS485 network with a lot of traffic for different nodes.
After an uC recognizes start of a packet, it can change it's state, decode (part of) the header and then decide what to do with the rest.

A disadvantage is that it reduces the available bandwidth by about 10% which is quite significant. More modern protocols such as CAN and USB do not use start and stop bits as the old UART does, but use bit stuffing instead. But both protocols have limited application. I decided not to use CAN because of it's very short 8 byte payload and weird adress space.

The old Modbus was quite well thought out for it's time. Packets must be sent as a continuous stream, and a silence on the serial stream indicates the end of a packet (and a re-sync event for the next packet). This is more efficient than using the 9th bit for packets longer then about 20 bytes. (assume 20 bits worth of idle time) STM32 has hardware support for this too. You can tell the uart peripheral to ignore data until after this "idle frame" is detected.

Using a pause between packets like modbus does, also makes it compatible with any uart on the market, because you only need 8N1 for the datastream.
 

Online PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 3432
  • Country: au
Re: Rules for standard UART format
« Reply #24 on: January 05, 2023, 11:24:49 pm »
Can 32 bit MCU transfer 1 byte to 8 bit MCU using UART?

Yes, it would be very rare to find any MCU UART that cannot send 8 bits.
They usually have more options.


What are the rules for standard UART ?

The common 'rule' is a leading start bit and a trailing stop bit.
That's a historic decision that unfortunately makes auto-baud more difficult than it could have been, but way, way back when that was done, MHz clocked MCU's were never dreamed of - 100+ years ago, things were using electromechanical technology  8)

https://en.wikipedia.org/wiki/Asynchronous_serial_communication


Suppose we have two different size of microcontroller's one is 32 bits long and other is 8 bit long.
We want to transfer one byte from 32 MCU to 8 bit MCU using UART over RS232 interface.
A single byte is easy, simply read the manuals.

Multiple bytes get more complex, if you want to recover from a break or disturbance. You need to know where in the packet you are.

A very simple coding scheme for small packets is to nibble encode, where some portion of each 8 bits is used to identify that data's location, or a start of frame.
a 4/4 bit split, allows 16 nibbles to be sent, each one easily identified, so 8 bytes can be managed over 16 characters sent.  Unpack/repack code is very simple.
If you need > 8 byte payloads, one nibble value can mean 'repeat and increment',  and code is slightly more complex.

It's a little wasteful in bandwidth, but you can use the predictable nibble as noise immunity improvement.

Some hardware supports 9th bit and BREAK send/detect, but that is less universal.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->