Sunday, 30 November 2014

What is a UART?


I'm taking an EdX class in embedded systems and I'm having a little trouble fully grasping what a UART is. The UART has been used as a means to an end, so it hasn't been described well yet.


A UART seems to be a communication system centered around a hardware component (also referred to as the UART?). Presumably they are controlled by standardized code libraries? We've been provided with some functions that control the UART, but it is not clear whether this is part of a standard library or written by the educators:


UART_Init();    // initialize UART for printing

It is not yet clear to me whether UARTs are standard on all microcontrollers, or specific to the device we're using. It seems that UARTs have a wide range of I/O capabilities, but what they are, and how widely they are used, is not clear to me.



A beginner-level explanation that can briefly describe and connect any/all of these threads together would rock:



  • UART hardware

  • UART software interface

  • UART's place in the wider electronics world

  • overview of usage & capability of UART



Answer



UART stands for Universal Asynchronous Receiver/Transmitter and is a way computers can "talk" to each other over a serial line. It is asynchronous because there is no separate clock line like there is for other serial protocols like SPI and I2C. Each byte is preceded by a start bit and followed by one or two stop bits (usually one).


Typically speeds are 9600, 19200, 57600, or 115200 baud, although some UARTs can go over a million baud. Baud rate is essentially bits per second, so a baud rate of 115,200 means you are sending 11,500 bytes per second since each byte has the overhead of one start bit and one stop bit.



Besides talking directly with another computer chip, UARTs are sometimes used to communicate with other peripherals. For example, it is very common to use a UART to communicate with a wireless modem, be it cell radio or Bluetooth using "AT" commands.


The outputs of a UART (Tx = transmit, Rx = receive) are at the same logic levels (usually 3.3v or 5v) as the rest of the circuit. The idle state of a UART line is a logic high. To connect two microcontrollers together in close proximity, you can tie the Tx lead of one UART to the Rx lead of another and vice versa.


However UARTs are also commonly used to drive RS-232 interfaces, which allow the signals to travel for several hundred feet. For this, you need a level-shifting circuit, since RS-232 uses different voltages than the logic levels on the PCB -- namely logic high becomes a negative voltage (typically -5v to -12v), and logic low becomes a positive voltage (typically +5v to +12v).


Do NOT connect the Tx and Rx leads of a UART directly to a RS-232 cable.


UARTs are usually included inside a microcontroller set of I/O peripherals, and there will be a number of registers associated with the UART. The UART will usually have a dedicated set of library routines provided by the microcontroller's manufacturer to access them. These will include a routine (or routines) to initiate the UART, such as setting its baud rate and other characteristics such as the character size (7 or 8 bits), parity, and stop bits (you can almost always ignore these last three items).


Then there will be routines to write a character, and read a character. At 115,200 baud, there can be a character coming in every 100 µs. So you often need to use an interrupt routine to collect them into a buffer so the base level of your firmware can process them later. UARTs often provide FIFO's (anywhere from four characters to 128 or more), so it is less likely you will miss a character.


There can be several UARTs in the same microcontroller, one PIC32 I have worked with had six of them. (Usually the pins for the UARTs are shared with other peripherals like SPI and I2C, so that meant you can have a few UARTs, a couple SPI and a couple of I2C interfaces, or a lot of UARTs, but none of the others). If one does run out of UARTs on the microcontroller, you can get them in separate ICs, usually interfaced via I2C or SPI interfaces.


No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...