Getting Started

Like before, we will start by creating a new project. Go to File->New Project... and create a new Lucid project. I'm calling mine Serial Port Echo. It is from the Base Project example.

Great! Now we have a bare-bones project. The Au's and Cu's USB port is accessible to the FPGA through the FTDI USB<->serial bridge. Serial is often called UART (Universal Asynchronous Receiver Transmitter) and this is the name of the components we will need.

Components

Components are prewritten modules that you will likely need to use in many of your projects. For this tutorial, we need to add two components to our project. One to receive data and one to send data.

Launch the Component Selector by going to Project->Add Components...

Under Protocols you will find the components UART TX and UART RX. Click the checkboxes next to each one.

You can click each component for a short description of what it does.

Feel free to explore the other categories to see what's available. The list of components grows over time with new releases of Alchitry Labs.

Click Add to copy the components into your project.

Notice that there is a new category called Components.

You can open any of the components you want to take a look at how they work. However, for this tutorial we will be using them as a black box.

Instantiating the Component

Now that the components we need are in our project, we need to use them.

This will create instances of uart_rx and uart_tx named rx and tx respectively.

We need to specify two parameters for each one. The BAUD parameter is the number of bits per second it should send. The important thing is that you match this rate to the one you set on your computer. The serial port monitor in Alchitry Labs uses 1M baud so that is what we specify here.

The other parameter, ﻿CLK_FREQ﻿, is the frequency of the clock. This is used to calculate how many clock cycles are required per bit.

If you only do this, you will actually get some errors. These errors are because some of the inputs to the modules were never assigned.

We are going to hook the modules up to each other so that when data is received, it is promptly sent back.

On lines 28 and 29, we connect the external input and output to our two modules.

On lines 31 and 32, we connect the rx module's outputs to the tx module's inputs.

On line 33, we set the block input of tx to 0. When this value is 1, the uart_tx component won't send any data out. This is useful if you have some way to tell that the receiver is busy. With the Au and Cu, we just assume the data is being read from the computer in a timely manner to keep the FTDI's buffer from overflowing. This is a reasonable assumption as long as there is some program actually reading the data.

Sending and Receiving Data

When new data arrives, the signal rx.new_data goes high. This tells you that the data on rx.data is valid. Normally you would want to wait for rx.new_data to go high and then do something with the data.

Writing data to the serial port follows the same idea. We set tx.data to the byte to send and we set tx.new_data high. However, there is one more signal to look out for. That is tx.busy. If this signal is high, the transmitter is busy for some reason, either it is currently sending a byte or block is high. Either way, if you try to send data when this is high, it will be ignored.

For this simple example, we are going to ignore tx.busy. This should not be a problem since we never block and the bytes coming in arrive at the same rate we can send them out.

The next tutorial will handle this more gracefully by actually respecting this flag.

You should now be able to build your project and load it on the board. You can then go to Tools->Serial Port Monitor in Alchitry Labs to launch the monitor. From here, choose the virtual serial port the board connected to and you should be able to type data into the monitor. Whatever you type should be shown.

Capturing Data

Before we wrap up, let's do a little more with the incoming data. We are going to save the last byte received and display it on the LEDs.

To do this this, we need an 8 bit dff. The following line goes inside the .clk(clk) block but outside the .rsr(rst) block. It could go inside the .rst(rst) block but it really doesn't need a reset.

dff data[8]; // flip-flops to store last character

We can then write to the dff when we have new data. These lines go at the end of the always block. You can also remove the previous assignment of 0 to led.

if (rx.new_data) // new byte received
data.d = rx.data; // save it
led = data.q; // output the data

On the last line, we connect the LEDs to the output of the dff.

If you don't assign a dff a value, then it will retain the last value it had. Since we are only assigning it a value when rx.new_data is high, it will hold the last byte until the next one comes in.

Now, if you build and load your project, when you fire up a serial port monitor you should not only see the text you send back in the monitor, but the LEDs on the board should also changed depending on the character you sent.

This tutorial covers the basis of using the HDMI Shield. There are a few components bundled in the Mojo IDE that make encoding and decoding HDMI streams easier. This tutorial gives a basic example for each one.