Vivado 使用 TCL 脚本自动生成 MCS 固化文件

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

自动化操作

  1. FLASH 接口和大小可配置,输出目录可设置

  2. 自动复制生成的 .elf 软核固件、 .bit 比特流文件和 .ltx ILA 探针文件

  3. 自动生成 .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
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值