ICE40 FPGA 开发全流程环境配置

ICE40 FPGA 开发全流程环境配置

使用开源工具链:Project IceStorm,适用于Lattice iCE40的FPGA芯片,具体的,本篇使用iCE40LP1K-CM36这款FPGA芯片(这块板子:wuxx/icesugar-nano: iCESugar-nano FPGA board (base on iCE40LP1K) (github.com)

安装MSYS2

我们使用MSYS2来实现在Windows上运行Linux软件包,MSYS2相比于虚拟机而言更加轻量化,并且内部集成了Arch Linux的软件包管理系统pacman,安装软件包异常方便。

首先前往MSYS2官网下载安装

然后打开MSYS2 MinGW x64,键入命令以更新pacman的软件包数据库

pacman -Syu

安装工具链

键入命令以安装EDA相关的开源工具链

pacman -S mingw-w64-x86_64-eda

会列出所有相关软件包,输入开头的数字就安装相应的软件包

:: There are 23 members in group mingw-w64-x86_64-eda:
:: Repository mingw64
   1) mingw-w64-x86_64-dfu-util  2) mingw-w64-x86_64-ecpprog
   3) mingw-w64-x86_64-fritzing  4) mingw-w64-x86_64-ghdl-llvm
   5) mingw-w64-x86_64-gtkwave  6) mingw-w64-x86_64-icesprog
   7) mingw-w64-x86_64-icestorm  8) mingw-w64-x86_64-iverilog
   9) mingw-w64-x86_64-kicad  10) mingw-w64-x86_64-kicad-footprints
   11) mingw-w64-x86_64-kicad-meta  12) mingw-w64-x86_64-kicad-packages3D
   13) mingw-w64-x86_64-kicad-symbols  14) mingw-w64-x86_64-kicad-templates
   15) mingw-w64-x86_64-nextpnr  16) mingw-w64-x86_64-ngspice
   17) mingw-w64-x86_64-openFPGALoader  18) mingw-w64-x86_64-prjtrellis
   19) mingw-w64-x86_64-scopehal-apps  20) mingw-w64-x86_64-serial-studio
   21) mingw-w64-x86_64-verilator  22) mingw-w64-x86_64-wb32-dfu-updater
   23) mingw-w64-x86_64-yosys

在这里,我们安装用于综合(synthesis)的yosys、用于布线(place&route tool)的nextpnricestorm、用于查看时序图的gtkwave、和用于编译和仿真Verilog的iverilog

编写TestBench

首先编写一个Verilog程序hello.v

// hello.v
module hello(   input CLK,
                output LED
                );
    reg [25:0] cnt;
    reg on_led;
    parameter SYSCLK_PERIOD = 0;

    assign LED = on_led;

    initial begin
        cnt = 0;
        on_led = 0;
    end

    always @(posedge CLK)
    begin
        cnt = cnt + 1;
        if(cnt[SYSCLK_PERIOD])
        begin
            on_led = ~on_led;
            cnt = 0;
        end
    end 

endmodule

之后我们需要对它进行仿真验证,我们还需要编写一个用于验证的程序hello_tb.v,这被称为TestBench

// hello_tb.v
`timescale 1ns/100ps
module hello_tb;
  initial 
  begin
    $display("Hello, World");
  end
  
  reg clk;
  // generate the clock
  initial begin
    clk = 0;
    forever #1 clk = ~clk;
  end


  wire led;
  hello hl(.CLK(clk), .LED(led));
  
  integer i;

  initial
  begin
    for(i=0;i<50;i=i+1)
    begin
      $display("Clock=%d, LED=%d", clk, led, $time);
      #1;
    end
    $finish;
  end
  
endmodule

编译仿真Verilog

编译命令:

iverilog hello.v hello_tb.v

如果编译通过,就会在目录下生成文件a.out(可以添加参数-o指定输出文件名字)

仿真运行命令:

vvp a.out

然后就能在控制台看到仿真的输出结果

Hello, World
Clock=0, LED=0                   0
Clock=1, LED=0                   1
Clock=0, LED=1                   2
Clock=1, LED=1                   3
Clock=0, LED=0                   4
Clock=1, LED=0                   5
Clock=0, LED=1                   6
Clock=1, LED=1                   7
Clock=0, LED=0                   8
Clock=1, LED=0                   9
hello_tb.v:28: $finish called at 100 (100ps)

可以看到LED确实是以一个时钟周期翻转的。

当然,想要肉眼看到LED闪烁得把SYSCLK_PERIOD改大一点(比如23),否则闪烁太快了,看不出来。

用GTKWave查看波形图

GTKWave是一个用GTK开发的可以查看VCD波形图的软件。

为了输出VCD波形图文件,需要在hello_tb.v中添加输出VCD文件的代码:

initial
begin            
    $dumpfile("wave.vcd");		// 指定生成的vcd文件名称
    $dumpvars(0, hello_tb);		// 指定要记录的信号,0代表记录hello_tb模块下的所有信号
end

默认下会生成VCD格式的波形图文件,不过GTKWave支持更快的LXT格式,所以我们键入指令,让vvp输出LXT格式的波形图文件

vvp a.out -lxt2

然后用GTKWave查看波形图

gtkwave .\wave.vcd

综合

仿真验证代码后,开始FPGA的综合(synthesis),将语言编译为门级表(电路结构)。

我们回到已经配置好工具链的MSYS2环境下,打开MSYS2 MinGW x64

使用yosys综合,-top参数指定hello为顶层模块

yosys -p "synth_ice40 -json hello.json -top hello" hello.v

布局布线

综合后,我们还需要确定门级表中门的位置与连线,这个过程就叫布局布线,我们使用nextpnr来布局布线(place&route tool),需要根据芯片的封装按照下表(来自Project IceStorm (clairexen.net))确定布局参数

我们的芯片是iCE40LP1K-CM36,查表得:

Part Package Pin Spacing I/Os nextpnr opts arachne-pnr opts icetime opts
iCE40-LP1K-CM36 36-ball ucBGA (2.5 x 2.5 mm) 0.40 mm 25 --lp1k --package cm36 -d 1k -P cm36 -d lp1k

由于Verilog代码是与硬件无关的,我们还需要一个物理约束文件(pcf, Physical Constraints file)来描述引脚(Pin)与Verilog中的端口(Ports)的绑定情况。

查阅我这块板子ICESugar-nano-v1.2的原理图后,在同目录下建立文件io.pcf

set_io LED B6
set_io CLK D1

键入命令使用nextpnr来布局布线

nextpnr-ice40 --lp1k --package cm36 --json hello.json --pcf io.pcf --asc hello.asc --freq 48

这里指定了时钟频率为48MHz

打包

使用icepack生成二进制流文件

icepack hello.asc hello.bin

然后直接拖拽烧录或者使用iceprog 烧录即可。

使用Makefile

我们还可以使用Makefile简化流程,自动化仿真、构建。

同目录下建立Makefile文件

filename = hello
top_model = hello
pcf_file = io.pcf
freq = 48

test:
	iverilog $(filename).v $(filename)_tb.v
	vvp a.out -lxt2
	gtkwave .\wave.vcd
build:
	yosys -p "synth_ice40 -json $(filename).json -top $(top_model)" $(filename).v
	nextpnr-ice40 --lp1k --package cm36 --json $(filename).json --pcf $(pcf_file) --asc $(filename).asc --freq $(freq)
	icepack $(filename).asc $(filename).bin

clean:
	rm -rf $(filename).json $(filename).asc $(filename).bin *.out *.vcd

我们把仿真验证、gtkwave查看波形图封装成test,综合、布局、打包封装成build,之后直接调用make命令即可。

make clean && make test && make build

参考

Getting Started | Icarus Verilog | Fandom

YosysHQ/nextpnr: nextpnr portable FPGA place and route tool (github.com)

全平台轻量开源verilog仿真工具iverilog+GTKWave使用教程 - 知乎 (zhihu.com)

makefile介绍 — 跟我一起写Makefile 1.0 文档 (seisman.github.io)

posted @ 2022-07-29 20:08  Santiego  阅读(966)  评论(1编辑  收藏  举报