Full name: Universal asynchronous receiver-transmitter.
Definition
A UART (universal asynchronous receiver-transmitter) is a hardware block that sends and receives bytes one bit at a time, over one wire for each direction. Asynchronous means that no clock signal travels with the data: both sides agree in advance on the bit rate, called the baud rate, and the receiver times each bit from the start of its byte. Each byte travels in a frame: a start bit, five to nine data bits, an optional parity bit and one or two stop bits.
The idea
A device without a screen needs a way to tell a developer what it is doing, and a UART is the simplest: two signal wires and a ground wire to a USB-to-serial adapter, and a terminal program on a computer shows every byte the firmware sends. UARTs are therefore the usual console and logging port of embedded devices. A print call looks free, but at 115,200 baud a byte takes 86.8 microseconds on the wire, so a 40-character log line takes about 3.5 ms (milliseconds), longer than many operations a developer wants to time.
Picture the UART as a narrow pipe and the log text as water. Firmware can pour straight into the pipe and wait until the water has run through: synchronous output. Or it can pour into a tank that drains into the pipe by itself: buffered output. The tank hides the waiting while it has room, but it drains only at the pipe’s rate, the draining itself costs processor time later, and a full tank makes the pourer wait or spill. Logging changes timing in each of these ways. The picture stops being accurate in one respect: water mixes, but bytes leave the UART in exactly the order they were written.
How it works
The frame on the wire. The line rests at the high level when idle. Each byte is sent as follows:
The start bit pulls the line low for one bit time; its falling edge tells the receiver that a byte begins.
The data bits follow, least significant bit first, usually 8 of them.
An optional parity bit makes the number of 1 bits even (even parity) or odd (odd parity), so that the receiver can detect a single flipped bit.
The stop bit, one or two bit times at the high level, ends the frame; the next start bit can follow at once.
The common setting 8N1 means 8 data bits, no parity bit and 1 stop bit, so each byte occupies bit times. The baud rate counts symbols per second, where a symbol, also called a signal element, is one interval during which the line holds one level. A UART line has two levels, so each symbol carries one bit and the baud rate equals the bit rate. The level changes only where a bit differs from the level before it, so an 8N1 frame that carries the byte 0xFF changes level only twice in its 10 bit times. Common rates are 9,600, 115,200 and 1,000,000 baud.
Timing without a shared clock. The receiver detects the falling edge of the start bit and samples each later bit near its middle, at 1.5, 2.5, 3.5 bit times after the edge, and so on; many UARTs sample 16 times per bit to place these points. With 8N1 the last sample, in the stop bit, comes 9.5 bit times after the edge. If the two sides’ bit rates differ by a fraction , that sample drifts by bit times and must stay within half a bit of the middle, so the combined mismatch must stay below ; real designs keep well inside that.
Wires and errors. The transmit line of one side, TX (transmit), goes to the receive line of the other, RX (receive), and both sides share ground. Chip pins use logic levels such as 0 V and 3.3 V. RS-232 (Recommended Standard 232), the older serial-port standard, uses larger voltages of opposite polarity and needs a level converter. Optional lines, RTS (request to send) and CTS (clear to send), let a receiver pause the sender. A UART reports a framing error when a stop bit is not high, a parity error, and an overrun error when a byte arrives before software has read the previous one, which is then lost.
How firmware feeds the transmitter.
Polling: the processor writes one byte into the transmit register, waits until the hardware can take the next one, and repeats. The calling code is busy for the whole line time.
Interrupt-driven: the bytes go into a software ring buffer §14.15, a fixed-size queue in memory, and an interrupt §6.11 fires whenever the hardware can take more; its handler moves the next bytes. The calling code returns once its bytes are in the buffer, and the handler’s cost is paid later.
Direct memory access §4.36: a DMA (direct memory access) controller copies a whole buffer to the transmitter without the processor handling each byte.
Logging frameworks offer the same choice one level up: Zephyr’s §6.33 logging subsystem, for example, has an immediate mode, which formats and sends a message inside the logging call, and a deferred mode, which lets a separate thread do that later.
How logging changes the timing of what it describes.
Formatting turns numbers into text inside the calling code, unless the logging is deferred, and costs processor cycles.
Waiting for the line: synchronous output holds the caller for the whole line time.
A full buffer: when messages are produced faster than the line drains them, the buffer fills, and then the caller either waits, which is backpressure §9.30, or messages are dropped.
Deferred work: the interrupt handler §6.12 or thread that drains the buffer runs later and delays whatever runs then, including the next measurement.
Locks: a lock that keeps messages from interleaving makes other threads wait, and a low-priority thread that holds it can delay a high-priority one, which is priority inversion §9.16.
Masked interrupts: a print routine that disables interrupts while it writes to the hardware delays every interrupt for that time.
A changed program: adding or removing log calls changes code size, memory layout and stack use, which can move timing elsewhere and hide or expose a race condition §9.2.
Separately, the time at which a log line reaches the computer is not the time of the event it reports: the line time, the buffering in a USB-to-serial adapter, which can hold bytes for several milliseconds, and the computer’s scheduling all add delay.
Worked example
Line time. At 115,200 baud with 8N1, one bit lasts s microseconds, a byte takes s microseconds, and the line carries at most bytes per second. A 40-byte log line is bits and takes s, which is 3.472 ms.
Synchronous logging inside a timed operation. An operation does 1 ms of work and then prints that 40-byte line through polling output. Its measured duration becomes ms plus the formatting time: more than four times the real work.
Buffered logging that cannot keep up. The same line is printed every 2 ms into a 1,024-byte buffer. The program produces bytes per second and the line drains 11,520, so the buffer gains bytes per second. Starting empty, it is full after s, about 60 lines. From then on, a logger that waits stretches each 2 ms period to 3.472 ms, the line time of one message. A logger that drops instead loses of the bytes. Either way the system has changed: in the first case its timing, in the second its record.
Stated precisely
Let be the number of data bits, the number of parity bits (0 or 1) and the number of stop bits. One frame has bits. At a baud rate of bits per second, sending bytes takes at least and the line carries at most bytes per second. Idle gaps between frames make the real time longer.
If a program produces log bytes at an average rate above , a buffer of bytes that starts empty fills after seconds. After that, either the producer waits and its average rate falls to , or a fraction of the bytes is lost. An interval measured between two timestamps in one thread includes all logging work that thread did between them and all time it spent preempted by other code, including the code that drains the log.
Limits and common mistakes
A returned print call proves nothing about delivery. The bytes may still be in a buffer, on the wire or in an adapter, or may be lost to an overrun at the receiver.
Parity detects any odd number of flipped bits in one byte and misses any even number. It offers no protection against deliberate changes and is not authentication §1.6.
A UART carries bytes, not messages. Where one message ends and the next begins must be defined by the application, for example with length-prefix framing §4.30 or with a delimiter byte.
Both sides must use the same settings. A wrong baud rate produces garbage characters and framing errors; swapped TX and RX lines, or a missing ground wire, produce nothing at all.
A measurement taken with one logging configuration describes that configuration only; timings measured with verbose logging do not carry over to a build without it. To keep logging out of a measurement, keep log calls outside the timed region, store raw values in memory and print them afterwards, or mark the events with a GPIO §6.22 (general-purpose input/output) pin instead.
Questions answered
How can logging alter the timing of the event being measured?
Logging alters timing because every log message costs time somewhere in the system, and that time can fall inside the measured interval or shift the events around it. Formatting numbers into text costs processor cycles in the caller. Synchronous output makes the caller wait for the line: on a UART (universal asynchronous receiver-transmitter) at 115,200 baud with 8 data bits, no parity and 1 stop bit, a byte takes 86.8 microseconds, so a 40-byte line costs about 3.5 ms. Buffered output returns quickly only while the buffer has room; once messages outpace the line, the caller waits or messages are lost. The interrupt handler, thread or DMA (direct memory access) transfer that drains the buffer runs later and delays whatever runs then. Locks around the output make other threads wait, a print routine that masks interrupts delays interrupt handling, and adding or removing log calls changes code layout and stack use, which can hide or reveal race conditions.
To keep a measurement valid, take the timestamps at the event itself, keep log calls outside the timed interval, store raw values in memory and format them afterwards, and keep the average log rate below the line’s capacity. The time at which a log line reaches the computer is later than the event it reports and is not the event’s time.
Related entries: Length-prefix framing (§4.30), Asynchronous completion (§9.28), Measurement uncertainty (§11.33), Backpressure (§9.30), Ring buffer (§14.15), Instrumentation overhead (§11.32).