Skip to content
systemcoding.dev
Hardware

Program an ECP5 Board With Yosys, nextpnr-ecp5 and openFPGALoader

Build and load a design for a Lattice ECP5 board such as the ULX3S with synth_ecp5, nextpnr-ecp5, ecppack and openFPGALoader, with an LPF constraint file.

7 min read
Summarize in AI
Flat illustration of logic gates wired into a small circuit, with the GNU, KiCad and Haskell logos.

The Lattice ECP5 is the step up from the iCE40 in the open toolchain: more logic, block RAM, hard multipliers, clock managers and serial transceivers on some parts, and enough room for a small system on a chip. The flow has the same four stages as for iCE40 with different tool names: Yosys with synth_ecp5, nextpnr-ecp5, ecppack from Project Trellis, and openFPGALoader to program the board. This post builds the blinking LED for a ULX3S board with an ECP5 LFE5U-85F in a 381-ball caBGA package, using Yosys 0.40, nextpnr 0.7 and openFPGALoader 0.12. Pin names and the clock frequency come from the ULX3S documentation, and you should check them against your board.

The design

The ULX3S has a 25 MHz oscillator and eight user LEDs. The first LED is on an active-high output. A 25-bit counter's top bit toggles every 2^24 cycles, which at 25 MHz is about 0.67 seconds, so this design uses bit 24:

blink25.vverilog
module blink25 (
    input  wire       clk_25mhz,
    output wire [7:0] led
);
    reg [24:0] counter = 25'd0;

    always @(posedge clk_25mhz)
        counter <= counter + 25'd1;

    assign led = {counter[24:17]};
endmodule

Eight LEDs show eight different bits, so you see a binary counter: the rightmost LED blinks fastest and the leftmost slowest. Bits 17 to 24 are chosen so the fastest LED toggles every 2^17 cycles, roughly every 5 ms at 25 MHz, which appears as a dim glow, and the slowest toggles every 0.67 s.

The concatenation {counter[24:17]} is just a part-select with braces for clarity. led[0] gets bit 17 and led[7] gets bit 24.

The constraint file

ECP5 constraints use Lattice's LPF format. Each port needs a LOCATE statement for the pin and an IOBUF statement for the electrical standard. The LED pins and clock pin for the ULX3S, taken from its published constraint file, are as follows:

ulx3s.lpfText
LOCATE COMP "clk_25mhz" SITE "G2";
IOBUF PORT "clk_25mhz" IO_TYPE=LVCMOS33;
FREQUENCY PORT "clk_25mhz" 25 MHZ;

LOCATE COMP "led[0]" SITE "B2";
LOCATE COMP "led[1]" SITE "C2";
LOCATE COMP "led[2]" SITE "C1";
LOCATE COMP "led[3]" SITE "D2";
LOCATE COMP "led[4]" SITE "D1";
LOCATE COMP "led[5]" SITE "E2";
LOCATE COMP "led[6]" SITE "E1";
LOCATE COMP "led[7]" SITE "H3";
IOBUF PORT "led[0]" IO_TYPE=LVCMOS33 DRIVE=4;
IOBUF PORT "led[1]" IO_TYPE=LVCMOS33 DRIVE=4;
IOBUF PORT "led[2]" IO_TYPE=LVCMOS33 DRIVE=4;
IOBUF PORT "led[3]" IO_TYPE=LVCMOS33 DRIVE=4;
IOBUF PORT "led[4]" IO_TYPE=LVCMOS33 DRIVE=4;
IOBUF PORT "led[5]" IO_TYPE=LVCMOS33 DRIVE=4;
IOBUF PORT "led[6]" IO_TYPE=LVCMOS33 DRIVE=4;
IOBUF PORT "led[7]" IO_TYPE=LVCMOS33 DRIVE=4;

The FREQUENCY PORT line tells nextpnr the clock's frequency, so a separate --freq option is unnecessary, though you can still give one. Bus ports are named with their index in quotes, led[0], exactly as the netlist names them.

The ECP5 groups pins into banks, each with its own supply voltage, and IO_TYPE=LVCMOS33 must match the bank's voltage on the board. The board's schematic lists each bank's voltage.

Step by step

Synthesise:

Shell
yosys -q -p "read_verilog blink25.v; synth_ecp5 -top blink25 -json blink25.json"

synth_ecp5 maps logic to the ECP5's LUT4 and TRELLIS_FF cells, with CCU2C cells for carry-chain arithmetic, and maps memories to DP16KD block RAM. A trimmed statistics block:

Text
=== blink25 ===

   Number of cells:                 57
     CCU2C                          13
     LUT4                            1
     TRELLIS_FF                     25

Counts are illustrative. A CCU2C handles two bits of carry-chain arithmetic, so a 25-bit counter needs about thirteen of them. Each of the 25 flip-flops is a TRELLIS_FF.

Place and route:

Shell
nextpnr-ecp5 --85k --package CABGA381 --json blink25.json --lpf ulx3s.lpf --textcfg blink25.config

--85k selects the device size, which must match the chip on your board, because the ULX3S is sold with 12F, 25F, 45F and 85F parts. --package CABGA381 selects the ball grid package. The output --textcfg is the textual configuration. The utilisation block lists TRELLIS_FF, TRELLIS_COMB, DP16KD and I/O cells, and the timing report gives the maximum frequency for the clock.

Pack:

Shell
ecppack --compress blink25.config blink25.bit

ecppack, from Project Trellis, writes the bitstream. --compress shortens the file, which helps when loading from slow SPI flash. A bitstream compressed this way is accepted by the chip's configuration logic.

Load:

Shell
openFPGALoader -b ulx3s blink25.bit

-b ulx3s names the board, so the loader knows the USB interface and the JTAG pin mapping. This writes to the FPGA's volatile configuration memory over JTAG, which is fast and lost at power-off. To store it in the board's SPI flash so it survives a power cycle, add -f:

Shell
openFPGALoader -b ulx3s -f blink25.bit

A successful load prints the detected device and then a progress bar, ending with Done. A detection line looks like this, with the identification code for the 85F part:

Text
Jtag frequency : requested 6.00MHz   -> real 6.00MHz
Open file: DONE
Parse file: DONE
Enable configuration: DONE
SRAM erase: DONE
Loading: [==================================================] 100.00%
Done
Disable configuration: DONE

Details vary with the loader version. The point is that each stage reports DONE, and the LEDs start counting.

A Makefile for the ECP5 flow

makefile
TOP  = blink25
LPF  = ulx3s.lpf

all: $(TOP).bit

$(TOP).json: $(TOP).v
	yosys -q -p "read_verilog $<; synth_ecp5 -top $(TOP) -json $@"

$(TOP).config: $(TOP).json $(LPF)
	nextpnr-ecp5 --85k --package CABGA381 --json $< --lpf $(LPF) --textcfg $@

$(TOP).bit: $(TOP).config
	ecppack --compress $< $@

prog: $(TOP).bit
	openFPGALoader -b ulx3s $<

flash: $(TOP).bit
	openFPGALoader -b ulx3s -f $<

.PHONY: all prog flash

The structure matches the iCE40 Makefile in the previous post. The only differences are the tool names, the device options and the file extensions.

ECP5 specifics worth knowing

Device sizes. The family comes in 12k, 25k, 45k and 85k logic element variants, and nextpnr has matching options --12k, --25k, --45k and --85k. A wrong size is a common first error, and the symptom is a package or pin that does not exist.

Package names. CABGA381, CABGA256 and others differ per board, so use the one printed on your chip or in the board documentation.

Clocking. The ECP5 has PLLs (EHXPLLL) and global clock networks. ecppll from the Trellis tools calculates PLL parameters, much as icepll does for the iCE40, and a later post in this cluster covers PLLs.

Hard blocks. The chip includes DSP multipliers and block RAM, which Yosys maps automatically for ordinary multiplications and memories. Serial transceivers, the SERDES blocks, are supported by the Trellis database but need careful setup.

Programming modes. JTAG programming to SRAM is quick and non-persistent. Writing the flash needs a second step through the board's flash-programming logic, which openFPGALoader performs when you pass -f.

Troubleshooting

  • **ERROR: ... is not a valid ... location**: the package option or the pin name is wrong for this chip.

  • **openFPGALoader finds no device**: check the cable, drivers and permissions. On Linux, a udev rule for the board's USB IDs is typically required.

  • LEDs do nothing after a successful load: confirm that the clock pin and its IO_TYPE are right, and that the LEDs are active high or low as the code assumes.

  • **The device ID does not match the --85k choice**: the loader prints the chip it detected, so compare it with your nextpnr option.

Reading the nextpnr report for the ECP5

The ECP5 report uses different resource names from the iCE40 one, and learning them tells you how full the chip is. TRELLIS_COMB counts the combinational parts of a slice, which hold lookup tables and carry logic. TRELLIS_FF counts flip-flops. TRELLIS_IO counts pads. DP16KD counts the 18-kilobit block RAMs, MULT18X18D the multiplier blocks, and EHXPLLL the phase-locked loops. The 85k device has far more of each than the small iCE40 parts, so a design that overflowed an iCE40 often fits here with room to spare.

The timing section reports the maximum frequency for each constrained clock, and because the LPF file carries FREQUENCY PORT, the clock name in the report is the port name. Seeing a PASS line with the right target confirms that the constraint was read. If the line is missing, the clock had no frequency constraint, and the tool optimised blindly.

A last tip concerns first-time power-up. When the board is loaded over JTAG, the configuration is lost on power-off, and a board that boots with an old flash image can mislead you into thinking the new design did not load. Keep one known-good design in flash, and load experiments into SRAM, so a power cycle always returns the board to a state you understand.

Also inToolchains
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.