Vivado 使用 TCL 脚本自动生成 MCS 固化文件
基础命令
write_cfgmem:生成 FLASH 配置文件
file copy : 复制文件
基本用法
## 输出路径
set exportDir ./bin
## 比特流文件名
set BitStreamName system_top.bit
## 生成的mcs文件名
set mcsName system_top
## FLASH 容量大小(MByte)
set size 32
## FLASH 接口类型
set interface SPIx4
set loadbitOptions "up 0x00000000 $exportDir/$BitStreamName"
set mcsPath "$exportDir/${mcsName}.mcs"
## 生成 FLASH 配置文件
write_cfgmem -format mcs -size $size -interface $interface -loadbit $loadbitOptions -force -file $mcsPath -quiet
自动化操作
-
FLASH 接口和大小可配置,输出目录可设置
-
自动复制生成的 .elf 软核固件、 .bit 比特流文件和 .ltx ILA 探针文件
-
自动生成 .mcs 文件
proc bootgen {args} {
## 显示帮助信息
set helper {
bootgen
Description:
(User-written application)
generate MCS file for flash
Syntax:
bootgen [-help] [-arch <arch>] [-size <size>] [outputDir]
Returns:
true (0) if success, else an error will be thrown
Usage:
Name Description
---------------------------------
[-help] Print this help
[-arch] FPGA Chip arch
[-size] FLASH size in MB
[-interface] FLASH interface type
[outputDir] The dir to put output files in
Categories:
user,projutils,user-written
Description:
Copy .bit, .ltx and .elf files to <outputDir>
generate .mcs file for flash by write_cfgmem command.
Arguments:
-help - (Optional) Print this help.
-arch <arch> - (Optional) FPGA Chip arch: "zynq"/"zynqmp"/"fpga", "fpga" by default .
-size <size> - (Optional) FLASH size in MB , 32 by default.
-interface <interface> - (Optional) FLASH interface type, "SPIx4" by default.
outputDir - (Optional) The dir to put output files in, "./bin" by default.
Examples:
# generate MCS file and put it ./bin dir
bootgen [pwd]/bin
# show help
bootgen [-help]
}
## 检查是否已经打开了一个工程
if {[llength [get_projects]] <= 0} {
puts "no project has been opened"
return -2
}
## 设置可选参数的默认值
set size 32
set arch "fpga"
set interface "SPIx4"
set hasOutputDir 0
## 遍历所有输入参数
set spiltArgs []
for { set index 0 } { $index < [ llength $args ] } { incr index } {
## 将参数按照空格分离并添加到参数列表中
set spiltArgs [concat $spiltArgs [split [lindex $args $index] { }]]
}
## 遍历参数列表
for { set index 0 } { $index < [ llength $spiltArgs ] } { incr index } {
switch -exact -- [ lindex $spiltArgs $index ] {
{-help} { return $helper }
{-arch} { set arch [ lindex $spiltArgs [ incr index ]]}
{-size} { set size [ lindex $spiltArgs [ incr index ]]}
{-interface} { set interface [ lindex $spiltArgs [ incr index ]]}
default { set outputDir [ lindex $spiltArgs $index ]; set hasOutputDir 1}
}
}
## 显示当前参数信息
puts "size:$size"
puts "arch:$arch"
puts "interface:$interface"
## 若没有输出文件夹参数则使用默认的 ./bin 目录
if {$hasOutputDir <= 0} {
puts "no outputDir"
## 获取工程目录
set prjDir [get_property "DIRECTORY" [get_projects]]
set outputDir ${prjDir}/bin
puts "use ${outputDir} instead"
}
## 若输出文件夹不存在则创建一个
if { ![file exists $outputDir] <=0 } {
puts "$outputDir doesn't exit"
file mkdir $outputDir
}
## 获取工程名
set projectName [get_projects -quiet]
## 获取顶层文件名
set topName [get_property top [current_fileset]]
## 推断 SDK elf 文件输出目录
set sdkDir ${outputDir}/../$projectName.sdk/$projectName/Debug
## 推断要复制的文件名
set elfName ${projectName}.elf
set LtxName ${topName}.ltx
set BitStreamName ${topName}.bit
set originPath $sdkDir/$elfName
if { [file exists $originPath] } {
file copy -force $originPath $outputDir
} else {
puts "$originPath doesn't exit"
}
## 遍历所有 impl
foreach impName [get_runs imp*] {
## 推断 imp 文件夹目录
set implDir ${outputDir}/../$projectName.runs/$impName
## 推断 .ltx 文件目录
set originPath $implDir/$LtxName
## 推断 .bit 文件目录
set originPath $implDir/$BitStreamName
## 在输出目录创建与 impl 文件夹同名的文件夹
set exportDir ${outputDir}/$impName
if { ![file exists $exportDir] } {
file mkdir $exportDir
}
if { [file exists $originPath] } {
## 复制 .ltx 文件
file copy -force $originPath $exportDir
} else {
puts "$originPath doesn't exit"
}
if { [file exists $originPath] } {
## 复制 .bit 文件
file copy -force $originPath $exportDir
## 检查文件复制是否成功
if { [file exists $exportDir/$BitStreamName]} {
## 生成 .mcs 文件
switch -exact -- $arch {
{zynq} -
{zynqmp} {
## 对于 ZYNQ 使用 bootgen 命令行工具
set fp [open $exportDir/bootgen.bif w+]
puts $fp "all:"
puts $fp "{"
puts $fp " $exportDir/$BitStreamName"
puts $fp "}"
close $fp
exec bootgen -image $exportDir/bootgen.bif -arch $arch -process_bitstream bin -w on
}
{fpga} {
## 对于 纯FPGA 使用 write_cfgmem 命令
set bitPath "up 0x00000000 $exportDir/$BitStreamName"
set mcsPath "$exportDir/${topName}.mcs"
write_cfgmem -format mcs -size $size -interface $interface -loadbit $bitPath -force -file $mcsPath -quiet
}
default {
puts "$arch is invalid"
continue
}
}
} else {
puts "$BitStreamName doesn't exit"
continue
}
} else {
puts "$originPath doesn't exit"
continue
}
}
return 0
}