STM32F429 使用外扩 SDRAM 运行程序
的方法
一. SDRAM 运行程序基本原理
STM32 的高端产品 429/439 添加了新的外设,SDRAM 控制器(FMC 总线)。不少客户都使用外扩的
SDRAM 作为变量的存储区,也有可能作为 C stack 和 heap 的存放区,因此,需要在 IAR 链接前(数
据的拷贝)完成 SDRAM 控制器的初始化工作,ST 已经提供相关代码供客户参考(初始化程序在
system_stm32f4.c 中)。
若对函数使用 IAR 链接器关键字 (例:__ramfunc void func1(void) ),IAR 在链接的时候会将程序放
在 SDRAM 的区域,并将函数的入口地址传给调用者。
二. 客户问题描述及原因
客户反映使用外扩 SDRAM 运行程序(使用链接器将 code 存放在 SDRAM 中,与编译器无关,采用
GCC 或者 IAR 都有这个问题)出错,Hard Fault 发生。
单步运行,使能 Usage, Memory, Bus Fault,发现 Hard Fault 是由 Memory Fault(bit 0 IACCVIOL)
引起的。
参考 ARM 关于 Cortex-M4 内核 IACCVIOL 位的说明
Instruction access violation flag:
0 = no instruction access violation fault
1 = the processor attempted an instruction fetch from a location that does not permit execution. The
PC value stacked for the exception return points to the faulting instruction. The processor has not
written a fault address to the MMAR. This fault condition occurs on any access to an XN (eXecute
Never) region, even when the MPU is disabled or not present. Potential reasons:
a) Branch to regions that are not defined in the MPU or defined as non-executable.
b) Invalid return due to corrupted stack content.
c) Incorrect entry in the exception vector table
在 M4 内核中,0x2000 0000 以上的地址是通过 System Bus 来访问的,system bus 是可以对数据和指
令读取的,但是内核的默认设置(即 MPU 处于 Disabled 的状况下),部分地址禁止执行指令,见下
图(STM32F429 的 SDRAM 取值范围设置在 0xC000 0000 以上),因此会发生该错误。
评论4