Write Pin Constraints in PCF and LPF Files
Write pin constraint files for iCE40 (PCF) and ECP5 (LPF) boards with pins, pull-ups, I/O standards, drive strength and clock frequency, then check them.

A pin constraint file is the bridge between your Verilog and the physical board. The synthesis tool knows your ports by name. The chip knows its pins by package position. Without a constraint file, the place-and-route tool is free to put a port on any pin it likes, and the design may build and do nothing. This post shows how to write constraint files in the two formats used by the open toolchain: PCF for the Lattice iCE40 and LPF for the Lattice ECP5. It covers pin locations, pull-ups, electrical standards, drive strength and the clock frequency, and shows how to confirm the tool used what you wrote. The examples use nextpnr 0.7 and apply to any board, with pin numbers given only as illustrations of the format that you should replace from your board's schematic.
Where pin numbers come from
A pin number is never something you guess. For a development board, the manufacturer publishes a schematic, a pin table or a ready-made constraint file. For your own board, the schematic shows which FPGA pin connects to each LED, button, header and oscillator. The chip's datasheet lists every pin's allowed functions and which I/O bank it belongs to, and the bank decides the supply voltage and so the allowed I/O standards. Start from the vendor's file whenever one exists, and edit it.
PCF files for iCE40
The Physical Constraints File format used with nextpnr-ice40 has one statement per line: set_io, the options, the port name and the pin.
set_io clk 35
set_io led_r 11
set_io led_g 37
set_io -pullup yes btn 10
set_io --warn-no-port spare 3Reading each line:
set_io clk 35puts the Verilog portclkon package pin 35. The port name must match the module port exactly, case included.set_io led_r 11andset_io led_g 37do the same for two LEDs.set_io -pullup yes btn 10places a button on pin 10 and enables the chip's internal pull-up resistor on that input. A button that shorts to ground when pressed then reads 1 when released and 0 when pressed, with no external resistor.set_io --warn-no-port spare 3places a port namedspare, but if the design has no such port, nextpnr issues a warning instead of failing. That lets one pin file serve several designs that use different subsets of pins.
Vectors use a bracketed index in quotes in some formats, and in PCF a bus bit is written with the index in brackets, for example set_io led[0] 99. Check that the pattern matches how Yosys names the bits in the netlist.
PCF has no per-pin I/O standard on most iCE40 parts, because the standard is set per bank by the supply voltage. A bank at 3.3 V gives LVCMOS33 behaviour. Some options exist for differential inputs and for keeping a pin as a clock input, which iCE40 handles through the SB_IO primitive. For ordinary signals the file above is enough.
LPF files for ECP5
The Lattice Preference File format used with nextpnr-ecp5 is richer. Each statement ends with a semicolon, and a pin usually needs two statements: one to locate it and one to set its buffer.
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";
IOBUF PORT "led[0]" IO_TYPE=LVCMOS33 DRIVE=4;
LOCATE COMP "btn" SITE "D6";
IOBUF PORT "btn" IO_TYPE=LVCMOS33 PULLMODE=DOWN;Reading the statements:
LOCATE COMP "name" SITE "G2";places the port or component on a ball position of the package. ECP5 packages use letter and number pairs for ball locations, such as G2 or B2, unlike iCE40's numbers.IOBUF PORT "name" IO_TYPE=LVCMOS33;sets the I/O standard. Common values areLVCMOS33,LVCMOS25,LVCMOS18andLVDS. The standard must be compatible with the bank's supply voltage on the board.DRIVE=4sets the output drive current in milliamps for an output. Higher values make faster edges and more noise, and a lower value suits LEDs.PULLMODE=DOWNenables a pull-down on an input. The options areUP,DOWN,NONEandKEEPER.FREQUENCY PORT "clk_25mhz" 25 MHZ;declares the clock's frequency, which nextpnr uses as a timing constraint for logic clocked by that port.
LPF is parsed by nextpnr-ecp5 through the --lpf option, and it accepts a subset of the full Lattice syntax. If a statement is not recognised, the tool reports it. Plain LOCATE, IOBUF and FREQUENCY statements are the dependable core.
Pull-ups, drive and slew
Three electrical options solve most practical problems.
Inputs from switches. Enable a pull-up for a button that connects to ground, or a pull-down for one that connects to the supply. A floating input reads randomly.
Outputs to LEDs. A low drive strength is enough, and it reduces ringing. Check that the board has a series resistor.
Fast outputs. For a clock or a fast bus leaving the chip, raise the drive and consider the slew rate option, which controls how steeply the edge rises. Slower edges cut noise, faster edges cut delay.
Bringing it together with Verilog
A design whose ports match the files:
module io_demo (
input wire clk_25mhz,
input wire btn,
output wire [7:0] led
);
reg [24:0] counter = 25'd0;
reg btn_s1 = 1'b0;
reg btn_s2 = 1'b0;
always @(posedge clk_25mhz) begin
btn_s1 <= btn;
btn_s2 <= btn_s1;
if (btn_s2)
counter <= 25'd0;
else
counter <= counter + 25'd1;
end
assign led = counter[24:17];
endmoduleThe button passes through a two-flop synchroniser and clears the counter while pressed. The ports clk_25mhz, btn and led[7:0] are the names the LPF file must use. If the file says led[0] through led[7] and the Verilog says led, the bits match. If the Verilog port were named leds, none of the constraints would match, and nextpnr would report them.
Checking that the constraints were used
Do not assume. The nextpnr log shows what it did with each port:
Info: constraining 'clk_25mhz' to site 'G2'
Info: constraining 'led[0]' to site 'B2'A missing line means a constraint did not apply. For iCE40 the log reports which pins were placed from constraints with a count such as Placed 2 cells based on constraints., which should equal the number of ports you constrained. Warnings that matter:
Warning: IO 'btn' is unconstrained in PCF and will be automatically placedAn automatically placed port is a bug waiting to happen. Fix it by adding the line to the file.
For ECP5 you can also ask nextpnr to emit a report of the final pin assignments with --report report.json, and look up the ports section. For a thorough check, compare the placed locations with the board's schematic once, for each new design, before powering unfamiliar hardware.
Common mistakes
A name mismatch. The constraint says
LEDand the port isled. Names are case-sensitive.Bus syntax.
led0in the file andled[0]in the netlist do not match.The wrong package. The same chip comes in several packages with different pin numbers. Use the package option that matches the board.
The wrong standard. A 3.3 V standard on a 1.8 V bank may fail in the tool or the hardware.
Missing pull-up. A button that works in an oscilloscope check but reads randomly on its own is floating.
Clock on a non-clock pin. Some pins have dedicated clock routing, and a clock on a general pin works but may route slowly.
Output pins left unconstrained. A signal you meant to observe lands on an unknown pin.
Keeping constraint files maintainable
Put the constraint file in version control with the design. Add comments with ##, which both PCF and the content build for this site accept, and name signals consistently across boards. When you support two boards, keep one file per board, and select it from the Makefile with a variable. For a design that moves between iCE40 and ECP5, the Verilog should not change, only the constraint file, the device options and the clock frequency.
## iCEBreaker pin map for blink
set_io clk 35
set_io led 11Comments in a PCF file start with a hash, so a double hash is simply a comment whose text begins with another hash character.


