<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>UART on Tom's Lab Notes</title><link>https://vtbl.dev/tags/uart/</link><description>Recent content in UART on Tom's Lab Notes</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Tue, 27 Oct 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://vtbl.dev/tags/uart/index.xml" rel="self" type="application/rss+xml"/><item><title>An Introduction to UART using FPGAs</title><link>https://vtbl.dev/posts/uart-intro/</link><pubDate>Tue, 27 Oct 2026 00:00:00 +0000</pubDate><guid>https://vtbl.dev/posts/uart-intro/</guid><description>&lt;p>This post introduces the UART communication protocol and shows a basic implementation of a transmitter in SystemVerilog using a Tang Nano 20k FPGA.&lt;br>
The transmitter created for this post sends an incrementing 16-bit counter, which is received by a small C program that just prints the data to the console.
This post is accompanied by a &lt;a class="link" href="https://github.com/tomschammo/uart_demo" target="_blank" rel="noopener"
>repo&lt;/a>, which contains the source code for this project.&lt;/p>
&lt;h1 id="what-is-uart">What is UART?
&lt;/h1>&lt;p>UART is one of the simplest communication protocols, if not the simplest. It requires only a tx/rx connection and ground but no clock line.
This however requires the speed to be pre-agreed so that both ends have the correct bit timing.
The protocol transmits data frame by frame with one bit per baud tick.
An example frame is displayed in the following figure.&lt;/p>
&lt;figure class="centered-fig">&lt;img src="https://vtbl.dev/images/uart-intro/frame.svg"
alt="Example 8-bit frame">&lt;figcaption>
&lt;p>Example 8-bit frame&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>A communication line idles in a high state and is pulled low for the duration of one bit to indicate a start.
This is followed by (in this case) 8 data bits, starting with the least significant bit (LSB).
A high data bit encodes a 1 and a low data bit encodes a 0.
The number of data bits can vary, however. Just like the baud rate, the number of data bits has to be agreed upon by sender and receiver.
A sequence is terminated by pulling the line high for the duration of one bit (2 is also possible) either returning it to the idle state or allowing it to be pulled low for a start bit for the next tick.
Optionally a parity bit can be inserted between the last data bit and the stop bit, however this demo does not use a parity bit for simplicity.&lt;br>
The figure below shows what transmission of one counter value would look like.&lt;/p>
&lt;figure class="centered-fig">&lt;img src="https://vtbl.dev/images/uart-intro/counter.svg"
alt="Example transmission of one counter value">&lt;figcaption>
&lt;p>Example transmission of one counter value&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>To transmit the full 16 bits, two frames are transmitted back to back, again starting with the LSB and the first byte forming the least significant byte.&lt;/p>
&lt;h2 id="building-the-transmitter">Building the Transmitter
&lt;/h2>&lt;p>The transmitter is based on the following simple state machine.&lt;/p>
&lt;figure class="centered-fig">&lt;img src="https://vtbl.dev/images/uart-intro/fsm.svg"
alt="Transmitter state machine. Diagram created by generative AI.">&lt;figcaption>
&lt;p>Transmitter state machine. Diagram created by generative AI.&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>When the board is powered on (or the reset button is pressed), the FPGA is in its initial state.
In the initial state, the &lt;code>state&lt;/code> variable is set to &lt;code>START&lt;/code>, which indicates the next state.
If the reset button is pressed, the LED closest to the USB-C port is turned on.
The corresponding bit for the LED is the LSB, and the LEDs are low-active, so it has to be set to 0 to light up and indicate a reset (and set to &lt;code>1&lt;/code> after the button has been released).
The data line is pulled high, as the system is currently not transmitting.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-verilog" data-lang="verilog">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">1&lt;/span>&lt;span>baud_counter &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">8&lt;/span>&lt;span style="color:#d3869b">&amp;#39;d0&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">2&lt;/span>&lt;span>counter &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">16&lt;/span>&lt;span style="color:#d3869b">&amp;#39;d0&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">3&lt;/span>&lt;span>bit_index &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">4&lt;/span>&lt;span style="color:#d3869b">&amp;#39;d0&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">4&lt;/span>&lt;span>bit_counter &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">3&lt;/span>&lt;span style="color:#d3869b">&amp;#39;d0&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">5&lt;/span>&lt;span>state &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> START;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">6&lt;/span>&lt;span>uart_tx &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">1&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b1&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">7&lt;/span>&lt;span>led &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">6&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b111110&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">8&lt;/span>&lt;span>idle_counter &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">16&lt;/span>&lt;span style="color:#d3869b">&amp;#39;d0&lt;/span>;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The frequency of the FPGA is 27 MHz&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup>. At the chosen baud rate of $115\,200 \text{ baud}$, that is $\frac{27\,000\,000 \text{ Hz}}{115\,200 \text{ baud}} = 234.375$ cycles per bit.
In the implementation, the baud counter counts up to 234, which is 235 cycles, resulting in an effective baud rate of $\frac{27\,000\,000 \text{ Hz}}{235 \text{ cycles/bit}} = 114\,893.617 \text{ baud}$,
which is $1-\frac{114\,893.617 \text{ baud}}{115\,200 \text{ baud}} = 0.002659 = 0.266\%$ too slow. Well within tolerance&lt;sup id="fnref:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup>.&lt;/p>
&lt;p>After transitioning into the &lt;code>START&lt;/code> state, the data line is pulled low and the state transitions to the &lt;code>DATA&lt;/code> state:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-verilog" data-lang="verilog">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">1&lt;/span>&lt;span>&lt;span style="color:#fb4934">START:&lt;/span> &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">2&lt;/span>&lt;span> uart_tx &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">1&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b0&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">3&lt;/span>&lt;span> state &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> DATA;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">4&lt;/span>&lt;span>&lt;span style="color:#fe8019">end&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>In the data state the counter data is put onto the data line bit by bit until one frame has been sent:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-verilog" data-lang="verilog">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 1&lt;/span>&lt;span>&lt;span style="color:#fb4934">DATA:&lt;/span> &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 2&lt;/span>&lt;span> uart_tx &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> counter[bit_index];
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 3&lt;/span>&lt;span> bit_index &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> bit_index &lt;span style="color:#fe8019">+&lt;/span> &lt;span style="color:#d3869b">1&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b1&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 4&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 5&lt;/span>&lt;span> &lt;span style="color:#928374;font-style:italic">// has one window been sent
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 6&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span> &lt;span style="color:#fe8019">if&lt;/span> (bit_counter &lt;span style="color:#fe8019">==&lt;/span> &lt;span style="color:#d3869b">3&lt;/span>&lt;span style="color:#d3869b">&amp;#39;d7&lt;/span>) &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 7&lt;/span>&lt;span> bit_counter &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">3&lt;/span>&lt;span style="color:#d3869b">&amp;#39;d0&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 8&lt;/span>&lt;span> state &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> STOP;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 9&lt;/span>&lt;span> &lt;span style="color:#fe8019">end&lt;/span> &lt;span style="color:#fe8019">else&lt;/span> &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">10&lt;/span>&lt;span> bit_counter &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> bit_counter &lt;span style="color:#fe8019">+&lt;/span> &lt;span style="color:#d3869b">1&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b1&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">11&lt;/span>&lt;span> &lt;span style="color:#fe8019">end&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">12&lt;/span>&lt;span>&lt;span style="color:#fe8019">end&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>After a frame has been transmitted, the state transitions to the &lt;code>STOP&lt;/code> state.
In the &lt;code>STOP&lt;/code> state, the data line is pulled high to send the stop bit and the next state is either set to &lt;code>IDLE&lt;/code> or back to &lt;code>START&lt;/code> depending on whether another frame follows immediately after.
This is decided by checking whether the &lt;code>bit_index&lt;/code> is 0. As a 4 bit value, it wraps around after 16 bits to 0, so a non-zero value means that the whole 16 bits have not been transmitted yet.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-verilog" data-lang="verilog">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 1&lt;/span>&lt;span>&lt;span style="color:#fb4934">STOP:&lt;/span> &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 2&lt;/span>&lt;span> uart_tx &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#d3869b">1&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b1&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 3&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 4&lt;/span>&lt;span> &lt;span style="color:#928374;font-style:italic">// continue transmitting
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 5&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span> &lt;span style="color:#fe8019">if&lt;/span> (bit_index &lt;span style="color:#fe8019">!=&lt;/span> &lt;span style="color:#d3869b">4&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b0&lt;/span>) &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 6&lt;/span>&lt;span> state &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> START;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 7&lt;/span>&lt;span> &lt;span style="color:#fe8019">end&lt;/span> &lt;span style="color:#fe8019">else&lt;/span> &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 8&lt;/span>&lt;span> state &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> IDLE;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59"> 9&lt;/span>&lt;span> &lt;span style="color:#fe8019">end&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">10&lt;/span>&lt;span>&lt;span style="color:#fe8019">end&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The transition to &lt;code>START&lt;/code> repeats the same sequence until both frames have been sent.
Then the state transitions into &lt;code>IDLE&lt;/code>.
In the &lt;code>IDLE&lt;/code> state a 16 bit counter is used to pace the transmission and to blink an LED to show that the transmitter is running:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-verilog" data-lang="verilog">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">1&lt;/span>&lt;span>&lt;span style="color:#fb4934">IDLE:&lt;/span> &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">2&lt;/span>&lt;span> idle_counter &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> idle_counter &lt;span style="color:#fe8019">+&lt;/span> &lt;span style="color:#d3869b">1&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b1&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">3&lt;/span>&lt;span> &lt;span style="color:#fe8019">if&lt;/span> (idle_counter &lt;span style="color:#fe8019">==&lt;/span> &lt;span style="color:#d3869b">16&amp;#39;hffff&lt;/span>) &lt;span style="color:#fe8019">begin&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">4&lt;/span>&lt;span> led[&lt;span style="color:#d3869b">5&lt;/span>] &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> &lt;span style="color:#fe8019">~&lt;/span>led[&lt;span style="color:#d3869b">5&lt;/span>];
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">5&lt;/span>&lt;span> counter &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> counter &lt;span style="color:#fe8019">+&lt;/span> &lt;span style="color:#d3869b">1&lt;/span>&lt;span style="color:#d3869b">&amp;#39;b1&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">6&lt;/span>&lt;span> state &lt;span style="color:#fe8019">&amp;lt;=&lt;/span> START;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">7&lt;/span>&lt;span> &lt;span style="color:#fe8019">end&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">8&lt;/span>&lt;span>&lt;span style="color:#fe8019">end&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>After a total of $235 \text{ cycles/tick} \cdot 2^{16} \text{ ticks} = 15\,400\,960 \text{ cycles}$, the counter is increased by one and the state machine transitions back to &lt;code>START&lt;/code> to start sending the next counter value.&lt;/p>
&lt;h2 id="reading-the-output">Reading the Output
&lt;/h2>&lt;h3 id="terminal">Terminal
&lt;/h3>&lt;p>One of the easiest ways to read data from a UART bus is using a program called &lt;code>screen&lt;/code>.
Screen interprets each byte as a character, but in this case the bytes are a binary counter, which results in output that looks like garbage.&lt;/p>
&lt;p>To address this, a small receiver has been &lt;a class="link" href="https://github.com/TomSchammo/uart_demo/blob/main/src/uart.c" target="_blank" rel="noopener"
>implemented&lt;/a> in C.
The receiver assumes that the first byte it reads is the low byte.
When compiling and running the program the counter increments correctly and pressing the reset button (S1) resets the counter to 0.&lt;/p>
&lt;h3 id="logic-analyser">Logic Analyser
&lt;/h3>&lt;p>A logic analyser can also be used to read the output and can be very useful for debugging.
For this however, the data line(s) need to be on probeable pins.&lt;/p>
&lt;p>One counter transmission from the logic analyser is displayed below.
The trace clearly shows the frame boundaries as well as the individual bits.&lt;/p>
&lt;figure class="centered-fig">&lt;img src="https://vtbl.dev/images/uart-intro/tx_trace.png"
alt="Trace of one counter transmission" width="95%">&lt;figcaption>
&lt;p>Trace of one counter transmission&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>For this analysis, I reduced the idle counter down to 8 bits to speed up the transmission. With 8 bits one idle period is 256 ticks.&lt;br>
One baud tick lasts $\frac{235 \text{ cycles}}{27 \text{ MHz}} = 8.704 \ \mu\text{s}$.&lt;br>
So $8.704 \ \mu\text{s/tick} \cdot 256 \text{ ticks} = 2\,228.224 \ \mu\text{s}$.&lt;/p>
&lt;p>The following figure displays two counter transmissions along with the idle period.
The idle period is measured here to check against the calculations that were just made, and indeed, the idle period lasted about the calculated amount.
The data is also correct; first byte is the least significant byte and its value has increased by one.
So no data has been missed/the counter increments correctly.&lt;/p>
&lt;figure class="centered-fig">&lt;img src="https://vtbl.dev/images/uart-intro/idle_trace.png"
alt="Two transmissions with an idle period of ~$2\,229 \ \mu\text{s}$" width="95%">&lt;figcaption>
&lt;p>Two transmissions with an idle period of ~$2\,229 \ \mu\text{s}$&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;h1 id="conclusion">Conclusion
&lt;/h1>&lt;p>This post explains how the UART protocol works and demonstrates a simplex implementation (communication only works in one direction) using an FPGA transmitter and PC receiver.
The protocol however can also work in a half- or full-duplex configuration.&lt;/p>
&lt;p>Different methods of testing the correctness of the implementation have been presented and it is confirmed that the transmitter works as expected.&lt;/p>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>Tang Nano 20k schematic: &lt;a class="link" href="https://dl.sipeed.com/fileList/TANG/Nano_20K/2_Schematic/Tang_Nano_20K_3923_Schematics.pdf" target="_blank" rel="noopener"
>https://dl.sipeed.com/fileList/TANG/Nano_20K/2_Schematic/Tang_Nano_20K_3923_Schematics.pdf&lt;/a>&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:2">
&lt;p>&amp;ldquo;Determining Clock Accuracy Requirements for UART Communications&amp;rdquo;: &lt;a class="link" href="https://www.analog.com/en/resources/technical-articles/determining-clock-accuracy-requirements-for-uart-communications.html" target="_blank" rel="noopener"
>https://www.analog.com/en/resources/technical-articles/determining-clock-accuracy-requirements-for-uart-communications.html&lt;/a>&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description></item></channel></rss>