ART-PI使用的主控MCU为STM32H750XBH6,官方芯片手册介绍该MCU片内FLASH只有128KB,但是实测2MB均可正常使用,该2MB内存自测或者学习可以使用,但产品开发不建议使用,毕竟官方未说明这部分,可能存在稳定性或者其他问题,以下介绍下如何在RT-Thread Studio与KEIL MDK开发环境下使用这2MB FLASH。
KEIL MDK开发环境下配置比较简单的一种方法是将芯片类型更改为STM322H742系列,或者采用一下方法:
Step 1:将Option->Target中的IROM1大小改为2MB(0x200000)
Step 2:将Option->Linker配置成下图所示,使用自定义分散加载link.sct
Step 3:编辑link.sct文件,如下:
LR_IROM1 0x08000000 0x00200000 ; load region size_region
{
;On chip flash, defined as 128 KB, but actual up to 2MB
ER_IROM1 0x08000000 0x00200000 ; load address = execution address
{
*.o (RESET, +First)
*(InRoot$$Sections)
.ANY (+RO)
.ANY (+XO)
}
; RW data -> 128KB DTCM
RW_IRAM1 0x20000000 0x00020000
{
.ANY (+RW +ZI)
*(.DTCM)
}
; RW data -> 512KB AXI SRAM
RW_IRAM2 0x24000000 0x00080000
{
.ANY (+RW +ZI)
;*(.RAM_D1)
}
; RW data -> 288KB = 128KB SRAM1(0x30000000) + 128KB SRAM2(0x3002 0000) + 32KB SRAM3(0x30040000)
RW_IRAM3 0x30000000 0x00048000
{
*(.RAM_D2)
}
; RW data -> 64KB SRAM4(0x38000000)
RW_IRAM4 0x38000000 0x00010000
{
*(.RAM_D3)
}
}
Step 4:添加下载算法,这里选择STM32H7x_2048,保存配置
Step 5:编译下载,可以看到,实际代码已经超出128KB,但软件能够正常下载且正常运行!
linking...
Program Size: Code=399814 RO-data=21238 RW-data=107380 ZI-data=79584
".\Obj\tool_box.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed: 00:00:05
Load "D:\\Firmware_STM32\\project\\Obj\\tool_box.axf"
Erase Done.
Programming Done.
Verify OK.
Application running ...
Flash Load finished at 11:40:34
Step 1:建立RT-Thread工程这里不叙述,默认你已经建立并打开了RT-Thread Studio建立的工程,编译后下载,此时下载是没问题的,因为默认下载到了外部8MB的QSPI FLASH中,要使用内部2MBFLASH运行软件,我们需要将代码下载到内部FLASH并且在内部FLASH运行。
Step 2:修改配置,将代码下载到内部FLASH,首先打开link.lds(在board\linker_scripts\STM32H750XBHx目录),修改存储区域如下:
RT-Thread Studio下修改link.lds:直接修改或者添加存储区域即可。
文本模式下修改link.lds:
MEMORY
{
ROM (rx) : ORIGIN =0x8000000,LENGTH =2048k
RAM (rw) : ORIGIN =0x24000000,LENGTH =512k
RxDecripSection (rw) : ORIGIN =0x30040000,LENGTH =32k
TxDecripSection (rw) : ORIGIN =0x30040060,LENGTH =32k
RxArraySection (rw) : ORIGIN =0x30040200,LENGTH =32k
DTCM (rw) : ORIGIN =0x20000000,LENGTH =128k
SRAM12 (rw) : ORIGIN =0x30000000,LENGTH =256k
SRAM4 (rw) : ORIGIN =0x38000000,LENGTH =64k
}
此时我们编译下载,会发现下载失败了,原因是使用STM32CubeProgrammer下载时,FLASH操作超区了,输出日志如下:
-------------------------------------------------------------------
STM32CubeProgrammer v2.7.0-RC1
-------------------------------------------------------------------
Warning: The external loader file does not exist:
mode=normal
ST-LINK SN : 066CFF554957886687083011
ST-LINK FW : V2J37M26
Board : --
Voltage : 3.29V
SWD freq : 4000 KHz
Connect mode: Normal
Reset mode : Software reset
Device ID : 0x450
Revision ID : Rev V
Device name : STM32H7xx
Flash size : 128 KBytes
Device type : MCU
Device CPU : Cortex-M7
Memory Programming ...
Opening and parsing file: rtthread.elf
File : rtthread.elf
Size : 488772 Bytes
Address : 0x08000000
Erasing memory corresponding to segment 0:
Error: Operation exceeds memory limits //这里FLASH操作超区导致FLASH操作失败
Error: failed to erase memory
Error: Unable to reset MCU!
因此要将代码下载进去,我们得修改STM32CubeProgrammer 的配置参数了,接下来我们就来更改STM32CubeProgrammer 配置参数。
Step 3:更改STM32CubeProgrammer 配置参数,解除H750的128KB下载限制
首先我们文本方式打开ST-LINK_Debugger\1.6.0\tools\Data_Base目录下的STM32_Prog_DB_0x450.xml,找到以下代码段,将内部FLASH的128KB 0x20000改为2MB 0x200000,然后保存退出。
<!-- Embedded Flash -->
<Peripheral>
<Name>Embedded Flash</Name>
<Type>Storage</Type>
<Description>The Flash memory interface manages CPU AHB I-Code and D-Code accesses to the Flash memory. It implements the erase and program Flash memory operations and the read and write protection mechanisms</Description>
<ErasedValue>0xFF</ErasedValue>
<Access>RWE</Access>
<FlashSize address="0x1FF1E880" default="0x200000"/>
<!-- 2MB Dual Bank -->
<Configuration config="0,1,2,3">
<Parameters address="0x08000000" name="2 MBytes Dual Bank Embedded Flash" size="0x200000"/>
<Description/>
<Organization>Dual</Organization>
<Allignement>0x20</Allignement>
<Bank name="Bank 1">
<Field>
<Parameters address="0x08000000" name="sector0" occurence="0x8" size="0x20000"/>
</Field>
</Bank>
<Bank name="Bank 2">
<Field>
<Parameters address="0x08100000" name="sector8" occurence="0x8" size="0x20000"/>
</Field>
</Bank>
</Configuration>
<!-- RomLess 128KB -->
<Configuration config="4,5,6,7">
<Parameters address="0x08000000" name="RomLess 128 KB Embedded Flash" size="0x200000"/>
<Description/>
<Organization>Single</Organization>
<Allignement>0x20</Allignement>
<Bank name="Bank 1">
<Field>
<Parameters address="0x08000000" name="sector0" occurence="0x1" size="0x200000"/>
</Field>
</Bank>
</Configuration>
</Peripheral>
Step 4:编译下载,可以看到,实际代码已经可以下载进去了,但软件却没有正常运行,这是因为项目工程代码中跳转到了外部FLASH,中断向量表映射到了0x90000000UL位置而不是内部的0x8000000UL位置,所以我们屏蔽掉跳转即可,代码如下,屏蔽或者删除main.c中的以下代码块即可;
-------------------------------------------------------------------
STM32CubeProgrammer v2.7.0-RC1
-------------------------------------------------------------------
Warning: The external loader file does not exist:
mode=normal
ST-LINK SN : 066CFF554957886687083011
ST-LINK FW : V2J37M26
Board : --
Voltage : 3.29V
SWD freq : 4000 KHz
Connect mode: Normal
Reset mode : Software reset
Device ID : 0x450
Revision ID : Rev V
Device name : STM32H7xx
Flash size : 128 KBytes
Device type : MCU
Device CPU : Cortex-M7
Memory Programming ...
Opening and parsing file: rtthread.elf
File : rtthread.elf
Size : 488772 Bytes
Address : 0x08000000
Erasing memory corresponding to segment 0:
Download in Progress:
Progress: 100%
File download complete
Time elapsed during download operation: 00:00:08.946
//#include "stm32h7xx.h"
//static int vtor_config(void)
//{
// /* Vector Table Relocation in Internal QSPI_FLASH */
// SCB->VTOR = QSPI_BASE;
// return 0;
//}
//INIT_BOARD_EXPORT(vtor_config);
Step 5:编译下载,实际代码已经可以下载进去了,软件也正常运行了。