查看: 4125|回复: 1

[分享] i.MXRT四位数系列Boot之编程利器Flashloader

[复制链接]
  • TA的每日心情
    开心
    2025-7-11 08:53
  • 签到天数: 301 天

    连续签到: 2 天

    [LV.8]以坛为家I

    3868

    主题

    7472

    帖子

    0

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    39227
    最后登录
    2025-7-18
    发表于 2019-12-31 12:40:27 | 显示全部楼层 |阅读模式
    i.MXRT四位数系列Boot之编程利器Flashloader


           今天痞子衡给大家介绍的是飞思卡尔i.MX RTyyyy系列MCU的Flashloader。


      在上一篇文章 Serial Downloader模式(sdphost, mfgtool) 里痞子衡为大家介绍了i.MXRTyyyy Boot的Serial Downloader模式,这种模式主要是用来引导启动Flashloader,那么Flashloader到底具有哪些功能?这是本篇文章痞子衡要为大家解惑的主题。


      痞子衡在前面提过Flashloader程序主要是用来将你的Application下载进i.MXRTyyyy支持的所有外部非易失性存储器中,为后续从外部存储器启动做准备。BootROM只有启动Application功能,没有下载更新Application功能,而Flashloader最核心的就是下载更新Application功能,所以Flashloader是BootROM的完美补充。你可能会疑问,为什么不把Flashloader的下载更新Application的功能也放进BootROM里?痞子衡个人觉得应该是芯片成本问题,ROM的空间是96KB(RT102x/RT105x)/128KB(RT106x),如果把Flashloader功能也放进BootROM里,势必要扩大ROM空间,从而导致芯片成本上升,做成二级Flashloader既不占ROM空间,也方便Flashloader程序自身的维护升级(目前RT1050的Flashloader版本是1.1,你看,这不显然升级过嘛)。


    一、进入Flashloader程序
    1.1 官方程序包
      恩智浦提供了 Flashloader程序包,你首先需要下载这个Flashloader包,Flashloader所有相关资源全在包里面。注:每个i.MXRTyyyy子系列均有一个以子系列名字命名的Flashloader包,即Flashloader程序并不是通用的(偷偷告诉你,其实RT105x与RT106x是通用的),此处以RT1050系列为例:
    46.png
     Flashloader程序是\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Flashloader\flashloader.elf
      Flashloader工具在\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Tools目录下


    1.2 三种引导方式
      其实引导启动Flashloader的方式并不唯一,Flashloader就是一段运行在SRAM中的应用程序而已,只要能有工具将Flashloader下载进SRAM,并将CPU的PC指针指向Flashloader的程序入口便可启动Flashloader。下面痞子衡分别介绍3种引导方式:


    1.2.1 标准方式:通过sdphost
      第一种引导方式是通过BootROM的Serial Downloader模式和sdphost.exe工具,这是恩智浦官方推荐的方式,这种方式在上一篇文章里已经详细介绍过了,这里不再赘述。
      这种方式的优点是不需要外接调试器,缺点是涉及到BootROM启动以及IVT的知识,需要有BootROM相关知识储备。


    1.2.2 简便方式:通过J-Link Commander
      第二种引导方式是通过外接J-Link调试器和J-Link Commander工具(JLink.exe)。i.MXRTyyyy芯片JTAG口连接上J-Link调试器后,安装好Jlink驱动(痞子衡安装的是v6.30e版本),打开J-Link Commander(即\SEGGER\JLink_V630e\JLink.exe),连接上i.MXRTyyyy的Core,按顺序执行如下JLink命令:


    J-Link>loadfile C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Flashloader\flashloader.srec


    Downloading file [C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Flashloader\flashloader.srec]...
    O.K.
    J-Link>mem32 0x20002000 2


    20002000 = 20215A70 20014B91
    J-Link>wreg MSP 20215A70


    MSP = 0x20215A70
    J-Link>wreg PSP 20215A70


    PSP = 0x20215A70
    J-Link>SetPC 20014B91
    J-Link>g


      到这里Flashloader就已经被成功启动了,有朋友看不懂上面的一串JLink命令,痞子衡为大家解释一下:
      - loadfile命令用于将Flashloader程序数据(.srec格式,含地址信息)下载进SRAM(0x20002000)中;
      - mem32命令用于从起始下载地址(0x20002000)读回部分数据(8字节)确认上一步的下载操作是否成功,并且获取Flashloader的起始SP和PC值。
      - wreg命令用于设置R13(MSP/PSP)寄存器的值,使其等于Flashloader的初始SP值。
      - SetPC命令用于设置R15(PC)寄存器的值,使其指向Flashloader的初始PC。
      - g命令用于让Core开始执行代码(Flashloader程序)。


      这种方式的优点是不需要借助BootROM(你可以不用了解BootROM相关工具用法),缺点是需要额外准备一个J-Link调试器。


    Note:关于JLink命令的详细解释请查阅JLink驱动软件安装目录下\SEGGER\JLink_V630e\Doc\UM08001_JLink.pdf文档里的3.2 J-Link Commander (Command line tool)一节。


    1.2.3 高级方式:通过Ozone
      前两种引导方式都是把Flashloader程序当做黑盒子,只需要将其启动运行即可,接下来痞子衡介绍的引导方式可以让你看到Flashloader源代码并且可以让你调试Flashloader。这第三种引导方式还是通过外接J-Link调试器,但还需要一个特殊的软件,即SEGGER公司提供的Ozone软件,去SEGGER先下载Ozone软件(痞子衡下载的版本是v2.56c),下载安装后打开Ozone软件,第一步选择CM7,第二步选择SWD,第三步选择elf文件时要选择Flashloader下载包里的flashloader.elf
    47.png
    确认elf文件后,点击“Download & Reset Program”
    48.png
    此时Flashloader程序已经被下载进i.MXRTyyyy中,并且在Disassembly窗口可以看到Flashloader汇编源程序,底下你就可以开始调试执行Flashloader程序。由于下载包里的flashloader.elf文件并没有包含所有Flashloader工程的信息(至少没有包含C代码信息,应该是恩智浦官方故意这么做的),所以我们使用Ozone软件调试看不到C代码,稍微有点遗憾。
    49.png
    1.3 支持的通信外设pinout
      Flashloader支持的通信外设与BootROM支持的通信外设是一模一样的,也是USB-HID和UART,并且pinout也一样(Pinout适用RT105x和RT102x):
    50.png
    Note: 如果硬件板上UART_RX引脚没有接上拉电阻,可能会导致USB-HID设备枚举成功率降低,因为UART_RX悬空输入会有干扰数据使得Flashloader误以为UART是active peripheral,所以安全起见,请保证UART_RX引脚连接上拉电阻。


    二、blhost/elftosb/mfgtool的使用
      Flashloader配套上位机工具有3个,elftosb.exe用于生成sb格式文件(这个工具后续会详细介绍),mfgtool是GUI软件(上一篇文章已经介绍过,其主要配合sb文件使用),blhost.exe是痞子衡在这里要着重介绍的软件。
      blhost.exe是命令行工具,使用blhost既可以通过UART口也可以通过USB口与Flashloader进行通信与命令交互。
      在命令行下打开blhost.exe,输入-?命令可以看到blhost使用帮助,相比sdphost,blhost支持的命令更多:
    1. PS C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Tools\blhost\win> .\blhost.exe -?
    2. usage: C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Tools\blhost\win\blhost.exe
    3.                        [-?|--help]
    4.                        [-p|--port <name>[,<speed>]]
    5.                        [-u|--usb [[[<vid>,]<pid>]]]
    6.                        [-t|--timeout <ms>]
    7.                        -- command <args...>

    8. Options:
    9.   -?/--help                    Show this help
    10.   -p/--port <name>[,<speed>]   Connect to target over UART. Specify COM port
    11.                                and optionally baud rate
    12.                                  (default=COM1,57600)
    13.                                  If -b, then port is BusPal port
    14.   -u/--usb [[[<vid>,]<pid>] | [<path>]]
    15.                                Connect to target over USB HID device denoted by
    16.                                vid/pid (default=0x15a2,0x0073) or device path
    17.   -t/--timeout <ms>            Set packet timeout in milliseconds
    18.                                  (default=5000)

    19. Memory ID:
    20.   Internal Memory              Device internal memory space
    21.     0                            Internal Memory
    22.                                  (Default selected memory)
    23.   Mapped External Memory       The memories that are remapped to internal space,
    24.                                and must be accessed by internal addresses.
    25.                                (IDs in this group are only used for flash-erase-all and
    26.                                configure-memory, and ignored by write-memory, read-memory,
    27.                                flash-erase-region and flash-image(use default 0))
    28.     1                            QuadSPI Memory
    29.     8                            SEMC NOR Memory
    30.     9                            FlexSPI NOR Memory
    31.   Unmapped External Memory     Memories which cannot be remapped to internal space,
    32.                                and only can be accessed by memories' addresses.
    33.                                (Must be specified for all commands with <memoryId> argument)
    34.     256 (0x100)                  SEMC NAND Memory
    35.     257 (0x101)                  SPI NAND Memory
    36.     272 (0x110)                  SPI NOR/EEPROM Memory
    37.     273 (0x111)                  I2C NOR/EEPROM Memory
    38.     288 (0x120)                  uSDHC SD Memory
    39.     289 (0x121)                  uSDHC MMC Memory

    40. ** Note that not all memories are supported on all
    41.       Kinetis Bootloader platforms.

    42. Command:
    43.   reset                        Reset the chip
    44.   get-property <tag> [<memoryId> | <index>]
    45.     1                          Bootloader version
    46.     2                          Available peripherals
    47.     7                          Available commands
    48.     10                         Verify Writes flag
    49.     11                         Max supported packet size
    50.     12                         Reserved regions
    51.     14                         Start of RAM, <index> is required
    52.     15                         Size of RAM, <index> is required
    53.     23                         QuadSpi initialization status
    54.     24                         Target version
    55.     25                         External Memory Attrubutes, <memoryId> is required.
    56.   set-property <tag> <value>
    57.     10                         Verify Writes flag
    58.   flash-erase-region <addr> <byte_count> [memory_id]
    59.                                Erase a region of flash according to [memory_id].
    60.   flash-erase-all [memory_id]  Erase all flash according to [memory_id],
    61.                                excluding protected regions.
    62.   read-memory <addr> <byte_count> [<file>] [memory_id]
    63.                                Read memory according to [memory_id] and write to file
    64.                                or stdout if no file specified
    65.   write-memory <addr> [<file>[,byte_count]| {{<hex-data>}}] [memory_id]
    66.                                Write memory according to [memory_id] from file
    67.                                or string of hex values,
    68.                                e.g. data.bin (writes entire file)
    69.                                e.g. data.bin 8 (writes first 8 bytes from file)
    70.                                e.g. "{{11 22 33 44}}" (w/quotes)
    71.                                e.g. {{11223344}} (no spaces)
    72.   fill-memory <addr> <byte_count> <pattern> [word | short | byte]
    73.                                Fill memory with pattern; size is
    74.                                word (default), short or byte
    75.   receive-sb-file <file>       Receive SB file
    76.   execute <addr> <arg> <stackpointer>
    77.                                Execute at address with arg and stack pointer
    78.   call <addr> <arg>            Call address with arg
    79.   configure-memory <memory_id> <internal_addr>
    80.                                Apply configuration block at internal memory address
    81.                                <internal_addr> to memory with ID <memory_id>
    82.   flash-image <file> [erase] [memory_id]
    83.                                Write a formated image <file> to memory with ID
    84.                                <memory_id>. Supported file types: SRecord
    85.                                (.srec and .s19) and HEX (.hex). Flash is erased
    86.                                before writing if [erase]=erase. The erase unit
    87.                                size depends on the target and the minimum erase
    88.                                unit size is 1K.
    89.   list-memory                  List all on-chip Flash and RAM regions, and off-chip
    90.                                memories, supported by current device.
    91.                                Only the configured off-chip memory will be list.
    92.   efuse-program-once <addr> <data>
    93.                                Program one word of OCOTP Field
    94.                                <addr> is ADDR of OTP word, not the shadowed memory address.
    95.                                <data> is hex digits without prefix '0x'
    96.   efuse-read-once <addr>
    97.                                Read one word of OCOTP Field
    98.                                <addr> is ADDR of OTP word, not the shadowed memory address.
    99.   generate-key-blob <dek_file> <blob_file>
    100.                                Generate the Blob for given Dek Key
    101.                                <dek_file> - input, a binary Dek Key (128 Bits) generated by CST tool.
    102.                                <blob_file> - output, a generated blob (72 Bytes) in binary format.

    103. ** Note that not all commands/properties are supported on all
    104.       Kinetis Bootloader platforms.
    复制代码


    PS C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Tools\blhost\win> .\blhost.exe -u 0x15a2,0x0073 -- get-property 1


    Inject command 'get-property'
    Response status = 0 (0x0) Success.
    Response word 1 = 1258422528 (0x4b020100)
    Current Version = K2.1.0


    三、下载更新Application示例
      因为BootROM支持启动的外部存储器很多,所以Flashloader支持下载更新的外部存储器也与BootROM一一对应。在上一节blhost的命令帮助里,我们可以看到Memory ID里已经给各种外部储存器分配了ID号,在使用blhost命令时使用不同的ID号即可操作相应外部存储器。
      其实Flashloader已经把外部存储器的下载更新Application操作封装得很简单也很统一,我们其实只需要3步操作即可完成Application的下载。以Block Size为128KB的Raw NAND为例(即SEMC NAND Memory,Memory ID=0x100):
    1. // 在SRAM里临时存储Raw NAND配置数据
    2. blhost -u -- fill-memory 0x2000 0x4 0xD0010101 // ONFI 1.0, non-EDO, Timing mode 0, 8bit IO, CSX0, HW ECC Check, inital HW ECC is enabled
    3. blhost -u -- fill-memory 0x2004 0x4 0x00010101 // image copy = 1, search stride = 1, search count = 1
    4. blhost -u -- fill-memory 0x2008 0x4 0x00020001 // block index = 2, block count = 1

    5. // 使用Raw NAND配置数据去配置Raw NAND接口
    6. blhost -u -- configure-memory 0x100 0x2000

    7. // 擦除Raw NAND并将image下载进Raw NAND
    8. blhost -u -- flash-erase-region 0x40000 0x20000 0x100    // Erase 1 block starting from block 2
    9. blhost -u -- write-memory 0x40000 ivt_image.bin 0x100    // Program ivt_image.bin to block 2
    复制代码

    其中image.bin是包含IVT的Application镜像数据,关于上述命令的具体意义痞子衡会在后续Raw NAND启动的文章里详尽解释,这里只是给大家一个初步体验。






    作者:痞子衡

    qiandao qiandao
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2025-7-2 15:00
  • 签到天数: 212 天

    连续签到: 1 天

    [LV.7]常住居民III

    34

    主题

    811

    帖子

    0

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    2161
    最后登录
    2025-7-2
    发表于 2019-12-31 12:51:02 | 显示全部楼层
    高级,学习了
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-19 04:48 , Processed in 0.093741 second(s), 21 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表