Build and Flash an iCE40 Design With Yosys, nextpnr and icepack
Go from Verilog to a flashed iCE40 board with Yosys, nextpnr-ice40, icepack and iceprog, with each command, file and log line explained for a beginner.

Building for an iCE40 takes four commands, and each produces a file you can open. This post walks through them for a small Lattice iCE40 board, naming every file and explaining the lines the tools print, so that the first time something goes wrong you know which stage to look at. It uses Yosys 0.40, nextpnr 0.7 and Project IceStorm as packaged in the OSS CAD Suite, and an iCEBreaker board with an iCE40 UP5K FPGA in a 48-pin SG48 package as the example target. Everything board-specific, such as pin numbers and the oscillator frequency, comes from the board's documentation, and you should confirm it there for yours.
What you need
You need a board with an iCE40 and a USB programmer, the tools on your path, and three files in one directory: the design, a pin constraint file and, optionally, a Makefile. If the programmer cannot open the board on Linux, a udev rule granting your user access to the FTDI device is the usual missing piece, and the IceStorm documentation shows it.
The design is the blinking counter from earlier in this cluster. The clock arrives from a 12 MHz oscillator, and the LED on the iCEBreaker is active low, meaning the LED lights when its pin is driven to 0, so the code inverts the counter bit.
module blink (
input wire clk,
output wire led
);
reg [23:0] counter = 24'd0;
always @(posedge clk)
counter <= counter + 24'd1;
assign led = ~counter[23];
endmoduleThe initial value on the counter declaration is honoured by iCE40 flows, which load register contents from the configuration, so this design needs no reset. A 24-bit counter at 12 MHz toggles bit 23 about every 0.7 seconds.
Step 1: pin constraints
The pin file tells nextpnr which package pin each port uses. For the iCEBreaker the 12 MHz clock is on pin 35 and the red user LED on pin 11, per the board's published pin map:
set_io clk 35
set_io led 11Each line is set_io <port name> <package pin>. The port name must match the Verilog port exactly. A port missing from this file is not an error by default, and nextpnr will choose a pin for it, which is almost never what you want, so check the log for unconstrained-port warnings.
Step 2: synthesise with Yosys
yosys -p "read_verilog blink.v; synth_ice40 -top blink -json blink.json"This reads the Verilog, runs the iCE40 synthesis script with blink as the top module, and writes a JSON netlist. Without -q Yosys prints a long log. The sections worth looking for are the end of the log, which contains a statistics block. A trimmed version looks like this:
=== blink ===
Number of cells: 49
SB_CARRY 23
SB_DFF 24
SB_LUT4 25
... The cell types tell you how the design mapped. SB_DFF is a flip-flop, one per counter bit. SB_LUT4 is a four-input lookup table, which here implements the sum logic and the inverter. SB_CARRY is the dedicated fast carry cell that makes the incrementer cheap. The exact counts depend on the Yosys version, so treat these as the pattern to expect.
Check the log for warnings before moving on. A Latch inferred line or a port-width warning is a design problem to fix now.
Step 3: place and route with nextpnr-ice40
nextpnr-ice40 --up5k --package sg48 --json blink.json --pcf blink.pcf --asc blink.asc --freq 12Each option does a specific job. --up5k selects the chip family variant, --package sg48 the package, which decides the pin names. --json and --pcf name the inputs. --asc names the output configuration file in IceStorm's text format. --freq 12 sets the target clock to 12 MHz, so the timing-driven placer and router know how hard to work and the report can say pass or fail.
The output is long. Four parts matter for a beginner.
First, the device utilisation:
Info: Device utilisation:
Info: ICESTORM_LC: 25/ 5280 0%
Info: ICESTORM_RAM: 0/ 30 0%
Info: SB_IO: 2/ 96 2%
Info: SB_GB: 1/ 8 12%A logic cell, ICESTORM_LC, combines a lookup table, a carry connection and a flip-flop. The design uses 25 of 5,280. Two I/O cells carry the clock and the LED, and one of the eight global clock buffers distributes the clock. These counts are illustrative and will differ slightly in your run.
Second, the placement and routing progress lines, where HeAP placement iterations and router passes scroll by. They end without an error if the design fits.
Third, the timing report:
Info: Max frequency for clock 'clk$SB_IO_IN_$glb_clk': 180.32 MHz (PASS at 12.00 MHz)PASS means the slowest register-to-register path fits within the 12 MHz period. The number is illustrative. If it said FAIL, the design could not run at the target frequency.
Fourth, any warnings. An unconstrained pin or a pin that does not exist for this package appears here.
Step 4: pack and program
icepack blink.asc blink.bin
iceprog blink.binicepack converts the text configuration into the binary bitstream. iceprog sends it over USB to the programmer on the board and loads it into the configuration flash. It prints progress, and a successful run ends like this:
init..
cdone: high
reset..
cdone: low
flash ID: 0xEF 0x40 0x18 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
file size: 104090
erase 64kB sector 0:
programming..
reading..
VERIFY OK
cdone: high
Bye.The flash identifier and file size depend on the board and the bitstream. The important lines are VERIFY OK, which means the flash content matches the file, and cdone: high, which means the FPGA configured itself from flash successfully. The LED should now blink about every 0.7 seconds.
If you want to try a design without writing flash, which is quicker and avoids wearing the flash, iceprog -S blink.bin loads the bitstream into the FPGA's volatile configuration memory only. It is lost at power-off.
Putting it in a Makefile
Typing four commands for every change gets old, and a Makefile makes the sequence repeatable:
TOP = blink
all: $(TOP).bin
$(TOP).json: $(TOP).v
yosys -q -p "read_verilog $<; synth_ice40 -top $(TOP) -json $@"
$(TOP).asc: $(TOP).json $(TOP).pcf
nextpnr-ice40 --up5k --package sg48 --json $< --pcf $(TOP).pcf --asc $@ --freq 12
$(TOP).bin: $(TOP).asc
icepack $< $@
prog: $(TOP).bin
iceprog $<
.PHONY: all progThe indentation of recipe lines must be a tab character. Running make prog rebuilds only the stages whose inputs changed, then flashes.
What each file is
Knowing the intermediate files helps when debugging.
| File | Made by | Contains |
|---|---|---|
blink.v | you | The design |
blink.pcf | you | Pin assignments |
blink.json | Yosys | The mapped netlist in iCE40 cells |
blink.asc | nextpnr | The placed and routed configuration as text |
blink.bin | icepack | The binary bitstream |
You can open blink.json and blink.asc in a text editor. The first lists cells and connections, and the second lists the bits of each tile. You rarely need to, but it removes the mystery.
Troubleshooting a first build
**
ERROR: Unable to find a pin or site named**: the package does not have that pin, or the--packageoption is wrong for your board.The LED never changes: confirm the clock pin and the LED polarity. If the LED looks permanently on or off, check whether it is active low and whether the counter bit is too fast to see.
**
iceprogcannot find the device**: check cable and permissions, and on boards with a shared USB interface make sure the right interface is selected.Timing fails at 12 MHz: not expected for this design. If it does, check you used
--freq 12and not a larger value by mistake.A warning that a port is unconstrained: add the port to the pin file.
Variations to try
A few small edits teach a lot. Change the bit index from 23 to 21 and rebuild: the blink speeds up by a factor of four. Add a second LED on pin 37, the green one on the iCEBreaker, driven by counter[22], and add it to the pin file. Replace the pin file entry for the clock with a wrong pin and read the resulting log to see how the error is reported. Each change takes under a minute to rebuild, which is the main reason the open flow is pleasant to work with.


