andeyqi · 2022年11月14日 · 辽宁

【GD32F427开发板试用】IAR flash loader 下载GD32F427流程简要分析

很高兴获的社区的GD32F427开发板的评测机会,这几年芯片慌大家都能感受到,项目上经常因为货源紧张不断更换替代料也是很普遍的现象,GD32作为国产的MCU芯片可以很方便的替换到现有的STM32相关的平台,再次感谢社区的评测机会。

1.IAR 工程的下载配置

打开GD32F427的demo 工程(..\GD32427V_START_Demo_Suites\Projects02_GPIOKey_Polling_mode\EWARM),从IAR的debug 选项的dowenload 配置可知,IAR 使用安装路径下的如下文件了下载程序,$TOOLKIT_DIR$\config\flashloader\GD\FlashGD32F427xK.board。

 title=

2.board文件说明

此*.board 即为我们本贴的主题,*.board 文件是什么文件,从IAR的帮助文档摘出如下说明,.board 文件是flash loader 下载镜像的配置文件被IAR 的C-SPY debug 对象引用。

Ext.Type of fileOutput fromInput to
boardConfiguration file for flash loaderText editorC-SPY

GD32F427 的flash loader 配置文件内容如下:

<?xml version="1.0" encoding="iso-8859-1"?>

<flash_board>
  <pass>
    <loader>$TOOLKIT_DIR$\config\flashloader\GD\FlashGD32F425xK.flash</loader>
  </pass>
</flash_board>

3.flash文件说明

从配置选项可以看出,此.board主要的配置信息是告诉flash loader 要根据FlashGD32F425xK.flash文件的配置下载镜像,此.flash 的文件是什么文件呢,从iar 的文件说明可知,.flash 文件也是C-SPY加载的文件,主要定义了flash 的相关属性配置。

Ext.Type of fileOutput fromInput to
flashConfiguration file for flash loaderText editorC-SPY

FlashGD32F425xK.flash 内容如下:

<?xml version="1.0" encoding="iso-8859-1"?>

<flash_device>
  <exe>$TOOLKIT_DIR$\config\flashloader\GD\FlashGD32F425xKRAM256K.out</exe>
  <page>8</page>
  <block>4 0x4000</block>
  <block>1 0x10000</block>
  <block>7 0x20000</block>
  <block>4 0x4000</block>
  <block>1 0x10000</block>
  <block>7 0x20000</block>
  <block>4 0x40000</block>
  <flash_base>0x08000000</flash_base>
  <macro>$TOOLKIT_DIR$\config\flashloader\GD\FlashGD32F425xK.mac</macro>
  <aggregate>1</aggregate>
</flash_device>

如下是上述xml文件的配置说明:

To accommodate a large range of different flash memories, C-SPY uses a few concepts which
detail the characteristics of flash memories.
Page
A page is the smallest writable unit of the flash memory. Many flash
memories cannot write less than for example 128 or 256 bytes in a
single write operation. C-SPY will never request the flash loader to write
anything smaller than a page, and uses padding if necessary to fill out a
page. Of course, some flash memories have no such restrictions and can
specify a page size of 1 byte.
Block
A block is the smallest erasable unit of the flash memory. For example,
a flash memory with a 256-byte page size could still require that flash
memory should be erased in 4-Kbyte chunks. The block size must
always be a multiple of the page size. A flash memory can consist of
several blocks of different sizes. It can also lack such restrictions, in
which case the block size would be the same as the page size.
Base address
This is the start address of the flash memory, when it is written. Some
flash memories are simply memory mapped into a fixed address range
and the base address is then the start of that range. Other flash memories
are mapped into different addresses when being programmed and when
the application is later executing. The base address is then the address
where these memories are mapped when being programmed. Yet other
flash memories are not memory mapped at all, but work more like
external disk-like devices. The base address is then simply the preferred
address to be used for the start of the memory when it is being
programmed.
From the C-SPY perspective, a flash memory starts at a given address and consists of a sequence
of blocks (possibly of different sizes), each of which consists of a number of pages. The
sequence can also contain gaps.

各配置项概要说明如下:

The mandatory elements include:
● exe—the path to the flash loader
● flash_base—the flash memory base address
● page—the flash memory page size
● block—the block layout of the flash memory

从上面的文件解析可以看出GD32F427内部的falsh 的最小变成单位page为8字节,flash 的 start 地址为0x08000000,内部的扇区0-3为16kb,扇区4为64kb,扇区5为128kb 其余的就不一一描述了,上述的配置和GD32手册内的layout 是一致的。
 title=

4.mac文件说明

上述的.flash 文件除了描述了flash的配置信息,在最后还配置了FlashGD32F425xK.mac 文件,此文件作用是什么呢,从iar 的帮助文档可知此文件为C-SPY下载的配置文件。

Ext.Type of fileOutput fromInput to
macC-SPY macro definitionText editorC-SPY

FlashGD32F425xK.mac 文件内容如下:

execUserFlashInit()
{
  __message "--------------------------execUserFlashInit start--------------------\n";
  if (0x0000AA00 != (0x0000FF00 & __readMemory32(0x40023C14,"Memory")))
  {
    __message "--------------------------Stop FWDGT if it's running so we can ask a question--------------------\n";
    //Stop FWDGT if it's running so we can ask a question
    __writeMemory32(__readMemory32(0xE0042008, "Memory") | 0x1000, 0xE0042008, "Memory");
    
    if (!__messageBoxYesCancel("Do you want to perform mass erase to unlock the device?", "Unlocking device"))
    {
      __abortLaunch("Unlock cancelled. Debug session cannot continue.");
    }

    __writeMemory32(0x45670123, 0x40023C04, "Memory"); // FMC_KEY = FMC_KEY1;
    __writeMemory32(0xCDEF89AB, 0x40023C04, "Memory"); // FMC_KEY = FMC_KEY1;
    __writeMemory32(0x00000000, 0x40023C00, "Memory"); // Flash 0 wait state
    __writeMemory32(0x08192A3B, 0x40023C08, "Memory"); // FMC_OBKEY = FMC_KEY1;
    __writeMemory32(0x4C5D6E7F, 0x40023C08, "Memory"); // FMC_OBKEY = FMC_KEY1;
      
    __message "Setting FLASH readout protection level 0 (disabled)";
  
    __var temp_obctl0;
    
    temp_obctl0 = 0xFFFF00FF & __readMemory32(0x40023C14,"Memory");
    temp_obctl0 |= 0xAA00;
 
    __writeMemory32(temp_obctl0 , 0x40023C14, "Memory"); // Set SPC bits of FMC_OBCTL0 register  to 0xAA
    __writeMemory32(0x2 | temp_obctl0 , 0x40023C14, "Memory"); // Set OB_START
    
    while(0x10000 & __readMemory32(0x40023C0C,"Memory"))
    {
    
      __delay(100);
    
    }  // Wait while FLASH busy
    
    __hwReset(0); // Reset to make changes take effect
  
  }

  //__message("-I- execUserFlashInit!");
  __writeMemory32(0x00000000, 0xE0042004, "Memory");
  __writeMemory32(0x00000000, 0x40013808, "Memory"); 
  __writeMemory32(0x00000000, 0x4001380C, "Memory");
  __writeMemory32(0x00000000, 0x40013814, "Memory");
  __writeMemory32(0x00000000, 0x40013810, "Memory"); 
  __writeMemory32(0x20000000, 0x50000010, "Memory");
  __writeMemory32(0x00000001, 0x50000000, "Memory");
  __writeMemory32(0x00000000, 0xE0042008, "Memory");
  __writeMemory32(0x00000000, 0xE004200C, "Memory");
  __writeMemory32(0x00000000, 0x40023830, "Memory"); // RCU_AHB1EN = 0
  __writeMemory32(0x00000000, 0x40023810, "Memory"); // RCU_AHB1RST = 0
  __writeMemory32(0x00000000, 0x40023814, "Memory"); // RCU_AHB2RST = 0
  __writeMemory32(0x00000000, 0x40023818, "Memory"); // RCU_AHB3RST = 0
  __writeMemory32(0x00000000, 0x40023834, "Memory"); // RCU_AHB2EN = 0
  __writeMemory32(0x00000000, 0x40023838, "Memory"); // RCU_AHB3EN = 0
  __writeMemory32(0x00000000, 0x40023808, "Memory"); // RCU_CFG0 = 0
  __writeMemory32(0x000000F1, 0x40023854, "Memory"); // RCU_AHB2SPEN = 0
  __writeMemory32(0x00000001, 0x40023858, "Memory"); // RCU_AHB3SPEN = 0
  __writeMemory32(0x7E6791FF, 0x40023850, "Memory"); // RCU_AHB1SPEN = 0
  __writeMemory32(0x00010083, 0x40023800, "Memory"); // RCU_CTL   = 0x83
  __writeMemory32(0x00000000, 0x4002380C, "Memory"); // RCU_INT  = 0;
  __writeMemory32(0x24003010, 0x40023804, "Memory"); // RCU_PLL  = 0;
  // unlock flash
  __writeMemory32(0x45670123, 0x40023C04, "Memory"); // FMC_KEY = FMC_KEY1;
  __writeMemory32(0xCDEF89AB, 0x40023C04, "Memory"); // FMC_KEY = FMC_KEY1;
  __writeMemory32(0x00000000, 0x40023C00, "Memory"); // Flash 0 wait state
  __writeMemory32(0x00000000, 0x40023C10, "Memory"); // FMC_CTL = 0;
  __writeMemory32(0x000000F0, 0x40023C0C, "Memory"); // FMC_STAT = PGSERR | PGMERR | WPERR;
    
  __message "--------------------------execUserFlashInit end--------------------\n";
}

5.下载验证

macros 文件是可以被C-SPY 调用的执行文件,为了验证个人的猜想在execUserFlashInit 入口和出口追加了log 输出,来确认猜测是否正确。
有了上述的配置信息及flash 的初始化处理,flash loader程序就可以吧编译的二进制下载到板子上执行了。准备下环境验证下上述猜测是否正确。

Mon Nov 14, 2022 23:48:55: IAR Embedded Workbench 8.20.1 (C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.0\arm\bin\armproc.dll)
Mon Nov 14, 2022 23:48:55: Loaded macro file: C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.0\arm\config\debugger\GD*GD32F4xx.dmac*
Mon Nov 14, 2022 23:48:55: Loaded macro file: C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.0\arm\config\flashloader\GD\FlashGD32F425xK.mac
Mon Nov 14, 2022 23:48:56: Loading the CMSIS-DAP driver
Mon Nov 14, 2022 23:48:56: Probe: CMSIS-DAP probe SW module ver 1.12
Mon Nov 14, 2022 23:48:56: CMSIS-DAP enumerator: No access to device S/N.
Mon Nov 14, 2022 23:48:56: Probe: CMSIS-DAP S/N '' mapped to a number 00001.
Mon Nov 14, 2022 23:48:56: Emulation layer version 4.26
Mon Nov 14, 2022 23:48:56: Notification to init-after-power-up hookup.
Mon Nov 14, 2022 23:48:56: Notification to core-connect hookup.
Mon Nov 14, 2022 23:48:56: Probe: ConnectSpec='CMSIS-DAP::7-14CA2BD-0-0000'.
Mon Nov 14, 2022 23:48:56: Connecting to TAP#0 DAP AHB-AP-CM port 0 (IDR=0x24770011).
Mon Nov 14, 2022 23:48:56: Recognized CPUID=0x410fc241 Cortex-M4 r0p1 arch ARMv7-M
Mon Nov 14, 2022 23:48:56: Debug resources: 6 instruction comparators, 4 data watchpoints.
Mon Nov 14, 2022 23:48:56: CPU status OK
Mon Nov 14, 2022 23:48:56: MultiCore: Asynchronous core execution FORCED.
Mon Nov 14, 2022 23:48:56: MultiCore: Synchronous core execution DISABLED.
Mon Nov 14, 2022 23:48:56: LowLevelReset(script, delay 200)
Mon Nov 14, 2022 23:48:56: Calling reset script: C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.0\arm\config\debugger\GD\GD32.ProbeScript@ConnectUnderReset
Mon Nov 14, 2022 23:48:57: Notification to init-after-hw-reset hookup.
Mon Nov 14, 2022 23:48:57: Probe: ConnectSpec='CMSIS-DAP::7-14CA2BD-0-0000'.
Mon Nov 14, 2022 23:48:57: Connecting to TAP#0 DAP AHB-AP-CM port 0 (IDR=0x24770011).
Mon Nov 14, 2022 23:48:57: Recognized CPUID=0x410fc241 Cortex-M4 r0p1 arch ARMv7-M
Mon Nov 14, 2022 23:48:57: Debug resources: 6 instruction comparators, 4 data watchpoints.
Mon Nov 14, 2022 23:48:58: --------------------------execUserFlashInit start--------------------
Mon Nov 14, 2022 23:48:58: --------------------------execUserFlashInit end--------------------
Mon Nov 14, 2022 23:48:58: Loaded debugee: C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.0\arm\config\flashloader\GD\FlashGD32F425xKRAM256K.out
Mon Nov 14, 2022 23:48:58: Target reset
Mon Nov 14, 2022 23:48:59: Unloaded macro file: C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.0\arm\config\flashloader\GD\FlashGD32F425xK.mac
Mon Nov 14, 2022 23:48:59: Downloaded C:\Users\Administrator\Desktop\GD32F427-VSTART\GD32F4xx_Demo_Suites_V2.6.1\GD32427V_START_Demo_Suites\Projects\02_GPIO_Key_Polling_mode\EWARM\
Debug\Exe\Project.out to flash memory.
Mon Nov 14, 2022 23:48:59: Loaded debugee: C:\Users\Administrator\Desktop\GD32F427-VSTART\GD32F4xx_Demo_Suites_V2.6.1\GD32427V_START_Demo_Suites\Projects\02_GPIO_Key_Polling_mode\EWARM\
Debug\Exe\Project.out
Mon Nov 14, 2022 23:48:59: LowLevelReset(software, delay 200)
Mon Nov 14, 2022 23:48:59: LowLevelReset(script, delay 200)
Mon Nov 14, 2022 23:48:59: Calling reset script: C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.0\arm\config\debugger\GD\GD32.ProbeScript@ConnectUnderReset
Mon Nov 14, 2022 23:48:59: 1996 bytes downloaded into FLASH (2.30 Kbytes/sec)
Mon Nov 14, 2022 23:48:59: Download completed.
Mon Nov 14, 2022 23:48:59: LowLevelReset(software, delay 200)
Mon Nov 14, 2022 23:49:00: Target reset
Mon Nov 14, 2022 23:49:00: INFO: Configuring trace using 'SWO,ETB' setting ...
Mon Nov 14, 2022 23:49:00: Probe: ConnectSpec='CMSIS-DAP::7-14CA2BD-0-0000'.
Mon Nov 14, 2022 23:49:00: Trace: Using detected ETMv3CM at address 0xe0041000
Mon Nov 14, 2022 23:49:00: Trace2: Trace source init error='Only ETMv3 (with ETMIDR register is suppored)'
Mon Nov 14, 2022 23:49:00: Probe: ConnectSpec='CMSIS-DAP::7-14CA2BD-0-0000'.
Mon Nov 14, 2022 23:49:00: INFO: SWO trace mode is not supported by the probe (use I-jet/I-jet-Trace probe) - trace is disabled.
Mon Nov 14, 2022 23:49:00: MultiCore: Synchronous core execution DISABLED.

根据 debug log 可以看出我们添加的log 已经打印,而且下载时也是解析FlashGD32F425xK.mac 文件,并通过FlashGD32F425xKRAM256K.out 程序将程序下载到falsh 中运行。

推荐阅读
关注数
10708
内容数
187
中国高性能通用微控制器领域的领跑者兆易创新GD系列芯片技术专栏。
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息