Getting to Blinky, CAT Board Style!

A fundamental rite of passage is getting your embedded system to blink an LED. In this post, I'll show you how to take a Raspberry Pi 3 fresh out of the box and get an LED blinking on the CAT Board.

(If you're already experienced with the RPi, then a lot of this post will be redundant for you. However, I've found it useful to document processes so I can repeat them reliably at a later date when the details have grown fuzzy.)

Use the Win32DiskImager to write the Raspbian OS to an SD card inserted in the PC. (This will take a while.)

SSH Over Ethernet

I'm going to use the RPi by connecting to it over a network SSH connection from my PC:

Look in Network Connections to find the IP address of an Ethernet port the RPi can attach to.

Go into the SD card and make a copy of the cmdline.txt file. Then rename the copy cmdline.normal. (This saves a copy of the original configuration in case I want to go back to it later.)

Edit the cmdline.txt file to assign an IP address to the RPi that's identical to the one for the Ethernet port except for the last field. In my case, I selected 169.254.68.2 and appended it to the first (and only) line in the file to get:

Also, change the name of the workgroup to whatever is being used for the Windows PC:

workgroup = XESS

Start the Samba server running on the RPi:

sudo service smbd start

Open the Network window on the PC. The RPi should be visible there:

Double-click the RASPBERRYPI icon and the shared directory will appear. Files can be transferred between the PC and RPi there:

Reconfigure the RPi

Before the RPi can be used to program the CAT Board FPGA, a few of its configuration settings need to be adjusted:

On the RPi, type the command:

sudo raspi-config

In the initial screen that appears, select option 1 for expanding the linux partition. (The stock Raspbian OS image creates a 4GB partition. This won't be enough to compile the FPGA tools. Believe me, I tried.)As an alternative, you can also use the command-line version to do the same thing:

sudo raspi-config --expand-rootfs

Communicating with the CAT Board also requires the use of the RPi's hardware SPI port. That is enabled using the following screens:

Finally, close raspi-config and reboot the RPi to let the changes take effect.

Install the FPGA Software Tools

There are five software packages needed for compiling bitstreams for the Lattice iCE40 HX FPGA on the CAT Board:

Build the Icarus tool that simulates the operation of the circuitry described by HDL source files. (This takes a long time. Icarus is used to test the functions of the yosys tool that's built next. If you trust the build process and don't want to run the tests, then you don't need Icarus.)

Install the litterbox tool that transfers a bitstream file from the RPi into the FPGA on the CAT Board:

sudo pip install litterbox

Create the Blinky Design

Finally, I've reached the point where the blinky is designed. The blinky only does one thing: it blinks an LED on and off once each second.

The CAT Board has four LEDs and a 100 MHz clock connected to the iCE40 FPGA. So all I need is a counter driven by the clock that increments to 50,000,000 and then toggles one of the LEDs in a repeated fashion. Here's some Verilog code I stored in the blinky.v file that does that:

In addition to the Verilog code, I need a way to tell the FPGA tools what pins of the FPGA are connected to the clock signal (pin C8) and the LED (LED1 is attached to pin A9). That information is stored in a pin constraints file called blinky.pcf:

set_io clk_i C8
set_io led_o A9

Compile the Blinky Design

The start of the FPGA compilation process begins with yosys. It parses the Verilog source and synthesizes a set of interconnected logic gates that implement the desired function. The gates and their interconnections are stored in a BLIF (Berkeley Logic Interchange Format) file.

yosys -p 'synth_ice40 -top blinky -blif blinky.blif' blinky.v

Then the BLIF and pin constraints files are sent to arachne-pnr which maps the gates into the logic array of the iCE40 HX8K FPGA and programs the switches in the connection matrix to connect the gates to each other and to the requested I/O pins.

arachne-pnr -d8k -o blinky.asc -p blinky.pcf blinky.blif

The blinky.asc file contains an ASCII representation of the settings for all the programming bits in the FPGA. The icepack tool turns this into a bitstream file with a binary format that can be downloaded into the FPGA.

icepack blinky.asc blinky.bin

Load the Blinky Bitstream Into the FPGA

The bitstream file is loaded into the FPGA with the litterbox tool:

sudo litterbox -c blinky.bin

After the FPGA configuration is complete, LED1 on the CAT Board will start to blink on and off once every second.

The bitstream can also be stored in the serial flash so the FPGA will start to blink whenever power is applied to the CAT Board:

sudo litterbox -p blinky.bin

Here's a video that shows the compilation and downloading steps for the blinky design:

Discussions

Become a member

Admittedly, I only figured that "build-yourself" bit only after adding the reference. But then again, not at least for outshining the regular devices from Lattice, I even felt like emphasising it :o) Also, your video is so encouraging, you truly tempted me. I keep thinking about what I would have built, or what you would build if you did it again. It all sounds like a wonderful kickstarter/indiegogo project to me.

Well done! The Debian folks now have packages for yosys, arache-pnr and icestorm and albeit litterbox is still missing as a package, I went on and added a reference to your tutorial on https://wiki.debian.org/FPGA/Lattice . If there are ideas of yours on what Debian should do to support FPGA development then please get in touch.