This is only a preview of the April 2020 issue of Practical Electronics. You can view 0 of the 80 pages in the full issue. Articles in this series:
|
Field programmable
gate arrays (FPGAs)
are extremely powerful,
but until recently,
programming them has
been an obscure art.
Now, thankfully, it has been made
much simpler and easier due
to the availability of beginner-friendly
development boards and free, opensource graphical programming
software. We explore what
you can do with the lowcost and compact
iCEstick board, and
free IceStudio
software.
Part 1
Introduction to
programming FPGAs
iCEstick
F
or a long time, programming
and developing FPGAs has been
difficult, especially for the hobbyist who doesn’t have access to the
often expensive tools that are needed.
On top of this, understanding the
language that is used to describe a design can be a challenge, as is getting
one’s head around the ways FPGAs
work differently to microcontrollers.
The iCEstick development board
from Lattice Semiconductor (a major
FPGA IC manufacturer) is a compact
unit which plugs into a USB port.
Thus the board and programming
hardware are one and the same, requiring only the extra components
for a particular application to be
added on.
Even this is not always necessary,
as the board sports five LEDs which
can be controlled by I/O pins, plus an
onboard infrared transceiver.
The code for the iCEstick can be
generated using Lattice iCEcube development software, available with a
free licence. The Diamond programmer software is then used to program
the iCEstick with the resulting file.
26
We also tried an open-source alternative called IceStudio. It has a
graphical interface, allowing logic
blocks to be dragged and dropped,
then connected by virtual wires to
create a representation of the circuit
to be synthesised. It is a complete
IDE, allowing design, building and
uploading to occur.
For users who are comfortable with
how logic gates and other basic elements like flip-flops work, this is an
ideal way to bridge the gap of understanding between having an idea in
one’s mind and turning it into a functioning circuit.
IceStudio also allows ‘code blocks’
containing Verilog code to be created,
so those who are familiar with Verilog are not limited by the included
graphical symbols.
Verilog is a bit like the C language,
as used to program an Arduino, but
is designed to produce logic block
structures rather than machine code.
by Tim Blythman
What is an FPGA?
FPGA stands for ‘field programmable
gate array’, and this means that it consists of logic gates, flip-flops and other
‘glue’ logic which can be (re-)configured to perform different functions.
While this is an over-simplification, you can think of an FPGA
as an IC containing thousands of
4000B/74HC/74LS chips connected
via crossbars; in effect allowing you
to change how the inputs and outputs of those devices are connected,
to form virtually any function. And
since they are all inside the same chip,
very high speeds are possible; up to
500-1000MHz in some parts.
The advantage that this arrangement has over a microcontroller is
that everything happens at the same
time in an FPGA.
Rather than having to wait for things
to process in a sequence, determined
by the list of instructions which form
the program, everything happens practically instantly in an FPGA.
This makes them ideal for tasks
where many different calculations
can be made in parallel. While some
Practical Electronics | April | 2020
Table1: iCEstick physical pin to
I/O pin mapping
The iCEstick, slightly under
life size. The huge (144-pin
TQFP) IC in the middle of
the iCEstick is the iCE40HX-1k.
To the right of it are
the various I/O
headers and five
user LEDs. To the
left are the flash
and EEPROM ICs, an
FTDI 2232H dual UART
and a 12MHz oscillator.
microcontroller processors do have
multiple cores, allowing several instructions to be executed simultaneously, in an FPGA, practically everything happens simultaneously.
So it’s a bit like having a processor
with thousands (or even millions) of
cores; even though each of those cores
may have fairly limited capabilities,
the overall result is a much more powerful and capable device.
A good example of a digital task
which is quite easy to do with an
FPGA but virtually impossible with
a regular microcontroller, as demonstrated by the Arduino MKR Vidor
4000, is the generation of an HDMI
digital video signal.
The FPGA can produce the HMDI
data (which is typically clocked at
hundreds of megahertz) far quicker
than any microcontroller could possibly manage.
And it can do this while performing whatever other tasks are required
simultaneously, without any concerns
that the different tasks may interfere
with the time-critical video generation process.
Rather than software code (eg, BASIC, C, assembly language), the FPGA
configuration is described in a hardware description language (HDL).
There are two main HDLs in widespread use: Verilog and VHDL.
We will mostly be dealing with
Verilog, which borrows some of its
syntax from the C language; but due
to the nature of FPGAs, it has some
important and significant differences.
The HDL is synthesised into a ‘bitstream’ (basically, a blob of binary
data), which is what is actually loaded
into the FPGA chip to configure it. In
the case of the iCE40HX-1k FPGA on
the iCEstick, this is up to 34kB in size.
The bitstream is roughly the equivalent of machine code to a microcontroller or microprocessor.
There is a lot more to this process
than this simple description suggests,
and much of how FPGAs and FPGA development tools work has been hidden
by the manufacturers until the advent of
the open source tools we are now using.
Practical Electronics | April | 2020
Pin Function
21 12MHz Osc.
8 UART TX
9 UART RX
78 PMOD 1
79 PMOD 2
80 PMOD 3
81 PMOD 4
87 PMOD 7
88 PMOD 8
ICE40HX chip and
iCEstick board capabilities
While touted as having a USB thumb
drive form factor, it actually measures
95 × 25mm. But when you consider
that a large portion of this board is
taken up by the sizeable FPGA chip,
its size seems reasonable.
This IC is a Lattice iCE40HX-1k
FPGA which comes in a 144-lead
TQFP package. While not all the input/output pins are broken out (the
chip has 96 I/O pins in total), an ample number are available.
The iCE40HX-1k contains 1280 flipflops, 1280 lookup tables, 160 programmable logic blocks and 16 RAM blocks.
Each RAM block holds four kilobits
(512 bytes), for a total of 8 kilobytes.
For comparison, its larger sibling,
the iCE40HX-8k, can emulate a 32-bit
RISC processor, but this is a bit beyond the iCE40HX-1k’s capabilities.
The core of the chip runs at 1.2V,
but external I/O on the iCEstick is
3.3V. There are four I/O banks on the
iCE40HX-1k which can (in a different implementation) be set to other
I/O voltages.
Also on the iCEstick board are several other components for communications and programming. The second-largest IC, nearest the USB plug,
provides the USB interface.
This is an FTDI 2232H dual UART
with USB 2.0 Hi-Speed. Typically, one
of the UARTs is used in SPI mode for
programming, and the second UART is
Pin Function
90 PMOD 9
91 PMOD 10
95 LED5 (GREEN)
96 LED4 (RED)
97 LED3 (RED)
98 LED2 (RED)
99 LED1 (RED)
105 IR TX
106 IR RX
available for communication with the
bitstream that is ‘running’ on the FPGA.
The two 8-pin SOIC devices are
a Flash IC and an EEPROM IC. The
Flash IC is 32Mbit and is used to store
the configuration bitstream in a nonvolatile fashion. The FPGA is configured using internal RAM, the contents
of which is lost on power-down, so it
must be loaded from the Flash chip
each time power is applied.
While the FPGA has the facility to
load its configuration from its own
internal non-volatile configuration
memory, this memory can only be
programmed once, so a reprogrammable Flash chip is used until a design is finalised.
The EEPROM is simply used to hold
the configuration for the FTDI 2232H
and the remaining IC is an LT3030 dual
low-dropout linear regulator.
There is also a 12MHz clock source
on the iCEstick. This clock source is
necessary for all but the most basic
logic designs.
The iCE40HX-1k also features a
PLL, so designs are not limited to
12MHz, as higher frequencies can
be generated by the PLL from the
12MHz source.
On the far side of the FPGA are
the I/O breakout headers. Five LEDs
(marked D1-D5) are arranged in a diamond pattern, flanked by two 0.1inch pitch 10-pin breakouts. Each of
these provides eight I/O pins plus
ground and 3.3V power.
Screen1: ensure that the correct device is selected in the Zadig application, and
that libusbK is selected before clicking ‘Replace’ and closing the window. If you
do change the wrong driver, you can uninstall it via device manager.
27
Screen3: if you need to remove
IceStudio’s drivers to allow the
Diamond Programmer to work with
the iCEstick then find this entry in
Device Manager, right-click it and
choose Uninstall Device. Unplug and
replug the iCEstick and Windows
should reinstall the default drivers.
Screen2: IceStudio’s ‘Two LEDs alternate blink’ example (which they incorrectly
refer to as ‘alternative’). The small yellow box at left represents the 12MHz
crystal clock on the iCEstick. It is followed by a 22-stage binary divider,
effectively dividing the 12MHz clock by a factor of 4,194,304 (ie, by 222, to
around 3Hz). Digital pins D1 and D2 are connected to two LEDs on the iCEstick
board, and are driven with square waves derived from the 3Hz clock, one
directly, and one via a NOT gate so that it is on while the other is off.
The 6×2 female header block
matches Digilent’s PMOD interface,
and provides eight more I/Os, plus
ground and power.
Finally, at the end of the board
opposite the USB connector is an IR
transceiver chip, which is connected to another two of the FPGA’s I/O
pins. This gives 24 unallocated I/O
pins available for use, plus at least
ten dedicated to I/O functions on the
board itself.
Other FPGA boards
After acquiring the iCEstick, we
looked around for other FPGA development boards; in particular,
those supported by IceStudio. Many
of these are open-sourced hardware
designs that are being promoted on
crowd-funded websites.
In general, we found that most of
them were more expensive than the
iCEstick. A few were cheaper, but
also required a separate programmer.
So for this reason, and because the
iCEstick is easy to buy, we decided
to stick with it.
The fact that two different software
packages can be used to program it
is also a plus.
Software for the iCEstick
In the following discussion of the software options, we will only give very
basic examples. If you want something more involved (and useful), see
our iCEstick VGA Terminal project,
which we will cover next month.
There, we’ll delve much deeper
into what can be done with the iCEstick and IceStudio.
IceStudio software
The open-source IceStudio software is a free download. We found
it straightforward to use, and had
28
a working project uploaded to the
board in minutes. There are example projects available which appear
quite basic, but they are all great
building blocks.
The version we tried was just over
100MB, although you also need to
download some other required software packages, such as the ‘toolchain’.
Installing IceStudio
IceStudio can be downloaded from
its Github page at https://github.com/
FPGAwars/icestudio
Like many open source tools, it’s
available for Windows, Linux and
macOS. We used the v0.4.0 release.
Although this release number indicates it is still in beta, we found the
software to be mature and didn’t run
into many bugs.
Behind the scenes, it uses the
open-source IceStorm project to synthesise the bitstream alongside some
configuration files, but you don’t
need to concern yourself with these
details while using IceStudio.
In this regard, it is similar to Arduino, which uses the open source
gcc compiler and the AVRDUDE programming tool to provide most of its
functions, with inbuilt board configuration files. This means the user
does not have to worry about the
minute details of the specific hardware used.
Installing IceStudio was quite
straightforward. About halfway
down the Github page (link above),
there is an installation guide, with
brief, simple instructions for Linux,
Windows and macOS, with links to
the downloads.
We installed on Windows 10, so some
of the steps below may not apply to
Linux or macOS; in particular, the driv-
er switching step is probably not needed
on these other operating systems.
The installer does not automatically install the required toolchain – you
will be prompted to install it when
the program first runs. No further input is required apart from confirming that installation should proceed.
IceStudio also includes a bitstream
programmer, but this does not work
with the default device driver for the
iCEstick under Windows. Again, a
simple tool allows the appropriate
driver to be installed and uninstalled
(which is necessary if you wish to
also use Lattice’s iCEcube software).
The driver switcher uses the Zadig driver utility. IceStudio gives you
some prompts which explain how to
use Zadig, then opens the program,
allows you to make the changes, and
then prompts you to unplug and replug the iCEstick.
This is all fairly seamless, and it’s
comforting that the program is upfront about what changes you are
making. The Zadig utility also has
the option of changing other drivers, so great care should be taken
that you don’t inadvertently change
the wrong driver.
We also noticed that, very occasionally, Windows would reload the
old driver (perhaps when the iCEstick was plugged into a different
USB port). In that case, it is merely
necessary to rerun the driver switcher routine.
Setup
Once the installer has finished, start
IceStudio. You will be prompted to
install the toolchain, which requires
the Python scripting language to be
installed, plus a few other packages. If
you are not prompted, check the Tools
→ Toolchain menu, and click Update
if you are unsure. We found that this
proceeded without any problems, although you need Internet access to
download these extra packages.
You will then be prompted to update the drivers. This is only possible
if you have an iCEstick connected.
Practical Electronics | April | 2020
Screen4: this screen
grab shows the
iCEcube2 new project
settings to suit the
iCEstick. The project
name and location
can be set to suit your
system, but the device
properties are critical
for correct operation.
If you don’t have an iCEstick, skip
this step. Again, there is no harm in
checking the drivers if you are not
prompted.
Now click Tools → Drivers → Enable. IceStudio will indicate a few
steps that will occur. Click OK to
proceed. Note the message about using USB 2.0 ports. We ran into problems using the iCEstick on a USB 3.0
port, but were able to use a USB 2.0
hub to ‘downgrade’ our connection
to USB 2.0 and it worked after that.
When the Zadig Driver Utility
opens (Windows may ask for permission for the program to make changes), take great care to change the correct drivers. Zadig has facilities for
many drivers, but we only want to
change those for the iCEstick.
Ensure that ‘Lattice FTUSB Interface Cable (Interface 0)’ is selected
in the dropdown and check that the
item to the right of the green arrow
is ‘libusbK’ (in our case, version
3.0.7.0), then click ‘replace driver’
(see Screen1).
IceStudio will now prompt you to
unplug and replug the iCEstick. Do
this to ensure that the drivers are
loaded correctly.
The final step is to select the development board. This is done from
the Select → Board menu; the iCEstick is found under the HX1K subheading. Selecting the correct board
means that friendly names are availPractical Electronics | April | 2020
able for the various I/O pins. For example, a pin named ‘D1’ can be selected, which maps directly to LED1
on the iCEstick.
This completes the setup. There
are examples available under the File
→ Examples menu. Many of these appear to be written for other boards,
but are simple enough to adapt for
the iCEstick. The only real differences appear to be the I/O pin mappings,
which are blanked on conversion.
We also suggest enabling the FPGA
resources view, by clicking View →
FPGA resources, and ensuring this
item is ticked. The bottom bar of the
window will now show the resource
usage, which is empty at this stage.
This will let you keep track of how
‘full’ your FPGA is.
Using IceStudio
A good place to start is the example
available under the following menu:
File → Examples → Basic → Two
LEDs alternate blink.
Upon opening this, you will be
prompted that it is designed for a
different board; simply click ‘convert’. As mentioned above, conversion involves removing any I/O pins
associated with the old board. To
complete the conversion, click on
the LED dropdown boxes, and select
D1 and D2 (see Screen2).
The next step is to compile the project into a bitstream. Click Tools →
Build or press Ctrl-B. After a few seconds, a message will pop up which
should say: ‘Build done’. Finally,
click Tools → Upload to send it to the
iCEstick. The LEDs will all light up
dimly during the upload stage, and if
the upload is successful, two of the
LEDs should be alternately flashing.
If you have trouble with the upload, check the drivers using the Enable Driver option or try a different
USB port.
We recommend looking at the examples to see what can be done with
IceStudio. The four menu items at
top right are various items that can
be dropped into the editor to create
your project.
Included in these (under Basic) is
a ‘Code’ option. This allows blocks
containing Verilog code to be included. For those familiar with Verilog,
the blocks are effectively the same as
Verilog modules. Such a code block
can even be exported and used in another project.
You can build just about any set of
logic using Verilog, including adders,
accumulators, multipliers, dividers,
multiplexers, memories, register files
and so on.
The various gates and other blocks
can be joined by wires. To create a
wire, move the mouse to an output
pin of a block until the pointer becomes a black cross. Click, and drag
the wire to the input of another block
and release.
We found the wires to be one of
the fiddliest parts of IceStudio. They
can only be dragged from output to
input, and often end up in awkward
places. They can be dragged to neaten the layout or removed by hovering over the wire, and then finding
the small red ‘x’ and clicking on it.
The software has all the usual editing facilities such as copy, paste
and undo, and they all work rather
well once you get used to it. You can
press and hold the right mouse button to pan around the window, and
the scroll wheel on the mouse allows
zooming in and out.
A full user guide is available at:
https://icestudio.readthedocs.io/en/latest/
Note that if you have used IceStudio to enable its driver, you will
need to disable it to allow the Diamond Programmer to use its driver.
The Tools → Drivers → Disable menu
is a bit cryptic about this.
What you need to do is open Device Manager, find the libusbK driver
entry, right click on it and uninstall
it (Screen3). Then unplug and replug
the iCEstick, and Windows will reinstall the default drivers.
This isn’t necessary on macOS or
29
Screen5: the
iCEcube window
after our project
has been
converted into a
bitstream.
Despite all
the red text,
everything
completed
without errors.
The Pin
Constraints
Editor is the icon
below the left
of the Window
menu.
Linux, as the same drivers are used
for both software packages.
iCEcube2
iCEcube2 is proprietary software,
and while you can freely download it
and install it, a license key is needed
to run it. This is all available at no
cost, but you will need to create an
account on the Lattice website to receive a license key.
We found the process of setting up
an account and requesting a key a bit
slow, but it worked, and we got our
key in the end.
The key is tied to a specific Ethernet MAC address, meaning you will
need multiple licenses if you want to
run the software on multiple computers. The iCEcube2 version we downloaded was around 750MB, and a separate download of the ‘Diamond’ programmer application is needed too.
There are versions of iCEcube2 available for Windows and Linux; download from: http://bit.ly/pe-apr20-ice1
The separate programmer software
is found at: http://bit.ly/pe-apr20-ice2
Ensure that you have the license
file for iCEcube2. There is a link on
the information page for iCEcube2
detailing how to receive the license
file via email. Although the email
notes that the license file should be
placed in the \license directory of
our install, there did not appear to
be such a directory.
Our install of iCEcube2 has the
path C:\lscc\iCEcube2.2017.08, so
we placed a copy of the license in
both the lscc and iCEcube2.2017.08
directories, and everything seemed to
work, although it did sometimes complain that the license file was missing.
We struggled to find simple examples that would work for the iCEstick
under iCEcube2, and certainly didn’t
find any on Lattice’s website. In the
end, we found a basic ‘blink’ exam30
ple at: http://bit.ly/pe-apr20-ice3 but
even this missed one or two steps, so
we had to modify it.
ICEcube2 uses VHDL, so if you prefer VHDL over Verilog, this may be an
option, although VHDL is generally
stricter and more verbose than Verilog
(Editor’s note: in my opinion, Verilog
is superior, although they both have
roughly the same capabilities).
To use iCEcube2, first create a
new project, and fill in the details as
shown in the screen grab (Screen4),
to match the hardware of the iCEstick. Click OK, and the ‘add files’ dialog box opens; click ‘Finish’, as files
can be added from within the project.
You can download the BLINK.vhdl
file from the April 2020 page of the
PE website. Copy this file to within the project folder, then add it to
the project by right-clicking on ‘Add
Synthesis Files’. Select the file and
then press the ‘>>’ button to add it
to the project.
The ‘Run Synplify Pro Synthesis’
button is the first step in turning the
project into a bitstream. Double-click
this, and check that there are no errors. We got an error message about
the license file, but it worked anyway.
Next click ‘Import P&R Input Files’.
You should see a pattern of working
through several steps along the lefthand side of the project window, with
the green triangles turning into ticks
as the steps are completed by doubleclicking on them (see Screen5).
After the P&R (place and route) files
have been imported, the pins need to
be assigned. This is done with the Pin
Constraints Editor, selected from the
row of icons below the menus. In our
version, it is the fourth icon, which
looks like a blue square with pins
coming out of it.
The physical pin to I/O pin mapping
is shown in Table 1. LED1 and LED2
should be set to pins 95 and 96 (or any
of the other LED pins from the table),
and ‘clk’ (the clock signal input) should
be set to pin 21. Save the project to register the new pin assignments.
Finally, in turn, double click ‘Run
Placer’, ‘Run Router’ and ‘Generate
Bitmap’. The generated bitmap is the file
that will be loaded onto the iCEstick. It
can be found buried within the project
folder; eg, \BLINK_Implmnt\sbt\
outputs\bitmap\BLINK_bitmap.bin
Diamond Programmer
Now we use the Diamond Programmer application to load the bitstream
onto the iCEstick. Open Diamond
Programmer, select ‘Create a new
blank project’ and click OK.
Under ‘Cable Settings’ to the right,
click ‘Detect Cable’; the selected cable should include FTDI in its name.
We found we had to set the port to
FTUSB-1 (see Screen6).
In the main window, set the device
family to iCE40 and the device to iCE-
Screen6: the Diamond Programmer window. Check the Device, Device family
and Cable settings to ensure they are correct. The Device Properties icon is
immediately below the Help menu item, while the Program button is the one
with the large green arrow.
Practical Electronics | April | 2020
40HX1K. Under File Name, browse to
the bitmap file created by iCEcube2
and select it.
Click the ‘Device Properties’ icon
(a chip with a small yellow pencil)
and set that as shown in the screen
grab (Screen7). Finally, you click the
‘Program’ button to transfer the bitstream to the iCEstick.
If all is well, your Output Window at
bottom left should look like our screenshot, and you should see two LEDs
flashing alternately on the iCEstick.
Conclusion
We devoted more space to describing
the iCEcube2 and Diamond Programmer software than IceStudio because
it requires more work to achieve the
same result.
We found that IceStudio was a real
pleasure to use and would highly
recommend it to anyone who has
not worked with FPGAs before. We
found a couple of small glitches, including occasional crashes even on
quite small projects. So save your
work often.
We also found IceStudio became
quite sluggish on larger projects,
taking some time to zoom and move
around. We imagine that as we become more proficient with Verilog,
that our IceStudio projects will consist of nothing more than a single
large code block, which should not
present the same performance issues
as lots of smaller blocks.
IceStudio also appears to have the
benefit of being written specifically for
development boards such as iCEstick.
If you are a professional developer, especially someone looking to
build an FPGA into an end product,
the flexibility and complexity of iCEcube2 will be warranted. Just choosing a different Flash IC to that fitted
or another small hardware change
different to the iCEstick would probably be enough to hamper IceStudio
in these cases.
With iCEcube2, once we had our
project set up, everything worked
quite well; similarly, Diamond Programmer worked quite well, although
the time spent pulling hair and debugging cryptic error messages was
quite a bit more than we had hoped.
But for someone who has not
worked with FPGAs before, IceStudio will give a smooth, easy way for
you to become accustomed to what
is possible.
Hardware
In terms of hardware, there are a few
development boards around which
feature more powerful FPGAs than
the iCEstick discussed here. However, for now, the iCEstick suits our
purposes, and we think it will be a
great starting point for those wishing
to try out FPGAs for the first time. It
is available from Mouser and Digikey
for around £20.
Next month
Finally, just a reminder that next
month in Part 2 we’ll put FPGA theory into practice by building an iCEstick VGA Terminal.
www.poscope.com/epe
- USB
- Ethernet
- Web server
- Modbus
- CNC (Mach3/4)
- IO
- PWM
- Encoders
- LCD
- Analog inputs
- Compact PLC
Reproduced by arrangement with
SILICON CHIP magazine 2020.
www.siliconchip.com.au
Screen7: we’d never
have guessed these
properties, so we’re
glad we found a
guide to help us
out. Make sure you
don’t select NVCM
programming.
That is the nonerasable (write-once)
memory built into
the iCE40HX-1k IC.
We use the Flash
memory instead, to
allow repeated write/
erase cycles.
- up to 256
- up to 32
microsteps
microsteps
- 50 V / 6 A
- 30 V / 2.5 A
- USB configuration
- Isolated
PoScope Mega1+
PoScope Mega50
- up to 50MS/s
- resolution up to 12bit
- Lowest power consumption
- Smallest and lightest
- 7 in 1: Oscilloscope, FFT, X/Y,
Recorder, Logic Analyzer, Protocol
decoder, Signal generator
Practical Electronics | April | 2020
31
|