这个配合link文件(就是后缀为.ld的那个文件)实现,如在MEMORY定义中:
[C] 纯文本查看 复制代码 /* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K
}
改为:
[C] 纯文本查看 复制代码 /* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
FLASH1 (rx) : ORIGIN = 0x08000000, LENGTH = 64K
FLASH2 (rx) : ORIGIN = 0x08010000, LENGTH = 64K
}
然后在SECTIONS中添加一个段,如:
[C] 纯文本查看 复制代码 /* Constant data goes into FLASH */
.rodata1 :
{
. = ALIGN(4);
*(.rodata1) /* .rodata sections (constants, strings, etc.) */
*(.rodata1*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH2
再在代码里定义:
[C] 纯文本查看 复制代码 const char Devname[16] __attribute__((section(".rodata1")))={"demo"};
即可定义到指定的0x08010000位置,gcc的链接器通过link脚本来决定程序内存布局,理论上可以通过调整link脚本来实现任意内存布局(调整时要考虑MCU实际内存块分布和字节对齐情况),最后实际编译出来变量在内存哪个位置可以通过.map文件查看。 |