65.9K
CodeProject 正在变化。 阅读更多。
Home

将Xilinx FPGA/CPLD转换为C源代码

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.71/5 (6投票s)

2011年5月28日

CPOL

12分钟阅读

viewsIcon

53833

downloadIcon

1781

将Xilinx比特流转换为C源代码以编程FPGA/CPLD的流程和工具

摘要

在上一篇文章中,我介绍了AVRILOS 实时多任务操作系统。在本文中,我将描述生成FPGA配置文件以集成到您的代码中,并能够在无需外部串行或PROM/Flash的情况下配置FPGA设备的流程和工具。这将减少芯片数量,就像我经常做的那样,如果您将FPGA与微控制器集成。我还将提供一个用于编译Xilinx FPGA代码的Makefile。在接下来的文章中,我将介绍如何从AVRILOS下载和使用此代码。

相关链接

引言

我将介绍生成包含用于配置Xilinx Spartan FPGA的比特流信息的C文件的过程,尽管您可以将此技术应用于几乎任何串行配置设备。我将提供任何必要的工具(及其代码)。

背景

微控制器之所以流行,是因为它们可以轻松控制系统并且灵活。如今,微控制器拥有更大的内存容量和大量的可以几乎完成所有事情的外围设备。另一类设备与微控制器相似,称为CPLD(复杂可编程逻辑器件)或FPGA(现场可编程门阵列)的可编程逻辑器件。这些器件使用硬件描述语言(如VHDL,Verilog,System C)进行编程,其基本功能是触发器和门,这与微控制器相比是低级别的。这些器件的好处是它们可以(像微控制器一样)被重新编程,并且可以轻松地实现硬件实现效果更好的功能。例如,在软件中进行比特置换是一场噩梦,而在硬件中,此操作将只需要布线连接 - 甚至不需要门!一个缺点是大多数SRAM基FPGA需要外部串行PROM/Flash来存储其配置程序(又称其代码)。此程序在启动时传输到内部FPGA SRAM。

我同时设计FPGA和微控制器,我最喜欢的设置是组合设计。我使用微控制器进行主要逻辑控制,并添加FPGA进行外围扩展。FPGA提供逻辑门、触发器和许多I/O,我可以使用它们来扩展标准的AVR DIP40封装。由于我的微控制器内存足够,我将FPGA配置包含在Flash中,从而消除了外部串行PROM/Flash。

在此介绍的流程中,我假设您知道如何编写、模拟、综合、布局和路由以及生成FPGA的最终比特流。虽然我将针对Xilinx Spartan器件展示此技术,但您可以几乎使用任何器件(当然也包括微控制器)。

描述

项目目标

项目的目标是提供或演示一个流程,用于从FPGA工具比特流生成C代码,该代码可用于从微控制器配置FPGA,从而省略特定的外部PROM/Flash。

工具

FPGA Spartan开发工具

  1. Xilinx Spartan4K开发工具
  2. 外部综合器(我使用的是Xilinx Foundation版综合)
  3. WebISE软件包及模拟器
  4. CVTFPGA工具,用于将Xilinx Spartan FPGA的串行比特流集成到我的代码中。

您可能需要的其他工具

  1. GNUWIN32(用于Makefile,如果我不使用WinAVR,即其他编译器包,如MPLAB)
  2. 您可以想象到的任何其他适合的工具

AVRILOS & FPGA

目录结构

目录结构如下:

00_dirtree.jpg

主要有两个目录,HW和SW。

  1. HW是所有硬件开发进行的目录。这包括电路板原理图和PCB文件以及FPGA设计。
  2. SW是微控制器的软件树,更多信息可以在我关于AVRILOS的第一篇文章中找到。

让我们专注于FPGA的目录结构。

  1. 目录VHDL:这里是我的FPGA源文件。
  2. 目录CFG:包含FPGA配置文件,如约束文件,其中放置了引脚锁定和时序指令。
  3. 目录Scripts:包含Makefile和其他脚本。
  4. 目录SIM:我在这里复制源文件(来自VHDL目录),并在那里进行模拟。任何模拟脚本或测试平台也放在这里。
  5. 目录Synthesis:我在这里复制用于逻辑综合的文件。
  6. 目录EDIF:在此放置生成的综合输出。当前脚本使用EDIF格式,但也可以使用XNF。
  7. 目录Xilinx:布局和布线,以及通用的Xilinx流程。
  8. 目录BIT:在此放置最终的比特流及其PROM十六进制或二进制格式。
  9. 目录BRD:包含生成的C输出及其头文件。

根Makefile(名为makefile)位于FPGA的scripts/目录中。

当然,您也可以选择WebISE提供的GUI,从而避免这种硬核命令行疯狂。Makefile的美妙之处在于,几个月或几年后当我需要进行改进或开始全新的设计时,我不需要记住各种GUI选项存在于何处。对于初学者来说更难,但一旦走过这条路,您将不想回头…每次编译今天、明天或明年在不同机器上都会完全相同。如果您升级工具,脚本可能会因参数含义的改变而中断,因此您将需要集中精力修复它(以专注的方式)。当然,您可以自由地选择更手动、更舒适的GUI方式。

流程说明

FPGA

有两种非常相似的可能流程。一种用于FPGA,这是我最初用于配置FPGA的流程。在此流程中,我使用从机串行配置接口通过微控制器编程FPGA。该流程的微控制器(AVRILOS)代码是我开发的。

FPGA流程

01_fpgaflow_s.png

以下链接提供了一个关于使用从机串行配置算法编程Spartan FPGA的非常有用的文档

此处提供的信息可用于大多数支持从机串行配置的FPGA(又称最新一代)。

CPLD

第二种流程用于CPLD,您只需通过JTAG接口将程序下载到内部CPLD闪存中一次。我已成功使用此方法编程Xilinx CPLD。但是,微控制器代码是根据Xilinx应用程序笔记XAPP503和XAPP058改编的,可在此处获取

Xilinx的代码可在此处获取

备用源(基于Xilinx代码)

CPLD编程流程如下所示

CPLD流程

02_cpldflow_s.png

请注意,CPLD XSVF文件很大(例如,XC9572XL大约为50K)。

您还需要一个综合项目文件(.prj),其中包含项目的VHDL文件。例如:top_cpld.prj

vhdl work "../vhdl/top_cpld.vhd"(添加所有项目文件)。

Makefile说明

请注意,此处介绍的流程将与FPGA设计流程相关,而不是CPLD,尽管过程相似。

Makefile假定您已综合设计,并且网表位于EDIF目录中。然后,它按顺序(或手动逐步)执行Xilinx工具链流程

  1. NGDBuild(初始解析)[make NET]
  2. MAP(将逻辑映射到FPGA原语)[make MAP]
  3. PAR(布局和布线)[make PAR]
  4. TRCE(时序分析)[make TIMING]
  5. 比特流生成[make BITFLASH]
  6. 从比特流生成AVR C代码。[make AVR]
  7. [make fpga]一次性完成所有这些步骤
  8. [make cpld]生成相应的xsvf文件以供CPLD下载

在Makefile的开头,您会看到以下定义

TOP设计是设计的基名。网表应以此为基础。在这种情况下,top_fpga.edif将是编译所需的网表。

#Name of TOP Design
BASENAME=top_fpga   

下一个变量说明了要编译的部件

#Part To Place & Route
PART=xcs05-4-pc84

指定的部件是Spartan器件,PLCC 84封装,-4传播延迟。有关其他特定器件,请咨询您的Xilinx手册。此Makefile用于Spartan4K器件。对于更新的器件,您应咨询手册以获取命令和选项。

我使用的是第一代Spartan器件,因为我可以使用简单的PCB(我可以在实验PCB上使用通孔PLCC插座),并且它们使用5V,这与许多外围设备和AVR CPU兼容。

cfg/目录中,您会找到相应的约束文件,该文件将各个引脚锁定到特定位置。如果您已经有PCB或进行设计迭代,则需要为I/O指定引脚位置。以下是此文件中引脚锁定的示例定义

NET "Clk"    		 	LOC =  "P29"; #SysClk
NET "MCUClk"    	 	LOC =  "P13";
NET "MCUData"    	 	LOC =  "P14";
NET "MCUFrame"    	 	LOC =  "P18";
NET "MCUData"			PULLUP;

NET "pulseout<0>"    	LOC =  "P19";
NET "pulseout<1>"    	LOC =  "P20";
NET "pulseout<2>"    	LOC =  "P23";
 

同一目录中还有一个以.ut为扩展名的文件,其中包含比特流生成的配置选项。

CVTFPGA工具说明

过去,我曾对该工具进行过许多变种,但没有一个是完整的。我依赖第三方工具进行Intel Hex到二进制的转换,而且主转换工具不是那么友好。

在此版本中,我同时支持直接二进制文件和Intel Hex文件作为输入。

因此,您可以手动将.hex文件转换为.bin,然后将.bin文件输入转换器。或者,您可以直接使用.hex文件进行C代码转换。接受的文件格式(和扩展名)

  1. .hex, .mcs(Xilinx FPGA PROM生成器,用于从机串行)
  2. .xsvf(Xilinx iMPACT生成,用于JTAG编程,如CPLD)
  3. .bin(任何您可能需要转换的二进制文件),格式与.xsvf相同

接受的参数是

  1. -o AVRILOS:这将生成AVRILOS兼容的定义在.C文件中。这将消除您自己进行的任何进一步编辑。您只需要将文件复制并重命名为适当的applic/cfg目录(请参阅关于AVRILOS的下一篇相关文章)。
  2. –o CFILOS:这是针对我的ColdFire OS(与AVRILOS相似且基于AVRILOS)。
  3. –f hexval:此命令将选择一个不同于0xFF的填充值,0xFF是未使用区域的默认值,即–f 0x55会将未使用区域填充为0x55。这主要适用于.hex文件,因为二进制文件在内存中是连续的。

您可以选择最适合您需求的选项。此工具也可能用于其他相关的二进制到C转换目的。

第一部分执行OS设置检查,并将内部变量v_os设置为相应的请求OS。默认是AVRILOS。

下一部分确定输入文件的扩展名。输入目标基名的确定是通过反向扫描文件名并获取最后三个字母来完成的。还检查缓冲区溢出(文件名应小于c_DESTLEN[200]个字符)。这是为了避免文件名路径如“./file.c”等情况,其中点字符在文件路径中出现多次。

在知道输入文件扩展名后,转换器根据扩展名确定文件格式。对于.bin.xsvf类型的输入文件,实际类型是原始二进制。对于.hex.mcs,输入文件是Hex文件,并使用Hex文件解析器。

在确定文件名后,打开相应的输入和输出文件。首先,我们写入头文件,该头文件根据请求的OS可能略有不同,仅用于各种MCU架构和编译器。

根据文件类型,使用两种解析器。一种用于二进制,这是一个非常简单的二进制到ASCII转换器。第二种解析器更复杂,因为它读取Intel HEX格式。解析每一行,检查校验和是否有错误,然后确定记录类型。这是通过函数f_GetHexLine实现的。此外,虚拟记录指针会根据记录类型及其地址字段进行更新。如果检测到空(未编程)区域(地址不连续),则使用填充值来填充该区域。默认值为0xFF。

然后生成实际的C源文件。

转换FPGA/CPLD流程

07_cvtfpga3_s.png 

还会生成一个头文件以供参考,该头文件将匹配C文件中的C表声明。在上图所示的示例中,我们将CPLD SVF文件转换为XSVF。然而,提供的Makefile直接通过iMPACT(Xilinx编程工具)生成XSVF。使用此Makefile,则不需要svf2xsvf502转换器。

示例编译

<project>/hw/fpga/scripts目录中打开命令提示符。

假设您已合成代码(模拟后),并且edif/ncf网表已准备就绪,您可以编译Xilinx工具链(NET, MAP,PAR, BITGEN, AVR)。

此外,我假设cvtfpga3.exe文件在您的系统路径中。

因此,在脚本目录中,运行命令

make avr 

此命令将尝试执行完整的流程以生成最终的.c/.h文件。

下面显示了示例输出

Netlist Parsing: top_fpga, Part: xcs05-4-pc84
cd ../xilinx/
ngdbuild -p xcs05-4-pc84 -uc ../cfg/top_fpga.ucf -dd ../xilinx ../edif/top_fpga.edf 
../xilinx/top_fpga.ngd
Release 4.2.03i - ngdbuild E.38
Copyright (c) 1995-2001 Xilinx, Inc.  All rights reserved.

Command Line: ngdbuild -p xcs05-4-pc84 -uc ../cfg/top_fpga.ucf -dd ../xilinx
../edif/top_fpga.edf ../xilinx/top_fpga.ngd

Launcher: Executing edif2ngd
"C:\Users\Ilias\work\OSS\AVRILOS_02\code\CodeProject_02\hw\fpga\edif\top_fpga.ed
f"
"C:\Users\Ilias\work\OSS\AVRILOS_02\code\CodeProject_02\hw\fpga\xilinx\top_fpga.
ngo"
INFO:NgdBuild - Release 4.2.03i - edif2ngd E.38
INFO:NgdBuild - Copyright (c) 1995-2001 Xilinx, Inc.  All rights reserved.
Reading NCF file
"C:/Users/Ilias/work/OSS/AVRILOS_02/code/CodeProject_02/hw/fpga/edif/top_fpga.nc
f"...
Writing the design to
"C:/Users/Ilias/work/OSS/AVRILOS_02/code/CodeProject_02/hw/fpga/xilinx/top_fpga.
ngo"...
Reading NGO file
"C:/Users/Ilias/work/OSS/AVRILOS_02/code/CodeProject_02/hw/fpga/xilinx/top_fpga.
ngo" ...
Reading component libraries for design expansion...

Annotating constraints to design from file "../cfg/top_fpga.ucf" ...
WARNING:NgdBuild:383 - A case sensitive search for the INST, PAD, or NET element
   referred to by a constraint entry in the UCF file that accompanies this design
   has failed, while a case insensitive search is in progress. The result of the
   case insensitive search will be used, but warnings will accompany each and
   every use of a case insensitive result. Constraints are case sensitive with
   respect to user-specified identifiers, which includes names of logic elements
   in a design. For the sake of compatibility with currently existing .xnf,
   .xtf, and .xff files, Software will allow a case insensitive search for INST,
   PAD, or NET elements referenced in a .ucf file.
WARNING:NgdBuild:385 - Found case insensitive match for NET name 'Clk'. NET is
   'clk'.
WARNING:NgdBuild:385 - Found case insensitive match for NET name 'MCUClk'. NET
   is 'mcuclk'.
WARNING:NgdBuild:385 - Found case insensitive match for NET name 'MCUData'. NET
   is 'mcudata'.
WARNING:NgdBuild:385 - Found case insensitive match for NET name 'MCUFrame'. NET
   is 'mcuframe'.
WARNING:NgdBuild:385 - Found case insensitive match for NET name 'MCUData'. NET
   is 'mcudata'.
Attached a PULLUP primitive to pad net mcudata

Checking timing specifications ...
Checking expanded design ...

NGDBUILD Design Results Summary:
  Number of errors:     0
  Number of warnings:   0

Writing NGD file "../xilinx/top_fpga.ngd" ...

Writing NGDBUILD log file "../xilinx/top_fpga.bld"...

NGDBUILD done.
Netlist Mapping: top_fpga, Part: xcs05-4-pc84
cd ../xilinx/
map -p xcs05-4-pc84 -o ../xilinx/map.ncd ../xilinx/top_fpga.ngd ../xilinx/top_fpga.pcf
Release 4.2.03i - Map E.38
Copyright (c) 1995-2001 Xilinx, Inc.  All rights reserved.
Reading NGD file "../xilinx/top_fpga.ngd"...
Using target part "s05pc84-4".
MAP spartan directives:
   Partname = "xcs05-4-pc84".
   Covermode = "area".
   Pack Unrelated Logic into CLBs targeting 97% of CLB resources.
Processing logical timing constraints...
Verifying F/HMAP validity based on pre-trimmed logic...
Removing unused logic...
Packing logic in CLBs...
   Running cover...
   Undirected packing...
Running physical design DRC...

Design Summary:
   Number of errors:        0
   Number of warnings:      2
   Number of CLBs:             97 out of   100   97%
      CLB Flip Flops:     154
      4 input LUTs:       168
      3 input LUTs:        59 (45 used as route-throughs)
   Number of bonded IOBs:      17 out of    61   27%
      IOB Flops:            4
      IOB Latches:          0
   Number of clock IOB pads:    1 out of     8   12%
   Number of secondary CLKs:    1 out of     4   25%
   Number of startup:           1 out of     1  100%
   15 unrelated functions packed into 11 CLBs.
   (11% of the CLBs used are affected.)
Total equivalent gate count for design: 2222
Additional JTAG gate count for IOBs:    816
Writing design file "../xilinx/map.ncd"...

Removed Logic Summary:
 145 block(s) removed
  43 block(s) optimized away
 126 signal(s) removed

Mapping completed.
See MAP report file "../xilinx/map.mrp" for details.
Device Place and Route: top_fpga, Part: xcs05-4-pc84
cd ../xilinx/
par  -w -ol 1 -x ../xilinx/map.ncd ../xilinx/top_fpga.ncd ../xilinx/top_fpga.pcf
Release 4.2.03i - Par E.38
Copyright (c) 1995-2001 Xilinx, Inc.  All rights reserved.




Constraints file: ../xilinx/top_fpga.pcf

Loading design for application par from file ../xilinx/map.ncd.
   "top_fpga" is an NCD, version 2.37, device xcs05, package pc84, speed -4
Loading device for application par from file '4003e.nph' in environment
c:/prog/xilinx.
Device speed data version:  D 1.3 FINAL.


Resolving physical constraints.
Finished resolving physical constraints.

Device utilization summary:

   Number of External IOBs            17 out of 80     27%
      Flops:                           4
      Latches:                         0
   Number of IOBs driving Global Buffers    1 out of 8      12%

   Number of CLBs                     97 out of 100    97%
      Total CLB Flops:               154 out of 200    77%
      4 input LUTs:                  168 out of 200    84%
      3 input LUTs:                   59 out of 100    59%

   Number of SEC-CLKs                  1 out of 4      25%
   Number of STARTUPs                  1 out of 1     100%



Overall effort level (-ol):   1 (set by user)
Placer effort level (-pl):    1 (set by user)
Placer cost table entry (-t): 1
Router effort level (-rl):    1 (set by user)
Extra effort level (-xe):     0 (default)

Starting initial Placement phase.  REAL time: 0 secs
Finished initial Placement phase.  REAL time: 0 secs

Starting Constructive Placer.  REAL time: 0 secs
Placer score = 96660
Placer score = 81240
Placer score = 72120
Placer score = 67080
Placer score = 63180
Placer score = 60240
Placer score = 57000
Placer score = 54420
Finished Constructive Placer.  REAL time: 0 secs

Dumping design to file ../xilinx/top_fpga.ncd.

Starting Optimizing Placer.  REAL time: 2 secs
Optimizing
Swapped 7 comps.
Xilinx Placer [1]   52980   REAL time: 2 secs

Finished Optimizing Placer.  REAL time: 2 secs

Dumping design to file ../xilinx/top_fpga.ncd.

Total REAL time to Placer completion: 2 secs
Total CPU time to Placer completion: 1 secs

0 connection(s) routed; 824 unrouted.
Starting router resource preassignment
Completed router resource preassignment. REAL time: 2 secs
Starting iterative routing.
Routing active signals.
End of iteration 1
824 successful; 0 unrouted; (0) REAL time: 2 secs
Constraints are met.
Dumping design to file ../xilinx/top_fpga.ncd.
Starting cleanup
Improving routing.
End of cleanup iteration 1
824 successful; 0 unrouted; (0) REAL time: 3 secs
Dumping design to file ../xilinx/top_fpga.ncd.
Total REAL time: 3 secs
Total CPU  time: 3 secs
End of route.  824 routed (100.00%); 0 unrouted.
No errors found.
Completely routed.

The design submitted for place and route did not meet the specified timing
requirements.  Please use the static timing analysis tools (TRCE or Timing
Analyzer) to report which constraints were not met.  To obtain a better result,
you may try the following:
  * Use the Re-entrant routing feature to run more router iterations on the
design.
  * Check the timing constraints to make sure the design is not
over-constrained.
  * Specify a higher placer effort level, if possible.
  * Specify a higher router effort level.
  * Use the Multi-Pass PAR (MPPR) feature.  This generates multiple placement
trials from which the best (i.e., lowest design score) placement can be used
with re-entrant routing to obtain a better result.

Please consult the Development System Reference Guide for more detailed
information about the usage options pertaining to these features.

Total REAL time to Router completion: 3 secs
Total CPU time to Router completion: 3 secs

Generating PAR statistics.
Dumping design to file ../xilinx/top_fpga.ncd.


All signals are completely routed.

Total REAL time to PAR completion: 4 secs
Total CPU time to PAR completion: 3 secs

Placement: Completed - No errors found.
Routing: Completed - No errors found.

PAR done.
Generating Configuration Bitstream for FLASH download: top_fpga, Part: xcs05-4-pc84
cd ../xilinx/
bitgen ../xilinx/top_fpga.ncd  -m -w -f ../cfg/bitgen_cclk.ut
Release 4.2.03i - Bitgen E.38
Copyright (c) 1995-2001 Xilinx, Inc.  All rights reserved.

Loading design for application Bitgen from file ../xilinx/top_fpga.ncd.
   "top_fpga" is an NCD, version 2.37, device xcs05, package pc84, speed -4
Loading device for application Bitgen from file '4003e.nph' in environment
c:/prog/xilinx.
Opened constraints file ../xilinx/top_fpga.pcf.

Wed Apr 06 22:28:27 2011

Running DRC.
DRC detected 0 errors and 0 warnings.
Creating bit map...
Saving bit stream in "../xilinx/top_fpga.bit".
Creating bit mask...
Saving mask bit stream in "../xilinx/top_fpga.msk".
Bitstream generation is complete.
cp ../xilinx/top_fpga.bit ../bit/top_fpga_xcs05-4-pc84_flash.bit
Generating AVR C Configuration file
promgen -u 0000 ../bit/top_fpga_xcs05-4-pc84_flash.bit -p bin -c FF -o 
../bit/top_fpga_xcs05-4-pc84_flash.bin -w
Release 4.2.03i - Promgen E.35
Copyright (c) 1995-2001 Xilinx, Inc.  All rights reserved.
0x1a5c (6748) bytes loaded up from 0x0
Using generated prom size of 8K
Writing file "../bit/top_fpga_xcs05-4-pc84_flash.bin".
Writing file "../bit/top_fpga_xcs05-4-pc84_flash.prm".
cvtfpga3.exe ../bit/top_fpga_xcs05-4-pc84_flash.bin 
../brd/top_fpga_xcs05-4-pc84_flash.c -t AVRILOS
Ilialex Research Lab, Convert FPGA V3.01
Convert a binary file (ie/ .bin, xsvf etc) to a C file
Convert an intel Hex file (ie/ .hex, mcs etc) to a C file
To be used for CPLD/FPGA in embedded systems
Input File: ../bit/top_fpga_xcs05-4-pc84_flash.bin
Output File: ../brd/top_fpga_xcs05-4-pc84_flash.c
Base: ../bit/top_fpga_xcs05-4-pc84_flash.bin, Ext: 
../bit/top_fpga_xcs05-4-pc84_flash.bin
Input File ok
Output File ok
Output Header File ok: ../brd/top_fpga_xcs05-4-pc84_flash.h
Total Bytes Written: 6748

Makefile完成后,您可以将brd/中的输出文件复制到您的软件开发树中。对于AVRILOS,您应该将.c/.h文件复制到../../sw/avr16/src/applic/cfg。但更多关于这些步骤的讨论将在下一篇文章中进行。

结论

在本文中,我介绍了一个将FPGA/CPLD配置数据嵌入C源代码中的简单流程。我还提供了一个示例Makefile,它仅通过命令行控制Xilinx FPGA的编译。提供了一些指向相关文档和代码的链接,作为构建您自己代码的参考。我还提供了一个整合工具,该工具直接获取FPGA/CPLD工具输出,并生成相应的C文件,其中包含准备集成到AVRILOS的变量的正确定义。在下一篇文章中,我将更深入地介绍AVRILOS与FPGA的集成和控制。

历史

  • 版本1.0,初次发布
© . All rights reserved.