痞子衡嵌入式:飞思卡尔i.MX RTyyyy系列MCU启动那些事(4)- Flashloader初体验(blhost)


  你们好,我是痞子衡,是正经搞技术的痞子。今天痞子衡给你们介绍的是飞思卡尔i.MX RTyyyy系列MCU的Flashloaderhtml

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

  痞子衡在前面提过Flashloader程序主要是用来将你的Application下载进i.MXRT支持的全部外部非易失性存储器中,为后续从外部存储器启动作准备。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,你看,这不显然升级过嘛)。sass

1、进入Flashloader程序

1.1 官方程序包

  恩智浦提供了 Flashloader程序包,你首先须要下载这个Flashloader包,Flashloader全部相关资源全在包里面。注:每一个i.MXRT子系列均有一个以子系列名字命名的Flashloader包,即Flashloader程序并非通用的(偷偷告诉你,其实RT105x与RT106x是通用的),此处以RT1050系列为例:安全

  Flashloader程序是\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Flashloader\flashloader.elf
  Flashloader工具在\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Tools目录下app

1.2 三种引导方式

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

1.2.1 标准方式:经过sdphost

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

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

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

Downloading file [C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Flashloader\flashloader.srec]...
O.K.

J-Link>mem32 0x20002000 2spa

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

  确认elf文件后,点击“Download & Reset Program”

  此时Flashloader程序已经被下载进i.MXRT中,而且在Disassembly窗口能够看到Flashloader汇编源程序,底下你就能够开始调试执行Flashloader程序。因为下载包里的flashloader.elf文件并无包含全部Flashloader工程的信息(至少没有包含C代码信息,应该是恩智浦官方故意这么作的),因此咱们使用Ozone软件调试看不到C代码,稍微有点遗憾。

1.3 支持的通讯外设pinout

  Flashloader支持的通讯外设与BootROM支持的通讯外设是如出一辙的,也是USB-HID和UART,而且pinout也同样(Pinout适用RT105x和RT102x):

Peripheral Instance PAD Port Mode
USB OTG1 USB_OTG1_DN / /
USB_OTG1_DP
USB_OTG1_VBUS
LPUART 1 GPIO_AD_B0_12 LPUART1_TX ALT2
GPIO_AD_B0_13 LPUART1_RX ALT2

Note: 若是硬件板上UART_RX引脚没有接上拉电阻,可能会致使USB-HID设备枚举成功率下降,由于UART_RX悬空输入会有干扰数据使得Flashloader误觉得UART是active peripheral,因此安全起见,请保证UART_RX引脚链接上拉电阻。

2、blhost/elftosb/mfgtool的使用

  Flashloader配套上位机工具备3个,elftosb.exe用于生成sb格式文件(这个工具后续会详细介绍),mfgtool是GUI软件(上一篇文章已经介绍过,其主要配合sb文件使用),blhost.exe是痞子衡在这里要着重介绍的软件。
  blhost.exe是命令行工具,使用blhost既能够经过UART口也能够经过USB口与Flashloader进行通讯与命令交互。
  在命令行下打开blhost.exe,输入-?命令能够看到blhost使用帮助,相比sdphost,blhost支持的命令更多:

PS C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Tools\blhost\win> .\blhost.exe -?
usage: C:\Flashloader_i.MXRT1050_GA\Flashloader_RT1050_1.1\Tools\blhost\win\blhost.exe
                       [-?|--help]
                       [-p|--port <name>[,<speed>]]
                       [-u|--usb [[[<vid>,]<pid>]]]
                       [-t|--timeout <ms>]
                       -- command <args...>

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

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

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

Command:
  reset                        Reset the chip
  get-property <tag> [<memoryId> | <index>]
    1                          Bootloader version
    2                          Available peripherals
    7                          Available commands
    10                         Verify Writes flag
    11                         Max supported packet size
    12                         Reserved regions
    14                         Start of RAM, <index> is required
    15                         Size of RAM, <index> is required
    23                         QuadSpi initialization status
    24                         Target version
    25                         External Memory Attrubutes, <memoryId> is required.
  set-property <tag> <value>
    10                         Verify Writes flag
  flash-erase-region <addr> <byte_count> [memory_id]
                               Erase a region of flash according to [memory_id].
  flash-erase-all [memory_id]  Erase all flash according to [memory_id],
                               excluding protected regions.
  read-memory <addr> <byte_count> [<file>] [memory_id]
                               Read memory according to [memory_id] and write to file
                               or stdout if no file specified
  write-memory <addr> [<file>[,byte_count]| {{<hex-data>}}] [memory_id]
                               Write memory according to [memory_id] from file
                               or string of hex values,
                               e.g. data.bin (writes entire file)
                               e.g. data.bin 8 (writes first 8 bytes from file)
                               e.g. "{{11 22 33 44}}" (w/quotes)
                               e.g. {{11223344}} (no spaces)
  fill-memory <addr> <byte_count> <pattern> [word | short | byte]
                               Fill memory with pattern; size is
                               word (default), short or byte
  receive-sb-file <file>       Receive SB file
  execute <addr> <arg> <stackpointer>
                               Execute at address with arg and stack pointer
  call <addr> <arg>            Call address with arg
  configure-memory <memory_id> <internal_addr>
                               Apply configuration block at internal memory address
                               <internal_addr> to memory with ID <memory_id>
  flash-image <file> [erase] [memory_id]
                               Write a formated image <file> to memory with ID
                               <memory_id>. Supported file types: SRecord
                               (.srec and .s19) and HEX (.hex). Flash is erased
                               before writing if [erase]=erase. The erase unit
                               size depends on the target and the minimum erase
                               unit size is 1K.
  list-memory                  List all on-chip Flash and RAM regions, and off-chip
                               memories, supported by current device.
                               Only the configured off-chip memory will be list.
  efuse-program-once <addr> <data>
                               Program one word of OCOTP Field
                               <addr> is ADDR of OTP word, not the shadowed memory address.
                               <data> is hex digits without prefix '0x'
  efuse-read-once <addr>
                               Read one word of OCOTP Field
                               <addr> is ADDR of OTP word, not the shadowed memory address.
  generate-key-blob <dek_file> <blob_file>
                               Generate the Blob for given Dek Key
                               <dek_file> - input, a binary Dek Key (128 Bits) generated by CST tool.
                               <blob_file> - output, a generated blob (72 Bytes) in binary format.

** Note that not all commands/properties are supported on all
      Kinetis Bootloader platforms.

  让咱们尝试一下使用blhost与Flashloader通讯(经过USB-HID),若是能获得以下结果,说明Flashloader工做正常,能够进行后续操做。

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

3、下载更新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):

// 在SRAM里临时存储Raw NAND配置数据
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
blhost -u -- fill-memory 0x2004 0x4 0x00010101 // image copy = 1, search stride = 1, search count = 1
blhost -u -- fill-memory 0x2008 0x4 0x00020001 // block index = 2, block count = 1

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

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

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

  至此,飞思卡尔i.MX RTyyyy系列MCU的Flashloader痞子衡便介绍完毕了,掌声在哪里~~~

相关文章
相关标签/搜索