现象是:在启动文件中使用FlexRAM重新分配RAM给DTCM(448K),发现在前128K内访问内存不用对齐,而128K之后访问内存必须对齐否则将引起非对齐访问错误HardFAUlt,想问为什么128K内可以不对齐而128K之后则需要对齐?该测试是在启动文件里重新分配的RAM,分散加载使用的数据区域是0x20000000~0x00070000,具体测试情况如下。
1.发现当使用超过128K地址(0x20000000~0x20020000)之后的RAM,memcpy必须四字节对齐,直接按short类型访问则必须2字节对齐,直接按int类型访问必须4字节对齐,否则引起非对齐访问错误HardFault;
2.在128K地址(0x20000000~0x20020000)内的空间,分别进行上述3种操作,不对齐时仍正常。
3.看(https://community.nxp.com/thread/487135)的评论NXP提供的两个重新分配代码,我使用后发现并没有解决这个问题。请问该如何解决?
4.我的代码如下:
unsigned char testtr[10] __attribute__((at(0x20020001)));
void HardFault_Handler() {
PRINTF("Hard_FaulT\r\n");
if(SCB->CFSR & (1<<24)) PRINTF(" 非对齐访问错误(UNALIGNED)!\r\n");
while(1);
}
main函数内的测试代码:
memcpy(testtr,"0123456789",10); /* 超过128k后 必须四字节对齐,否则引起非对齐访问错误 */
testtr[0] = 0x3;
PRINTF("单字节访问成功\r\n");
*((uint16_t *)testtr) = 0xff;/* 超过128k后,双字节访问时必须2字节对齐,否则引起非对齐访问错误 */
PRINTF("双字节访问成功\r\n");
*((uint32_t *)testtr) = 0xff;/* 超过128k后,四字节访问时必须4字节对齐,否则引起非对齐访问错误 */
PRINTF("四字节访问成功\r\n");
启动文件测试代码有三个,测试结果是一样的。
;测试代码1:512KB全分配给DTCM 配置结果是GPR14 = 0x00A00000 GPR16 =0x00200006 GPR17 = 0xAAAAAAAA
LDR R0, =0x400AC038
LDR R1, =0x00A00000
STR R1, [R0]
LDR R0, =0x400AC040
LDR R1, =0x00200006
STR R1, [R0]
LDR R0, =0x400ac044
LDR R1, =0xAAAAAAAA
STR R1, [R0]
;测试代码2,来自于NXP社区:512KB全分配给DTCM.配置结果是GPR16 =0x00200007 GPR17 = 0xAAAAAAAA
; LDR R0, =0x400ac044
; LDR R1, =0xaaaaaaaa
; STR R1,[R0]
; LDR R0,=0x400ac040
; LDR R1,=0x04
; LDR R3,[R0]
; ORR R2,R1,R3
; STR R2,[R0]
;测试代码3,来自于NXP社区:DTCM是448K OCRAM是64K
; LDR R0, =OCRAM_Reallocate
; BLX R0
; LDR R0, =0x400B0000
; LDR R2, =FLEXRAM_Init
; BLX R2
Hi 永超 赵 ,
非常感谢使用NXP产品,很高兴为你提供技术支持!
请分享你的测试代码,我这边复现确认一下并查找其原因。
期待你的回复。
Have a great day,
TIC
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------
谢谢,进一步分析得到的结论:128k之后(0x20020000之后)的空间,使用STRH指令访问必须2字节对齐(C语言中按short类型访问),使用STR指令访问必须4字节对齐(C语言中按int类型访问),否则将引起UNALIGNED HardFault。而0x20000000~0x20020000内的空间,STRH指令与STR指令无限制,不会引起hardFault。而memcpy会调用STR指令,所以memcpy要求四字节对齐。
代码路径:\SDK_2.4.1_EasyARM-RT1052-V1_0_2018-12-27\boards\EasyARM-RT1052-V1_0\demo_apps\hello_world 0.1 《hello_world flexspi_nor_debug标签》
您好,这个问题已经解决了,很感谢您的支持。详情见https://community.nxp.com/thread/487135,大致概括一下原因:FlexRAM的配置之后未相应改变MPU的设置。
Hi 永超 赵 ,
感谢回复,很高兴听到问题解决。
Have a great day,
TIC
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------